chado_base__dbxref_id.inc 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. <?php
  2. class chado_base__dbxref_id extends TripalField {
  3. // The default lable for this field.
  4. public static $default_label = 'Accession';
  5. // The default description for this field.
  6. public static $default_description = 'This field specifies the unique stable accession (ID) for
  7. this record. It requires that this site have a database entry.';
  8. // Add any default settings elements. If you override the globalSettingsForm()
  9. // or the instanceSettingsForm() functions then you need to be sure that
  10. // any settings you want those functions to manage are listed in this
  11. // array.
  12. public static $default_settings = array(
  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::formatterView()
  23. */
  24. public function formatterView(&$element, $entity_type, $entity, $langcode, $items, $display) {
  25. foreach ($items as $delta => $item) {
  26. if ($item['value']) {
  27. $content = $item['value']['vocabulary'] . ':' . $item['value']['accession'];
  28. $element[$delta] = array(
  29. '#type' => 'markup',
  30. '#markup' => $content,
  31. );
  32. }
  33. }
  34. }
  35. /**
  36. * @see TripalField::widgetForm()
  37. */
  38. public function widgetForm(&$widget, &$form, &$form_state, $langcode, $items, $delta, $element) {
  39. $field_name = $this->field['field_name'];
  40. $field_type = $this->field['type'];
  41. $field_table = $this->field['settings']['chado_table'];
  42. $field_column = $this->field['settings']['chado_column'];
  43. // Get the field defaults.
  44. $fk_val = '';
  45. $dbxref_id = '';
  46. $db_id = '';
  47. $accession = '';
  48. $version = '';
  49. $description = '';
  50. // If the field already has a value then it will come through the $items
  51. // array. This happens when editing an existing record.
  52. if (count($items) > 0 and array_key_exists($delta, $items)) {
  53. $fk_val = $items[$delta]['chado-' . $field_table . '__' . $field_column];
  54. $dbxref_id = $items[$delta]['chado-' . $field_table . '__' . $field_column . '--dbxref_id'];
  55. $db_id = $items[$delta]['chado-' . $field_table . '__' . $field_column . '--db_id'];
  56. $accession = $items[$delta]['chado-' . $field_table . '__' . $field_column . '--accession'];
  57. $version = $items[$delta]['chado-' . $field_table . '__' . $field_column . '--version'];
  58. $description = $items[$delta]['chado-' . $field_table . '__' . $field_column . '--description'];
  59. }
  60. // Check $form_state['values'] to see if an AJAX call set the values.
  61. if (array_key_exists('values', $form_state)) {
  62. // $fk_val = tripal_chado_get_field_form_values($field_name, $form_state, 0, $field_table . '__' . $field_column);
  63. // $dbxref_id = tripal_chado_get_field_form_values($field_name, $form_state, 0, $field_table . '__' . $field_column . '--dbxref_id');
  64. // $db_id = tripal_chado_get_field_form_values($field_name, $form_state, 0, $field_table . '__' . $field_column . '--db_id');
  65. // $accession = tripal_chado_get_field_form_values($field_name, $form_state, 0, $field_table . '__' . $field_column . '--accession');
  66. // $version = tripal_chado_get_field_form_values($field_name, $form_state, 0, $field_table . '__' . $field_column . '--version');
  67. // $description = tripal_chado_get_field_form_values($field_name, $form_state, 0, $field_table . '__' . $field_column . '--description');
  68. }
  69. // If we are here because our parent was triggered in a form submit
  70. // then that means an ajax call was made and we don't want the fieldset to
  71. // be closed when it returns from the ajax call.
  72. // $collapsed = TRUE;
  73. // if (array_key_exists('triggering_element', $form_state) and
  74. // $form_state['triggering_element']['#parents'][0] == $field_name) {
  75. // $collapsed = FALSE;
  76. // }
  77. // if ($dbxref_id) {
  78. // $collapsed = FALSE;
  79. // }
  80. $schema = chado_get_schema('dbxref');
  81. $options = tripal_get_db_select_options();
  82. //$widget['#element_validate'] = array('chado_base__dbxref_id_widget_validate');
  83. $widget['#theme'] = 'chado_base__dbxref_id_widget';
  84. $widget['#prefix'] = "<span id='$field_name-dbxref--db-id'>";
  85. $widget['#suffix'] = "</span>";
  86. // A temporary element used for theming the fieldset.
  87. $widget['#theme_settings'] = array(
  88. '#title' => $element['#title'],
  89. '#description' => $element['#description'],
  90. '#weight' => isset($element['#weight']) ? $element['#weight'] : 0,
  91. '#theme' => 'chado_base__dbxref_id_widget',
  92. //'#collapsible' => TRUE,
  93. //'#collapsed' => $collapsed,
  94. );
  95. $widget['value'] = array(
  96. '#type' => 'value',
  97. '#value' => array_key_exists($delta, $items) ? $items[$delta]['value'] : '',
  98. );
  99. $widget['chado-' . $field_table . '__' . $field_column] = array(
  100. '#type' => 'value',
  101. '#default_value' => $fk_val,
  102. );
  103. $widget['chado-' . $field_table . '__' . $field_column . '--dbxref_id'] = array(
  104. '#type' => 'value',
  105. '#default_value' => $dbxref_id,
  106. );
  107. $widget['chado-' . $field_table . '__' . $field_column . '--db_id'] = array(
  108. '#type' => 'select',
  109. '#title' => t('Database'),
  110. '#options' => $options,
  111. '#required' => $element['#required'],
  112. '#default_value' => $db_id,
  113. '#ajax' => array(
  114. 'callback' => "chado_base__dbxref_id_widget_form_ajax_callback",
  115. 'wrapper' => "$field_name-dbxref--db-id",
  116. 'effect' => 'fade',
  117. 'method' => 'replace'
  118. ),
  119. );
  120. $widget['chado-' . $field_table . '__' . $field_column . '--accession'] = array(
  121. '#type' => 'textfield',
  122. '#title' => t('Accession'),
  123. '#default_value' => $accession,
  124. '#required' => $element['#required'],
  125. '#maxlength' => array_key_exists('length', $schema['fields']['accession']) ? $schema['fields']['accession']['length'] : 255,
  126. '#size' => 15,
  127. '#autocomplete_path' => 'admin/tripal/storage/chado/auto_name/dbxref/' . $db_id,
  128. '#ajax' => array(
  129. 'callback' => "tripal_chado_dbxref_widget_form_ajax_callback",
  130. 'wrapper' => "$field_name-dbxref--db-id",
  131. 'effect' => 'fade',
  132. 'method' => 'replace'
  133. ),
  134. '#disabled' => $db_id ? FALSE : TRUE,
  135. );
  136. $widget['chado-' . $field_table . '__' . $field_column . '--version'] = array(
  137. '#type' => 'textfield',
  138. '#title' => t('Version'),
  139. '#default_value' => $version,
  140. '#maxlength' => array_key_exists('length', $schema['fields']['version']) ? $schema['fields']['version']['length'] : 255,
  141. '#size' => 5,
  142. '#disabled' => $db_id ? FALSE : TRUE,
  143. );
  144. $widget['chado-' . $field_table . '__' . $field_column . '--description'] = array(
  145. '#type' => 'textfield',
  146. '#title' => t('Description'),
  147. '#default_value' => $description,
  148. '#size' => 20,
  149. '#disabled' => $db_id ? FALSE : TRUE,
  150. );
  151. $widget['links'] = array(
  152. '#type' => 'item',
  153. '#markup' => l('Add a new database', 'admin/tripal/legacy/tripal_db/add', array('attributes' => array('target' => '_blank')))
  154. );
  155. }
  156. /**
  157. * Callback function for validating the tripal_chado_dbxref_select_widget.
  158. */
  159. /**
  160. * @see TripalField::submit()
  161. */
  162. public function widgetFormSubmit($entity_type, $entity, $langcode, &$items, $form, &$form_state) {
  163. $field_name = $this->field['field_name'];
  164. $field = $form_state['field'][$field_name]['und']['field'];
  165. $settings = $field['settings'];
  166. $field_name = $field['field_name'];
  167. $field_type = $field['type'];
  168. $field_table = $field['settings']['chado_table'];
  169. $field_column = $field['settings']['chado_column'];
  170. $field_prefix = 'chado-' . $field_table . '__' . $field_column;
  171. // If the form ID is field_ui_field_edit_form, then the user is editing the
  172. // field's values in the manage fields form of Drupal. We don't want
  173. // to validate it as if it were being used in a data entry form.
  174. if ($form_state['build_info']['form_id'] =='field_ui_field_edit_form') {
  175. return;
  176. }
  177. // Get the field values.
  178. // $fk_value = tripal_chado_get_field_form_values($field_name, $form_state, 0, $field_table . '__' . $field_column);
  179. // $dbxref_id = tripal_chado_get_field_form_values($field_name, $form_state, 0, $field_prefix . '--dbxref_id');
  180. // $db_id = tripal_chado_get_field_form_values($field_name, $form_state, 0, $field_prefix . '--db_id');
  181. // $accession = tripal_chado_get_field_form_values($field_name, $form_state, 0, $field_prefix . '--accession');
  182. // $version = tripal_chado_get_field_form_values($field_name, $form_state, 0, $field_prefix . '--version');
  183. // $description = tripal_chado_get_field_form_values($field_name, $form_state, 0, $field_prefix . '--description');
  184. // Make sure that if a database ID is provided that an accession is also
  185. // provided. Here we use the form_set_error function rather than the
  186. // form_error function because the form_error will add a red_highlight
  187. // around all of the fields in the fieldset which is confusing as it's not
  188. // clear to the user what field is required and which isn't. Therefore,
  189. // we borrow the code from the 'form_error' function and append the field
  190. // so that the proper field is highlighted on error.
  191. if (!$db_id and $accession) {
  192. form_set_error(implode('][', $element ['#parents']) . '][' . $field_prefix . '--db_id', t("A database and the accession must both be provided for the primary cross reference."));
  193. }
  194. if ($db_id and !$accession) {
  195. form_set_error(implode('][', $element ['#parents']) . '][' . $field_prefix . '--accession', t("A database and the accession must both be provided for the primary cross reference."));
  196. }
  197. if (!$db_id and !$accession and ($version or $description)) {
  198. form_set_error(implode('][', $element ['#parents']) . '][' . $field_prefix . '--db_id', t("A database and the accession must both be provided for the primary cross reference."));
  199. }
  200. // If user did not select a database, we want to remove dbxref_id from the
  201. // field.
  202. if (!$db_id) {
  203. tripal_chado_set_field_form_values($field_name, $form_state, '__NULL__', 0, $field_prefix . '--dbxref_id');
  204. tripal_chado_set_field_form_values($field_name, $form_state, '__NULL__', 0, $field_table . '__' . $field_column);
  205. }
  206. // If the dbxref_id does not match the db_id + accession then the user
  207. // has selected a new dbxref record and we need to update the hidden
  208. // value accordingly.
  209. if ($db_id and $accession) {
  210. $dbxref = chado_generate_var('dbxref', array('db_id' => $db_id, 'accession' => $accession));
  211. if ($dbxref and $dbxref->dbxref_id != $dbxref_id) {
  212. tripal_chado_set_field_form_values($field_name, $form_state, $dbxref->dbxref_id, 0, $field_table . '__' . $field_column);
  213. tripal_chado_set_field_form_values($field_name, $form_state, $dbxref->dbxref_id, 0, $field_prefix . '--dbxref_id');
  214. }
  215. }
  216. }
  217. /**
  218. * @see TripalField::load()
  219. */
  220. public function load($entity, $details = array()) {
  221. $record = $details['record'];
  222. $field_name = $this->field['field_name'];
  223. $field_type = $this->field['type'];
  224. $field_table = $this->field['settings']['chado_table'];
  225. $field_column = $this->field['settings']['chado_column'];
  226. // Set some defauls for the empty record
  227. $entity->{$field_name}['und'][0] = array(
  228. 'value' => array(
  229. 'vocabulary' => '',
  230. 'accession' => '',
  231. 'URL' => '',
  232. ),
  233. 'chado-' . $field_table . '__' . $field_column => '',
  234. 'chado-' . $field_table . '__dbxref_id--dbxref_id' => '',
  235. 'chado-' . $field_table . '__dbxref_id--db_id' => '',
  236. 'chado-' . $field_table . '__dbxref_id--accession' => '',
  237. 'chado-' . $field_table . '__dbxref_id--version' => '',
  238. 'chado-' . $field_table . '__dbxref_id--description' => '',
  239. );
  240. // Get the primary dbxref record (if it's not NULL). Because we have a
  241. // dbxref_id passed in by the base record, we will only have one record.
  242. if ($record->$field_column) {
  243. $dbxref = $record->$field_column;
  244. $value = $dbxref->db_id->name . ':' . $dbxref->accession;
  245. $entity->{$field_name}['und'][0] = array(
  246. 'value' => array(
  247. 'vocabulary' => $dbxref->db_id->name,
  248. 'accession' => $dbxref->accession,
  249. 'URL' => tripal_get_dbxref_url($dbxref),
  250. ),
  251. 'chado-' . $field_table . '__' . $field_column => $record->$field_column->$field_column,
  252. 'chado-' . $field_table . '__' . $field_column . '--dbxref_id' => $dbxref->dbxref_id,
  253. 'chado-' . $field_table . '__' . $field_column . '--db_id' => $dbxref->db_id->db_id,
  254. 'chado-' . $field_table . '__' . $field_column . '--accession' => $dbxref->accession,
  255. 'chado-' . $field_table . '__' . $field_column . '--version' => $dbxref->version,
  256. 'chado-' . $field_table . '__' . $field_column . '--description' => $dbxref->description,
  257. );
  258. }
  259. }
  260. }
  261. /**
  262. * An Ajax callback for the tripal_chado_admin_publish_form..
  263. */
  264. function chado_base__dbxref_id_widget_form_ajax_callback($form, $form_state) {
  265. $field_name = $form_state['triggering_element']['#parents'][0];
  266. $field = field_info_field($field_name);
  267. $field_type = $field['type'];
  268. $field_table = $field['settings']['chado_table'];
  269. $field_column = $field['settings']['chado_column'];
  270. $field_prefix = 'chado-' . $field_table . '__' . $field_column;
  271. // $db_id = tripal_chado_get_field_form_values($field_name, $form_state, 0, $field_prefix . '--db_id');
  272. // $accession = tripal_chado_get_field_form_values($field_name, $form_state, 0, $field_prefix . '--accession');
  273. if ($db_id and $accession) {
  274. $values = array(
  275. 'db_id' => $db_id,
  276. 'accession' => $accession,
  277. );
  278. $options = array('is_duplicate' => TRUE);
  279. $has_duplicate = chado_select_record('dbxref', array('*'), $values, $options);
  280. if (!$has_duplicate) {
  281. drupal_set_message('The selected cross reference is new and will be added for future auto completions.', 'warning');
  282. }
  283. }
  284. return $form[$field_name];
  285. }
  286. function theme_chado_base__dbxref_id_widget($variables) {
  287. $element = $variables['element'];
  288. $field_name = $element['#field_name'];
  289. $field = field_info_field($field_name);
  290. $field_type = $field['type'];
  291. $field_table = $field['settings']['chado_table'];
  292. $field_column = $field['settings']['chado_column'];
  293. $field_prefix = 'chado-' . $field_table . '__' . $field_column;
  294. $layout = "
  295. <div class=\"primary-dbxref-widget\">
  296. <div class=\"primary-dbxref-widget-item\">" .
  297. drupal_render($element[$field_prefix . '--db_id']) . "
  298. </div>
  299. <div class=\"primary-dbxref-widget-item\">" .
  300. drupal_render($element[$field_prefix . '--accession']) . "
  301. </div>
  302. <div class=\"primary-dbxref-widget-item\">" .
  303. drupal_render($element[$field_prefix . '--version']) . "
  304. </div>
  305. <div class=\"primary-dbxref-widget-item\">" .
  306. drupal_render($element[$field_prefix . '--description']) . "
  307. </div>
  308. <div class=\"primary-dbxref-widget-links\">" . drupal_render($element['links']) . "</div>
  309. </div>
  310. ";
  311. // $classes = array();
  312. // $classes[] = 'collapsible';
  313. // $theme_settings = $element['#theme_settings'];
  314. // if ($theme_settings['#collapsed'] == FALSE) {
  315. // $classes[] = 'collapsed';
  316. // }
  317. $fieldset = array(
  318. '#title' => $element['#title'],
  319. '#value' => '',
  320. '#description' => $element['#description'],
  321. '#children' => $layout,
  322. // '#attributes' => array('class' => $classes),
  323. );
  324. return theme('fieldset', array('element' => $fieldset));
  325. }