chado_base__dbxref_id.inc 13 KB

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