chado_base__organism_id.inc 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. <?php
  2. class chado_base__organism_id extends TripalField {
  3. /**
  4. * @see TripalField::info()
  5. */
  6. public function field_info() {
  7. return array(
  8. 'label' => t('Organism'),
  9. 'description' => t('A field for specifying an organism.'),
  10. 'default_widget' => 'chado_base__organism_id_widget',
  11. 'default_formatter' => 'chado_base__organism_id_formatter',
  12. 'settings' => array(
  13. 'field_display_string' => '<i>[organism.genus] [organism.species]</i>',
  14. ),
  15. 'storage' => array(
  16. 'type' => 'field_chado_storage',
  17. 'module' => 'tripal_chado',
  18. 'active' => TRUE
  19. ),
  20. );
  21. }
  22. /**
  23. * @see TripalField::attach_info()
  24. */
  25. public function attach_info($entity_type, $bundle, $settings) {
  26. $field_info = array();
  27. $table_name = $settings['data_table'];
  28. $type_table = $settings['type_table'];
  29. $type_field = $settings['field'];
  30. $cv_id = $settings['cv_id'];
  31. $cvterm_id = $settings['cvterm_id'];
  32. // Check the schema for the data table if it does not have
  33. // an 'organism_id' column then we don't want to attach this field.
  34. $schema = chado_get_schema($table_name);
  35. if (!array_key_exists('organism_id', $schema['fields'])) {
  36. return $field_info;
  37. }
  38. // If this is the organism table then do not attach as that is the
  39. // primary key
  40. if ($table_name == 'organism') {
  41. return $field_info;
  42. }
  43. // There is an organism_id column so attach the field!
  44. $field_info = array(
  45. 'field_name' => $table_name . '__organism_id',
  46. 'field_type' => 'chado_base__organism_id',
  47. 'widget_type' => 'chado_base__organism_id_widget',
  48. 'description' => 'Select an organism.',
  49. 'label' => 'Organism',
  50. 'is_required' => 0,
  51. 'storage' => 'field_chado_storage',
  52. 'widget_settings' => array(
  53. 'display_label' => 1
  54. ),
  55. 'field_settings' => array(
  56. 'chado_table' => $table_name,
  57. 'chado_column' => 'organism_id',
  58. 'semantic_web' => array(
  59. 'name' => 'organism',
  60. 'accession' => 'organism',
  61. 'ns' => 'local',
  62. 'nsurl' => '',
  63. ),
  64. ),
  65. );
  66. return $field_info;
  67. }
  68. /**
  69. * @see TripalField::widget_info()
  70. */
  71. public function widget_info() {
  72. return array(
  73. 'label' => t('Organism Select'),
  74. 'field types' => array('chado_base__organism_id')
  75. );
  76. }
  77. /**
  78. * @see TripalField::formatter_info()
  79. */
  80. public function formatter_info() {
  81. return array(
  82. 'label' => t('Organism'),
  83. 'field types' => array('chado_base__organism_id'),
  84. 'settings' => array(
  85. ),
  86. );
  87. }
  88. /**
  89. * @see TripalField::formatter_view()
  90. */
  91. public function formatter_view(&$element, $entity_type, $entity,
  92. $field, $instance, $langcode, $items, $display) {
  93. $content = $items[0]['value'];
  94. if (array_key_exists('entity_id', $items[0])) {
  95. $content = l(strip_tags($items[0]['value']), 'bio_data/' . $items[0]['entity_id']);
  96. }
  97. // The cardinality of this field is 1 so we don't have to
  98. // iterate through the items array, as there will never be more than 1.
  99. $element[0] = array(
  100. '#type' => 'markup',
  101. '#markup' => $content,
  102. );
  103. }
  104. /**
  105. * @see TripalField::widget()
  106. */
  107. public function widget_form(&$widget, $form, $form_state, $field, $instance,
  108. $langcode, $items, $delta, $element) {
  109. $default_value = 0;
  110. if (array_key_exists('organism_id', $items[0])) {
  111. $default_value = $items[0]['organism_id'];
  112. }
  113. $options = tripal_get_organism_select_options(FALSE);
  114. $widget['value'] = array(
  115. '#type' => 'select',
  116. '#title' => $element['#title'],
  117. '#description' => $element['#description'],
  118. '#options' => $options,
  119. '#default_value' => $default_value,
  120. '#required' => $element['#required'],
  121. '#weight' => isset($element['#weight']) ? $element['#weight'] : 0,
  122. '#delta' => $delta,
  123. '#element_validate' => array('chado_base__organism_id_widget_validate'),
  124. );
  125. $widget['add_organism'] = array(
  126. '#type' => 'item',
  127. '#markup' => l('Add a new species', 'admin/content/bio_data/add/species', array('attributes' => array('target' => '_blank'))),
  128. );
  129. }
  130. /**
  131. * @see TripalField::load()
  132. */
  133. public function load($field, $entity, $details) {
  134. $record = $details['record'];
  135. $settings = $field['settings'];
  136. $field_name = $field['field_name'];
  137. $field_type = $field['type'];
  138. $field_table = $field['settings']['chado_table'];
  139. $field_column = $field['settings']['chado_column'];
  140. $organism = $record->organism_id;
  141. $string = $settings['field_display_string'];
  142. $value = tripal_replace_chado_tokens($string, $organism);
  143. $entity->{$field_name}['und'][0]['value'] = $value;
  144. $entity->{$field_name}['und'][0]['organism_id'] = $organism->organism_id;
  145. // Is there a published entity for this organism?
  146. if (property_exists($entity->chado_record->$field_column, 'entity_id')) {
  147. $fk_entity_id = $entity->chado_record->$field_column->entity_id;
  148. $entity->{$field_name}['und'][0]['entity_id'] = $fk_entity_id;
  149. $entity->{$field_name}['und'][0]['entity_type'] = 'TripalEntity';
  150. }
  151. }
  152. /**
  153. * @see TripalField::settings_form()
  154. */
  155. public function settings_form($field, $instance, $has_data) {
  156. $settings = $field['settings'];
  157. $element = array();
  158. $element['instructions'] = array(
  159. '#type' => 'item',
  160. '#markup' => 'You may rewrite the way this field is presented to the end-user.
  161. The Rewrite Value field allows you to use tokens to indicate how the
  162. value should be displayed. Tokens will be substituted with appriorate
  163. data from the database. See the Available tokens list for the
  164. tokens you may use.'
  165. );
  166. $element['field_display_string'] = array(
  167. '#type' => 'textfield',
  168. '#title' => 'Rewrite Value',
  169. '#description' => t('Provide a mixture of text and/or tokens for the format.
  170. For example: [organism.genus] [organism.species]. When displayed,
  171. the tokens will be replaced with the actual value.'),
  172. '#default_value' => $settings['field_display_string'],
  173. );
  174. $element['tokens'] = array(
  175. '#type' => 'fieldset',
  176. '#collapsed' => TRUE,
  177. '#collapsible' => TRUE,
  178. '#title' => 'Available Tokens'
  179. );
  180. $headers = array('Token', 'Description');
  181. $rows = array();
  182. // Here we use the tripal_get_chado_tokens rather than the
  183. // tripal_get_entity_tokens because we can't gurantee that all organisms
  184. // have entities.
  185. $tokens = tripal_get_chado_tokens('organism');
  186. foreach ($tokens as $token) {
  187. $rows[] = array(
  188. $token['token'],
  189. $token['description'],
  190. );
  191. }
  192. $table_vars = array(
  193. 'header' => $headers,
  194. 'rows' => $rows,
  195. 'attributes' => array(),
  196. 'sticky' => FALSE,
  197. 'caption' => '',
  198. 'colgroups' => array(),
  199. 'empty' => 'There are no tokens',
  200. );
  201. $project_details = theme('table', $table_vars);
  202. $element['tokens']['list'] = array(
  203. '#type' => 'item',
  204. '#markup' => theme_table($table_vars),
  205. );
  206. // Add in the semantic web fields.
  207. $parent_elements = parent::settings_form($field, $instance, $has_data);
  208. $element = array_merge($element, $parent_elements);
  209. return $element;
  210. }
  211. }
  212. /**
  213. * Callback function for validating the chado_base__organism_id_widget.
  214. */
  215. function chado_base__organism_id_widget_validate($element, &$form_state) {
  216. $field_name = $element['#parents'][0];
  217. // If the form ID is field_ui_field_edit_form, then the user is editing the
  218. // field's values in the manage fields form of Drupal. We don't want
  219. // to validate it as if it were being used in a data entry form.
  220. if ($form_state['build_info']['form_id'] =='field_ui_field_edit_form') {
  221. return;
  222. }
  223. $organism_id = tripal_chado_get_field_form_values($field_name, $form_state);
  224. if (!$organism_id) {
  225. form_error($element, t("Please specify an organism."));
  226. }
  227. }