chado_linker__expression.inc 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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::formatterView()
  23. */
  24. public function formatterView(&$element, $entity_type, $entity, $langcode, $items, $display) {
  25. // Get the settings
  26. $settings = $display['settings'];
  27. $content = '';
  28. $rows = array();
  29. foreach ($items as $delta => $item) {
  30. if (!$item['value']) {
  31. continue;
  32. }
  33. // Iterate through all of the children of the $item['value']. Add each
  34. // one as an independent row in the table.
  35. foreach ($item['value'] as $key => $value) {
  36. // If this key is the name, then we want to link to the entity if one
  37. // exists.
  38. if ($key == 'name') {
  39. if (array_key_exists('entity', $item['value']) and $item['value']['$entity_id']) {
  40. list($entity_type, $entity_id) = explode(':', $item['value']['entity']);
  41. $value = l($value, "bio_data/" . $entity_id, array('attributes' => array('target' => "_blank")));
  42. }
  43. }
  44. // If this key is the publication then we want to get the citation
  45. // and link to the pub if an entity exits.
  46. if ($key == 'publication') {
  47. $pub = $value['Citation'];
  48. if (array_key_exists('publication', $item) and array_key_exists('entity', $item['publication'][0])) {
  49. $entity_id = $item['publication'][0]['entity_id'];
  50. $title = $item['value']['publication']['Title'];
  51. $link = l($title, 'bio_data/' . $entity_id);
  52. $pub = preg_replace("/$title/", $link, $pub);
  53. }
  54. $value = $pub;
  55. }
  56. // Add the item as a new row.
  57. $rows[] = array(
  58. array(
  59. 'data' => ucfirst(str_replace('_', ' ', $key)),
  60. 'header' => TRUE,
  61. 'width' => '20%',
  62. ),
  63. $value
  64. );
  65. }
  66. }
  67. $table = array(
  68. 'header' => array(),
  69. 'rows' => $rows,
  70. 'attributes' => array(
  71. 'id' => 'tripal_linker-table-expression-object',
  72. 'class' => 'tripal-data-table'
  73. ),
  74. 'sticky' => FALSE,
  75. 'caption' => "",
  76. 'colgroups' => array(),
  77. 'empty' => 'There is no curated expression data.',
  78. );
  79. $content = theme_table($table);
  80. if (count($items) > 0) {
  81. // once we have our table array structure defined, we call Drupal's theme_table()
  82. // function to generate the table.
  83. $element[0] = array(
  84. '#type' => 'markup',
  85. '#markup' => $content,
  86. );
  87. }
  88. }
  89. /**
  90. * @see TripalField::load()
  91. */
  92. public function load($entity, $details = array()) {
  93. $record = $details['record'];
  94. $field_name = $this->field['field_name'];
  95. $field_type = $this->field['type'];
  96. $field_table = $this->field['settings']['chado_table'];
  97. $field_column = $this->field['settings']['chado_column'];
  98. // Get the FK that links to the base record.
  99. $schema = chado_get_schema($field_table);
  100. $base_table = $details['record']->tablename;
  101. $pkey = $schema['primary key'][0];
  102. $fkey_lcolumn = key($schema['foreign keys'][$base_table]['columns']);
  103. $fkey_rcolumn = $schema['foreign keys'][$base_table]['columns'][$fkey_lcolumn];
  104. // Set some defaults for the empty record.
  105. $entity->{$field_name}['und'][0] = array(
  106. 'value' => array(),
  107. );
  108. $linker_table = $base_table . '_expression';
  109. $options = array(
  110. 'return_array' => 1,
  111. );
  112. $record = chado_expand_var($record, 'table', $linker_table, $options);
  113. $exp_linkers = $record->$linker_table;
  114. if ($exp_linkers) {
  115. foreach ($exp_linkers as $i => $exp_linker) {
  116. // Because the unqiuename is a text field we must expand it.
  117. $expression = $exp_linker->expression_id;
  118. $expression = chado_expand_var($expression, 'field', 'expression.uniquename', $options);
  119. // Set the values that will be seen by the user on the page and in
  120. // web services, or anwhere this field is viewed.
  121. $entity->{$field_name}['und'][$i]['value'] = array(
  122. 'name' => $expression->uniquename,
  123. 'description' => $expression->description,
  124. //'md5checksum' => $expression->md5checksum,
  125. );
  126. // Add the pub information if a real pub is associated with the record.
  127. $pub = $exp_linker->pub_id;
  128. if ($pub->uniquename != 'null') {
  129. $pub_details = tripal_get_minimal_pub_info($pub);
  130. $entity->{$field_name}['und'][$i]['value']['publication'] = $pub_details;
  131. $entity->{$field_name}['und'][$i]['value']['publication']['type'] = $pub->type_id->name;
  132. if (property_exists($pub, 'entity_id')) {
  133. $entity->{$field_name}['und'][$i]['publication'][0]['value']['entity'] = 'TripalEntity:' . $pub->entity_id;
  134. }
  135. }
  136. // Add the linker_expressionprop
  137. $linkerprop_table = $linker_table . 'prop';
  138. if (chado_table_exists($linkerprop_table)) {
  139. $exp_linker = chado_expand_var($exp_linker, 'table', $linkerprop_table, $options);
  140. $exp_linkerprops = $exp_linker->feature_expressionprop;
  141. if ($exp_linkerprops) {
  142. foreach ($exp_linkerprops AS $linkerprop) {
  143. $entity->{$field_name}['und'][$i]['value'][$linkerprop->type_id->name] = $linkerprop->value;
  144. }
  145. }
  146. }
  147. // Add the fields for the widget form. The name requres the following
  148. // format if the fields should be used as values for insertint/updating
  149. // into the chado table: [table_name]__[field_name]
  150. $entity->{$field_name}['und'][$i][$linker_table . '__expression_id'] = $expression->expression_id;
  151. $entity->{$field_name}['und'][$i][$linker_table . '__uniquename'] = $expression->uniquename;
  152. //$entity->{$field_name}['und'][$i][$linker_table . '__md5checksum'] = $expression->md5checksum;
  153. $entity->{$field_name}['und'][$i][$linker_table . '__description'] = $expression->description;
  154. }
  155. }
  156. }
  157. /**
  158. * We don't want a widget so override this function.
  159. */
  160. public static function widgetInfo() {
  161. return array();
  162. }
  163. }