tripal_stock-db_references.inc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. <?php
  2. /**
  3. * Display the ADD Database References to Stock Page
  4. *
  5. * This page is displayed after the stock is created as part of a simulated multi-part form
  6. *
  7. * @param $node
  8. * The stock node to add the database references to
  9. *
  10. * @return
  11. * HTML formatted output
  12. *
  13. * @ingroup tripal_stock
  14. */
  15. function tripal_stock_add_ALL_dbreferences_page($node) {
  16. $output = '';
  17. $output .= tripal_stock_add_chado_properties_progress('db_references') . '<br>';
  18. $output .= '<b>All Database References should strictly pertain to THE CURRENT Individual</b><br>';
  19. $output .= '<br>';
  20. $output .= theme('tripal_stock_references', $node);
  21. $output .= '<br><br>';
  22. $output .= drupal_get_form('tripal_stock_add_ONE_dbreference_form', $node);
  23. $output .= '<br>';
  24. $output .= drupal_get_form('tripal_stock_add_chado_properties_navigate', 'db_references', $node->nid);
  25. return $output;
  26. }
  27. /**
  28. * Implements Hook_form(): Handles adding of Database References to Stocks
  29. *
  30. * @param $form_state
  31. * An array describing the current state of the form
  32. * @param $node
  33. * The stock node to add the database reference to
  34. *
  35. * @return
  36. * An array describing the form to be rendered
  37. *
  38. * @ingroup tripal_stock
  39. */
  40. function tripal_stock_add_ONE_dbreference_form($form_state, $node) {
  41. $stock_id = $node->stock->stock_id;
  42. $form['db_nid'] = array(
  43. '#type' => 'hidden',
  44. '#value' => $node->nid
  45. );
  46. $form['add_dbreference'] = array(
  47. '#type' => 'fieldset',
  48. '#title' => t('Add Database References') . '<span class="form-optional" title="This field is optional">(optional)</span>',
  49. );
  50. $form['add_properties']['db_stock_id'] = array(
  51. '#type' => 'value',
  52. '#value' => $stock_id,
  53. '#required' => TRUE
  54. );
  55. $form['add_dbreference']['accession'] = array(
  56. '#type' => 'textfield',
  57. '#title' => t('Accession Number'),
  58. );
  59. $form['add_dbreference']['db_description'] = array(
  60. '#type' => 'textarea',
  61. '#title' => t('Description of Database Reference') . '<span class="form-optional" title="This field is optional">+</span>',
  62. '#description' => t('Optionally enter a description about the database accession.')
  63. );
  64. $db_options = tripal_db_get_db_options();
  65. $db_options[0] = 'Select a Database';
  66. ksort($db_options);
  67. $form['add_dbreference']['database'] = array(
  68. '#type' => 'select',
  69. '#title' => t('Database'),
  70. '#options' => $db_options,
  71. );
  72. $form['add_dbreference']['submit'] = array(
  73. '#type' => 'submit',
  74. '#value' => t('Add Database Reference')
  75. );
  76. return $form;
  77. }
  78. /**
  79. * Implements hook_form_validate(): Validates the input from tripal_stock_add_ONE_dbreference_form()
  80. *
  81. * @param $form
  82. * An array describing the form that was rendered
  83. * @param $form_state
  84. * An array describing the current state of the form including user input
  85. *
  86. * @ingroup tripal_stock
  87. */
  88. function tripal_stock_add_ONE_dbreference_form_validate($form, &$form_state) {
  89. // Only ensure db reference valid if adding
  90. if ($form_state['clicked_button']['#value'] == t('Add Database Reference') ) {
  91. //Do work of required validators
  92. if ($form_state['values']['accession'] == '') {
  93. form_set_error('accession', 'Accession field is Required.');
  94. }
  95. // Check database is valid db_id in chado
  96. if ( $form_state['values']['database'] > 0) {
  97. $previous_db = tripal_db_set_active('chado');
  98. $tmp_obj = db_fetch_object(db_query("SELECT count(*) as count FROM {db} WHERE db_id=%d", $form_state['values']['database']));
  99. tripal_db_set_active($previous_db);
  100. if ($tmp_obj->count != 1) {
  101. form_set_error('database', 'The database you selected is not valid. Please choose another one.');
  102. }
  103. }
  104. else {
  105. form_set_error('database', 'Please select a database');
  106. }
  107. // Check Accession is unique for database
  108. $previous_db = tripal_db_set_active('chado');
  109. $tmp_obj = db_fetch_object(db_query("SELECT count(*) as count FROM {dbxref} WHERE accession='%s'", $form_state['values']['accession']));
  110. tripal_db_set_active($previous_db);
  111. if ($tmp_obj->count > 0) {
  112. form_set_error('accession', 'This accession has already been assigned to another stock.');
  113. }
  114. } //end of if adding
  115. }
  116. /**
  117. * Implements hoook_form_submit(): Actually adds the db reference to the stock
  118. *
  119. * @param $form
  120. * An array describing the form that was rendered
  121. * @param $form_state
  122. * An array describing the current state of the form including user input
  123. *
  124. * @ingroup tripal_stock
  125. */
  126. function tripal_stock_add_ONE_dbreference_form_submit($form, &$form_state) {
  127. // FIX: Sometimes on programatic submission of form (drupal_execute) values in the form state get lost
  128. // however, the post values always seem to be correct
  129. if (empty($form_state['values']['db_stock_id'])) {
  130. $form_state['values']['db_stock_id'] = $form_state['clicked_button']['#post']['db_stock_id']; }
  131. // Only Create if valid
  132. if ($form_state['values']['database'] > 0) {
  133. // create dbxref
  134. $previous_db = tripal_db_set_active('chado');
  135. db_query(
  136. "INSERT INTO {dbxref} (db_id, accession, description) VALUES (%d, '%s', '%s')",
  137. $form_state['values']['database'],
  138. $form_state['values']['accession'],
  139. $form_state['values']['db_description']
  140. );
  141. tripal_db_set_active($previous_db);
  142. //create stock_dbxref
  143. $dbxref = tripal_db_get_dbxref_by_accession($form_state['values']['accession'], $form_state['values']['database']);
  144. if (!empty($dbxref->dbxref_id)) {
  145. $previous_db = tripal_db_set_active('chado');
  146. db_query(
  147. "INSERT INTO {stock_dbxref} (stock_id, dbxref_id) VALUES (%d, %d)",
  148. $form_state['values']['db_stock_id'],
  149. $dbxref->dbxref_id
  150. );
  151. tripal_db_set_active($previous_db);
  152. drupal_set_message('Successfully Added Database Reference');
  153. }
  154. else {
  155. drupal_set_message('Database reference NOT successfully created...', 'error');
  156. } //end of if dbxref was created successfully
  157. } //end of if valid db reference
  158. }
  159. /**
  160. * Display the EDIT Database References to Stock Page
  161. *
  162. * This page is displayed as a tab on each stock details page (by default)
  163. *
  164. * @param $node
  165. * The stock node to add the database references to
  166. *
  167. * @return
  168. * HTML formatted output
  169. *
  170. * @ingroup tripal_stock
  171. */
  172. function tripal_stock_edit_ALL_dbreferences_page($node) {
  173. $output = '';
  174. $output .= drupal_get_form('tripal_stock_edit_ALL_db_references_form', $node);
  175. $output .= '<br>';
  176. $output .= drupal_get_form('tripal_stock_add_ONE_dbreference_form', $node);
  177. $output .= '<br>';
  178. $output .= drupal_get_form('tripal_stock_back_to_stock_button', $node->nid);
  179. return $output;
  180. }
  181. /**
  182. * Implements Hook_form(): Handles adding of Database References to Stocks
  183. *
  184. * Specifically this adds dbxrefs to a current stock using the stock_dbxref table
  185. *
  186. * @param $form_state
  187. * An array describing the current state of the form
  188. * @param $node
  189. * The stock node whose database references should be edited
  190. *
  191. * @return
  192. * An array describing the form to be rendered
  193. *
  194. * @ingroup tripal_stock
  195. */
  196. function tripal_stock_edit_ALL_db_references_form($form_state, $node) {
  197. $form = array();
  198. // Add database references to the node
  199. $node = tripal_core_expand_chado_vars($node, 'table', 'stock_dbxref');
  200. $form['nid'] = array(
  201. '#type' => 'hidden',
  202. '#value' => $node->nid
  203. );
  204. $i=0;
  205. if (!$node->stock->stock_dbxref) {
  206. $node->stock->stock_dbxref = array();
  207. }
  208. elseif (!is_array($node->stock->stock_dbxref)) {
  209. $node->stock->stock_dbxref = array($node->stock->stock_dbxref);
  210. }
  211. if (sizeof($node->stock->stock_dbxref) != 0) {
  212. foreach ($node->stock->stock_dbxref as $ref) {
  213. $i++;
  214. $form["num-$i"] = array(
  215. '#type' => 'item',
  216. '#value' => $i . '.'
  217. );
  218. $form["accession-$i"] = array(
  219. '#type' => 'textfield',
  220. //'#title' => t('Accession'),
  221. '#size' => 30,
  222. '#required' => TRUE,
  223. '#default_value' => $ref->dbxref_id->accession
  224. );
  225. $db_options = tripal_db_get_db_options();
  226. $db_options[0] = 'Select a Database';
  227. ksort($db_options);
  228. $form["database-$i"] = array(
  229. '#type' => 'select',
  230. //'#title' => t('Database'),
  231. '#options' => $db_options,
  232. '#default_value' => $ref->dbxref_id->db_id->db_id
  233. );
  234. $form["id-$i"] = array(
  235. '#type' => 'hidden',
  236. '#value' => $ref->dbxref_id->dbxref_id
  237. );
  238. $form["submit-$i"] = array(
  239. '#type' => 'submit',
  240. '#value' => t("Delete #$i")
  241. );
  242. }} //end of foreach db ref
  243. $form['num_db_references'] = array(
  244. '#type' => 'hidden',
  245. '#value' => $i
  246. );
  247. $form["submit-edits"] = array(
  248. '#type' => 'submit',
  249. '#value' => t('Update DB References')
  250. );
  251. return $form;
  252. }
  253. /**
  254. * Implements hook_form_submit(): Actually edits the database references
  255. *
  256. * @param $form
  257. * An array representing the form
  258. * @param $form_state
  259. * An array representing the current state of the form including user input
  260. *
  261. * @ingroup tripal_stock
  262. */
  263. function tripal_stock_edit_ALL_db_references_form_submit($form, &$form_state) {
  264. if ($form_state['clicked_button']['#value'] == t('Update DB References') ) {
  265. //Update all
  266. for ($i=1; $i<=$form_state['values']['num_db_references']; $i++) {
  267. tripal_stock_update_db_reference(
  268. $form_state['values']["id-$i"],
  269. $form_state['values']["database-$i"],
  270. $form_state['values']["accession-$i"]
  271. );
  272. }
  273. drupal_set_message("Updated all Database References");
  274. drupal_goto('node/' . $form_state['values']['nid']);
  275. }
  276. elseif ( preg_match('/Delete #(\d+)/', $form_state['clicked_button']['#value'], $matches) ) {
  277. $i = $matches[1];
  278. tripal_stock_delete_db_reference($form_state['values']["id-$i"]);
  279. drupal_set_message("Deleted Database Reference");
  280. }
  281. else {
  282. drupal_set_message("Unrecognized Button Pressed", 'error');
  283. }
  284. }
  285. /**
  286. * Updates a Database Reference
  287. *
  288. * @todo Make this function more generic ie: update all parts of the dbxref and db
  289. *
  290. * @param $dbxref_id
  291. * The unique chado identifier of the database reference to be updated
  292. * @param $database_id
  293. * The new database ID
  294. * @param $accession
  295. * The new accession
  296. *
  297. * @ingroup tripal_stock
  298. */
  299. function tripal_stock_update_db_reference($dbxref_id, $database_id, $accession) {
  300. $previous_db = tripal_db_set_active('chado');
  301. db_query(
  302. "UPDATE {dbxref} SET db_id=%d, accession='%s' WHERE dbxref_id=%d",
  303. $database_id,
  304. $accession,
  305. $dbxref_id
  306. );
  307. tripal_db_set_active($previous_db);
  308. }
  309. /**
  310. * Deletes a given database reference
  311. *
  312. * @param $dbxref_id
  313. * The chado unique idenfier for the database reference to be deleted
  314. *
  315. * @return
  316. * TRUE on success; FALSE otherwise
  317. *
  318. * @ingroup tripal_stock
  319. */
  320. function tripal_stock_delete_db_reference($dbxref_id) {
  321. $previous_db = tripal_db_set_active('chado');
  322. db_query(
  323. "DELETE FROM {dbxref} WHERE dbxref_id=%d",
  324. $dbxref_id
  325. );
  326. db_query(
  327. "DELETE FROM {stock_dbxref} WHERE dbxref_id=%d",
  328. $dbxref_id
  329. );
  330. tripal_db_set_active($previous_db);
  331. }
  332. /**
  333. * Themes the Edit All Database References for a stock form
  334. *
  335. * @param $form
  336. * An array describing the form to be themed
  337. *
  338. * @return
  339. * An HTML rendering of the form
  340. *
  341. * @ingroup tripal_stock
  342. */
  343. function theme_tripal_stock_edit_ALL_db_references_form($form) {
  344. $output = '';
  345. $output .= '<br><fieldset>';
  346. $output .= '<legend>Edit Existing Database References<span class="form-optional" title="This field is optional">(optional)</span></legend>';
  347. $output .= '<p>Below is a list of already existing database references, one per line. When entering a database reference, the accession '
  348. .'is a unique identifier for this stock in the specified database.</p>';
  349. $output .= '<table>';
  350. $output .= '<tr><th>#</th><th>Database</th><th>Accession</th><th></th></tr>';
  351. for ($i=1; $i<=$form['num_db_references']['#value']; $i++) {
  352. $output .= '<tr><td>' . drupal_render($form["num-$i"]) . '</td><td>'
  353. . drupal_render($form["database-$i"]) . '</td><td>'
  354. . drupal_render($form["accession-$i"]) . '</td><td>'
  355. . drupal_render($form["submit-$i"]) . '</td></tr>';
  356. }
  357. $output .= '</table><br>';
  358. $output .= drupal_render($form);
  359. $output .= '</fieldset>';
  360. return $output;
  361. }
  362. /**
  363. * List all database references for a given node
  364. *
  365. * @todo Make this function a theme function
  366. *
  367. * @param $db_references
  368. * An array of database references to be listed
  369. *
  370. * @return
  371. * HTML representation of the list
  372. *
  373. * @ingroup tripal_stock
  374. */
  375. function tripal_stock_list_dbreferences_for_node($db_references) {
  376. if (!empty($db_references) ) {
  377. $output = '<table>';
  378. $output .= '<tr><th>Database</th><th>Accession</th></tr>';
  379. foreach ($db_references as $db) {
  380. $output .= '<tr><td>' . $db->db_name . '</td><td>' . $db->accession . '</td></tr>';
  381. } // end of foreach db reference
  382. $output .= '</table>';
  383. }
  384. else {
  385. $output = 'No Database References Added to the Current Stock';
  386. }
  387. return $output;
  388. }