chado_base__organism_id.inc 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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. 'deafult' => array(
  124. 'label' => 'above',
  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'];
  162. if (array_key_exists('entity_id', $items[0])) {
  163. $content = l(strip_tags($items[0]['value']), 'bio_data/' . $items[0]['entity_id']);
  164. }
  165. // The cardinality of this field is 1 so we don't have to
  166. // iterate through the items array, as there will never be more than 1.
  167. $element[0] = array(
  168. '#type' => 'markup',
  169. '#markup' => $content,
  170. );
  171. }
  172. }
  173. /**
  174. * @see TripalField::widget()
  175. */
  176. public static function widgetForm(&$widget, &$form, &$form_state, $field, $instance,
  177. $langcode, $items, $delta, $element) {
  178. $settings = $field['settings'];
  179. $field_name = $field['field_name'];
  180. $field_type = $field['type'];
  181. $field_table = $field['settings']['chado_table'];
  182. $field_column = $field['settings']['chado_column'];
  183. $organism_id = 0;
  184. if (count($items) > 0 and array_key_exists($field_table . '__organism_id', $items[0])) {
  185. $organism_id = $items[0][$field_table . '__organism_id'];
  186. }
  187. $widget['value'] = array(
  188. '#type' => 'value',
  189. '#value' => array_key_exists($delta, $items) ? $items[$delta]['value'] : '',
  190. );
  191. $options = tripal_get_organism_select_options(FALSE);
  192. $widget[$field_table . '__organism_id'] = array(
  193. '#type' => 'select',
  194. '#title' => $element['#title'],
  195. '#description' => $element['#description'],
  196. '#options' => $options,
  197. '#default_value' => $organism_id,
  198. '#required' => $element['#required'],
  199. '#weight' => isset($element['#weight']) ? $element['#weight'] : 0,
  200. '#delta' => $delta,
  201. '#element_validate' => array('chado_base__organism_id_widget_validate'),
  202. );
  203. }
  204. /**
  205. * @see TripalField::load()
  206. */
  207. static function load($field, $entity, $details = array()) {
  208. $record = $details['record'];
  209. $settings = $field['settings'];
  210. $field_name = $field['field_name'];
  211. $field_type = $field['type'];
  212. $field_table = $field['settings']['chado_table'];
  213. $field_column = $field['settings']['chado_column'];
  214. // Set some defaults for the empty record.
  215. $entity->{$field_name}['und'][0] = array(
  216. 'value' => '',
  217. );
  218. if ($record) {
  219. $organism = $record->organism_id;
  220. $string = $settings['field_display_string'];
  221. $value = tripal_replace_chado_tokens($string, $organism);
  222. $entity->{$field_name}['und'][0]['value'] = $value;
  223. $entity->{$field_name}['und'][0][$field_table . '__organism_id'] = $organism->organism_id;
  224. // Is there a published entity for this organism?
  225. if (property_exists($entity->chado_record->$field_column, 'entity_id')) {
  226. $fk_entity_id = $entity->chado_record->$field_column->entity_id;
  227. $entity->{$field_name}['und'][0]['entity_id'] = $fk_entity_id;
  228. $entity->{$field_name}['und'][0]['entity_type'] = 'TripalEntity';
  229. }
  230. }
  231. }
  232. /**
  233. * @see TripalField::settings_form()
  234. */
  235. public function settingsForm($field, $instance, $has_data) {
  236. $settings = $field['settings'];
  237. $element = array();
  238. $element['instructions'] = array(
  239. '#type' => 'item',
  240. '#markup' => 'You may rewrite the way this field is presented to the end-user.
  241. The Rewrite Value field allows you to use tokens to indicate how the
  242. value should be displayed. Tokens will be substituted with appriorate
  243. data from the database. See the Available tokens list for the
  244. tokens you may use.'
  245. );
  246. $element['field_display_string'] = array(
  247. '#type' => 'textfield',
  248. '#title' => 'Rewrite Value',
  249. '#description' => t('Provide a mixture of text and/or tokens for the format.
  250. For example: [organism.genus] [organism.species]. When displayed,
  251. the tokens will be replaced with the actual value.'),
  252. '#default_value' => $settings['field_display_string'],
  253. );
  254. $element['tokens'] = array(
  255. '#type' => 'fieldset',
  256. '#collapsed' => TRUE,
  257. '#collapsible' => TRUE,
  258. '#title' => 'Available Tokens'
  259. );
  260. $headers = array('Token', 'Description');
  261. $rows = array();
  262. // Here we use the tripal_get_chado_tokens rather than the
  263. // tripal_get_entity_tokens because we can't gurantee that all organisms
  264. // have entities.
  265. $tokens = tripal_get_chado_tokens('organism');
  266. foreach ($tokens as $token) {
  267. $rows[] = array(
  268. $token['token'],
  269. $token['description'],
  270. );
  271. }
  272. $table_vars = array(
  273. 'header' => $headers,
  274. 'rows' => $rows,
  275. 'attributes' => array(),
  276. 'sticky' => FALSE,
  277. 'caption' => '',
  278. 'colgroups' => array(),
  279. 'empty' => 'There are no tokens',
  280. );
  281. $element['tokens']['list'] = array(
  282. '#type' => 'item',
  283. '#markup' => theme_table($table_vars),
  284. );
  285. // Add in the semantic web fields.
  286. $parent_elements = parent::settings_form($field, $instance, $has_data);
  287. $element = array_merge($element, $parent_elements);
  288. return $element;
  289. }
  290. }
  291. /**
  292. * Callback function for validating the chado_base__organism_id_widget.
  293. */
  294. function chado_base__organism_id_widget_validate($element, &$form_state) {
  295. $field_name = $element['#parents'][0];
  296. $field = $form_state['field'][$field_name]['und']['field'];
  297. $settings = $field['settings'];
  298. $field_name = $field['field_name'];
  299. $field_type = $field['type'];
  300. $field_table = $field['settings']['chado_table'];
  301. $field_column = $field['settings']['chado_column'];
  302. // If the form ID is field_ui_field_edit_form, then the user is editing the
  303. // field's values in the manage fields form of Drupal. We don't want
  304. // to validate it as if it were being used in a data entry form.
  305. if ($form_state['build_info']['form_id'] =='field_ui_field_edit_form') {
  306. return;
  307. }
  308. $organism_id = tripal_chado_get_field_form_values($field_name, $form_state, 0, $field_table . '__organism_id');
  309. if (!$organism_id) {
  310. form_error($element, t("Please specify an organism."));
  311. }
  312. }