chado_linker__dbxref.inc 15 KB

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