chado_linker__expression.inc 6.7 KB

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