chado_base__dbxref_id.inc 17 KB

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