chado_base__dbxref_id.inc 14 KB

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