tripal_core.database_references.api.inc 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  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. tripal_core_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. tripal_core_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. tripal_core_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. /**
  73. * @section
  74. * Additional Database References Form to be added to node forms
  75. */
  76. /**
  77. * Provides a form for adding to BASE_dbxref and dbxref tables
  78. *
  79. * @param $form
  80. * The Drupal form array into which the dbxref elements will be added
  81. * @param $form_state
  82. * The corresponding form_state array for the form
  83. * @param $details
  84. * An array defining details needed by this form. Required Keys are:
  85. * - linking_table: the name of the dbxref linking table (ie: feature_dbxref)
  86. * - base_foreign_key: the name of the foreign key linking this table to the non-dbxref table (ie: feature_id)
  87. * - base_key_value: the value of the base_foreign_key for the current form (ie: 999 if the feature_id=999)
  88. * Optional keys include:
  89. * - fieldset_title: the non-translated title for this fieldset
  90. * - additional_instructions: a non-translated string providing additional instructions
  91. */
  92. function tripal_core_additional_dbxrefs_form(&$form, &$form_state, $details) {
  93. // Set Defaults for optional fields
  94. $details['fieldset_title'] = 'Additional Database References';
  95. $details['additional_instructions'] = '';
  96. $form['addtl_dbxrefs'] = array(
  97. '#type' => 'fieldset',
  98. '#title' => t($details['fieldset_title']),
  99. '#description' => t('You may add additional database references by
  100. selecting a database reference type from the dropdown and adding text. You may add
  101. as many database references as desired by clicking the add button on the right. To
  102. remove a database reference, click the remove button. ' . $details['additional_instructions']),
  103. '#prefix' => "<div id='addtl-dbxrefs-fieldset'>",
  104. '#suffix' => '</div>'
  105. );
  106. $form['addtl_dbxrefs']['dbxref_table'] = array(
  107. '#type' => 'markup',
  108. '#tree' => TRUE,
  109. '#prefix' => '<div id="tripal-generic-edit-addtl_dbxrefs-table">',
  110. '#suffix' => '</div>',
  111. '#theme' => 'tripal_core_additional_dbxrefs_form_table'
  112. );
  113. // Add dbxrefs already attached to the node
  114. //---------------------------------------------
  115. if (isset($form_state['addtl_dbxrefs'])) {
  116. $existing_dbxrefs = $form_state['addtl_dbxrefs'];
  117. }
  118. else {
  119. $existing_dbxrefs = chado_query(
  120. "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
  121. FROM {dbxref} dbxref
  122. LEFT JOIN {db} db ON db.db_id = dbxref.db_id
  123. LEFT JOIN {".$details['linking_table']."} linking_table ON linking_table.dbxref_id = dbxref.dbxref_id
  124. WHERE linking_table.".$details['base_foreign_key']."= :base_key_value
  125. ORDER BY db.name ASC, dbxref.version ASC",
  126. array(':base_key_value' => $details['base_key_value'])
  127. );
  128. }
  129. foreach ($existing_dbxrefs as $dbxref) {
  130. $version = (!empty($dbxref->version)) ? $dbxref->version : 'NONE';
  131. $form['addtl_dbxrefs']['dbxref_table'][$dbxref->db_id] = array(
  132. '#type' => 'markup',
  133. '#value' => ''
  134. );
  135. $form['addtl_dbxrefs']['dbxref_table'][$dbxref->db_id][$version] = array(
  136. '#type' => 'markup',
  137. '#value' => ''
  138. );
  139. $form['addtl_dbxrefs']['dbxref_table'][$dbxref->db_id][$version]['db_id'] = array(
  140. '#type' => 'hidden',
  141. '#value' => $dbxref->db_id
  142. );
  143. $form['addtl_dbxrefs']['dbxref_table'][$dbxref->db_id][$version]['accession'] = array(
  144. '#type' => 'hidden',
  145. '#value' => $dbxref->accession
  146. );
  147. $form['addtl_dbxrefs']['dbxref_table'][$dbxref->db_id][$version]['dbxref_id'] = array(
  148. '#type' => 'hidden',
  149. '#value' => $dbxref->dbxref_id
  150. );
  151. $form['addtl_dbxrefs']['dbxref_table'][$dbxref->db_id][$version]['db'] = array(
  152. '#type' => 'markup',
  153. '#markup' => $dbxref->db_name
  154. );
  155. $form['addtl_dbxrefs']['dbxref_table'][$dbxref->db_id][$version]['dbxref_version'] = array(
  156. '#type' => 'markup',
  157. '#markup' => $dbxref->version
  158. );
  159. $form['addtl_dbxrefs']['dbxref_table'][$dbxref->db_id][$version]['dbxref_accession'] = array(
  160. '#type' => 'markup',
  161. '#markup' => $dbxref->accession
  162. );
  163. $form['addtl_dbxrefs']['dbxref_table'][$dbxref->db_id][$version]['dbxref_action'] = array(
  164. '#type' => 'submit',
  165. '#value' => t('Remove'),
  166. '#name' => "dbxref_remove-".$dbxref->db_id.'-'.$version,
  167. '#ajax' => array(
  168. 'callback' => "tripal_core_additional_dbxrefs_form_ajax_update",
  169. 'wrapper' => 'tripal-generic-edit-addtl_dbxrefs-table',
  170. 'effect' => 'fade',
  171. 'method' => 'replace',
  172. 'prevent' => 'click'
  173. ),
  174. '#validate' => array('tripal_core_additional_dbxrefs_form_remove_button_validate'),
  175. '#submit' => array('tripal_core_additional_dbxrefs_form_remove_button_submit'),
  176. );
  177. }
  178. // Form elements for adding a new dbxref
  179. //---------------------------------------------
  180. $form['addtl_dbxrefs']['dbxref_table']['new'] = array(
  181. '#type' => 'markup',
  182. '#prefix' => '<span class="addtl-dbxrefs-add-new-dbxref">',
  183. '#suffix' => '</span>'
  184. );
  185. $db_options = array(0 => 'Select a Database');
  186. $select = tripal_core_chado_select('db',array('db_id','name'),array());
  187. foreach($select as $db) {
  188. $db_options[$db->db_id] = $db->name;
  189. }
  190. $form['addtl_dbxrefs']['dbxref_table']['new']['db'] = array(
  191. '#type' => 'select',
  192. '#options' => $db_options,
  193. );
  194. $form['addtl_dbxrefs']['dbxref_table']['new']['dbxref_accession'] = array(
  195. '#type' => 'textfield',
  196. );
  197. $form['addtl_dbxrefs']['dbxref_table']['new']['dbxref_version'] = array(
  198. '#type' => 'textfield',
  199. );
  200. $form['addtl_dbxrefs']['dbxref_table']['new']['dbxref_action'] = array(
  201. '#type' => 'submit',
  202. '#value' => t('Add'),
  203. '#name' => "dbxref-add",
  204. '#ajax' => array(
  205. 'callback' => "tripal_core_additional_dbxrefs_form_ajax_update",
  206. 'wrapper' => 'tripal-generic-edit-addtl_dbxrefs-table',
  207. 'effect' => 'fade',
  208. 'method' => 'replace',
  209. 'prevent' => 'click'
  210. ),
  211. '#validate' => array('tripal_core_additional_dbxrefs_form_add_button_validate'),
  212. '#submit' => array('tripal_core_additional_dbxrefs_form_add_button_submit'),
  213. );
  214. }
  215. /**
  216. * Validate the user input for creating a new dbxref
  217. * Called by the add button in tripal_core_additional_dbxrefs_form
  218. */
  219. function tripal_core_additional_dbxrefs_form_add_button_validate($form, &$form_state) {
  220. // Ensure the db_id is supplied & Valid
  221. $db = tripal_core_chado_select(
  222. 'db',
  223. array('db_id', 'name'),
  224. array('db_id' => $form_state['input']['dbxref_table']['new']['db'])
  225. );
  226. if (!isset($db[0])) {
  227. form_set_error('db', 'Please select a database before attempting to add a new database reference.');
  228. }
  229. else {
  230. $form_state['values']['dbxref_table']['new']['db_name'] = $db[0]->name;
  231. }
  232. // Ensure accession is supplied
  233. if (empty($form_state['input']['dbxref_table']['new']['dbxref_accession'])) {
  234. form_set_error('dbxref_accession','You must enter the accession before attempting to add a new database reference.');
  235. }
  236. }
  237. /**
  238. * Called by the add button in tripal_core_additional_dbxrefs_form
  239. *
  240. * Create an array of additional dbxrefs in the form state. This array will then be
  241. * used to rebuild the form in subsequent builds
  242. */
  243. function tripal_core_additional_dbxrefs_form_add_button_submit(&$form, &$form_state) {
  244. // if the addtl_dbxrefs array is not set then this is the first time modifying the
  245. // dbxref table. this means we need to include all the dbxrefs from the db
  246. if (!isset($form_state['addtl_dbxrefs'])) {
  247. tripal_core_additional_dbxrefs_form_create_dbxref_formstate_array($form, $form_state);
  248. }
  249. // get details for the new dbxref
  250. $dbxref = array(
  251. 'db_id' => $form_state['values']['dbxref_table']['new']['db'],
  252. 'db_name' => $form_state['values']['dbxref_table']['new']['db_name'],
  253. 'dbxref_id' => NULL,
  254. 'version' => $form_state['values']['dbxref_table']['new']['dbxref_version'],
  255. 'accession' => $form_state['values']['dbxref_table']['new']['dbxref_accession'],
  256. );
  257. $form_state['addtl_dbxrefs'][] = (object) $dbxref;
  258. $form_state['rebuild'] = TRUE;
  259. }
  260. /**
  261. * There is no user input for the remove buttons so there is no need to validate
  262. * However, both a submit & validate need to be specified so this is just a placeholder
  263. *
  264. * Called by the many remove buttons in tripal_core_additional_dbxrefs_form
  265. */
  266. function tripal_core_additional_dbxrefs_form_remove_button_validate($form, $form_state) {
  267. // No Validation needed for remove
  268. }
  269. /**
  270. * Remove the correct dbxref from the form
  271. * Called by the many remove buttons in tripal_core_additional_dbxrefs_form
  272. */
  273. function tripal_core_additional_dbxrefs_form_remove_button_submit(&$form, &$form_state) {
  274. // if the addtl_dbxrefs array is not set then this is the first time modifying the
  275. // dbxref table. this means we need to include all the dbxrefs from the db
  276. if (!isset($form_state['addtl_dbxrefs'])) {
  277. tripal_core_additional_dbxrefs_form_create_dbxref_formstate_array($form, $form_state);
  278. }
  279. // remove the specified dbxref from the form dbxref table
  280. if(preg_match('/dbxref_remove-([^-]+-[^-]+)/',$form_state['triggering_element']['#name'],$match)) {
  281. $key = $match[1];
  282. if (array_key_exists($key, $form_state['addtl_dbxrefs'])) {
  283. unset($form_state['addtl_dbxrefs'][$key]);
  284. }
  285. }
  286. $form_state['rebuild'] = TRUE;
  287. }
  288. /**
  289. * Ajax function which returns the section of the form to be re-rendered
  290. */
  291. function tripal_core_additional_dbxrefs_form_ajax_update($form, $form_state) {
  292. return $form['addtl_dbxrefs']['dbxref_table'];
  293. }
  294. /**
  295. * Creates an array in form_state containing the existing addtl_dbxrefs. This array is
  296. * then modified by the add/remove buttons and used as a source for rebuilding the form.
  297. */
  298. function tripal_core_additional_dbxrefs_form_create_dbxref_formstate_array($form, &$form_state) {
  299. $form_state['addtl_dbxrefs'] = array();
  300. foreach (element_children($form['addtl_dbxrefs']['dbxref_table']) as $db_id) {
  301. if ($db_id != 'new') {
  302. foreach (element_children($form['addtl_dbxrefs']['dbxref_table'][$db_id]) as $version) {
  303. $element = $form['addtl_dbxrefs']['dbxref_table'][$db_id][$version];
  304. $dbxref = array(
  305. 'db_id' => $element['db_id']['#value'],
  306. 'db_name' => $element['db']['#markup'],
  307. 'dbxref_id' => $element['dbxref_id']['#value'],
  308. 'version' => $element['dbxref_version']['#markup'],
  309. 'accession' => $element['dbxref_accession']['#markup'],
  310. );
  311. $version = (!empty($dbxref['version'])) ? $dbxref['version'] : 'NONE';
  312. $key = $dbxref['db_id'] . '-' . $version;
  313. $form_state['addtl_dbxrefs'][$key] = (object) $dbxref;
  314. }
  315. }
  316. }
  317. }
  318. /**
  319. * Function to theme the add/remove dbxrefs form into a table
  320. */
  321. function theme_tripal_core_additional_dbxrefs_form_table($variables) {
  322. $element = $variables['element'];
  323. $header = array(
  324. 'db' => t('Database'),
  325. 'dbxref_accession' => t('Accession'),
  326. 'dbxref_version' => t('Version'),
  327. 'dbxref_action' => t('Actions'),
  328. );
  329. $rows = array();
  330. foreach (element_children($element) as $db_id) {
  331. if ($db_id == 'new') {
  332. $row = array();
  333. $row['data'] = array();
  334. foreach ($header as $fieldname => $title) {
  335. $row['data'][] = drupal_render($element[$db_id][$fieldname]);
  336. }
  337. $rows[] = $row;
  338. }
  339. else {
  340. foreach (element_children($element[$db_id]) as $version) {
  341. $row = array();
  342. $row['data'] = array();
  343. foreach ($header as $fieldname => $title) {
  344. $row['data'][] = drupal_render($element[$db_id][$version][$fieldname]);
  345. }
  346. $rows[] = $row;
  347. }
  348. }
  349. }
  350. return theme('table', array(
  351. 'header' => $header,
  352. 'rows' => $rows
  353. ));
  354. }
  355. /**
  356. * This function is used in a hook_insert, hook_update for a node form
  357. * when the additional_dbxrefs form has been added to the form. It retrieves all of the dbxrefs
  358. * and returns them in an array of the format:
  359. *
  360. * $dbxefs[<db_id>][<version>][<dbxref_id>] = <accession>
  361. *
  362. * This array can then be used for inserting or updating dbxrefs using the API call
  363. * tripal_hook_insert_dbxref()
  364. *
  365. * @param $node
  366. *
  367. * @return
  368. * A dbxref array
  369. *
  370. * @ingroup tripal_databaserefernce_api
  371. */
  372. function tripal_core_additional_dbxrefs_form_retreive($node) {
  373. $dbxrefs = array();
  374. foreach ($node->dbxref_table as $db_id => $elements) {
  375. if ($db_id != 'new') {
  376. foreach ($elements as $version => $dbxref) {
  377. $dbxref_id = (!empty($dbxref['dbxref_id'])) ? $dbxref['dbxref_id'] : 'NONE';
  378. $dbxrefs[$db_id][$version][$dbxref_id] = $dbxref['accession'];
  379. }
  380. }
  381. }
  382. return $dbxrefs;
  383. }
  384. /**
  385. * This function is used in hook_insert or hook_update and handles inserting of any new
  386. * dbxrefs and creation of links between those dbxrefs and node content
  387. *
  388. * @param $node
  389. * The node passed into hook_insert & hook_update
  390. * @param $linking_table
  391. * The name of the _dbxref linking table (ie: feature_dbxref)
  392. * @param $foreignkey_name
  393. * The name of the foreign key used to link to the node content (ie: feature_id)
  394. * @param $foreignkey_value
  395. * The value of the foreign key (ie: 445, if there exists a feature where feature_id=445)
  396. */
  397. function tripal_core_additional_dbxrefs_form_update_dbxrefs($node, $linking_table, $foreignkey_name, $foreignkey_value) {
  398. // First remove existing dbxref links
  399. tripal_core_chado_delete($linking_table, array($foreignkey_name => $foreignkey_value));
  400. // Add back in dbxref links and insert dbxrefs as needed
  401. $dbxrefs = tripal_core_additional_dbxrefs_form_retreive($node);
  402. foreach ($dbxrefs as $db_id => $versions) {
  403. foreach ($versions as $version => $elements) {
  404. foreach ($elements as $dbxref_id => $accession) {
  405. // If there is no dbxref then we have to create that first
  406. if ($dbxref_id == 'NONE') {
  407. $version = ($version == 'NONE') ? '' : $version;
  408. $success = tripal_db_add_dbxref(
  409. $db_id,
  410. $accession,
  411. $version,
  412. NULL
  413. );
  414. if ($success) {
  415. $dbxref_id = $success->dbxref_id;
  416. }
  417. else {
  418. $dbxref_id = FALSE;
  419. }
  420. }
  421. // add _dbxref linker
  422. if ($dbxref_id) {
  423. $success_link = tripal_db_add_dbxref_link(
  424. $linking_table,
  425. $dbxref_id,
  426. $foreignkey_name,
  427. $foreignkey_value
  428. );
  429. }
  430. }
  431. }
  432. }
  433. }