tripal_stock-db_references.inc 13 KB

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