chado_base__organism_id.inc 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  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. 'storage' => array(
  14. 'type' => 'field_chado_storage',
  15. 'module' => 'tripal_chado',
  16. 'active' => TRUE
  17. ),
  18. );
  19. }
  20. /**
  21. * @see TripalField::attach_info()
  22. */
  23. public function attach_info($entity_type, $bundle, $settings) {
  24. $field_info = array();
  25. $table_name = $settings['data_table'];
  26. $type_table = $settings['type_table'];
  27. $type_field = $settings['field'];
  28. $cv_id = $settings['cv_id'];
  29. $cvterm_id = $settings['cvterm_id'];
  30. // Check the schema for the data table if it does not have
  31. // an 'organism_id' column then we don't want to attach this field.
  32. $schema = chado_get_schema($table_name);
  33. if (!array_key_exists('organism_id', $schema['fields'])) {
  34. return $field_info;
  35. }
  36. // There is an organism_id column so attach the field!
  37. $field_info = array(
  38. 'field_name' => $table_name . '__organism_id',
  39. 'field_type' => 'chado_base__organism_id',
  40. 'widget_type' => 'chado_base__organism_id_widget',
  41. 'description' => 'Select an organism.',
  42. 'label' => 'Oraganism',
  43. 'is_required' => 0,
  44. 'storage' => 'field_chado_storage',
  45. 'widget_settings' => array(
  46. 'display_label' => 1
  47. ),
  48. 'field_settings' => array(
  49. 'chado_table' => $table_name,
  50. 'chado_column' => 'organism_id',
  51. 'semantic_web' => array(
  52. 'type' => 'organism',
  53. 'ns' => 'local',
  54. 'nsurl' => '',
  55. ),
  56. ),
  57. );
  58. return $field_info;
  59. }
  60. /**
  61. * @see TripalField::widget_info()
  62. */
  63. public function widget_info() {
  64. return array(
  65. 'label' => t('Organism Select'),
  66. 'field types' => array('chado_base__organism_id')
  67. );
  68. }
  69. /**
  70. * @see TripalField::formatter_info()
  71. */
  72. public function formatter_info() {
  73. return array(
  74. 'label' => t('Organism'),
  75. 'field types' => array('chado_base__organism_id'),
  76. 'settings' => array(
  77. 'field_display_teaser' => 0,
  78. 'field_display_string' => '<i>[organism.genus] [organism.species]</i>',
  79. ),
  80. );
  81. }
  82. /**
  83. * @see TripalField::formatter_settings_summary()
  84. */
  85. public function formatter_settings_summary($field, $instance, $view_mode) {
  86. $display = $instance['display'][$view_mode];
  87. $settings = $display['settings'];
  88. $summary = 'Settings summary: ';
  89. if (array_key_exists('field_display_teaser', $settings) and
  90. $settings['field_display_teaser'] == 1) {
  91. $summary .= 'displays the organism teaser if published.';
  92. }
  93. else if (array_key_exists('field_display_string', $settings)) {
  94. $summary .= 'uses the token string "' . $settings['field_display_string'] . '"';
  95. }
  96. else {
  97. $summary .= "uses display defaults.";
  98. }
  99. return $summary;
  100. }
  101. /**
  102. * @see TripalField::formatter_settings_form()
  103. */
  104. public function formatter_settings_form($field, $instance,
  105. $view_mode, $form, &$form_state) {
  106. $display = $instance['display'][$view_mode];
  107. $settings = $display['settings'];
  108. $element = array();
  109. $term = NULL;
  110. $entity = NULL;
  111. // Check to see if the organism bundle exists
  112. $term = tripal_load_term_entity(array(
  113. 'namespace' => $field['settings']['semantic_web']['ns'],
  114. 'accession' => $field['settings']['semantic_web']['type']
  115. ));
  116. if ($term) {
  117. $entity = tripal_load_bundle_entity(array('term_id' => $term->id));
  118. }
  119. $element['instructions'] = array(
  120. '#type' => 'item',
  121. '#markup' => 'Please provide the format for viewing the organism. You
  122. can specify the format using tokens that correspond to each field'
  123. );
  124. $element['field_display_string'] = array(
  125. '#type' => 'textfield',
  126. '#title' => 'Display Format',
  127. '#description' => t('Provide a mixture of text and/or tokens for the format.
  128. For example: [organism.genus] [organism.species]. When displayed,
  129. the tokens will be replaced with the actual value.'),
  130. '#default_value' => $settings['field_display_string'],
  131. );
  132. $element['field_display_teaser'] = array(
  133. '#type' => 'checkbox',
  134. '#title' => 'Display teaser if available',
  135. '#description' => t('If the organism that this field is associated with is
  136. a published page then display the teaser rather use the display
  137. format above.'),
  138. '#default_value' => $settings['field_display_teaser'],
  139. );
  140. $element['tokens'] = array(
  141. '#type' => 'fieldset',
  142. '#collapsed' => TRUE,
  143. '#collapsible' => TRUE,
  144. '#title' => 'Available Tokens'
  145. );
  146. $headers = array('Token', 'Description');
  147. $rows = array();
  148. // Here we use the tripal_get_chado_tokens rather than the
  149. // tripal_get_entity_tokens because we can't gurantee that all organisms
  150. // have entities.
  151. $tokens = tripal_get_chado_tokens('organism');
  152. foreach ($tokens as $token) {
  153. $rows[] = array(
  154. $token['token'],
  155. $token['description'],
  156. );
  157. }
  158. $table_vars = array(
  159. 'header' => $headers,
  160. 'rows' => $rows,
  161. 'attributes' => array(),
  162. 'sticky' => FALSE,
  163. 'caption' => '',
  164. 'colgroups' => array(),
  165. 'empty' => 'There are no tokens',
  166. );
  167. $project_details = theme('table', $table_vars);
  168. $element['tokens']['list'] = array(
  169. '#type' => 'item',
  170. '#markup' => theme_table($table_vars),
  171. );
  172. return $element;
  173. }
  174. /**
  175. * @see TripalField::formatter_view()
  176. */
  177. public function formatter_view(&$element, $entity_type, $entity,
  178. $field, $instance, $langcode, $items, $display) {
  179. // Get the settings
  180. $settings = $display['settings'];
  181. $record = $entity->chado_record;
  182. foreach ($items as $delta => $item) {
  183. $organism = $record->organism_id;
  184. if ($settings['field_display_teaser']) {
  185. }
  186. else {
  187. $field_name = $field['field_name'];
  188. $string = $settings['field_display_string'];
  189. $field_data = $entity->$field_name;
  190. $content = tripal_replace_chado_tokens($string, $organism);
  191. $element[$delta] = array(
  192. '#type' => 'markup',
  193. '#markup' => $content,
  194. );
  195. }
  196. }
  197. }
  198. /**
  199. * @see TripalField::widget()
  200. */
  201. public function widget_form(&$widget, $form, $form_state, $field, $instance,
  202. $langcode, $items, $delta, $element) {
  203. $options = tripal_get_organism_select_options(FALSE);
  204. $widget['value'] = array(
  205. '#type' => 'select',
  206. '#title' => $element['#title'],
  207. '#description' => $element['#description'],
  208. '#options' => $options,
  209. '#default_value' => count($items) > 0 ? $items[0]['value'] : 0,
  210. '#required' => $element['#required'],
  211. '#weight' => isset($element['#weight']) ? $element['#weight'] : 0,
  212. '#delta' => $delta,
  213. '#element_validate' => array('chado_base__organism_id_widget_validate'),
  214. );
  215. $widget['add_organism'] = array(
  216. '#type' => 'item',
  217. '#markup' => l('Add a new species', 'admin/content/bio_data/add/species', array('attributes' => array('target' => '_blank'))),
  218. );
  219. }
  220. /**
  221. * @see TripalField::ws_formatter()
  222. */
  223. public function ws_formatter($entity_type, $entity, $field, $instance, $items) {
  224. $organism = (array) $entity->chado_record->organism_id;
  225. unset($organism->tablename);
  226. return $organism;
  227. }
  228. }
  229. /**
  230. * Callback function for validating the chado_base__organism_id_widget.
  231. */
  232. function chado_base__organism_id_widget_validate($element, &$form_state) {
  233. $field_name = $element['#parents'][0];
  234. // If the form ID is field_ui_field_edit_form, then the user is editing the
  235. // field's values in the manage fields form of Drupal. We don't want
  236. // to validate it as if it were being used in a data entry form.
  237. if ($form_state['build_info']['form_id'] =='field_ui_field_edit_form') {
  238. return;
  239. }
  240. $organism_id = tripal_chado_get_field_form_values($field_name, $form_state);
  241. if (!$organism_id) {
  242. form_error($element, t("Please specify an organism."));
  243. }
  244. }