chado_base__organism_id.inc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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::can_attach()
  24. */
  25. protected function can_attach($entity_type, $bundle, $details) {
  26. $table_name = $details['chado_table'];
  27. $type_table = $details['chado_type_table'];
  28. $type_field = $details['chado_type_column'];
  29. $cv_id = $details['chado_cv_id'];
  30. $cvterm_id = $details['chado_cvterm_id'];
  31. $schema = chado_get_schema($table_name);
  32. // If this is the organism table then do not attach as the organism_id
  33. // field is the primary key and we don't want a field for that.
  34. if ($table_name == 'organism') {
  35. return FALSE;
  36. }
  37. // Check the schema for the data table if it has
  38. // an 'organism_id' column then we want to attach this field.
  39. if (array_key_exists('organism_id', $schema['fields'])) {
  40. return TRUE;
  41. }
  42. return FALSE;
  43. }
  44. /**
  45. * @see TripalField::create_info()
  46. */
  47. public function create_info($entity_type, $bundle, $details) {
  48. if (!$this->can_attach($entity_type, $bundle, $details)) {
  49. return;
  50. }
  51. $table_name = $details['chado_table'];
  52. $type_table = $details['chado_type_table'];
  53. $type_field = $details['chado_type_column'];
  54. $cv_id = $details['chado_cv_id'];
  55. $cvterm_id = $details['chado_cvterm_id'];
  56. return array(
  57. 'field_name' => $table_name . '__organism_id',
  58. 'type' => 'chado_base__organism_id',
  59. 'cardinality' => 1,
  60. 'locked' => FALSE,
  61. 'storage' => array(
  62. 'type' => 'field_chado_storage',
  63. ),
  64. 'settings' => array(
  65. 'chado_table' => $table_name,
  66. 'chado_column' => 'organism_id',
  67. 'semantic_web' => 'local:organism',
  68. ),
  69. );
  70. }
  71. /**
  72. * @see TripalField::create_instance_info()
  73. */
  74. public function create_instance_info($entity_type, $bundle, $details) {
  75. if (!$this->can_attach($entity_type, $bundle, $details)) {
  76. return;
  77. }
  78. $table_name = $details['chado_table'];
  79. $type_table = $details['chado_type_table'];
  80. $type_field = $details['chado_type_column'];
  81. $cv_id = $details['chado_cv_id'];
  82. $cvterm_id = $details['chado_cvterm_id'];
  83. $is_required = FALSE;
  84. $schema = chado_get_schema($table_name);
  85. if (array_key_exists('not null', $schema['fields']['organism_id']) and
  86. $schema['fields']['organism_id']['not null']) {
  87. $is_required = TRUE;
  88. }
  89. return array(
  90. 'field_name' => $table_name . '__organism_id',
  91. 'entity_type' => $entity_type,
  92. 'bundle' => $bundle->name,
  93. 'label' => 'Organism',
  94. 'description' => 'Select an organism.',
  95. 'required' => $is_required,
  96. 'settings' => array(
  97. 'auto_attach' => TRUE,
  98. ),
  99. 'widget' => array(
  100. 'type' => 'chado_base__organism_id_widget',
  101. 'settings' => array(
  102. 'display_label' => 1,
  103. ),
  104. ),
  105. 'display' => array(
  106. 'deafult' => array(
  107. 'label' => 'above',
  108. 'type' => 'chado_base__organism_id_formatter',
  109. 'settings' => array(),
  110. ),
  111. ),
  112. );
  113. }
  114. /**
  115. * @see TripalField::widget_info()
  116. */
  117. public function widget_info() {
  118. return array(
  119. 'label' => t('Organism Select'),
  120. 'field types' => array('chado_base__organism_id')
  121. );
  122. }
  123. /**
  124. * @see TripalField::formatter_info()
  125. */
  126. public function formatter_info() {
  127. return array(
  128. 'label' => t('Organism'),
  129. 'field types' => array('chado_base__organism_id'),
  130. 'settings' => array(
  131. ),
  132. );
  133. }
  134. /**
  135. * @see TripalField::formatter_view()
  136. */
  137. public function formatter_view(&$element, $entity_type, $entity,
  138. $field, $instance, $langcode, $items, $display) {
  139. if (count($items) > 0) {
  140. $content = $items[0]['value'];
  141. if (array_key_exists('entity_id', $items[0])) {
  142. $content = l(strip_tags($items[0]['value']), 'bio_data/' . $items[0]['entity_id']);
  143. }
  144. // The cardinality of this field is 1 so we don't have to
  145. // iterate through the items array, as there will never be more than 1.
  146. $element[0] = array(
  147. '#type' => 'markup',
  148. '#markup' => $content,
  149. );
  150. }
  151. }
  152. /**
  153. * @see TripalField::widget()
  154. */
  155. public function widget_form(&$widget, &$form, &$form_state, $field, $instance,
  156. $langcode, $items, $delta, $element) {
  157. $settings = $field['settings'];
  158. $field_name = $field['field_name'];
  159. $field_type = $field['type'];
  160. $field_table = $field['settings']['chado_table'];
  161. $field_column = $field['settings']['chado_column'];
  162. $organism_id = 0;
  163. if (count($items) > 0 and array_key_exists($field_table . '__organism_id', $items[0])) {
  164. $organism_id = $items[0][$field_table . '__organism_id'];
  165. }
  166. $widget['value'] = array(
  167. '#type' => 'value',
  168. '#value' => array_key_exists($delta, $items) ? $items[$delta]['value'] : '',
  169. );
  170. $options = tripal_get_organism_select_options(FALSE);
  171. $widget[$field_table . '__organism_id'] = array(
  172. '#type' => 'select',
  173. '#title' => $element['#title'],
  174. '#description' => $element['#description'],
  175. '#options' => $options,
  176. '#default_value' => $organism_id,
  177. '#required' => $element['#required'],
  178. '#weight' => isset($element['#weight']) ? $element['#weight'] : 0,
  179. '#delta' => $delta,
  180. '#element_validate' => array('chado_base__organism_id_widget_validate'),
  181. );
  182. }
  183. /**
  184. * @see TripalField::load()
  185. */
  186. public function load($field, $entity, $details) {
  187. $record = $details['record'];
  188. $settings = $field['settings'];
  189. $field_name = $field['field_name'];
  190. $field_type = $field['type'];
  191. $field_table = $field['settings']['chado_table'];
  192. $field_column = $field['settings']['chado_column'];
  193. // Set some defaults for the empty record.
  194. $entity->{$field_name}['und'][0] = array(
  195. 'value' => '',
  196. );
  197. if ($record) {
  198. $organism = $record->organism_id;
  199. $string = $settings['field_display_string'];
  200. $value = tripal_replace_chado_tokens($string, $organism);
  201. $entity->{$field_name}['und'][0]['value'] = $value;
  202. $entity->{$field_name}['und'][0][$field_table . '__organism_id'] = $organism->organism_id;
  203. // Is there a published entity for this organism?
  204. if (property_exists($entity->chado_record->$field_column, 'entity_id')) {
  205. $fk_entity_id = $entity->chado_record->$field_column->entity_id;
  206. $entity->{$field_name}['und'][0]['entity_id'] = $fk_entity_id;
  207. $entity->{$field_name}['und'][0]['entity_type'] = 'TripalEntity';
  208. }
  209. }
  210. }
  211. /**
  212. * @see TripalField::settings_form()
  213. */
  214. public function settings_form($field, $instance, $has_data) {
  215. $settings = $field['settings'];
  216. $element = array();
  217. $element['instructions'] = array(
  218. '#type' => 'item',
  219. '#markup' => 'You may rewrite the way this field is presented to the end-user.
  220. The Rewrite Value field allows you to use tokens to indicate how the
  221. value should be displayed. Tokens will be substituted with appriorate
  222. data from the database. See the Available tokens list for the
  223. tokens you may use.'
  224. );
  225. $element['field_display_string'] = array(
  226. '#type' => 'textfield',
  227. '#title' => 'Rewrite Value',
  228. '#description' => t('Provide a mixture of text and/or tokens for the format.
  229. For example: [organism.genus] [organism.species]. When displayed,
  230. the tokens will be replaced with the actual value.'),
  231. '#default_value' => $settings['field_display_string'],
  232. );
  233. $element['tokens'] = array(
  234. '#type' => 'fieldset',
  235. '#collapsed' => TRUE,
  236. '#collapsible' => TRUE,
  237. '#title' => 'Available Tokens'
  238. );
  239. $headers = array('Token', 'Description');
  240. $rows = array();
  241. // Here we use the tripal_get_chado_tokens rather than the
  242. // tripal_get_entity_tokens because we can't gurantee that all organisms
  243. // have entities.
  244. $tokens = tripal_get_chado_tokens('organism');
  245. foreach ($tokens as $token) {
  246. $rows[] = array(
  247. $token['token'],
  248. $token['description'],
  249. );
  250. }
  251. $table_vars = array(
  252. 'header' => $headers,
  253. 'rows' => $rows,
  254. 'attributes' => array(),
  255. 'sticky' => FALSE,
  256. 'caption' => '',
  257. 'colgroups' => array(),
  258. 'empty' => 'There are no tokens',
  259. );
  260. $project_details = theme('table', $table_vars);
  261. $element['tokens']['list'] = array(
  262. '#type' => 'item',
  263. '#markup' => theme_table($table_vars),
  264. );
  265. // Add in the semantic web fields.
  266. $parent_elements = parent::settings_form($field, $instance, $has_data);
  267. $element = array_merge($element, $parent_elements);
  268. return $element;
  269. }
  270. }
  271. /**
  272. * Callback function for validating the chado_base__organism_id_widget.
  273. */
  274. function chado_base__organism_id_widget_validate($element, &$form_state) {
  275. $field_name = $element['#parents'][0];
  276. $field = $form_state['field'][$field_name]['und']['field'];
  277. $settings = $field['settings'];
  278. $field_name = $field['field_name'];
  279. $field_type = $field['type'];
  280. $field_table = $field['settings']['chado_table'];
  281. $field_column = $field['settings']['chado_column'];
  282. // If the form ID is field_ui_field_edit_form, then the user is editing the
  283. // field's values in the manage fields form of Drupal. We don't want
  284. // to validate it as if it were being used in a data entry form.
  285. if ($form_state['build_info']['form_id'] =='field_ui_field_edit_form') {
  286. return;
  287. }
  288. $organism_id = tripal_chado_get_field_form_values($field_name, $form_state, 0, $field_table . '__organism_id');
  289. if (!$organism_id) {
  290. form_error($element, t("Please specify an organism."));
  291. }
  292. }