obi__organism.inc 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. <?php
  2. class obi__organism extends ChadoField {
  3. // The default lable for this field.
  4. public static $default_label = 'Organism';
  5. // The default description for this field.
  6. public static $description = 'The organism to which this resource is associated.';
  7. // Provide a list of instance specific settings. These can be access within
  8. // the instanceSettingsForm. When the instanceSettingsForm is submitted
  9. // then Drupal with automatically change these settings for the instnace.
  10. // It is recommended to put settings at the instance level whenever possible.
  11. // If you override this variable in a child class be sure to replicate the
  12. // term_name, term_vocab, term_accession and term_fixed keys as these are
  13. // required for all TripalFields.
  14. public static $default_instance_settings = array(
  15. // The short name for the vocabulary (e.g. shcema, SO, GO, PATO, etc.).
  16. 'term_vocabulary' => 'OBI',
  17. // The name of the term.
  18. 'term_name' => 'organism',
  19. // The unique ID (i.e. accession) of the term.
  20. 'term_accession' => '0100026',
  21. // Set to TRUE if the site admin is allowed to change the term
  22. // type. This will create form elements when editing the field instance
  23. // to allow the site admin to change the term settings above.
  24. 'term_fixed' => FALSE,
  25. // The format for display of the organism.
  26. 'field_display_string' => '<i>[organism.genus] [organism.species]</i>',
  27. );
  28. // The default widget for this field.
  29. public static $default_widget = 'OBI__organism_widget';
  30. // The default formatter for this field.
  31. public static $default_formatter = 'OBI__organism_formatter';
  32. /**
  33. * @see TripalField::validate()
  34. */
  35. public function validate($entity_type, $entity, $field, $items, &$errors) {
  36. $settings = $this->field['settings'];
  37. $field_name = $this->field['field_name'];
  38. $field_type = $this->field['type'];
  39. $field_table = $this->instance['settings']['chado_table'];
  40. $field_column = $this->instance['settings']['chado_column'];
  41. // Get the field values.
  42. foreach ($items as $delta => $values) {
  43. // Get the field values.
  44. $organism_id = $values['chado-' . $field_table . '__organism_id'];
  45. if (!$organism_id or $organism_id == 0) {
  46. $errors[$field_name]['und'][0][] = array(
  47. 'message' => t("Please specify an organism."),
  48. 'error' => 'chado_base__organism_id'
  49. );
  50. }
  51. }
  52. }
  53. /**
  54. * @see TripalField::load()
  55. */
  56. public function load($entity) {
  57. $record = $entity->chado_record;
  58. $settings = $this->instance['settings'];
  59. $field_name = $this->field['field_name'];
  60. $field_type = $this->field['type'];
  61. $field_table = $this->instance['settings']['chado_table'];
  62. $field_column = $this->instance['settings']['chado_column'];
  63. // Get the terms for each of the keys for the 'values' property.
  64. $label_term = 'rdfs:label';
  65. $genus_term = tripal_get_chado_semweb_term('organism', 'genus');
  66. $species_term = tripal_get_chado_semweb_term('organism', 'species');
  67. $infraspecific_name_term = tripal_get_chado_semweb_term('organism', 'infraspecific_name');
  68. $infraspecific_type_term = tripal_get_chado_semweb_term('organism', 'type_id');
  69. // Set some defaults for the empty record.
  70. $entity->{$field_name}['und'][0] = array(
  71. 'value' => array(
  72. /*
  73. // Types of elements that will appear in the value array.
  74. $label_term => '',
  75. $genus_term => '',
  76. $species_term => '',
  77. */
  78. ),
  79. );
  80. if ($record) {
  81. $organism = $record->organism_id;
  82. $string = $settings['field_display_string'];
  83. $label = tripal_replace_chado_tokens($string, $organism);
  84. $entity->{$field_name}['und'][0]['value'] = array(
  85. $label_term => $label,
  86. $genus_term => $organism->genus,
  87. $species_term => $organism->species,
  88. );
  89. // The infraspecific fiels were introdcued in Chado v1.3.
  90. if (property_exists($organism, 'infraspecific_name')) {
  91. $entity->{$field_name}['und'][0]['value'][$infraspecific_type_term] = NULL;
  92. $entity->{$field_name}['und'][0]['value'][$infraspecific_name_term] = $organism->infraspecific_name;
  93. if ($organism->type_id) {
  94. $entity->{$field_name}['und'][0]['value'][$infraspecific_type_term] = $organism->type_id->name;
  95. }
  96. }
  97. $entity->{$field_name}['und'][0]['chado-' . $field_table . '__organism_id'] = $organism->organism_id;
  98. // Is there a published entity for this organism?
  99. if (property_exists($entity->chado_record->$field_column, 'entity_id')) {
  100. $fk_entity_id = $entity->chado_record->$field_column->entity_id;
  101. $entity->{$field_name}['und'][0]['value']['entity'] = 'TripalEntity:' . $fk_entity_id;
  102. }
  103. }
  104. }
  105. /**
  106. * @see TripalField::globalSettingsForm()
  107. */
  108. public function settingsForm($has_data) {
  109. $element = parent::globalSettingsForm($has_data);
  110. $settings = $this->field['settings'];
  111. $element['instructions'] = array(
  112. '#type' => 'item',
  113. '#markup' => 'You may rewrite the way this field is presented to the end-user.
  114. The Rewrite Value field allows you to use tokens to indicate how the
  115. value should be displayed. Tokens will be substituted with appriorate
  116. data from the database. See the Available tokens list for the
  117. tokens you may use.'
  118. );
  119. $element['field_display_string'] = array(
  120. '#type' => 'textfield',
  121. '#title' => 'Rewrite Value',
  122. '#description' => t('Provide a mixture of text and/or tokens for the format.
  123. For example: [organism.genus] [organism.species]. When displayed,
  124. the tokens will be replaced with the actual value.'),
  125. '#default_value' => $settings['field_display_string'],
  126. );
  127. $element['tokens'] = array(
  128. '#type' => 'fieldset',
  129. '#collapsed' => TRUE,
  130. '#collapsible' => TRUE,
  131. '#title' => 'Available Tokens'
  132. );
  133. $headers = array('Token', 'Description');
  134. $rows = array();
  135. // Here we use the tripal_get_chado_tokens rather than the
  136. // tripal_get_entity_tokens because we can't gurantee that all organisms
  137. // have entities.
  138. $tokens = tripal_get_chado_tokens('organism');
  139. foreach ($tokens as $token) {
  140. $rows[] = array(
  141. $token['token'],
  142. $token['description'],
  143. );
  144. }
  145. $table_vars = array(
  146. 'header' => $headers,
  147. 'rows' => $rows,
  148. 'attributes' => array(),
  149. 'sticky' => FALSE,
  150. 'caption' => '',
  151. 'colgroups' => array(),
  152. 'empty' => 'There are no tokens',
  153. );
  154. $element['tokens']['list'] = array(
  155. '#type' => 'item',
  156. '#markup' => theme_table($table_vars),
  157. );
  158. // Add in the semantic web fields.
  159. $parent_elements = parent::settings_form($field, $instance, $has_data);
  160. $element = array_merge($element, $parent_elements);
  161. return $element;
  162. }
  163. /**
  164. * @see ChadoField::query()
  165. */
  166. public function query($query, $condition) {
  167. $alias = $this->field['field_name'];
  168. $operator = $condition['operator'];
  169. $query->join('organism', $alias, "base.organism_id = $alias.organism_id");
  170. if ($condition['column'] == 'organism.species') {
  171. $query->condition("$alias.species", $condition['value'], $operator);
  172. }
  173. if ($condition['column'] == 'organism.genus') {
  174. $query->condition("$alias.genus", $condition['value'], $operator);
  175. }
  176. if ($condition['column'] == 'organism.infraspecies') {
  177. $query->condition("$alias.infraspecific_name", $condition['value'], $operator);
  178. }
  179. if ($condition['column'] == 'organism.infraspecific_taxon') {
  180. $query->join('cvterm', 'CVT', "base.type_id = CVT.cvterm_id");
  181. $query->condition("CVT.name", $condition['value'], $operator);
  182. }
  183. }
  184. /**
  185. * @see ChadoField::queryOrder()
  186. */
  187. public function queryOrder($query, $order) {
  188. // If the table hasn't yet been joined then add it.
  189. $joins = $query->getTables();
  190. if (!in_array($this->field['field_name'], $joins)) {
  191. $alias = $this->field['field_name'];
  192. $query->join('organism', $alias, "base.organism_id = $alias.organism_id");
  193. }
  194. // Now perform the sort.
  195. if ($order['column'] == 'organism.species') {
  196. $query->orderBy("$alias.genus", $order['direction']);
  197. }
  198. if ($order['column'] == 'organism.genus') {
  199. $query->orderBy("$alias.species", $order['direction']);
  200. }
  201. if ($order['column'] == 'organism.infraspecies') {
  202. $query->orderBy("$alias.infraspecific_name", $order['direction']);
  203. }
  204. if ($order['column'] == 'organism.infraspecies') {
  205. if (!in_array('CVT', $joins)) {
  206. $query->join('cvterm', 'CVT', "base.type_id = CVT.cvterm_id");
  207. }
  208. $query->orderBy("CVT.name", $order['direction']);
  209. }
  210. }
  211. }