chado_base__organism_id.inc 11 KB

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