chado_linker__expression.inc 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. <?php
  2. class chado_linker__expression extends TripalField {
  3. /**
  4. * @see TripalField::field_info()
  5. */
  6. public function field_info() {
  7. return array(
  8. 'label' => t('Expression'),
  9. 'description' => t('Associates an expression with
  10. this record.'),
  11. 'default_widget' => 'chado_linker__expression_widget',
  12. 'default_formatter' => 'chado_linker__expression_formatter',
  13. 'settings' => array(),
  14. 'storage' => array(
  15. 'type' => 'field_chado_storage',
  16. 'module' => 'tripal_chado',
  17. 'active' => TRUE
  18. ),
  19. );
  20. }
  21. /**
  22. * @see TripalField::can_attach()
  23. */
  24. protected function can_attach($entity_type, $bundle, $details) {
  25. $table_name = $details['chado_table'];
  26. $type_table = $details['chado_type_table'];
  27. $type_field = $details['chado_type_column'];
  28. $cv_id = $details['chado_cv_id'];
  29. $cvterm_id = $details['chado_cvterm_id'];
  30. // If the linker table does not exists then we don't want to add attach.
  31. $expression_table = $table_name . '_expression';
  32. if (chado_table_exists($expression_table)) {
  33. return TRUE;
  34. }
  35. return FALSE;
  36. }
  37. /**
  38. * @see TripalField::create_info()
  39. */
  40. function create_info($entity_type, $bundle, $details) {
  41. if (!$this->can_attach($entity_type, $bundle, $details)) {
  42. return;
  43. }
  44. $table_name = $details['chado_table'];
  45. $type_table = $details['chado_type_table'];
  46. $type_field = $details['chado_type_column'];
  47. $cv_id = $details['chado_cv_id'];
  48. $cvterm_id = $details['chado_cvterm_id'];
  49. $schema = chado_get_schema('featureloc');
  50. $pkey = $schema['primary key'][0];
  51. return array(
  52. 'field_name' => $table_name . '_expression',
  53. 'type' => 'chado_linker__expression',
  54. 'cardinality' => FIELD_CARDINALITY_UNLIMITED,
  55. 'locked' => FALSE,
  56. 'storage' => array(
  57. 'type' => 'field_chado_storage',
  58. ),
  59. 'settings' => array(
  60. 'chado_table' => $table_name . '_expression',
  61. 'chado_column' => $pkey,
  62. 'base_table' => $table_name,
  63. 'semantic_web' => 'local:expression',
  64. ),
  65. );
  66. }
  67. /**
  68. * @see TripalField::create_instance_info()
  69. */
  70. function create_instance_info($entity_type, $bundle, $details) {
  71. if (!$this->can_attach($entity_type, $bundle, $details)) {
  72. return;
  73. }
  74. $table_name = $details['chado_table'];
  75. $type_table = $details['chado_type_table'];
  76. $type_field = $details['chado_type_column'];
  77. $cv_id = $details['chado_cv_id'];
  78. $cvterm_id = $details['chado_cvterm_id'];
  79. return array(
  80. 'field_name' => $table_name . '_expression',
  81. 'entity_type' => $entity_type,
  82. 'bundle' => $bundle->name,
  83. 'label' => 'Expression',
  84. 'description' => 'Information about the expression of this record.',
  85. 'required' => FALSE,
  86. 'settings' => array(
  87. 'auto_attach' => FALSE,
  88. ),
  89. 'widget' => array(
  90. 'type' => 'chado_linker__expression_widget',
  91. 'settings' => array(
  92. 'display_label' => 1,
  93. ),
  94. ),
  95. 'display' => array(
  96. 'deafult' => array(
  97. 'label' => 'above',
  98. 'type' => 'chado_linker__expression_formatter',
  99. 'settings' => array(),
  100. ),
  101. ),
  102. );
  103. }
  104. /**
  105. * @see TripalField::widget_info()
  106. */
  107. function widget_info() {
  108. return array(
  109. 'label' => t('Expressions'),
  110. 'field types' => array('chado_linker__expression'),
  111. );
  112. }
  113. /**
  114. * @see TripalField::formatter_info()
  115. */
  116. public function formatter_info() {
  117. return array(
  118. 'label' => t('Expression'),
  119. 'field types' => array('chado_linker__expression'),
  120. 'settings' => array(
  121. ),
  122. );
  123. }
  124. /**
  125. * @see TripalField::formatter_view()
  126. */
  127. public function formatter_view(&$element, $entity_type, $entity,
  128. $field, $instance, $langcode, $items, $display) {
  129. // Get the settings
  130. $settings = $display['settings'];
  131. $record = $entity->chado_record;
  132. $content = '';
  133. foreach ($items as $delta => $item) {
  134. if (!$item['value']) {
  135. continue;
  136. }
  137. // Iterate through all of the children of the $item['value']. Add each
  138. // one as an independent row in the table.
  139. $rows = array();
  140. foreach ($item['value'] as $key => $value) {
  141. // If this key is the name, then we want to link to the entity if one
  142. // exists.
  143. if ($key == 'name') {
  144. if (array_key_exists('entity_id', $item) and $item['$entity_id']) {
  145. $entity_id = $item['entity_id'];
  146. $value = l($value, "bio_data/" . $entity_id, array('attributes' => array('target' => "_blank")));
  147. }
  148. }
  149. // If this key is the publication then we want to get the citation
  150. // and link to the pub if an entity exits.
  151. if ($key == 'publication') {
  152. $pub = $value['Citation'];
  153. if (array_key_exists('publication', $item) and array_key_exists('entity_id', $item['publication'][0])) {
  154. $entity_id = $item['publication'][0]['entity_id'];
  155. $title = $item['value']['publication']['Title'];
  156. $link = l($title, 'bio_data/' . $entity_id);
  157. $pub = preg_replace("/$title/", $link, $pub);
  158. }
  159. $value = $pub;
  160. }
  161. // Add the item as a new row.
  162. $rows[] = array(
  163. array(
  164. 'data' => ucfirst(str_replace('_', ' ', $key)),
  165. 'header' => TRUE,
  166. 'width' => '20%',
  167. ),
  168. $value
  169. );
  170. }
  171. $table = array(
  172. 'header' => array(),
  173. 'rows' => $rows,
  174. 'attributes' => array(
  175. 'id' => 'tripal_linker-table-expression-object',
  176. 'class' => 'tripal-data-table'
  177. ),
  178. 'sticky' => FALSE,
  179. 'caption' => "",
  180. 'colgroups' => array(),
  181. 'empty' => '',
  182. );
  183. $content .= theme_table($table);
  184. }
  185. // once we have our table array structure defined, we call Drupal's theme_table()
  186. // function to generate the table.
  187. if (count($items) > 0) {
  188. $element[0] = array(
  189. '#type' => 'markup',
  190. '#markup' => $content,
  191. );
  192. }
  193. }
  194. /**
  195. * @see TripalField::load()
  196. */
  197. public function load($field, $entity, $details) {
  198. $record = $details['record'];
  199. $field_name = $field['field_name'];
  200. $field_type = $field['type'];
  201. $field_table = $field['settings']['chado_table'];
  202. $field_column = $field['settings']['chado_column'];
  203. // Get the FK that links to the base record.
  204. $schema = chado_get_schema($field_table);
  205. $base_table = $details['record']->tablename;
  206. $pkey = $schema['primary key'][0];
  207. $fkey_lcolumn = key($schema['foreign keys'][$base_table]['columns']);
  208. $fkey_rcolumn = $schema['foreign keys'][$base_table]['columns'][$fkey_lcolumn];
  209. $linker_table = $base_table . '_expression';
  210. $options = array(
  211. 'return_array' => 1,
  212. );
  213. $record = chado_expand_var($record, 'table', $linker_table, $options);
  214. $exp_linkers = $record->$linker_table;
  215. if ($exp_linkers) {
  216. foreach ($exp_linkers as $i => $exp_linker) {
  217. // Because the unqiuename is a text field we must expand it.
  218. $expression = $exp_linker->expression_id;
  219. $expression = chado_expand_var($expression, 'field', 'expression.uniquename', $options);
  220. // Set the values that will be seen by the user on the page and in
  221. // web services, or anwhere this field is viewed.
  222. $entity->{$field_name}['und'][$i]['value'] = array(
  223. 'name' => $expression->uniquename,
  224. 'description' => $expression->description,
  225. //'md5checksum' => $expression->md5checksum,
  226. );
  227. // Add the pub information if a real pub is associated with the record.
  228. $pub = $exp_linker->pub_id;
  229. if ($pub->uniquename != 'null') {
  230. $pub_details = tripal_get_minimal_pub_info($pub);
  231. $entity->{$field_name}['und'][$i]['value']['publication'] = $pub_details;
  232. $entity->{$field_name}['und'][$i]['value']['publication']['@type'] = $pub->type_id->dbxref_id->db_id->name . ':' . $pub->type_id->dbxref_id->accession;
  233. $entity->{$field_name}['und'][$i]['value']['publication']['type'] = $pub->type_id->name;
  234. if (property_exists($pub, 'entity_id')) {
  235. $entity->{$field_name}['und'][$i]['publication'][0]['entity_id'] = $pub->entity_id;
  236. $entity->{$field_name}['und'][$i]['publication'][0]['entity_type'] = 'TripalEntity';
  237. }
  238. }
  239. // Add the linker_expressionprop
  240. $linkerprop_table = $linker_table . 'prop';
  241. if (db_table_exists('chado.' . $linkerprop_table)) {
  242. $exp_linker = chado_expand_var($exp_linker, 'table', $linkerprop_table, $options);
  243. $exp_linkerprops = $exp_linker->feature_expressionprop;
  244. if ($exp_linkerprops) {
  245. foreach ($exp_linkerprops AS $linkerprop) {
  246. $entity->{$field_name}['und'][$i]['value'][$linkerprop->type_id->name] = $linkerprop->value;
  247. }
  248. }
  249. }
  250. // Add the fields for the widget form. The name requres the following
  251. // format if the fields should be used as values for insertint/updating
  252. // into the chado table: [table_name]__[field_name]
  253. $entity->{$field_name}['und'][$i][$linker_table . '__expression_id'] = $expression->expression_id;
  254. $entity->{$field_name}['und'][$i][$linker_table . '__uniquename'] = $expression->uniquename;
  255. //$entity->{$field_name}['und'][$i][$linker_table . '__md5checksum'] = $expression->md5checksum;
  256. $entity->{$field_name}['und'][$i][$linker_table . '__description'] = $expression->description;
  257. }
  258. }
  259. }
  260. }