chado_base__organism_id.inc 13 KB

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