obi__organism.inc 7.2 KB

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