chado_base__organism_id.inc 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. <?php
  2. class chado_base__organism_id 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 $default_description = 'A field for specifying an organism.';
  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. // 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. if (count($items) > 0) {
  25. $content = $items[0]['value']['label'];
  26. if (array_key_exists('entity', $items[0]['value'])) {
  27. list($entity_type, $entity_id) = explode(':', $items[0]['value']['entity']);
  28. $content = l(strip_tags($items[0]['value']['label']), 'bio_data/' . $entity_id);
  29. }
  30. // The cardinality of this field is 1 so we don't have to
  31. // iterate through the items array, as there will never be more than 1.
  32. $element[0] = array(
  33. '#type' => 'markup',
  34. '#markup' => $content,
  35. );
  36. }
  37. }
  38. /**
  39. * @see TripalField::widget()
  40. */
  41. public function widgetForm(&$widget, &$form, &$form_state, $langcode, $items, $delta, $element) {
  42. parent::widgetForm($widget, $form, $form_state, $langcode, $items, $delta, $element);
  43. $settings = $this->field['settings'];
  44. $field_name = $this->field['field_name'];
  45. $field_type = $this->field['type'];
  46. $field_table = $this->instance['settings']['chado_table'];
  47. $field_column = $this->instance['settings']['chado_column'];
  48. $organism_id = 0;
  49. if (count($items) > 0 and array_key_exists('chado-' . $field_table . '__organism_id', $items[0])) {
  50. $organism_id = $items[0]['chado-' . $field_table . '__organism_id'];
  51. }
  52. $widget['value'] = array(
  53. '#type' => 'value',
  54. '#value' => array_key_exists($delta, $items) ? $items[$delta]['value'] : '',
  55. );
  56. $options = tripal_get_organism_select_options(FALSE);
  57. $widget['chado-' . $field_table . '__organism_id'] = array(
  58. '#type' => 'select',
  59. '#title' => $element['#title'],
  60. '#description' => $element['#description'],
  61. '#options' => $options,
  62. '#default_value' => $organism_id,
  63. '#required' => $element['#required'],
  64. '#weight' => isset($element['#weight']) ? $element['#weight'] : 0,
  65. '#delta' => $delta,
  66. );
  67. }
  68. /**
  69. * @see TripalField::validate()
  70. */
  71. public function validate($entity_type, $entity, $field, $items, &$errors) {
  72. $settings = $this->field['settings'];
  73. $field_name = $this->field['field_name'];
  74. $field_type = $this->field['type'];
  75. $field_table = $this->instance['settings']['chado_table'];
  76. $field_column = $this->instance['settings']['chado_column'];
  77. // Get the field values.
  78. foreach ($items as $delta => $values) {
  79. // Get the field values.
  80. $organism_id = $values['chado-' . $field_table . '__organism_id'];
  81. if (!$organism_id or $organism_id == 0) {
  82. $errors[$field_name]['und'][0][] = array(
  83. 'message' => t("Please specify an organism."),
  84. 'error' => 'chado_base__organism_id'
  85. );
  86. }
  87. }
  88. }
  89. /**
  90. * @see TripalField::load()
  91. */
  92. public function load($entity, $details = array()) {
  93. $record = $details['record'];
  94. $settings = $this->field['settings'];
  95. $field_name = $this->field['field_name'];
  96. $field_type = $this->field['type'];
  97. $field_table = $this->instance['settings']['chado_table'];
  98. $field_column = $this->instance['settings']['chado_column'];
  99. // Set some defaults for the empty record.
  100. $entity->{$field_name}['und'][0] = array(
  101. 'value' => array(
  102. 'label' => '',
  103. 'genus' => '',
  104. 'species' => '',
  105. ),
  106. 'semantic_web' => array(
  107. 'label' => 'rdfs:label',
  108. 'genus' => tripal_get_chado_semweb_term('organism', 'genus'),
  109. 'species' => tripal_get_chado_semweb_term('organism', 'species'),
  110. 'infraspecific_name' => tripal_get_chado_semweb_term('organism', 'infraspecific_name'),
  111. 'infraspecific_type' => tripal_get_chado_semweb_term('organism', 'type_id'),
  112. ),
  113. 'chado_mapping' => array(
  114. 'genus' => 'organism_id,genus',
  115. 'species' => 'organism_id,genus',
  116. 'infraspecific_name' => 'organism_id,infraspecific_name',
  117. 'infraspecific_type' => 'organism_id,infraspecific_type',
  118. )
  119. );
  120. if ($record) {
  121. $organism = $record->organism_id;
  122. $string = $settings['field_display_string'];
  123. $label = tripal_replace_chado_tokens($string, $organism);
  124. $entity->{$field_name}['und'][0]['value'] = array(
  125. 'label' => $label,
  126. 'genus' => $organism->genus,
  127. 'species' => $organism->species,
  128. );
  129. // The infraspecific fiels were introdcued in Chado v1.3.
  130. if (property_exists($organism, 'infraspecific_name')) {
  131. $entity->{$field_name}['und'][0]['value']['infraspecific_type'] = NULL;
  132. $entity->{$field_name}['und'][0]['value']['infraspecific_name'] = $organism->infraspecific_name;
  133. if ($organism->type_id) {
  134. $entity->{$field_name}['und'][0]['value']['infraspecific_type'] = $organism->type_id->name;
  135. }
  136. }
  137. $entity->{$field_name}['und'][0]['chado-' . $field_table . '__organism_id'] = $organism->organism_id;
  138. // Is there a published entity for this organism?
  139. if (property_exists($entity->chado_record->$field_column, 'entity_id')) {
  140. $fk_entity_id = $entity->chado_record->$field_column->entity_id;
  141. $entity->{$field_name}['und'][0]['value']['entity'] = 'TripalEntity:' . $fk_entity_id;
  142. }
  143. }
  144. }
  145. /**
  146. * @see TripalField::globalSettingsForm()
  147. */
  148. public function globalSettingsForm($has_data) {
  149. $element = parent::globalSettingsForm($has_data);
  150. $settings = $this->field['settings'];
  151. $element['instructions'] = array(
  152. '#type' => 'item',
  153. '#markup' => 'You may rewrite the way this field is presented to the end-user.
  154. The Rewrite Value field allows you to use tokens to indicate how the
  155. value should be displayed. Tokens will be substituted with appriorate
  156. data from the database. See the Available tokens list for the
  157. tokens you may use.'
  158. );
  159. $element['field_display_string'] = array(
  160. '#type' => 'textfield',
  161. '#title' => 'Rewrite Value',
  162. '#description' => t('Provide a mixture of text and/or tokens for the format.
  163. For example: [organism.genus] [organism.species]. When displayed,
  164. the tokens will be replaced with the actual value.'),
  165. '#default_value' => $settings['field_display_string'],
  166. );
  167. $element['tokens'] = array(
  168. '#type' => 'fieldset',
  169. '#collapsed' => TRUE,
  170. '#collapsible' => TRUE,
  171. '#title' => 'Available Tokens'
  172. );
  173. $headers = array('Token', 'Description');
  174. $rows = array();
  175. // Here we use the tripal_get_chado_tokens rather than the
  176. // tripal_get_entity_tokens because we can't gurantee that all organisms
  177. // have entities.
  178. $tokens = tripal_get_chado_tokens('organism');
  179. foreach ($tokens as $token) {
  180. $rows[] = array(
  181. $token['token'],
  182. $token['description'],
  183. );
  184. }
  185. $table_vars = array(
  186. 'header' => $headers,
  187. 'rows' => $rows,
  188. 'attributes' => array(),
  189. 'sticky' => FALSE,
  190. 'caption' => '',
  191. 'colgroups' => array(),
  192. 'empty' => 'There are no tokens',
  193. );
  194. $element['tokens']['list'] = array(
  195. '#type' => 'item',
  196. '#markup' => theme_table($table_vars),
  197. );
  198. // Add in the semantic web fields.
  199. $parent_elements = parent::settings_form($field, $instance, $has_data);
  200. $element = array_merge($element, $parent_elements);
  201. return $element;
  202. }
  203. }