chado_linker__dbxref.inc 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494
  1. <?php
  2. /**
  3. * This class provides fields for adding database cross-references.
  4. *
  5. * It supports two types of cross-references. The primary cross reference
  6. * which is usually in the base table as an FK to the dbxref table, and
  7. * secondary cross references which are in a linking table that connects
  8. * the base table with the dbxref table.
  9. *
  10. */
  11. class chado_linker__dbxref extends TripalField {
  12. /**
  13. * Implements hook_info() for fields.
  14. *
  15. * This is a hook provided by the tripal_chado module for offloading the
  16. * hook_fieldInfo() hook for each field to specify.
  17. */
  18. static function fieldInfo() {
  19. return array(
  20. 'label' => t('Cross references'),
  21. 'description' => t('This record can be cross referenced with a record in
  22. another online database. This field is intended for one or more
  23. references. At a minimum, the database and accession must be provided.'),
  24. 'default_widget' => 'chado_linker__dbxref_widget',
  25. 'default_formatter' => 'chado_linker__dbxref_formatter',
  26. 'settings' => array(),
  27. 'storage' => array(
  28. 'type' => 'field_chado_storage',
  29. 'module' => 'tripal_chado',
  30. 'active' => TRUE
  31. ),
  32. );
  33. }
  34. /**
  35. * @see TripalField::can_attach()
  36. */
  37. protected function setCanAttach() {
  38. $table_name = $this->details['chado_table'];
  39. $type_table = $this->details['chado_type_table'];
  40. $type_field = $this->details['chado_type_column'];
  41. $cv_id = $this->details['chado_cv_id'];
  42. $cvterm_id = $this->details['chado_cvterm_id'];
  43. // If the linker table does not exists then we don't want to add attach.
  44. $dbxref_table = $table_name . '_dbxref';
  45. if (chado_table_exists($dbxref_table)) {
  46. $this->can_attach = TRUE;
  47. return;
  48. }
  49. $this->can_attach = FALSE;
  50. }
  51. /**
  52. * @see TripalField::create_info()
  53. */
  54. function createInfo() {
  55. if (!$this->can_attach) {
  56. return;
  57. }
  58. $table_name = $this->details['chado_table'];
  59. $type_table = $this->details['chado_type_table'];
  60. $type_field = $this->details['chado_type_column'];
  61. $cv_id = $this->details['chado_cv_id'];
  62. $cvterm_id = $this->details['chado_cvterm_id'];
  63. $dbxref_table = $table_name . '_dbxref';
  64. $schema = chado_get_schema($dbxref_table);
  65. $pkey = $schema['primary key'][0];
  66. return array(
  67. 'field_name' => $dbxref_table,
  68. 'type' => 'chado_linker__dbxref',
  69. 'cardinality' => FIELD_CARDINALITY_UNLIMITED,
  70. 'locked' => FALSE,
  71. 'storage' => array(
  72. 'type' => 'field_chado_storage',
  73. ),
  74. 'settings' => array(
  75. 'chado_table' => $dbxref_table,
  76. 'chado_column' => $pkey,
  77. 'base_table' => $table_name,
  78. 'semantic_web' => 'SBO:0000554',
  79. ),
  80. );
  81. }
  82. /**
  83. * @see TripalField::createInstanceInfo()
  84. */
  85. function createInstanceInfo() {
  86. if (!$this->can_attach) {
  87. return;
  88. }
  89. $table_name = $this->details['chado_table'];
  90. $type_table = $this->details['chado_type_table'];
  91. $type_field = $this->details['chado_type_column'];
  92. $cv_id = $this->details['chado_cv_id'];
  93. $cvterm_id = $this->details['chado_cvterm_id'];
  94. $dbxref_table = $table_name . '_dbxref';
  95. return array(
  96. 'field_name' => $dbxref_table,
  97. 'entity_type' => $this->entity_type,
  98. 'bundle' => $this->bundle->name,
  99. 'label' => 'Cross References',
  100. 'description' => 'The IDs where this record may be available in other external online databases.',
  101. 'required' => FALSE,
  102. 'settings' => array(
  103. 'auto_attach' => FALSE,
  104. ),
  105. 'widget' => array(
  106. 'type' => 'chado_linker__dbxref_widget',
  107. 'settings' => array(
  108. 'display_label' => 1,
  109. ),
  110. ),
  111. 'display' => array(
  112. 'deafult' => array(
  113. 'label' => 'above',
  114. 'type' => 'chado_linker__dbxref_formatter',
  115. 'settings' => array(),
  116. ),
  117. ),
  118. );
  119. }
  120. /**
  121. * @see TripalField::widgetInfo()
  122. */
  123. public static function widgetInfo() {
  124. return array(
  125. 'chado_linker__dbxref_widget' => array(
  126. 'label' => t('Cross references'),
  127. 'field types' => array('chado_linker__dbxref'),
  128. 'description' => t('This record can be cross referenced with a record
  129. in another online database. This field is intended for the most
  130. prominent reference. At a minimum, the database and accession
  131. must be provided.'),
  132. ),
  133. );
  134. }
  135. /**
  136. * @see TripalField::formatterInfo()
  137. */
  138. static function formatterInfo() {
  139. return array(
  140. 'chado_linker__dbxref_formatter' => array(
  141. 'label' => t('Cross references'),
  142. 'field types' => array('chado_linker__dbxref'),
  143. 'settings' => array(
  144. ),
  145. ),
  146. );
  147. }
  148. /**
  149. * @see TripalField::formatterView()
  150. */
  151. static function formatterView(&$element, $entity_type, $entity, $field,
  152. $instance, $langcode, $items, $display) {
  153. $chado_table = $field['settings']['chado_table'];
  154. foreach ($items as $delta => $item) {
  155. if ($item['value']) {
  156. $content = $item['value']['vocabulary'] . ':' . $item['value']['accession'];
  157. if ($item['value']['URL']) {
  158. $content = l($item['value']['URL'], $item['value']['URL']);
  159. }
  160. $element[$delta] = array(
  161. '#type' => 'markup',
  162. '#markup' => $content,
  163. );
  164. }
  165. }
  166. }
  167. /**
  168. * @see TripalField::widgetForm()
  169. */
  170. public static function widgetForm(&$widget, &$form, &$form_state, $field, $instance,
  171. $langcode, $items, $delta, $element) {
  172. $field_name = $field['field_name'];
  173. $field_type = $field['type'];
  174. $field_table = $field['settings']['chado_table'];
  175. $field_column = $field['settings']['chado_column'];
  176. // Get the FK column that links to the base table.
  177. $chado_table = $field['settings']['chado_table'];
  178. $base_table = $field['settings']['base_table'];
  179. $schema = chado_get_schema($chado_table);
  180. $pkey = $schema['primary key'][0];
  181. $fkeys = array_values($schema['foreign keys'][$base_table]['columns']);
  182. $fkey = $fkeys[0];
  183. // Get the field defaults.
  184. $record_id = '';
  185. $fkey_value = '';
  186. $dbxref_id = '';
  187. $db_id = '';
  188. $accession = '';
  189. $version = '';
  190. $description = '';
  191. // If the field already has a value then it will come through the $items
  192. // array. This happens when editing an existing record.
  193. if (array_key_exists($delta, $items)) {
  194. $record_id = $items[$delta][$field_table . '__' . $pkey];
  195. $fkey_value = $items[$delta][$field_table . '__' . $fkey];
  196. $dbxref_id = $items[$delta][$field_table . '__dbxref_id'];
  197. $db_id = $items[$delta][$field_table . '__dbxref_id--db_id'];
  198. $accession = $items[$delta][$field_table . '__dbxref_id--accession'];
  199. $version = $items[$delta][$field_table . '__dbxref_id--version'];
  200. $description = $items[$delta][$field_table . '__dbxref_id--description'];
  201. }
  202. // Check $form_state['values'] to see if an AJAX call set the values.
  203. if (array_key_exists('values', $form_state) and array_key_exists($delta, $form_state['values'])) {
  204. $record_id = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $field_table . '__' . $pkey);
  205. $fkey_value = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $field_table . '__' . $fkey);
  206. $dbxref_id = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $field_table . '__dbxref_id');
  207. $db_id = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $field_table . '__dbxref_id--db_id');
  208. $accession = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $field_table . '__dbxref_id--accession');
  209. $version = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $field_table . '__dbxref_id--version');
  210. $description = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $field_table . '__dbxref_id--description');
  211. }
  212. $schema = chado_get_schema('dbxref');
  213. $options = tripal_get_db_select_options();
  214. $widget['#table_name'] = $chado_table;
  215. $widget['#fkey_field'] = $fkey;
  216. $widget['#element_validate'] = array('chado_linker__dbxref_widget_validate');
  217. $widget['#theme'] = 'chado_linker__dbxref_widget';
  218. $widget['#prefix'] = "<span id='$field_name-dbxref--db-id-$delta'>";
  219. $widget['#suffix'] = "</span>";
  220. $widget['value'] = array(
  221. '#type' => 'value',
  222. '#value' => array_key_exists($delta, $items) ? $items[$delta]['value'] : '',
  223. );
  224. $widget[$field_table . '__' . $pkey] = array(
  225. '#type' => 'value',
  226. '#default_value' => $record_id,
  227. );
  228. $widget[$field_table . '__' . $fkey] = array(
  229. '#type' => 'value',
  230. '#default_value' => $fkey_value,
  231. );
  232. $widget[$field_table . '__dbxref_id'] = array(
  233. '#type' => 'value',
  234. '#default_value' => $dbxref_id,
  235. );
  236. $widget[$field_table . '__dbxref_id--dbxref_id'] = array(
  237. '#type' => 'value',
  238. '#default_value' => $dbxref_id,
  239. );
  240. $widget[$field_table . '__dbxref_id--db_id'] = array(
  241. '#type' => 'select',
  242. '#title' => t('Database'),
  243. '#options' => $options,
  244. '#required' => $element['#required'],
  245. '#default_value' => $db_id,
  246. '#ajax' => array(
  247. 'callback' => "chado_linker__dbxref_widget_form_ajax_callback",
  248. 'wrapper' => "$field_name-dbxref--db-id-$delta",
  249. 'effect' => 'fade',
  250. 'method' => 'replace'
  251. ),
  252. );
  253. $widget[$field_table . '__dbxref_id--accession'] = array(
  254. '#type' => 'textfield',
  255. '#title' => t('Accession'),
  256. '#default_value' => $accession,
  257. '#required' => $element['#required'],
  258. '#maxlength' => array_key_exists('length', $schema['fields']['accession']) ? $schema['fields']['accession']['length'] : 255,
  259. '#size' => 15,
  260. '#autocomplete_path' => 'admin/tripal/storage/chado/auto_name/dbxref/' . $db_id,
  261. '#ajax' => array(
  262. 'callback' => "chado_linker__dbxref_widget_form_ajax_callback",
  263. 'wrapper' => "$field_name-dbxref--db-id-$delta",
  264. 'effect' => 'fade',
  265. 'method' => 'replace'
  266. ),
  267. '#disabled' => $db_id ? FALSE : TRUE,
  268. );
  269. $widget[$field_table . '__dbxref_id--version'] = array(
  270. '#type' => 'textfield',
  271. '#title' => t('Version'),
  272. '#default_value' => $version,
  273. '#maxlength' => array_key_exists('length', $schema['fields']['version']) ? $schema['fields']['version']['length'] : 255,
  274. '#size' => 5,
  275. '#disabled' => $db_id ? FALSE : TRUE,
  276. );
  277. $widget[$field_table . '__dbxref_id--description'] = array(
  278. '#type' => 'textfield',
  279. '#title' => t('Description'),
  280. '#default_value' => $description,
  281. '#size' => 20,
  282. '#disabled' => $db_id ? FALSE : TRUE,
  283. );
  284. if (!$db_id) {
  285. $widget['links'] = array(
  286. '#type' => 'item',
  287. '#markup' => l('Add a database', 'admin/tripal/chado/tripal_db/add', array('attributes' => array('target' => '_blank')))
  288. );
  289. }
  290. }
  291. /**
  292. * @see TripalField::load()
  293. */
  294. function load($field, $entity, $details) {
  295. $record = $details['record'];
  296. $field_name = $field['field_name'];
  297. $field_type = $field['type'];
  298. $field_table = $field['settings']['chado_table'];
  299. $field_column = $field['settings']['chado_column'];
  300. $base_table = $record->tablename;
  301. $schema = chado_get_schema($field_table);
  302. $pkey = $schema['primary key'][0];
  303. $fkeys = array_values($schema['foreign keys'][$base_table]['columns']);
  304. $fkey = $fkeys[0];
  305. // Set some defaults for the empty record.
  306. $entity->{$field_name}['und'][0] = array(
  307. 'value' => array(),
  308. $field_table . '__' . $pkey => '',
  309. $field_table . '__' . $fkey => '',
  310. $field_table . '__dbxref_id' => '',
  311. $field_table . '__dbxref_id--dbxref_id' => '',
  312. $field_table . '__dbxref_id--db_id' => '',
  313. $field_table . '__dbxref_id--accession' => '',
  314. $field_table . '__dbxref_id--version' => '',
  315. $field_table . '__dbxref_id--description' => '',
  316. );
  317. $linker_table = $base_table . '_dbxref';
  318. $options = array('return_array' => 1);
  319. $record = chado_expand_var($record, 'table', $linker_table, $options);
  320. if (count($record->$linker_table) > 0) {
  321. $i = 0;
  322. foreach ($record->$linker_table as $index => $linker) {
  323. $dbxref = $linker->dbxref_id;
  324. $entity->{$field_name}['und'][$i] = array(
  325. 'value' => array(
  326. 'vocabulary' => $dbxref->db_id->name,
  327. 'accession' => $dbxref->accession,
  328. 'URL' => $URL,
  329. ),
  330. $field_table . '__' . $pkey => $linker->$pkey,
  331. $field_table . '__' . $fkey => $linker->$fkey->$fkey,
  332. $field_table . '__dbxref_id' => $dbxref->dbxref_id,
  333. $field_table . '__dbxref_id--dbxref_id' => $dbxref->dbxref_id,
  334. $field_table . '__dbxref_id--db_id' => $dbxref->db_id->db_id,
  335. $field_table . '__dbxref_id--accession' => $dbxref->accession,
  336. $field_table . '__dbxref_id--version' => $dbxref->version,
  337. $field_table . '__dbxref_id--description' => $dbxref->description,
  338. );
  339. $i++;
  340. }
  341. }
  342. }
  343. }
  344. /**
  345. * Theme function for the dbxref_id_widget.
  346. *
  347. * @param $variables
  348. */
  349. function theme_chado_linker__dbxref_widget($variables) {
  350. $element = $variables['element'];
  351. // These two fields were added to the widget to help identify the fields
  352. // for layout.
  353. $table_name = $element['#table_name'];
  354. $fkey = $element['#fkey_field'];
  355. $layout = "
  356. <div class=\"secondary-dbxref-widget\">
  357. <div class=\"secondary-dbxref-widget-item\">" .
  358. drupal_render($element[$table_name . '__dbxref_id--db_id']) . "
  359. </div>
  360. <div class=\"secondary-dbxref-widget-item\">" .
  361. drupal_render($element[$table_name . '__dbxref_id--accession']) . "
  362. </div>
  363. <div class=\"secondary-dbxref-widget-item\">" .
  364. drupal_render($element[$table_name . '__dbxref_id--version']) . "
  365. </div>
  366. <div class=\"secondary-dbxref-widget-item\">" .
  367. drupal_render($element[$table_name . '__dbxref_id--description']) . "
  368. </div>
  369. <div class=\"secondary-dbxref-widget-links\">" . drupal_render($element['links']) . "</div>
  370. </div>
  371. ";
  372. return $layout;
  373. }
  374. /**
  375. * Callback function for validating the chado_linker__dbxref_widget.
  376. */
  377. function chado_linker__dbxref_widget_validate($element, &$form_state) {
  378. $field_name = $element['#field_name'];
  379. $delta = $element['#delta'];
  380. $table_name = $element['#table_name'];
  381. $fkey = $element['#fkey_field'];
  382. $field = field_info_field($field_name);
  383. $field_type = $field['type'];
  384. $field_table = $field['settings']['chado_table'];
  385. $field_column = $field['settings']['chado_column'];
  386. $field_prefix = $field_table . '__dbxref_id';
  387. // If the form ID is field_ui_field_edit_form, then the user is editing the
  388. // field's values in the manage fields form of Drupal. We don't want
  389. // to validate it as if it were being used in a data entry form.
  390. if ($form_state['build_info']['form_id'] =='field_ui_field_edit_form') {
  391. return;
  392. }
  393. // Get the field values.
  394. $dbxref_id = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $field_table . '__dbxref_id');
  395. $db_id = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $field_prefix . '--db_id');
  396. $accession = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $field_prefix . '--accession');
  397. $version = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $field_prefix . '--version');
  398. $description = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $field_prefix . '--description');
  399. // Make sure that if a database ID is provided that an accession is also
  400. // provided. Here we use the form_set_error function rather than the
  401. // form_error function because the form_error will add a red_highlight
  402. // around all of the fields in the fieldset which is confusing as it's not
  403. // clear to the user what field is required and which isn't. Therefore,
  404. // we borrow the code from the 'form_error' function and append the field
  405. // so that the proper field is highlighted on error.
  406. if (!$db_id and $accession) {
  407. form_set_error(implode('][', $element ['#parents']) . '][' . $field_prefix . '--db_id', t("A database and the accession must both be provided."));
  408. }
  409. if ($db_id and !$accession) {
  410. form_set_error(implode('][', $element ['#parents']) . '][' . $field_prefix . '--accession', t("A database and the accession must both be provided."));
  411. }
  412. if (!$db_id and !$accession and ($version or $description)) {
  413. form_set_error(implode('][', $element ['#parents']) . '][' . $field_prefix . '--db_id', t("A database and the accession must both be provided."));
  414. }
  415. // If the dbxref_id does not match the db_id + accession then the user
  416. // has selected a new dbxref record and we need to update the hidden
  417. // value accordingly.
  418. if ($db_id and $accession) {
  419. $dbxref = chado_generate_var('dbxref', array('db_id' => $db_id, 'accession' => $accession));
  420. if ($dbxref and $dbxref->dbxref_id != $dbxref_id) {
  421. tripal_chado_set_field_form_values($field_name, $form_state, $dbxref->dbxref_id, $delta, $table_name . '__dbxref_id');
  422. tripal_chado_set_field_form_values($field_name, $form_state, $dbxref->dbxref_id, $delta, $field_prefix . '--dbxref_id');
  423. }
  424. }
  425. else {
  426. // If the db_id and accession are not set, then remove the linker FK value to the base table.
  427. tripal_chado_set_field_form_values($field_name, $form_state, '', $delta, $table_name . '__' . $fkey);
  428. }
  429. }
  430. /**
  431. * An Ajax callback for the dbxref widget.
  432. */
  433. function chado_linker__dbxref_widget_form_ajax_callback($form, $form_state) {
  434. $field_name = $form_state['triggering_element']['#parents'][0];
  435. $delta = $form_state['triggering_element']['#parents'][2];
  436. $field = field_info_field($field_name);
  437. $field_type = $field['type'];
  438. $field_table = $field['settings']['chado_table'];
  439. $field_column = $field['settings']['chado_column'];
  440. $field_prefix = $field_table . '__dbxref_id';
  441. // Check to see if this dbxref already exists. If not then
  442. // give a notice to the user that the dbxref will be added.
  443. $db_id = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $field_prefix . '--db_id');
  444. $accession = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $field_prefix . '--accession');
  445. if ($db_id and $accession) {
  446. $values = array(
  447. 'db_id' => $db_id,
  448. 'accession' => $accession,
  449. );
  450. $options = array('is_duplicate' => TRUE);
  451. $has_duplicate = chado_select_record('dbxref', array('*'), $values, $options);
  452. if (!$has_duplicate) {
  453. drupal_set_message('The selected cross reference is new and will be added for future auto completions.');
  454. }
  455. }
  456. return $form[$field_name]['und'][$delta];
  457. }