chado_linker__expression.inc 10 KB

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