chado_linker__dbxref.inc 19 KB

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