chado_linker__expression.inc 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. <?php
  2. class chado_linker__expression extends TripalField {
  3. public function field_info() {
  4. return array(
  5. 'label' => t('Expression'),
  6. 'description' => t('Associates an expression with
  7. this record.'),
  8. 'default_widget' => 'chado_linker__expression_widget',
  9. 'default_formatter' => 'chado_linker__expression_formatter',
  10. 'settings' => array(),
  11. 'storage' => array(
  12. 'type' => 'field_chado_storage',
  13. 'module' => 'tripal_chado',
  14. 'active' => TRUE
  15. ),
  16. );
  17. }
  18. function widget_info() {
  19. return array(
  20. 'label' => t('Expressions'),
  21. 'field types' => array('chado_linker__expression'),
  22. );
  23. }
  24. public function formatter_info() {
  25. return array(
  26. 'label' => t('Expression'),
  27. 'field types' => array('chado_linker__expression'),
  28. 'settings' => array(
  29. ),
  30. );
  31. }
  32. public function attach_info($entity_type, $bundle, $settings) {
  33. $field_info = array();
  34. $table_name = $settings['data_table'];
  35. $type_table = $settings['type_table'];
  36. $type_field = $settings['field'];
  37. $cv_id = $settings['cv_id'];
  38. $cvterm_id = $settings['cvterm_id'];
  39. // If the linker table does not exists then we don't want to add attach.
  40. $expression_table = $table_name . '_expression';
  41. if (!chado_table_exists($expression_table)) {
  42. return $field_info;
  43. }
  44. $schema = chado_get_schema($expression_table);
  45. $pkey = $schema['primary key'][0];
  46. // Initialize the field array.
  47. $field_info = array(
  48. 'field_name' => $table_name . '_expression',
  49. 'field_type' => 'chado_linker__expression',
  50. 'widget_type' => 'chado_linker__expression_widget',
  51. 'widget_settings' => array('display_label' => 1),
  52. 'description' => '',
  53. 'label' => 'Expression',
  54. 'is_required' => 0,
  55. 'cardinality' => FIELD_CARDINALITY_UNLIMITED,
  56. 'storage' => 'field_chado_storage',
  57. 'field_settings' => array(
  58. 'chado_table' => $expression_table,
  59. 'chado_column' => $pkey,
  60. 'base_table' => $table_name,
  61. 'semantic_web' => 'local:expression',
  62. ),
  63. );
  64. return $field_info;
  65. }
  66. public function formatter_settings_summary($field, $instance, $view_mode) {
  67. }
  68. public function formatter_settings_form($field, $instance,
  69. $view_mode, $form, &$form_state) {
  70. }
  71. public function formatter_view(&$element, $entity_type, $entity,
  72. $field, $instance, $langcode, $items, $display) {
  73. // Get the settings
  74. $settings = $display['settings'];
  75. $record = $entity->chado_record;
  76. $content = '';
  77. foreach ($items as $delta => $item) {
  78. if (!$item['value']) {
  79. continue;
  80. }
  81. // Iterate through all of the children of the $item['value']. Add each
  82. // one as an independent row in the table.
  83. $rows = array();
  84. foreach ($item['value'] as $key => $value) {
  85. // If this key is the name, then we want to link to the entity if one
  86. // exists.
  87. if ($key == 'name') {
  88. if (array_key_exists('entity_id', $item) and $item['$entity_id']) {
  89. $entity_id = $item['entity_id'];
  90. $value = l($value, "bio_data/" . $entity_id, array('attributes' => array('target' => "_blank")));
  91. }
  92. }
  93. // If this key is the publication then we want to get the citation
  94. // and link to the pub if an entity exits.
  95. if ($key == 'publication') {
  96. $pub = $value['Citation'];
  97. if (array_key_exists('publication', $item) and array_key_exists('entity_id', $item['publication'][0])) {
  98. $entity_id = $item['publication'][0]['entity_id'];
  99. $title = $item['value']['publication']['Title'];
  100. $link = l($title, 'bio_data/' . $entity_id);
  101. $pub = preg_replace("/$title/", $link, $pub);
  102. }
  103. $value = $pub;
  104. }
  105. // Add the item as a new row.
  106. $rows[] = array(
  107. array(
  108. 'data' => ucfirst(str_replace('_', ' ', $key)),
  109. 'header' => TRUE,
  110. 'width' => '20%',
  111. ),
  112. $value
  113. );
  114. }
  115. $table = array(
  116. 'header' => array(),
  117. 'rows' => $rows,
  118. 'attributes' => array(
  119. 'id' => 'tripal_linker-table-expression-object',
  120. 'class' => 'tripal-data-table'
  121. ),
  122. 'sticky' => FALSE,
  123. 'caption' => "",
  124. 'colgroups' => array(),
  125. 'empty' => '',
  126. );
  127. $content .= theme_table($table);
  128. }
  129. // once we have our table array structure defined, we call Drupal's theme_table()
  130. // function to generate the table.
  131. $element[$delta] = array(
  132. '#type' => 'markup',
  133. '#markup' => $content,
  134. );
  135. }
  136. public function widget_form(&$widget, $form, $form_state, $field, $instance,
  137. $langcode, $items, $delta, $element) {
  138. }
  139. public function load($field, $entity, $details) {
  140. $record = $details['record'];
  141. $field_name = $field['field_name'];
  142. $field_type = $field['type'];
  143. $field_table = $field['settings']['chado_table'];
  144. $field_column = $field['settings']['chado_column'];
  145. // Get the FK that links to the base record.
  146. $schema = chado_get_schema($field_table);
  147. $base_table = $details['record']->tablename;
  148. $pkey = $schema['primary key'][0];
  149. $fkey_lcolumn = key($schema['foreign keys'][$base_table]['columns']);
  150. $fkey_rcolumn = $schema['foreign keys'][$base_table]['columns'][$fkey_lcolumn];
  151. $linker_table = $base_table . '_expression';
  152. $options = array(
  153. 'return_array' => 1,
  154. );
  155. $record = chado_expand_var($record, 'table', $linker_table, $options);
  156. $exp_linkers = $record->$linker_table;
  157. if ($exp_linkers) {
  158. foreach ($exp_linkers as $i => $exp_linker) {
  159. // Because the unqiuename is a text field we must expand it.
  160. $expression = $exp_linker->expression_id;
  161. $expression = chado_expand_var($expression, 'field', 'expression.uniquename', $options);
  162. // Set the values that will be seen by the user on the page and in
  163. // web services, or anwhere this field is viewed.
  164. $entity->{$field_name}['und'][$i]['value'] = array(
  165. 'name' => $expression->uniquename,
  166. 'description' => $expression->description,
  167. //'md5checksum' => $expression->md5checksum,
  168. );
  169. // Add the pub information if a real pub is associated with the record.
  170. $pub = $exp_linker->pub_id;
  171. if ($pub->uniquename != 'null') {
  172. $pub_details = tripal_get_minimal_pub_info($pub);
  173. $entity->{$field_name}['und'][$i]['value']['publication'] = $pub_details;
  174. $entity->{$field_name}['und'][$i]['value']['publication']['@type'] = $pub->type_id->dbxref_id->db_id->name . ':' . $pub->type_id->dbxref_id->accession;
  175. $entity->{$field_name}['und'][$i]['value']['publication']['type'] = $pub->type_id->name;
  176. if (property_exists($pub, 'entity_id')) {
  177. $entity->{$field_name}['und'][$i]['publication'][0]['entity_id'] = $pub->entity_id;
  178. $entity->{$field_name}['und'][$i]['publication'][0]['entity_type'] = 'TripalEntity';
  179. }
  180. }
  181. // Add the linker_expressionprop
  182. $linkerprop_table = $linker_table . 'prop';
  183. if (db_table_exists('chado.' . $linkerprop_table)) {
  184. $exp_linker = chado_expand_var($exp_linker, 'table', $linkerprop_table, $options);
  185. $exp_linkerprops = $exp_linker->feature_expressionprop;
  186. if ($exp_linkerprops) {
  187. foreach ($exp_linkerprops AS $linkerprop) {
  188. $entity->{$field_name}['und'][$i]['value'][$linkerprop->type_id->name] = $linkerprop->value;
  189. }
  190. }
  191. }
  192. // Add the fields for the widget form. The name requres the following
  193. // format if the fields should be used as values for insertint/updating
  194. // into the chado table: [table_name]__[field_name]
  195. $entity->{$field_name}['und'][$i][$linker_table . '__expression_id'] = $expression->expression_id;
  196. $entity->{$field_name}['und'][$i][$linker_table . '__uniquename'] = $expression->uniquename;
  197. //$entity->{$field_name}['und'][$i][$linker_table . '__md5checksum'] = $expression->md5checksum;
  198. $entity->{$field_name}['und'][$i][$linker_table . '__description'] = $expression->description;
  199. }
  200. }
  201. }
  202. public function settings_form($field, $instance, $has_data) {
  203. }
  204. }