chado_base__dbxref_id.inc 18 KB

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