chado_base__organism_id.inc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. <?php
  2. /**
  3. * Implements hook_info() for fields.
  4. *
  5. * This is a hook provided by the tripal_chado module for offloading the
  6. * hook_field_info() hook for each field to specify.
  7. */
  8. function chado_base__organism_id_info() {
  9. return array(
  10. 'label' => t('Organism'),
  11. 'description' => t('A field for specifying an organism.'),
  12. 'default_widget' => 'chado_base__organism_id_widget',
  13. 'default_formatter' => 'chado_base__organism_id_formatter',
  14. 'settings' => array(),
  15. 'storage' => array(
  16. 'type' => 'field_chado_storage',
  17. 'module' => 'tripal_chado',
  18. 'active' => TRUE
  19. ),
  20. );
  21. }
  22. /**
  23. * Implements hook_attach_info().
  24. *
  25. * This is a hook provided by the tripal_Chado module. It allows the field
  26. * to specify which bundles it will attach to and to specify thee settings.
  27. *
  28. * @param $entity_type
  29. * @param $entity
  30. * @param $term
  31. *
  32. * @return
  33. * A field array
  34. */
  35. function chado_base__organism_id_attach_info($entity_type, $bundle, $target) {
  36. $field_info = array();
  37. $table_name = $target['data_table'];
  38. $type_table = $target['type_table'];
  39. $type_field = $target['field'];
  40. $cv_id = $target['cv_id'];
  41. $cvterm_id = $target['cvterm_id'];
  42. // Check the schema for the data table if it does not have
  43. // an 'organism_id' column then we don't want to attach this field.
  44. $schema = chado_get_schema($table_name);
  45. if (!array_key_exists('organism_id', $schema['fields'])) {
  46. return $field_info;
  47. }
  48. // There is an organism_id column so attach the field!
  49. $field_info = array(
  50. 'field_name' => $table_name . '__organism_id',
  51. 'field_type' => 'chado_base__organism_id',
  52. 'widget_type' => 'chado_base__organism_id_widget',
  53. 'description' => 'Select an organism.',
  54. 'label' => 'Oraganism',
  55. 'is_required' => 0,
  56. 'storage' => 'field_chado_storage',
  57. 'widget_settings' => array(
  58. 'display_label' => 1
  59. ),
  60. 'field_settings' => array(
  61. 'chado_table' => $table_name,
  62. 'chado_column' => 'organism_id',
  63. 'semantic_web' => array(
  64. 'type' => 'organism',
  65. 'ns' => 'local',
  66. 'nsurl' => '',
  67. ),
  68. ),
  69. );
  70. return $field_info;
  71. }
  72. /**
  73. * Implements hook_widget_info.
  74. *
  75. * This is a hook provided by the tripal_chado module for offloading
  76. * the hook_field_widget_info() hook for each field to specify.
  77. */
  78. function chado_base__organism_id_widget_info() {
  79. return array(
  80. 'label' => t('Organism Select'),
  81. 'field types' => array('chado_base__organism_id')
  82. );
  83. }
  84. /**
  85. * Implements hook_formatter_info.
  86. *
  87. * This is a hook provided by the tripal_chado module for
  88. * offloading the hook_field_formatter_info() for each field
  89. * to specify.
  90. *
  91. */
  92. function chado_base__organism_id_formatter_info() {
  93. return array(
  94. 'label' => t('Organism'),
  95. 'field types' => array('chado_base__organism_id'),
  96. 'settings' => array(
  97. 'field_display_teaser' => 0,
  98. 'field_display_string' => '<i>[organism__genus] [organism__species]</i>',
  99. ),
  100. );
  101. }
  102. /**
  103. * Implements hook_formatter_settings_summary.
  104. *
  105. * This is a hook provided by the tripal_chado module for
  106. * offloading the hook_field_formatter_settings_summary() for each field
  107. * to specify.
  108. *
  109. */
  110. function chado_base__organism_id_formatter_settings_summary($field, $instance, $view_mode) {
  111. $display = $instance['display'][$view_mode];
  112. $settings = $display['settings'];
  113. $summary = 'Settings summary: ';
  114. if (array_key_exists('field_display_teaser', $settings) and
  115. $settings['field_display_teaser'] == 1) {
  116. $summary .= 'displays the organism teaser if published.';
  117. }
  118. else if (array_key_exists('field_display_string', $settings)) {
  119. $summary .= 'uses the token string "' . $settings['field_display_string'] . '"';
  120. }
  121. else {
  122. $summary .= "uses display defaults.";
  123. }
  124. return $summary;
  125. }
  126. /**
  127. * Provides a settings form for the formatter.
  128. *
  129. * This is a hook provided by the tripal_chado module for
  130. * offloading the hook_field_formatter_settings_form() for each field
  131. * to specify.
  132. */
  133. function chado_base__organism_id_formatter_settings_form($field, $instance,
  134. $view_mode, $form, &$form_state) {
  135. $display = $instance['display'][$view_mode];
  136. $settings = $display['settings'];
  137. $element = array();
  138. $term = NULL;
  139. $entity = NULL;
  140. // Check to see if the organism bundle exists
  141. $term = tripal_load_term_entity(array(
  142. 'namespace' => $field['settings']['semantic_web']['ns'],
  143. 'accession' => $field['settings']['semantic_web']['type']
  144. ));
  145. if ($term) {
  146. $entity = tripal_load_bundle_entity(array('term_id' => $term->id));
  147. }
  148. $element['instructions'] = array(
  149. '#type' => 'item',
  150. '#markup' => 'Please provide the format for viewing the organism. You
  151. can specify the format using tokens that correspond to each field'
  152. );
  153. $element['field_display_string'] = array(
  154. '#type' => 'textfield',
  155. '#title' => 'Display Format',
  156. '#description' => t('Provide a mixture of text and/or tokens for the format.
  157. For example: [organism__genus] [organism__species]. When displayed,
  158. the tokens will be replaced with the actual value.'),
  159. '#default_value' => $settings['field_display_string'],
  160. );
  161. $element['field_display_teaser'] = array(
  162. '#type' => 'checkbox',
  163. '#title' => 'Display teaser if available',
  164. '#description' => t('If the organism that this field is associated with is
  165. a published page then display the teaser rather use the display
  166. format above.'),
  167. '#default_value' => $settings['field_display_teaser'],
  168. );
  169. $element['tokens'] = array(
  170. '#type' => 'fieldset',
  171. '#collapsed' => TRUE,
  172. '#collapsible' => TRUE,
  173. '#title' => 'Available Tokens'
  174. );
  175. $headers = array('Token', 'Description');
  176. $rows = array();
  177. $tokens = tripal_get_tokens($entity);
  178. foreach ($tokens as $token) {
  179. $rows[] = array(
  180. $token['token'],
  181. $token['description'],
  182. );
  183. }
  184. $table_vars = array(
  185. 'header' => $headers,
  186. 'rows' => $rows,
  187. 'attributes' => array(),
  188. 'sticky' => FALSE,
  189. 'caption' => '',
  190. 'colgroups' => array(),
  191. 'empty' => 'There are no tokens',
  192. );
  193. $project_details = theme('table', $table_vars);
  194. $element['tokens']['list'] = array(
  195. '#type' => 'item',
  196. '#markup' => theme_table($table_vars),
  197. );
  198. $element['#element_validate'] = array('chado_base__organism_id_formatter_settings_form_validate');
  199. return $element;
  200. }
  201. /**
  202. * Validation function for the chado_base__organism_id_formatter_settings_form.
  203. */
  204. function chado_base__organism_id_formatter_settings_form_validate(&$form, &$form_state) {
  205. // Place here as an example for validating the settings form.
  206. }
  207. /**
  208. *
  209. */
  210. function chado_base__organism_id_formatter(&$element, $entity_type, $entity,
  211. $field, $instance, $langcode, $items, $display) {
  212. // Get the settings
  213. $settings = $display['settings'];
  214. foreach ($items as $delta => $item) {
  215. $organism = chado_select_record('organism', array('genus', 'species'), array('organism_id' => $item['value']));
  216. if ($settings['field_display_teaser'] == 0) {
  217. $field_name = $field['field_name'];
  218. $string = $settings['field_display_string'];
  219. $field_data = $entity->$field_name;
  220. // TODO: add a way to get tokens from Chado records. this is
  221. // needed in the case that a record is not published as an entity.
  222. $content = tripal_replace_tokens($string, $field_data['und'][0]['entity']);
  223. $element[$delta] = array(
  224. '#type' => 'markup',
  225. '#markup' => $content,
  226. );
  227. }
  228. else {
  229. }
  230. }
  231. }
  232. /**
  233. * Loads the field values with appropriate data.
  234. *
  235. * This function is called by the tripal_chado_field_storage_load() for
  236. * each property managed by the field_chado_storage storage type. This is
  237. * an optional hook function that is only needed if the field has
  238. * multiple form elements.
  239. *
  240. * @param $field
  241. * @param $entity
  242. * @param $base_table
  243. * @param $record
  244. */
  245. function chado_base__organism_id_load($field, $entity, $base_table, $record) {
  246. $field_name = $field['field_name'];
  247. // The organism_id is already set as the value. We need to get it and
  248. // see if there is a published entity for this organism. If there is
  249. // then we want to add the organism object generated using chado_generate_var
  250. // as well as the entity to the record.
  251. $organism_id = $entity->{$field_name}['und'][0]['value'];
  252. $organism = chado_generate_var('organism', array('organism_id' => $organism_id));
  253. $entity->{$field_name}['und'][0]['organism'] = $organism;
  254. $org_entity = tripal_load_chado_entity('organism', $organism_id);
  255. $entity->{$field_name}['und'][0]['entity'] = $org_entity;
  256. }
  257. /**
  258. * Implements hook_ws_formatter().
  259. */
  260. function chado_base__organism_id_ws_formatter(&$element, $entity_type, $entity,
  261. $field, $instance, $items) {
  262. foreach ($items as $delta => $item) {
  263. $organism = $item['organism'];
  264. $entity = $item['entity'];
  265. if ($entity) {
  266. $element[$delta]['#entity'] = $entity;
  267. }
  268. $element[$delta]['genus'] = $organism->genus;
  269. $element[$delta]['species'] = $organism->species;
  270. $element[$delta]['common_name'] = $organism->common_name;
  271. }
  272. }
  273. /**
  274. * Implements hook_widget().
  275. */
  276. function chado_base__organism_id_widget(&$widget, $form, $form_state, $field, $instance, $langcode, $items, $delta, $element) {
  277. $options = tripal_get_organism_select_options(FALSE);
  278. $widget['value'] = array(
  279. '#type' => 'select',
  280. '#title' => $element['#title'],
  281. '#description' => $element['#description'],
  282. '#options' => $options,
  283. '#default_value' => count($items) > 0 ? $items[0]['value'] : 0,
  284. '#required' => $element['#required'],
  285. '#weight' => isset($element['#weight']) ? $element['#weight'] : 0,
  286. '#delta' => $delta,
  287. '#element_validate' => array('chado_base__organism_id_widget_validate'),
  288. );
  289. $widget['add_organism'] = array(
  290. '#type' => 'item',
  291. '#markup' => l('Add a new species', 'admin/content/bio_data/add/species', array('attributes' => array('target' => '_blank'))),
  292. );
  293. }
  294. /**
  295. * Callback function for validating the chado_base__organism_id_widget.
  296. */
  297. function chado_base__organism_id_widget_validate($element, &$form_state) {
  298. $field_name = $element['#parents'][0];
  299. // If the form ID is field_ui_field_edit_form, then the user is editing the
  300. // field's values in the manage fields form of Drupal. We don't want
  301. // to validate it as if it were being used in a data entry form.
  302. if ($form_state['build_info']['form_id'] =='field_ui_field_edit_form') {
  303. return;
  304. }
  305. $organism_id = tripal_chado_get_field_form_values($field_name, $form_state);
  306. if (!$organism_id) {
  307. form_error($element, t("Please specify an organism."));
  308. }
  309. }