chado_linker__expression.inc 9.7 KB

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