tripal_stock-db_references.inc 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  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. } else {
  104. form_set_error('database', 'Please select a database');
  105. }
  106. // Check Accession is unique for database
  107. $previous_db = tripal_db_set_active('chado');
  108. $tmp_obj = db_fetch_object(db_query("SELECT count(*) as count FROM dbxref WHERE accession='%s'",$form_state['values']['accession']));
  109. tripal_db_set_active($previous_db);
  110. if ($tmp_obj->count > 0) {
  111. form_set_error('accession', 'This accession has already been assigned to another stock.');
  112. }
  113. } //end of if adding
  114. }
  115. /**
  116. * Implements hoook_form_submit(): Actually adds the db reference to the stock
  117. *
  118. * @param $form
  119. * An array describing the form that was rendered
  120. * @param $form_state
  121. * An array describing the current state of the form including user input
  122. *
  123. * @ingroup tripal_stock
  124. */
  125. function tripal_stock_add_ONE_dbreference_form_submit($form, &$form_state) {
  126. // FIX: Sometimes on programatic submission of form (drupal_execute) values in the form state get lost
  127. // however, the post values always seem to be correct
  128. if (empty($form_state['values']['db_stock_id'])) { $form_state['values']['db_stock_id'] = $form_state['clicked_button']['#post']['db_stock_id']; }
  129. // Only Create if valid
  130. if ($form_state['values']['database'] > 0) {
  131. // create dbxref
  132. $previous_db = tripal_db_set_active('chado');
  133. db_query(
  134. "INSERT INTO dbxref (db_id, accession, description) VALUES (%d, '%s', '%s')",
  135. $form_state['values']['database'],
  136. $form_state['values']['accession'],
  137. $form_state['values']['db_description']
  138. );
  139. tripal_db_set_active($previous_db);
  140. //create stock_dbxref
  141. $dbxref = tripal_db_get_dbxref_by_accession($form_state['values']['accession'], $form_state['values']['database']);
  142. if (!empty($dbxref->dbxref_id)) {
  143. $previous_db = tripal_db_set_active('chado');
  144. db_query(
  145. "INSERT INTO stock_dbxref (stock_id, dbxref_id) VALUES (%d, %d)",
  146. $form_state['values']['db_stock_id'],
  147. $dbxref->dbxref_id
  148. );
  149. tripal_db_set_active($previous_db);
  150. drupal_set_message('Successfully Added Database Reference');
  151. } else {
  152. drupal_set_message('Database reference NOT successfully created...','error');
  153. } //end of if dbxref was created successfully
  154. } //end of if valid db reference
  155. }
  156. /**
  157. * Display the EDIT Database References to Stock Page
  158. *
  159. * This page is displayed as a tab on each stock details page (by default)
  160. *
  161. * @param $node
  162. * The stock node to add the database references to
  163. *
  164. * @return
  165. * HTML formatted output
  166. *
  167. * @ingroup tripal_stock
  168. */
  169. function tripal_stock_edit_ALL_dbreferences_page($node) {
  170. $output = '';
  171. $output .= drupal_get_form('tripal_stock_edit_ALL_db_references_form', $node);
  172. $output .= '<br>';
  173. $output .= drupal_get_form('tripal_stock_add_ONE_dbreference_form', $node);
  174. $output .= '<br>';
  175. $output .= drupal_get_form('tripal_stock_back_to_stock_button', $node->nid);
  176. return $output;
  177. }
  178. /**
  179. * Implements Hook_form(): Handles adding of Database References to Stocks
  180. *
  181. * Specifically this adds dbxrefs to a current stock using the stock_dbxref table
  182. *
  183. * @param $form_state
  184. * An array describing the current state of the form
  185. * @param $node
  186. * The stock node whose database references should be edited
  187. *
  188. * @return
  189. * An array describing the form to be rendered
  190. *
  191. * @ingroup tripal_stock
  192. */
  193. function tripal_stock_edit_ALL_db_references_form($form_state, $node) {
  194. $form = array();
  195. // Add database references to the node
  196. $node = tripal_core_expand_chado_vars($node, 'table', 'stock_dbxref');
  197. $form['nid'] = array(
  198. '#type' => 'hidden',
  199. '#value' => $node->nid
  200. );
  201. $i=0;
  202. if (!$node->stock->stock_dbxref) {
  203. $node->stock->stock_dbxref = array();
  204. } elseif (!is_array($node->stock->stock_dbxref)) {
  205. $node->stock->stock_dbxref = array($node->stock->stock_dbxref);
  206. }
  207. if (sizeof($node->stock->stock_dbxref) != 0) {
  208. foreach ($node->stock->stock_dbxref as $ref) {
  209. $i++;
  210. $form["num-$i"] = array(
  211. '#type' => 'item',
  212. '#value' => $i.'.'
  213. );
  214. $form["accession-$i"] = array(
  215. '#type' => 'textfield',
  216. //'#title' => t('Accession'),
  217. '#size' => 30,
  218. '#required' => TRUE,
  219. '#default_value' => $ref->dbxref_id->accession
  220. );
  221. $db_options = tripal_db_get_db_options();
  222. $db_options[0] = 'Select a Database';
  223. ksort($db_options);
  224. $form["database-$i"] = array(
  225. '#type' => 'select',
  226. //'#title' => t('Database'),
  227. '#options' => $db_options,
  228. '#default_value' => $ref->dbxref_id->db_id->db_id
  229. );
  230. $form["id-$i"] = array(
  231. '#type' => 'hidden',
  232. '#value' => $ref->dbxref_id->dbxref_id
  233. );
  234. $form["submit-$i"] = array(
  235. '#type' => 'submit',
  236. '#value' => t("Delete #$i")
  237. );
  238. }} //end of foreach db ref
  239. $form['num_db_references'] = array(
  240. '#type' => 'hidden',
  241. '#value' => $i
  242. );
  243. $form["submit-edits"] = array(
  244. '#type' => 'submit',
  245. '#value' => t('Update DB References')
  246. );
  247. return $form;
  248. }
  249. /**
  250. * Implements hook_form_submit(): Actually edits the database references
  251. *
  252. * @param $form
  253. * An array representing the form
  254. * @param $form_state
  255. * An array representing the current state of the form including user input
  256. *
  257. * @ingroup tripal_stock
  258. */
  259. function tripal_stock_edit_ALL_db_references_form_submit($form, &$form_state) {
  260. if ($form_state['clicked_button']['#value'] == t('Update DB References') ) {
  261. //Update all
  262. for ($i=1; $i<=$form_state['values']['num_db_references']; $i++) {
  263. tripal_stock_update_db_reference(
  264. $form_state['values']["id-$i"],
  265. $form_state['values']["database-$i"],
  266. $form_state['values']["accession-$i"]
  267. );
  268. }
  269. drupal_set_message("Updated all Database References");
  270. drupal_goto('node/'.$form_state['values']['nid']);
  271. } elseif ( preg_match('/Delete #(\d+)/', $form_state['clicked_button']['#value'], $matches) ) {
  272. $i = $matches[1];
  273. tripal_stock_delete_db_reference($form_state['values']["id-$i"]);
  274. drupal_set_message("Deleted Database Reference");
  275. } else {
  276. drupal_set_message("Unrecognized Button Pressed",'error');
  277. }
  278. }
  279. /**
  280. * Updates a Database Reference
  281. *
  282. * @todo Make this function more generic ie: update all parts of the dbxref and db
  283. *
  284. * @param $dbxref_id
  285. * The unique chado identifier of the database reference to be updated
  286. * @param $database_id
  287. * The new database ID
  288. * @param $accession
  289. * The new accession
  290. *
  291. * @ingroup tripal_stock
  292. */
  293. function tripal_stock_update_db_reference($dbxref_id, $database_id, $accession) {
  294. $previous_db = tripal_db_set_active('chado');
  295. db_query(
  296. "UPDATE dbxref SET db_id=%d, accession='%s' WHERE dbxref_id=%d",
  297. $database_id,
  298. $accession,
  299. $dbxref_id
  300. );
  301. tripal_db_set_active($previous_db);
  302. }
  303. /**
  304. * Deletes a given database reference
  305. *
  306. * @param $dbxref_id
  307. * The chado unique idenfier for the database reference to be deleted
  308. *
  309. * @return
  310. * TRUE on success; FALSE otherwise
  311. *
  312. * @ingroup tripal_stock
  313. */
  314. function tripal_stock_delete_db_reference($dbxref_id) {
  315. $previous_db = tripal_db_set_active('chado');
  316. db_query(
  317. "DELETE FROM dbxref WHERE dbxref_id=%d",
  318. $dbxref_id
  319. );
  320. db_query(
  321. "DELETE FROM stock_dbxref WHERE dbxref_id=%d",
  322. $dbxref_id
  323. );
  324. tripal_db_set_active($previous_db);
  325. }
  326. /**
  327. * Themes the Edit All Database References for a stock form
  328. *
  329. * @param $form
  330. * An array describing the form to be themed
  331. *
  332. * @return
  333. * An HTML rendering of the form
  334. *
  335. * @ingroup tripal_stock
  336. */
  337. function theme_tripal_stock_edit_ALL_db_references_form ($form) {
  338. $output = '';
  339. $output .= '<br><fieldset>';
  340. $output .= '<legend>Edit Existing Database References<span class="form-optional" title="This field is optional">(optional)</span></legend>';
  341. $output .= '<p>Below is a list of already existing database references, one per line. When entering a database reference, the accession '
  342. .'is a unique identifier for this stock in the specified database.</p>';
  343. $output .= '<table>';
  344. $output .= '<tr><th>#</th><th>Database</th><th>Accession</th><th></th></tr>';
  345. for ($i=1; $i<=$form['num_db_references']['#value']; $i++) {
  346. $output .= '<tr><td>'.drupal_render($form["num-$i"]).'</td><td>'
  347. .drupal_render($form["database-$i"]).'</td><td>'
  348. .drupal_render($form["accession-$i"]).'</td><td>'
  349. .drupal_render($form["submit-$i"]).'</td></tr>';
  350. }
  351. $output .= '</table><br>';
  352. $output .= drupal_render($form);
  353. $output .= '</fieldset>';
  354. return $output;
  355. }
  356. /**
  357. * List all database references for a given node
  358. *
  359. * @todo Make this function a theme function
  360. *
  361. * @param $db_references
  362. * An array of database references to be listed
  363. *
  364. * @return
  365. * HTML representation of the list
  366. *
  367. * @ingroup tripal_stock
  368. */
  369. function tripal_stock_list_dbreferences_for_node($db_references) {
  370. if (!empty($db_references) ) {
  371. $output = '<table>';
  372. $output .= '<tr><th>Database</th><th>Accession</th></tr>';
  373. foreach ($db_references as $db) {
  374. $output .= '<tr><td>'.$db->db_name.'</td><td>'.$db->accession.'</td></tr>';
  375. } // end of foreach db reference
  376. $output .= '</table>';
  377. } else {
  378. $output = 'No Database References Added to the Current Stock';
  379. }
  380. return $output;
  381. }