chado_linker__expression.inc 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  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. 'widget' => array(
  88. 'type' => 'chado_linker__expression_widget',
  89. 'settings' => array(
  90. 'display_label' => 1,
  91. ),
  92. ),
  93. 'display' => array(
  94. 'deafult' => array(
  95. 'label' => 'above',
  96. 'type' => 'chado_linker__expression_formatter',
  97. 'settings' => array(),
  98. ),
  99. ),
  100. );
  101. }
  102. /**
  103. * @see TripalField::widget_info()
  104. */
  105. function widget_info() {
  106. return array(
  107. 'label' => t('Expressions'),
  108. 'field types' => array('chado_linker__expression'),
  109. );
  110. }
  111. /**
  112. * @see TripalField::formatter_info()
  113. */
  114. public function formatter_info() {
  115. return array(
  116. 'label' => t('Expression'),
  117. 'field types' => array('chado_linker__expression'),
  118. 'settings' => array(
  119. ),
  120. );
  121. }
  122. /**
  123. * @see TripalField::formatter_view()
  124. */
  125. public function formatter_view(&$element, $entity_type, $entity,
  126. $field, $instance, $langcode, $items, $display) {
  127. // Get the settings
  128. $settings = $display['settings'];
  129. $record = $entity->chado_record;
  130. $content = '';
  131. foreach ($items as $delta => $item) {
  132. if (!$item['value']) {
  133. continue;
  134. }
  135. // Iterate through all of the children of the $item['value']. Add each
  136. // one as an independent row in the table.
  137. $rows = array();
  138. foreach ($item['value'] as $key => $value) {
  139. // If this key is the name, then we want to link to the entity if one
  140. // exists.
  141. if ($key == 'name') {
  142. if (array_key_exists('entity_id', $item) and $item['$entity_id']) {
  143. $entity_id = $item['entity_id'];
  144. $value = l($value, "bio_data/" . $entity_id, array('attributes' => array('target' => "_blank")));
  145. }
  146. }
  147. // If this key is the publication then we want to get the citation
  148. // and link to the pub if an entity exits.
  149. if ($key == 'publication') {
  150. $pub = $value['Citation'];
  151. if (array_key_exists('publication', $item) and array_key_exists('entity_id', $item['publication'][0])) {
  152. $entity_id = $item['publication'][0]['entity_id'];
  153. $title = $item['value']['publication']['Title'];
  154. $link = l($title, 'bio_data/' . $entity_id);
  155. $pub = preg_replace("/$title/", $link, $pub);
  156. }
  157. $value = $pub;
  158. }
  159. // Add the item as a new row.
  160. $rows[] = array(
  161. array(
  162. 'data' => ucfirst(str_replace('_', ' ', $key)),
  163. 'header' => TRUE,
  164. 'width' => '20%',
  165. ),
  166. $value
  167. );
  168. }
  169. $table = array(
  170. 'header' => array(),
  171. 'rows' => $rows,
  172. 'attributes' => array(
  173. 'id' => 'tripal_linker-table-expression-object',
  174. 'class' => 'tripal-data-table'
  175. ),
  176. 'sticky' => FALSE,
  177. 'caption' => "",
  178. 'colgroups' => array(),
  179. 'empty' => '',
  180. );
  181. $content .= theme_table($table);
  182. }
  183. // once we have our table array structure defined, we call Drupal's theme_table()
  184. // function to generate the table.
  185. $element[$delta] = array(
  186. '#type' => 'markup',
  187. '#markup' => $content,
  188. );
  189. }
  190. /**
  191. * @see TripalField::load()
  192. */
  193. public function load($field, $entity, $details) {
  194. $record = $details['record'];
  195. $field_name = $field['field_name'];
  196. $field_type = $field['type'];
  197. $field_table = $field['settings']['chado_table'];
  198. $field_column = $field['settings']['chado_column'];
  199. // Get the FK that links to the base record.
  200. $schema = chado_get_schema($field_table);
  201. $base_table = $details['record']->tablename;
  202. $pkey = $schema['primary key'][0];
  203. $fkey_lcolumn = key($schema['foreign keys'][$base_table]['columns']);
  204. $fkey_rcolumn = $schema['foreign keys'][$base_table]['columns'][$fkey_lcolumn];
  205. $linker_table = $base_table . '_expression';
  206. $options = array(
  207. 'return_array' => 1,
  208. );
  209. $record = chado_expand_var($record, 'table', $linker_table, $options);
  210. $exp_linkers = $record->$linker_table;
  211. if ($exp_linkers) {
  212. foreach ($exp_linkers as $i => $exp_linker) {
  213. // Because the unqiuename is a text field we must expand it.
  214. $expression = $exp_linker->expression_id;
  215. $expression = chado_expand_var($expression, 'field', 'expression.uniquename', $options);
  216. // Set the values that will be seen by the user on the page and in
  217. // web services, or anwhere this field is viewed.
  218. $entity->{$field_name}['und'][$i]['value'] = array(
  219. 'name' => $expression->uniquename,
  220. 'description' => $expression->description,
  221. //'md5checksum' => $expression->md5checksum,
  222. );
  223. // Add the pub information if a real pub is associated with the record.
  224. $pub = $exp_linker->pub_id;
  225. if ($pub->uniquename != 'null') {
  226. $pub_details = tripal_get_minimal_pub_info($pub);
  227. $entity->{$field_name}['und'][$i]['value']['publication'] = $pub_details;
  228. $entity->{$field_name}['und'][$i]['value']['publication']['@type'] = $pub->type_id->dbxref_id->db_id->name . ':' . $pub->type_id->dbxref_id->accession;
  229. $entity->{$field_name}['und'][$i]['value']['publication']['type'] = $pub->type_id->name;
  230. if (property_exists($pub, 'entity_id')) {
  231. $entity->{$field_name}['und'][$i]['publication'][0]['entity_id'] = $pub->entity_id;
  232. $entity->{$field_name}['und'][$i]['publication'][0]['entity_type'] = 'TripalEntity';
  233. }
  234. }
  235. // Add the linker_expressionprop
  236. $linkerprop_table = $linker_table . 'prop';
  237. if (db_table_exists('chado.' . $linkerprop_table)) {
  238. $exp_linker = chado_expand_var($exp_linker, 'table', $linkerprop_table, $options);
  239. $exp_linkerprops = $exp_linker->feature_expressionprop;
  240. if ($exp_linkerprops) {
  241. foreach ($exp_linkerprops AS $linkerprop) {
  242. $entity->{$field_name}['und'][$i]['value'][$linkerprop->type_id->name] = $linkerprop->value;
  243. }
  244. }
  245. }
  246. // Add the fields for the widget form. The name requres the following
  247. // format if the fields should be used as values for insertint/updating
  248. // into the chado table: [table_name]__[field_name]
  249. $entity->{$field_name}['und'][$i][$linker_table . '__expression_id'] = $expression->expression_id;
  250. $entity->{$field_name}['und'][$i][$linker_table . '__uniquename'] = $expression->uniquename;
  251. //$entity->{$field_name}['und'][$i][$linker_table . '__md5checksum'] = $expression->md5checksum;
  252. $entity->{$field_name}['und'][$i][$linker_table . '__description'] = $expression->description;
  253. }
  254. }
  255. }
  256. }