chado_linker__dbxref.inc 18 KB

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