chado_linker__expression.inc 9.2 KB

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