chado_base__dbxref_id.inc 18 KB

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