obi__organism.inc 6.9 KB

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