chado_linker__expression.inc 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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. $content = '';
  148. $rows = array();
  149. foreach ($items as $delta => $item) {
  150. if (!$item['value']) {
  151. continue;
  152. }
  153. // Iterate through all of the children of the $item['value']. Add each
  154. // one as an independent row in the table.
  155. foreach ($item['value'] as $key => $value) {
  156. // If this key is the name, then we want to link to the entity if one
  157. // exists.
  158. if ($key == 'name') {
  159. if (array_key_exists('entity', $item['value']) and $item['value']['$entity_id']) {
  160. list($entity_type, $entity_id) = explode(':', $item['value']['entity']);
  161. $value = l($value, "bio_data/" . $entity_id, array('attributes' => array('target' => "_blank")));
  162. }
  163. }
  164. // If this key is the publication then we want to get the citation
  165. // and link to the pub if an entity exits.
  166. if ($key == 'publication') {
  167. $pub = $value['Citation'];
  168. if (array_key_exists('publication', $item) and array_key_exists('entity', $item['publication'][0])) {
  169. $entity_id = $item['publication'][0]['entity_id'];
  170. $title = $item['value']['publication']['Title'];
  171. $link = l($title, 'bio_data/' . $entity_id);
  172. $pub = preg_replace("/$title/", $link, $pub);
  173. }
  174. $value = $pub;
  175. }
  176. // Add the item as a new row.
  177. $rows[] = array(
  178. array(
  179. 'data' => ucfirst(str_replace('_', ' ', $key)),
  180. 'header' => TRUE,
  181. 'width' => '20%',
  182. ),
  183. $value
  184. );
  185. }
  186. }
  187. $table = array(
  188. 'header' => array(),
  189. 'rows' => $rows,
  190. 'attributes' => array(
  191. 'id' => 'tripal_linker-table-expression-object',
  192. 'class' => 'tripal-data-table'
  193. ),
  194. 'sticky' => FALSE,
  195. 'caption' => "",
  196. 'colgroups' => array(),
  197. 'empty' => 'There is no curated expression data.',
  198. );
  199. $content = theme_table($table);
  200. if (count($items) > 0) {
  201. // once we have our table array structure defined, we call Drupal's theme_table()
  202. // function to generate the table.
  203. $element[0] = array(
  204. '#type' => 'markup',
  205. '#markup' => $content,
  206. );
  207. }
  208. }
  209. /**
  210. * @see TripalField::load()
  211. */
  212. static function load($field, $entity, $details = array()) {
  213. $record = $details['record'];
  214. $field_name = $field['field_name'];
  215. $field_type = $field['type'];
  216. $field_table = $field['settings']['chado_table'];
  217. $field_column = $field['settings']['chado_column'];
  218. // Get the FK that links to the base record.
  219. $schema = chado_get_schema($field_table);
  220. $base_table = $details['record']->tablename;
  221. $pkey = $schema['primary key'][0];
  222. $fkey_lcolumn = key($schema['foreign keys'][$base_table]['columns']);
  223. $fkey_rcolumn = $schema['foreign keys'][$base_table]['columns'][$fkey_lcolumn];
  224. // Set some defaults for the empty record.
  225. $entity->{$field_name}['und'][0] = array(
  226. 'value' => array(),
  227. );
  228. $linker_table = $base_table . '_expression';
  229. $options = array(
  230. 'return_array' => 1,
  231. );
  232. $record = chado_expand_var($record, 'table', $linker_table, $options);
  233. $exp_linkers = $record->$linker_table;
  234. if ($exp_linkers) {
  235. foreach ($exp_linkers as $i => $exp_linker) {
  236. // Because the unqiuename is a text field we must expand it.
  237. $expression = $exp_linker->expression_id;
  238. $expression = chado_expand_var($expression, 'field', 'expression.uniquename', $options);
  239. // Set the values that will be seen by the user on the page and in
  240. // web services, or anwhere this field is viewed.
  241. $entity->{$field_name}['und'][$i]['value'] = array(
  242. 'name' => $expression->uniquename,
  243. 'description' => $expression->description,
  244. //'md5checksum' => $expression->md5checksum,
  245. );
  246. // Add the pub information if a real pub is associated with the record.
  247. $pub = $exp_linker->pub_id;
  248. if ($pub->uniquename != 'null') {
  249. $pub_details = tripal_get_minimal_pub_info($pub);
  250. $entity->{$field_name}['und'][$i]['value']['publication'] = $pub_details;
  251. $entity->{$field_name}['und'][$i]['value']['publication']['type'] = $pub->type_id->name;
  252. if (property_exists($pub, 'entity_id')) {
  253. $entity->{$field_name}['und'][$i]['publication'][0]['value']['entity'] = 'TripalEntity:' . $pub->entity_id;
  254. }
  255. }
  256. // Add the linker_expressionprop
  257. $linkerprop_table = $linker_table . 'prop';
  258. if (db_table_exists('chado.' . $linkerprop_table)) {
  259. $exp_linker = chado_expand_var($exp_linker, 'table', $linkerprop_table, $options);
  260. $exp_linkerprops = $exp_linker->feature_expressionprop;
  261. if ($exp_linkerprops) {
  262. foreach ($exp_linkerprops AS $linkerprop) {
  263. $entity->{$field_name}['und'][$i]['value'][$linkerprop->type_id->name] = $linkerprop->value;
  264. }
  265. }
  266. }
  267. // Add the fields for the widget form. The name requres the following
  268. // format if the fields should be used as values for insertint/updating
  269. // into the chado table: [table_name]__[field_name]
  270. $entity->{$field_name}['und'][$i][$linker_table . '__expression_id'] = $expression->expression_id;
  271. $entity->{$field_name}['und'][$i][$linker_table . '__uniquename'] = $expression->uniquename;
  272. //$entity->{$field_name}['und'][$i][$linker_table . '__md5checksum'] = $expression->md5checksum;
  273. $entity->{$field_name}['und'][$i][$linker_table . '__description'] = $expression->description;
  274. }
  275. }
  276. }
  277. }