chado_base__dbxref_id.inc 18 KB

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