tripal_core.chado_nodes.dbxrefs.api.inc 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  1. <?php
  2. /**
  3. * @file
  4. * API to manage the chado _dbxref table for various Tripal Node Types
  5. *
  6. * How To Use:
  7. * @code
  8. function chado_example_form($form, $form_state) {
  9. // Default values for form elements can come in the following ways:
  10. //
  11. // 1) as elements of the $node object. This occurs when editing an existing node
  12. // 2) in the $form_state['values'] array which occurs on a failed validation or
  13. // ajax callbacks when the ajax call originates from non-submit fields other
  14. // than button
  15. // 3) in the $form_state['input'] array which occurs on ajax callbacks from submit
  16. // form elements (e.g. buttons) and the form is being rebuilt but has not yet
  17. // been validated
  18. //
  19. // The reference elements added by this function do use AJAX calls from buttons,
  20. // therefore, it is important to check for form values in the $form_state['values']
  21. // for case #2 above, and in the $form_state['input'] for case #3.
  22. // See the chado analysis node form for an example.
  23. // Next, add in all the form array definition particular to your node type
  24. // To add in the additional database reference form elements, you first need to prepare the arguments
  25. // for the function call.
  26. $details = array(
  27. 'linking_table' => 'example_dbxref', // the name of the table linking additional dbxrefs to this node
  28. 'base_foreign_key' => 'example_id', // key to link to the chado content created by this node
  29. 'base_key_value' => $example_id, // the value of the above key
  30. 'fieldset_title' => 'Additional References', // the non-translated title for this fieldset
  31. 'additional_instructions' => '' // a non-stranslated string providing additional instructions
  32. );
  33. // Finally, and add the additional form elements to the form
  34. chado_node_additional_dbxrefs_form($form, $form_state, $details);
  35. return $form;
  36. }
  37. function chado_example_insert($node) {
  38. // if there is an example_id in the $node object then this must be a sync so
  39. // we can skip adding the chado_example as it is already there, although
  40. // we do need to proceed with the rest of the insert
  41. if (!property_exists($node, 'example_id')) {
  42. // Add record to chado example table
  43. // Add to any other tables needed
  44. // Add all additional database references
  45. // This function will create new database references as needed and select existing ones.
  46. // Existing _dbxref links will be cleared and then re-added
  47. chado_node_additional_dbxrefs_form_update_dbxrefs(
  48. $node, // the node object passed in via hook_insert()
  49. 'example_dbxref', // the name of the _dbxref linking table
  50. 'example_id', // key to link to the chado content created by this node
  51. $node->example_id // value of the above key
  52. );
  53. }
  54. // Add record to chado_example linking example_id to new node
  55. }
  56. function chado_example_update($node) {
  57. // Update record in chado example table
  58. // Update any other tables needed
  59. // Update all additional database references
  60. // This function will create new database references as needed and select existing ones.
  61. // Existing _dbxref links will be cleared and then re-added
  62. chado_node_additional_dbxrefs_form_update_dbxrefs(
  63. $node, // the node object passed in via hook_insert()
  64. 'example_dbxref', // the name of the _dbxref linking table
  65. 'example_id', // key to link to the chado content created by this node
  66. $node->example_id // value of the above key
  67. );
  68. // Don't need to update chado_example linking table since niether example_id or nid can be changed in update
  69. }
  70. * @endcode
  71. *
  72. * @ingroup tripal_chado_node_api
  73. */
  74. /**
  75. * @section
  76. * Additional Database References Form to be added to node forms
  77. */
  78. /**
  79. * Provides a form for adding to BASE_dbxref and dbxref tables
  80. *
  81. * @param $form
  82. * The Drupal form array into which the dbxref elements will be added
  83. * @param $form_state
  84. * The corresponding form_state array for the form
  85. * @param $details
  86. * An array defining details needed by this form. Required Keys are:
  87. * - linking_table: the name of the dbxref linking table (ie: feature_dbxref)
  88. * - base_foreign_key: the name of the foreign key linking this table to the non-dbxref table (ie: feature_id)
  89. * - base_key_value: the value of the base_foreign_key for the current form (ie: 999 if the feature_id=999)
  90. * Optional keys include:
  91. * - fieldset_title: the non-translated title for this fieldset
  92. * - additional_instructions: a non-translated string providing additional instructions
  93. *
  94. * @ingroup tripal_chado_node_api
  95. */
  96. function chado_node_additional_dbxrefs_form(&$form, &$form_state, $details) {
  97. // Set Defaults for optional fields
  98. $details['fieldset_title'] = 'Additional Database References';
  99. $details['additional_instructions'] = '';
  100. // the fieldset of the dbxref elements
  101. $form['addtl_dbxrefs'] = array(
  102. '#type' => 'fieldset',
  103. '#title' => t($details['fieldset_title']),
  104. '#description' => t('You may add additional database references by
  105. selecting a database reference type from the dropdown and adding text. You may add
  106. as many database references as desired by clicking the add button on the right. To
  107. remove a database reference, click the remove button. ' . $details['additional_instructions']),
  108. '#prefix' => "<div id='addtl-dbxrefs-fieldset'>",
  109. '#suffix' => '</div>'
  110. );
  111. // this form element is a tree, so that we don't puke all of the values into then node variable
  112. // it is set as a tree, and keeps them in the $form_state['values']['dbxref_table'] heading.
  113. $form['addtl_dbxrefs']['dbxref_table'] = array(
  114. '#type' => 'markup',
  115. '#tree' => TRUE,
  116. '#prefix' => '<div id="tripal-generic-edit-addtl_dbxrefs-table">',
  117. '#suffix' => '</div>',
  118. '#theme' => 'chado_node_additional_dbxrefs_form_table'
  119. );
  120. /* DBxrefs can come to us in two ways:
  121. *
  122. * 1) In the form state in the $form_state['chado_additional_dbxrefs']. Data is in this field
  123. * when an AJAX call updates the form state or a validation error.
  124. *
  125. * 2) Directly from the database if the record already has dbxref's associated. This
  126. * data is only used the first time the form is loaded. On AJAX calls or validation
  127. * errors the fields on the form are populated from the $form_state['chado_additional_dbxrefs']
  128. * entry.
  129. */
  130. if (isset($form_state['chado_additional_dbxrefs'])) {
  131. $existing_dbxrefs = $form_state['chado_additional_dbxrefs'];
  132. }
  133. else {
  134. $existing_dbxrefs = chado_query(
  135. "SELECT dbxref.dbxref_id, db.name as db_name, db.db_id as db_id, dbxref.accession as accession, dbxref.description as description, dbxref.version
  136. FROM {dbxref} dbxref
  137. LEFT JOIN {db} db ON db.db_id = dbxref.db_id
  138. LEFT JOIN {".$details['linking_table']."} linking_table ON linking_table.dbxref_id = dbxref.dbxref_id
  139. WHERE linking_table.".$details['base_foreign_key']."= :base_key_value
  140. ORDER BY db.name ASC, dbxref.version ASC",
  141. array(':base_key_value' => $details['base_key_value'])
  142. );
  143. }
  144. /* The format of the $existing_dbxref's array is either:
  145. *
  146. * From the chado_additional_dbxrefs array:
  147. * $form_state['chado_additional_dbxrefs'] = array(
  148. * '[db_id]-[version]' => array(
  149. * 'db_id' => [the db.db_id value]
  150. * 'db_name' => [the db.name value]
  151. * 'dbxref_id' => [the dbxref.dbxref_id value, or NULL if it doesn't yet exists],
  152. * 'version' => [the dbxref.version value],
  153. * 'accession' => [the dbxref.accession value],
  154. * ),
  155. * );
  156. *
  157. * OR
  158. * Populated from the database:
  159. * $existing_dbxref = array(
  160. * 0 => array(
  161. * 'dbxref_id' => [the dbxref.dbxref_id value],
  162. * 'db_name' => [the db.name value],
  163. * 'db_id' => [the db.db_id value],
  164. * 'accession' => [the dbxref.accession value],
  165. * 'description' => [the dbxref.description value],
  166. * 'version' => [the dbxref.versiion value],
  167. * ),
  168. * );
  169. *
  170. * NOTE: The main difference is the key
  171. *
  172. * Loop on the array elements of the $existing_dbxrefs array and add
  173. * an element to the form for each one.
  174. */
  175. foreach ($existing_dbxrefs as $dbxref) {
  176. // Since the version is part of the key, when it is '' we need to use something
  177. // in the key to indicate this case. Otherwise, you wouldn't be able to select
  178. // those elements from the array (ie: $form['addtl_dbxrefs']['dbxref_table'][9999]['']
  179. // doesn't work as expected whereas $form['addtl_dbxrefs']['dbxref_table'][9999][NONE]
  180. // is much better)
  181. $version = (!empty($dbxref->version)) ? $dbxref->version : 'NONE';
  182. $form['addtl_dbxrefs']['dbxref_table'][$dbxref->db_id] = array(
  183. '#type' => 'markup',
  184. '#value' => ''
  185. );
  186. $form['addtl_dbxrefs']['dbxref_table'][$dbxref->db_id][$version] = array(
  187. '#type' => 'markup',
  188. '#value' => ''
  189. );
  190. $form['addtl_dbxrefs']['dbxref_table'][$dbxref->db_id][$version]['db_id'] = array(
  191. '#type' => 'hidden',
  192. '#value' => $dbxref->db_id
  193. );
  194. $form['addtl_dbxrefs']['dbxref_table'][$dbxref->db_id][$version]['accession'] = array(
  195. '#type' => 'hidden',
  196. '#value' => $dbxref->accession
  197. );
  198. $form['addtl_dbxrefs']['dbxref_table'][$dbxref->db_id][$version]['dbxref_id'] = array(
  199. '#type' => 'hidden',
  200. '#value' => $dbxref->dbxref_id
  201. );
  202. $form['addtl_dbxrefs']['dbxref_table'][$dbxref->db_id][$version]['db'] = array(
  203. '#type' => 'markup',
  204. '#markup' => $dbxref->db_name
  205. );
  206. $form['addtl_dbxrefs']['dbxref_table'][$dbxref->db_id][$version]['dbxref_version'] = array(
  207. '#type' => 'markup',
  208. '#markup' => $dbxref->version
  209. );
  210. $form['addtl_dbxrefs']['dbxref_table'][$dbxref->db_id][$version]['dbxref_accession'] = array(
  211. '#type' => 'markup',
  212. '#markup' => $dbxref->accession
  213. );
  214. // remove button
  215. $form['addtl_dbxrefs']['dbxref_table'][$dbxref->db_id][$version]['dbxref_action'] = array(
  216. '#type' => 'submit',
  217. '#value' => t('Remove'),
  218. '#name' => "dbxref_remove-".$dbxref->db_id.'-'.$version,
  219. '#ajax' => array(
  220. 'callback' => "chado_node_additional_dbxrefs_form_ajax_update",
  221. 'wrapper' => 'tripal-generic-edit-addtl_dbxrefs-table',
  222. 'effect' => 'fade',
  223. 'method' => 'replace',
  224. 'prevent' => 'click'
  225. ),
  226. // When this button is clicked, the form will be validated and submitted.
  227. // Therefore, we set custom submit and validate functions to override the
  228. // default node form submit. In the validate function we validate only the
  229. // additional dbxref fields and in the submit we remove the indicated dbxref
  230. // from the chado_additional_dbxrefs array. In order to keep validate errors
  231. // from the node form validate and Drupal required errors for non-dbxref fields
  232. // preventing the user from removing dbxrefs we set the #limit_validation_errors below
  233. '#validate' => array('chado_node_additional_dbxrefs_form_remove_button_validate'),
  234. '#submit' => array('chado_node_additional_dbxrefs_form_remove_button_submit'),
  235. // Limit the validation of the form upon clicking this button to the dbxref_table tree
  236. // No other fields will be validated (ie: no fields from the main form or any other api
  237. // added form).
  238. '#limit_validation_errors' => array(
  239. array('dbxref_table') // Validate all fields within $form_state['values']['dbxref_table']
  240. )
  241. );
  242. }
  243. // Form elements for adding a new dbxref
  244. //---------------------------------------------
  245. $form['addtl_dbxrefs']['dbxref_table']['new'] = array(
  246. '#type' => 'markup',
  247. '#prefix' => '<span class="addtl-dbxrefs-add-new-dbxref">',
  248. '#suffix' => '</span>'
  249. );
  250. // add in the existing databases
  251. $db_options = array(0 => 'Select a Database');
  252. $select = tripal_core_chado_select('db', array('db_id','name'), array(), array('order_by' => array('name' => 'ASC')));
  253. foreach($select as $db) {
  254. $db_options[$db->db_id] = $db->name;
  255. }
  256. $form['addtl_dbxrefs']['dbxref_table']['new']['db'] = array(
  257. '#type' => 'select',
  258. '#options' => $db_options,
  259. );
  260. $form['addtl_dbxrefs']['dbxref_table']['new']['dbxref_accession'] = array(
  261. '#type' => 'textfield',
  262. );
  263. $form['addtl_dbxrefs']['dbxref_table']['new']['dbxref_version'] = array(
  264. '#type' => 'textfield',
  265. );
  266. // add button
  267. $form['addtl_dbxrefs']['dbxref_table']['new']['dbxref_action'] = array(
  268. '#type' => 'submit',
  269. '#value' => t('Add'),
  270. '#name' => "dbxref-add",
  271. '#ajax' => array(
  272. 'callback' => "chado_node_additional_dbxrefs_form_ajax_update",
  273. 'wrapper' => 'tripal-generic-edit-addtl_dbxrefs-table',
  274. 'effect' => 'fade',
  275. 'method' => 'replace',
  276. 'prevent' => 'click'
  277. ),
  278. // When this button is clicked, the form will be validated and submitted.
  279. // Therefore, we set custom submit and validate functions to override the
  280. // default node form submit. In the validate function we validate only the
  281. // additional dbxref fields and in the submit we add them to the chado_additional_dbxrefs
  282. // array. In order to keep validate errors from the node form validate and Drupal
  283. // required errors for non-dbxref fields preventing the user from adding dbxrefs we
  284. // set the #limit_validation_errors below
  285. '#validate' => array('chado_node_additional_dbxrefs_form_add_button_validate'),
  286. '#submit' => array('chado_node_additional_dbxrefs_form_add_button_submit'),
  287. // Limit the validation of the form upon clicking this button to the dbxref_table tree
  288. // No other fields will be validated (ie: no fields from the main form or any other api
  289. // added form).
  290. '#limit_validation_errors' => array(
  291. array('dbxref_table') // Validate all fields within $form_state['values']['dbxref_table']
  292. )
  293. );
  294. }
  295. /**
  296. * Validate the user input for creating a new dbxref
  297. * Called by the add button in chado_node_additional_dbxrefs_form
  298. *
  299. * @ingroup tripal_chado_node_api
  300. */
  301. function chado_node_additional_dbxrefs_form_add_button_validate($form, &$form_state) {
  302. // Ensure the db_id is supplied & Valid
  303. $db = tripal_core_chado_select(
  304. 'db',
  305. array('db_id', 'name'),
  306. array('db_id' => $form_state['values']['dbxref_table']['new']['db'])
  307. );
  308. if (!isset($db[0])) {
  309. form_set_error('dbxref_table][new][db', 'Please select a database before attempting to add a new database reference.');
  310. }
  311. else {
  312. $form_state['values']['dbxref_table']['new']['db_name'] = $db[0]->name;
  313. }
  314. // Ensure accession is supplied
  315. if (empty($form_state['values']['dbxref_table']['new']['dbxref_accession'])) {
  316. form_set_error('dbxref_table][new][dbxref_accession','You must enter the accession before attempting to add a new database reference.');
  317. }
  318. }
  319. /**
  320. * Called by the add button in chado_node_additional_dbxrefs_form
  321. *
  322. * Create an array of additional dbxrefs in the form state. This array will then be
  323. * used to rebuild the form in subsequent builds
  324. *
  325. * @ingroup tripal_chado_node_api
  326. */
  327. function chado_node_additional_dbxrefs_form_add_button_submit(&$form, &$form_state) {
  328. // if the chado_additional_dbxrefs array is not set then this is the first time modifying the
  329. // dbxref table. this means we need to include all the dbxrefs from the db
  330. if (!isset($form_state['chado_additional_dbxrefs'])) {
  331. chado_node_additional_dbxrefs_form_create_dbxref_formstate_array($form, $form_state);
  332. }
  333. // get details for the new dbxref
  334. $dbxref = array(
  335. 'db_id' => $form_state['values']['dbxref_table']['new']['db'],
  336. 'db_name' => $form_state['values']['dbxref_table']['new']['db_name'],
  337. 'dbxref_id' => NULL,
  338. 'version' => $form_state['values']['dbxref_table']['new']['dbxref_version'],
  339. 'accession' => $form_state['values']['dbxref_table']['new']['dbxref_accession'],
  340. );
  341. $version = (!empty($dbxref['version'])) ? $dbxref['version'] : 'NONE';
  342. $key = $dbxref['db_id'] . '-' . $version;
  343. $form_state['chado_additional_dbxrefs'][$key] = (object) $dbxref;
  344. $form_state['rebuild'] = TRUE;
  345. }
  346. /**
  347. * There is no user input for the remove buttons so there is no need to validate
  348. * However, both a submit & validate need to be specified so this is just a placeholder
  349. *
  350. * Called by the many remove buttons in chado_node_additional_dbxrefs_form
  351. *
  352. * @ingroup tripal_chado_node_api
  353. */
  354. function chado_node_additional_dbxrefs_form_remove_button_validate($form, $form_state) {
  355. // No Validation needed for remove
  356. }
  357. /**
  358. * Remove the correct dbxref from the form
  359. * Called by the many remove buttons in chado_node_additional_dbxrefs_form
  360. *
  361. * @ingroup tripal_chado_node_api
  362. */
  363. function chado_node_additional_dbxrefs_form_remove_button_submit(&$form, &$form_state) {
  364. // if the chado_additional_dbxrefs array is not set then this is the first time modifying the
  365. // dbxref table. this means we need to include all the dbxrefs from the db
  366. if (!isset($form_state['chado_additional_dbxrefs'])) {
  367. chado_node_additional_dbxrefs_form_create_dbxref_formstate_array($form, $form_state);
  368. }
  369. // remove the specified dbxref from the form dbxref table
  370. if(preg_match('/dbxref_remove-([^-]+-[^-]+)/',$form_state['triggering_element']['#name'],$match)) {
  371. $key = $match[1];
  372. if (array_key_exists($key, $form_state['chado_additional_dbxrefs'])) {
  373. unset($form_state['chado_additional_dbxrefs'][$key]);
  374. }
  375. }
  376. $form_state['rebuild'] = TRUE;
  377. }
  378. /**
  379. * Ajax function which returns the section of the form to be re-rendered
  380. *
  381. * @ingroup tripal_chado_node_api
  382. */
  383. function chado_node_additional_dbxrefs_form_ajax_update($form, $form_state) {
  384. return $form['addtl_dbxrefs']['dbxref_table'];
  385. }
  386. /**
  387. * Creates an array in form_state containing the existing addtl_dbxrefs. This array is
  388. * then modified by the add/remove buttons and used as a source for rebuilding the form.
  389. * This function get's called at each button (add and remove) button submits the first
  390. * time one of the button's is clicked to instantiates the $form_state['chado_additional_dbxrefs'] array
  391. *
  392. * $form_state['chado_additional_dbxrefs'] = array(
  393. * '[db_id]-[version]' => array(
  394. * 'db_id' => [the db.db_id value]
  395. * 'db_name' => [the db.name value]
  396. * 'dbxref_id' => [the dbxref.dbxref_id value, or NULL if it doesn't yet exists],
  397. * 'version' => [the dbxref.version value],
  398. * 'accession' => [the dbxref.accession value],
  399. * ),
  400. * );
  401. *
  402. * @ingroup tripal_chado_node_api
  403. */
  404. function chado_node_additional_dbxrefs_form_create_dbxref_formstate_array($form, &$form_state) {
  405. $form_state['chado_additional_dbxrefs'] = array();
  406. foreach (element_children($form['addtl_dbxrefs']['dbxref_table']) as $db_id) {
  407. if ($db_id != 'new') {
  408. foreach (element_children($form['addtl_dbxrefs']['dbxref_table'][$db_id]) as $version) {
  409. $element = $form['addtl_dbxrefs']['dbxref_table'][$db_id][$version];
  410. $dbxref = array(
  411. 'db_id' => $element['db_id']['#value'],
  412. 'db_name' => $element['db']['#markup'],
  413. 'dbxref_id' => $element['dbxref_id']['#value'],
  414. 'version' => $element['dbxref_version']['#markup'],
  415. 'accession' => $element['dbxref_accession']['#markup'],
  416. );
  417. $version = (!empty($dbxref['version'])) ? $dbxref['version'] : 'NONE';
  418. $key = $dbxref['db_id'] . '-' . $version;
  419. $form_state['chado_additional_dbxrefs'][$key] = (object) $dbxref;
  420. }
  421. }
  422. }
  423. }
  424. /**
  425. * Function to theme the add/remove dbxrefs form into a table
  426. *
  427. * @ingroup tripal_chado_node_api
  428. */
  429. function theme_chado_node_additional_dbxrefs_form_table($variables) {
  430. $element = $variables['element'];
  431. $header = array(
  432. 'db' => t('Database'),
  433. 'dbxref_accession' => t('Accession'),
  434. 'dbxref_version' => t('Version'),
  435. 'dbxref_action' => t('Actions'),
  436. );
  437. $rows = array();
  438. foreach (element_children($element) as $db_id) {
  439. if ($db_id == 'new') {
  440. $row = array();
  441. $row['data'] = array();
  442. foreach ($header as $fieldname => $title) {
  443. $row['data'][] = drupal_render($element[$db_id][$fieldname]);
  444. }
  445. $rows[] = $row;
  446. }
  447. else {
  448. foreach (element_children($element[$db_id]) as $version) {
  449. $row = array();
  450. $row['data'] = array();
  451. foreach ($header as $fieldname => $title) {
  452. $row['data'][] = drupal_render($element[$db_id][$version][$fieldname]);
  453. }
  454. $rows[] = $row;
  455. }
  456. }
  457. }
  458. return theme('table', array(
  459. 'header' => $header,
  460. 'rows' => $rows
  461. ));
  462. }
  463. /**
  464. * This function is used in a hook_insert, hook_update for a node form
  465. * when the additional_dbxrefs form has been added to the form. It retrieves all of the dbxrefs
  466. * and returns them in an array of the format:
  467. *
  468. * $dbxefs[<db_id>][<version>][<dbxref_id>] = <accession>
  469. *
  470. * This array can then be used for inserting or updating dbxrefs using the API call
  471. * tripal_hook_insert_dbxref()
  472. *
  473. * @param $node
  474. *
  475. * @return
  476. * A dbxref array
  477. *
  478. * @ingroup tripal_chado_node_api
  479. */
  480. function chado_node_additional_dbxrefs_form_retreive($node) {
  481. $dbxrefs = array();
  482. if (isset($node->dbxref_table)) {
  483. foreach ($node->dbxref_table as $db_id => $elements) {
  484. if ($db_id != 'new') {
  485. foreach ($elements as $version => $dbxref) {
  486. $dbxref_id = (!empty($dbxref['dbxref_id'])) ? $dbxref['dbxref_id'] : 'NONE';
  487. $dbxrefs[$db_id][$version][$dbxref_id] = $dbxref['accession'];
  488. }
  489. }
  490. }
  491. }
  492. return $dbxrefs;
  493. }
  494. /**
  495. * This function is used in hook_insert or hook_update and handles inserting of any new
  496. * dbxrefs and creation of links between those dbxrefs and node content
  497. *
  498. * @param $node
  499. * The node passed into hook_insert & hook_update
  500. * @param $linking_table
  501. * The name of the _dbxref linking table (ie: feature_dbxref)
  502. * @param $foreignkey_name
  503. * The name of the foreign key used to link to the node content (ie: feature_id)
  504. * @param $foreignkey_value
  505. * The value of the foreign key (ie: 445, if there exists a feature where feature_id=445)
  506. *
  507. * @ingroup tripal_chado_node_api
  508. */
  509. function chado_node_additional_dbxrefs_form_update_dbxrefs($node, $linking_table, $foreignkey_name, $foreignkey_value) {
  510. if (isset($node->dbxref_table) AND ($foreignkey_value > 0)) {
  511. // First remove existing dbxref links
  512. tripal_core_chado_delete($linking_table, array($foreignkey_name => $foreignkey_value));
  513. // Add back in dbxref links and insert dbxrefs as needed
  514. $dbxrefs = chado_node_additional_dbxrefs_form_retreive($node);
  515. foreach ($dbxrefs as $db_id => $versions) {
  516. foreach ($versions as $version => $elements) {
  517. foreach ($elements as $dbxref_id => $accession) {
  518. // If there is no dbxref then we have to create that first
  519. if ($dbxref_id == 'NONE') {
  520. $version = ($version == 'NONE') ? '' : $version;
  521. $success = tripal_db_add_dbxref(
  522. $db_id,
  523. $accession,
  524. $version,
  525. NULL
  526. );
  527. if ($success) {
  528. $dbxref_id = $success->dbxref_id;
  529. }
  530. else {
  531. $dbxref_id = FALSE;
  532. }
  533. }
  534. // add _dbxref linker
  535. if ($dbxref_id) {
  536. $success_link = tripal_db_add_dbxref_link(
  537. $linking_table,
  538. $dbxref_id,
  539. $foreignkey_name,
  540. $foreignkey_value
  541. );
  542. }
  543. }
  544. }
  545. }
  546. }
  547. }