chado_linker__dbxref.inc 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506
  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. 'deafult' => array(
  124. 'label' => 'above',
  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. }
  178. /**
  179. * @see TripalField::widgetForm()
  180. */
  181. public static function widgetForm(&$widget, &$form, &$form_state, $field, $instance,
  182. $langcode, $items, $delta, $element) {
  183. $field_name = $field['field_name'];
  184. $field_type = $field['type'];
  185. $field_table = $field['settings']['chado_table'];
  186. $field_column = $field['settings']['chado_column'];
  187. // Get the FK column that links to the base table.
  188. $chado_table = $field['settings']['chado_table'];
  189. $base_table = $field['settings']['base_table'];
  190. $schema = chado_get_schema($chado_table);
  191. $pkey = $schema['primary key'][0];
  192. $fkeys = array_values($schema['foreign keys'][$base_table]['columns']);
  193. $fkey = $fkeys[0];
  194. // Get the field defaults.
  195. $record_id = '';
  196. $fkey_value = '';
  197. $dbxref_id = '';
  198. $db_id = '';
  199. $accession = '';
  200. $version = '';
  201. $description = '';
  202. // If the field already has a value then it will come through the $items
  203. // array. This happens when editing an existing record.
  204. if (array_key_exists($delta, $items)) {
  205. $record_id = $items[$delta][$field_table . '__' . $pkey];
  206. $fkey_value = $items[$delta][$field_table . '__' . $fkey];
  207. $dbxref_id = $items[$delta][$field_table . '__dbxref_id'];
  208. $db_id = $items[$delta][$field_table . '__dbxref_id--db_id'];
  209. $accession = $items[$delta][$field_table . '__dbxref_id--accession'];
  210. $version = $items[$delta][$field_table . '__dbxref_id--version'];
  211. $description = $items[$delta][$field_table . '__dbxref_id--description'];
  212. }
  213. // Check $form_state['values'] to see if an AJAX call set the values.
  214. if (array_key_exists('values', $form_state) and array_key_exists($delta, $form_state['values'])) {
  215. $record_id = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $field_table . '__' . $pkey);
  216. $fkey_value = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $field_table . '__' . $fkey);
  217. $dbxref_id = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $field_table . '__dbxref_id');
  218. $db_id = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $field_table . '__dbxref_id--db_id');
  219. $accession = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $field_table . '__dbxref_id--accession');
  220. $version = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $field_table . '__dbxref_id--version');
  221. $description = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $field_table . '__dbxref_id--description');
  222. }
  223. $schema = chado_get_schema('dbxref');
  224. $options = tripal_get_db_select_options();
  225. $widget['#table_name'] = $chado_table;
  226. $widget['#fkey_field'] = $fkey;
  227. $widget['#element_validate'] = array('chado_linker__dbxref_widget_validate');
  228. $widget['#theme'] = 'chado_linker__dbxref_widget';
  229. $widget['#prefix'] = "<span id='$field_name-dbxref--db-id-$delta'>";
  230. $widget['#suffix'] = "</span>";
  231. $widget['value'] = array(
  232. '#type' => 'value',
  233. '#value' => array_key_exists($delta, $items) ? $items[$delta]['value'] : '',
  234. );
  235. $widget[$field_table . '__' . $pkey] = array(
  236. '#type' => 'value',
  237. '#default_value' => $record_id,
  238. );
  239. $widget[$field_table . '__' . $fkey] = array(
  240. '#type' => 'value',
  241. '#default_value' => $fkey_value,
  242. );
  243. $widget[$field_table . '__dbxref_id'] = array(
  244. '#type' => 'value',
  245. '#default_value' => $dbxref_id,
  246. );
  247. $widget[$field_table . '__dbxref_id--dbxref_id'] = array(
  248. '#type' => 'value',
  249. '#default_value' => $dbxref_id,
  250. );
  251. $widget[$field_table . '__dbxref_id--db_id'] = array(
  252. '#type' => 'select',
  253. '#title' => t('Database'),
  254. '#options' => $options,
  255. '#required' => $element['#required'],
  256. '#default_value' => $db_id,
  257. '#ajax' => array(
  258. 'callback' => "chado_linker__dbxref_widget_form_ajax_callback",
  259. 'wrapper' => "$field_name-dbxref--db-id-$delta",
  260. 'effect' => 'fade',
  261. 'method' => 'replace'
  262. ),
  263. );
  264. $widget[$field_table . '__dbxref_id--accession'] = array(
  265. '#type' => 'textfield',
  266. '#title' => t('Accession'),
  267. '#default_value' => $accession,
  268. '#required' => $element['#required'],
  269. '#maxlength' => array_key_exists('length', $schema['fields']['accession']) ? $schema['fields']['accession']['length'] : 255,
  270. '#size' => 15,
  271. '#autocomplete_path' => 'admin/tripal/storage/chado/auto_name/dbxref/' . $db_id,
  272. '#ajax' => array(
  273. 'callback' => "chado_linker__dbxref_widget_form_ajax_callback",
  274. 'wrapper' => "$field_name-dbxref--db-id-$delta",
  275. 'effect' => 'fade',
  276. 'method' => 'replace'
  277. ),
  278. '#disabled' => $db_id ? FALSE : TRUE,
  279. );
  280. $widget[$field_table . '__dbxref_id--version'] = array(
  281. '#type' => 'textfield',
  282. '#title' => t('Version'),
  283. '#default_value' => $version,
  284. '#maxlength' => array_key_exists('length', $schema['fields']['version']) ? $schema['fields']['version']['length'] : 255,
  285. '#size' => 5,
  286. '#disabled' => $db_id ? FALSE : TRUE,
  287. );
  288. $widget[$field_table . '__dbxref_id--description'] = array(
  289. '#type' => 'textfield',
  290. '#title' => t('Description'),
  291. '#default_value' => $description,
  292. '#size' => 20,
  293. '#disabled' => $db_id ? FALSE : TRUE,
  294. );
  295. if (!$db_id) {
  296. $widget['links'] = array(
  297. '#type' => 'item',
  298. '#markup' => l('Add a database', 'admin/tripal/chado/tripal_db/add', array('attributes' => array('target' => '_blank')))
  299. );
  300. }
  301. }
  302. /**
  303. * @see TripalField::load()
  304. */
  305. static function load($field, $entity, $details = array()) {
  306. $record = $details['record'];
  307. $field_name = $field['field_name'];
  308. $field_type = $field['type'];
  309. $field_table = $field['settings']['chado_table'];
  310. $field_column = $field['settings']['chado_column'];
  311. $base_table = $record->tablename;
  312. $schema = chado_get_schema($field_table);
  313. $pkey = $schema['primary key'][0];
  314. $fkeys = array_values($schema['foreign keys'][$base_table]['columns']);
  315. $fkey = $fkeys[0];
  316. // Set some defaults for the empty record.
  317. $entity->{$field_name}['und'][0] = array(
  318. 'value' => array(),
  319. $field_table . '__' . $pkey => '',
  320. $field_table . '__' . $fkey => '',
  321. $field_table . '__dbxref_id' => '',
  322. $field_table . '__dbxref_id--dbxref_id' => '',
  323. $field_table . '__dbxref_id--db_id' => '',
  324. $field_table . '__dbxref_id--accession' => '',
  325. $field_table . '__dbxref_id--version' => '',
  326. $field_table . '__dbxref_id--description' => '',
  327. );
  328. $linker_table = $base_table . '_dbxref';
  329. $options = array('return_array' => 1);
  330. $record = chado_expand_var($record, 'table', $linker_table, $options);
  331. if (count($record->$linker_table) > 0) {
  332. $i = 0;
  333. foreach ($record->$linker_table as $index => $linker) {
  334. $dbxref = $linker->dbxref_id;
  335. $entity->{$field_name}['und'][$i] = array(
  336. 'value' => array(
  337. 'vocabulary' => $dbxref->db_id->name,
  338. 'accession' => $dbxref->accession,
  339. 'URL' => $URL,
  340. ),
  341. $field_table . '__' . $pkey => $linker->$pkey,
  342. $field_table . '__' . $fkey => $linker->$fkey->$fkey,
  343. $field_table . '__dbxref_id' => $dbxref->dbxref_id,
  344. $field_table . '__dbxref_id--dbxref_id' => $dbxref->dbxref_id,
  345. $field_table . '__dbxref_id--db_id' => $dbxref->db_id->db_id,
  346. $field_table . '__dbxref_id--accession' => $dbxref->accession,
  347. $field_table . '__dbxref_id--version' => $dbxref->version,
  348. $field_table . '__dbxref_id--description' => $dbxref->description,
  349. );
  350. $i++;
  351. }
  352. }
  353. }
  354. }
  355. /**
  356. * Theme function for the dbxref_id_widget.
  357. *
  358. * @param $variables
  359. */
  360. function theme_chado_linker__dbxref_widget($variables) {
  361. $element = $variables['element'];
  362. // These two fields were added to the widget to help identify the fields
  363. // for layout.
  364. $table_name = $element['#table_name'];
  365. $fkey = $element['#fkey_field'];
  366. $layout = "
  367. <div class=\"secondary-dbxref-widget\">
  368. <div class=\"secondary-dbxref-widget-item\">" .
  369. drupal_render($element[$table_name . '__dbxref_id--db_id']) . "
  370. </div>
  371. <div class=\"secondary-dbxref-widget-item\">" .
  372. drupal_render($element[$table_name . '__dbxref_id--accession']) . "
  373. </div>
  374. <div class=\"secondary-dbxref-widget-item\">" .
  375. drupal_render($element[$table_name . '__dbxref_id--version']) . "
  376. </div>
  377. <div class=\"secondary-dbxref-widget-item\">" .
  378. drupal_render($element[$table_name . '__dbxref_id--description']) . "
  379. </div>
  380. <div class=\"secondary-dbxref-widget-links\">" . drupal_render($element['links']) . "</div>
  381. </div>
  382. ";
  383. return $layout;
  384. }
  385. /**
  386. * Callback function for validating the chado_linker__dbxref_widget.
  387. */
  388. function chado_linker__dbxref_widget_validate($element, &$form_state) {
  389. $field_name = $element['#field_name'];
  390. $delta = $element['#delta'];
  391. $table_name = $element['#table_name'];
  392. $fkey = $element['#fkey_field'];
  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 . '__dbxref_id';
  398. // If the form ID is field_ui_field_edit_form, then the user is editing the
  399. // field's values in the manage fields form of Drupal. We don't want
  400. // to validate it as if it were being used in a data entry form.
  401. if ($form_state['build_info']['form_id'] =='field_ui_field_edit_form') {
  402. return;
  403. }
  404. // Get the field values.
  405. $dbxref_id = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $field_table . '__dbxref_id');
  406. $db_id = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $field_prefix . '--db_id');
  407. $accession = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $field_prefix . '--accession');
  408. $version = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $field_prefix . '--version');
  409. $description = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $field_prefix . '--description');
  410. // Make sure that if a database ID is provided that an accession is also
  411. // provided. Here we use the form_set_error function rather than the
  412. // form_error function because the form_error will add a red_highlight
  413. // around all of the fields in the fieldset which is confusing as it's not
  414. // clear to the user what field is required and which isn't. Therefore,
  415. // we borrow the code from the 'form_error' function and append the field
  416. // so that the proper field is highlighted on error.
  417. if (!$db_id and $accession) {
  418. form_set_error(implode('][', $element ['#parents']) . '][' . $field_prefix . '--db_id', t("A database and the accession must both be provided."));
  419. }
  420. if ($db_id and !$accession) {
  421. form_set_error(implode('][', $element ['#parents']) . '][' . $field_prefix . '--accession', t("A database and the accession must both be provided."));
  422. }
  423. if (!$db_id and !$accession and ($version or $description)) {
  424. form_set_error(implode('][', $element ['#parents']) . '][' . $field_prefix . '--db_id', t("A database and the accession must both be provided."));
  425. }
  426. // If the dbxref_id does not match the db_id + accession then the user
  427. // has selected a new dbxref record and we need to update the hidden
  428. // value accordingly.
  429. if ($db_id and $accession) {
  430. $dbxref = chado_generate_var('dbxref', array('db_id' => $db_id, 'accession' => $accession));
  431. if ($dbxref and $dbxref->dbxref_id != $dbxref_id) {
  432. tripal_chado_set_field_form_values($field_name, $form_state, $dbxref->dbxref_id, $delta, $table_name . '__dbxref_id');
  433. tripal_chado_set_field_form_values($field_name, $form_state, $dbxref->dbxref_id, $delta, $field_prefix . '--dbxref_id');
  434. }
  435. }
  436. else {
  437. // If the db_id and accession are not set, then remove the linker FK value to the base table.
  438. tripal_chado_set_field_form_values($field_name, $form_state, '', $delta, $table_name . '__' . $fkey);
  439. }
  440. }
  441. /**
  442. * An Ajax callback for the dbxref widget.
  443. */
  444. function chado_linker__dbxref_widget_form_ajax_callback($form, $form_state) {
  445. $field_name = $form_state['triggering_element']['#parents'][0];
  446. $delta = $form_state['triggering_element']['#parents'][2];
  447. $field = field_info_field($field_name);
  448. $field_type = $field['type'];
  449. $field_table = $field['settings']['chado_table'];
  450. $field_column = $field['settings']['chado_column'];
  451. $field_prefix = $field_table . '__dbxref_id';
  452. // Check to see if this dbxref already exists. If not then
  453. // give a notice to the user that the dbxref will be added.
  454. $db_id = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $field_prefix . '--db_id');
  455. $accession = tripal_chado_get_field_form_values($field_name, $form_state, $delta, $field_prefix . '--accession');
  456. if ($db_id and $accession) {
  457. $values = array(
  458. 'db_id' => $db_id,
  459. 'accession' => $accession,
  460. );
  461. $options = array('is_duplicate' => TRUE);
  462. $has_duplicate = chado_select_record('dbxref', array('*'), $values, $options);
  463. if (!$has_duplicate) {
  464. drupal_set_message('The selected cross reference is new and will be added for future auto completions.');
  465. }
  466. }
  467. return $form[$field_name]['und'][$delta];
  468. }