chado_linker__expression.inc 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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' => array(
  62. 'name' => '',
  63. 'accession' => '',
  64. 'ns' => '',
  65. 'nsurl' => '',
  66. ),
  67. ),
  68. );
  69. return $field_info;
  70. }
  71. public function formatter_settings_summary($field, $instance, $view_mode) {
  72. }
  73. public function formatter_settings_form($field, $instance,
  74. $view_mode, $form, &$form_state) {
  75. }
  76. public function formatter_view(&$element, $entity_type, $entity,
  77. $field, $instance, $langcode, $items, $display) {
  78. // Get the settings
  79. $settings = $display['settings'];
  80. $record = $entity->chado_record;
  81. $content = '';
  82. foreach ($items as $delta => $item) {
  83. if (!$item['value']) {
  84. continue;
  85. }
  86. // Iterate through all of the children of the $item['value']. Add each
  87. // one as an independent row in the table.
  88. $rows = array();
  89. foreach ($item['value'] as $key => $value) {
  90. // If this key is the name, then we want to link to the entity if one
  91. // exists.
  92. if ($key == 'name') {
  93. if (array_key_exists('entity_id', $item) and $item['$entity_id']) {
  94. $entity_id = $item['entity_id'];
  95. $value = l($value, "bio_data/" . $entity_id, array('attributes' => array('target' => "_blank")));
  96. }
  97. }
  98. // If this key is the publication then we want to get the citation
  99. // and link to the pub if an entity exits.
  100. if ($key == 'publication') {
  101. $pub = $value['Citation'];
  102. if (array_key_exists('publication', $item) and array_key_exists('entity_id', $item['publication'][0])) {
  103. $entity_id = $item['publication'][0]['entity_id'];
  104. $title = $item['value']['publication']['Title'];
  105. $link = l($title, 'bio_data/' . $entity_id);
  106. $pub = preg_replace("/$title/", $link, $pub);
  107. }
  108. $value = $pub;
  109. }
  110. // Add the item as a new row.
  111. $rows[] = array(
  112. array(
  113. 'data' => ucfirst(str_replace('_', ' ', $key)),
  114. 'header' => TRUE,
  115. 'width' => '20%',
  116. ),
  117. $value
  118. );
  119. }
  120. $table = array(
  121. 'header' => array(),
  122. 'rows' => $rows,
  123. 'attributes' => array(
  124. 'id' => 'tripal_linker-table-expression-object',
  125. 'class' => 'tripal-data-table'
  126. ),
  127. 'sticky' => FALSE,
  128. 'caption' => "",
  129. 'colgroups' => array(),
  130. 'empty' => '',
  131. );
  132. $content .= theme_table($table);
  133. }
  134. // once we have our table array structure defined, we call Drupal's theme_table()
  135. // function to generate the table.
  136. $element[$delta] = array(
  137. '#type' => 'markup',
  138. '#markup' => $content,
  139. );
  140. }
  141. public function widget_form(&$widget, $form, $form_state, $field, $instance,
  142. $langcode, $items, $delta, $element) {
  143. }
  144. public function load($field, $entity, $details) {
  145. $record = $details['record'];
  146. $field_name = $field['field_name'];
  147. $field_type = $field['type'];
  148. $field_table = $field['settings']['chado_table'];
  149. $field_column = $field['settings']['chado_column'];
  150. // Get the FK that links to the base record.
  151. $schema = chado_get_schema($field_table);
  152. $base_table = $details['record']->tablename;
  153. $pkey = $schema['primary key'][0];
  154. $fkey_lcolumn = key($schema['foreign keys'][$base_table]['columns']);
  155. $fkey_rcolumn = $schema['foreign keys'][$base_table]['columns'][$fkey_lcolumn];
  156. $linker_table = $base_table . '_expression';
  157. $options = array(
  158. 'return_array' => 1,
  159. );
  160. $record = chado_expand_var($record, 'table', $linker_table, $options);
  161. $exp_linkers = $record->$linker_table;
  162. if ($exp_linkers) {
  163. foreach ($exp_linkers as $i => $exp_linker) {
  164. // Because the unqiuename is a text field we must expand it.
  165. $expression = $exp_linker->expression_id;
  166. $expression = chado_expand_var($expression, 'field', 'expression.uniquename', $options);
  167. // Set the values that will be seen by the user on the page and in
  168. // web services, or anwhere this field is viewed.
  169. $entity->{$field_name}['und'][$i]['value'] = array(
  170. 'name' => $expression->uniquename,
  171. 'description' => $expression->description,
  172. //'md5checksum' => $expression->md5checksum,
  173. );
  174. // Add the pub information if a real pub is associated with the record.
  175. $pub = $exp_linker->pub_id;
  176. if ($pub->uniquename != 'null') {
  177. $pub_details = tripal_get_minimal_pub_info($pub);
  178. $entity->{$field_name}['und'][$i]['value']['publication'] = $pub_details;
  179. $entity->{$field_name}['und'][$i]['value']['publication']['@type'] = $pub->type_id->dbxref_id->db_id->name . ':' . $pub->type_id->dbxref_id->accession;
  180. $entity->{$field_name}['und'][$i]['value']['publication']['type'] = $pub->type_id->name;
  181. if (property_exists($pub, 'entity_id')) {
  182. $entity->{$field_name}['und'][$i]['publication'][0]['entity_id'] = $pub->entity_id;
  183. $entity->{$field_name}['und'][$i]['publication'][0]['entity_type'] = 'TripalEntity';
  184. }
  185. }
  186. // Add the linker_expressionprop
  187. $linkerprop_table = $linker_table . 'prop';
  188. if (db_table_exists('chado.' . $linkerprop_table)) {
  189. $exp_linker = chado_expand_var($exp_linker, 'table', $linkerprop_table, $options);
  190. $exp_linkerprops = $exp_linker->feature_expressionprop;
  191. if ($exp_linkerprops) {
  192. foreach ($exp_linkerprops AS $linkerprop) {
  193. $entity->{$field_name}['und'][$i]['value'][$linkerprop->type_id->name] = $linkerprop->value;
  194. }
  195. }
  196. }
  197. // Add the fields for the widget form. The name requres the following
  198. // format if the fields should be used as values for insertint/updating
  199. // into the chado table: [table_name]__[field_name]
  200. $entity->{$field_name}['und'][$i][$linker_table . '__expression_id'] = $expression->expression_id;
  201. $entity->{$field_name}['und'][$i][$linker_table . '__uniquename'] = $expression->uniquename;
  202. //$entity->{$field_name}['und'][$i][$linker_table . '__md5checksum'] = $expression->md5checksum;
  203. $entity->{$field_name}['und'][$i][$linker_table . '__description'] = $expression->description;
  204. }
  205. }
  206. }
  207. public function settings_form($field, $instance, $has_data) {
  208. }
  209. }