chado_linker__dbxref.inc 17 KB

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