chado_linker__dbxref.inc 18 KB

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