chado_base__dbxref_id.inc 14 KB

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