tripal_stock-db_references.inc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  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. $tmp_obj = db_fetch_object(chado_query("SELECT count(*) as count FROM {db} WHERE db_id=%d", $form_state['values']['database']));
  102. if ($tmp_obj->count != 1) {
  103. form_set_error('database', 'The database you selected is not valid. Please choose another one.');
  104. }
  105. }
  106. else {
  107. form_set_error('database', 'Please select a database');
  108. }
  109. // Check Accession is unique for database
  110. $tmp_obj = db_fetch_object(chado_query("SELECT count(*) as count FROM {dbxref} WHERE accession='%s'", $form_state['values']['accession']));
  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. chado_query(
  135. "INSERT INTO {dbxref} (db_id, accession, description) VALUES (%d, '%s', '%s')",
  136. $form_state['values']['database'],
  137. $form_state['values']['accession'],
  138. $form_state['values']['db_description']
  139. );
  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. chado_query(
  144. "INSERT INTO {stock_dbxref} (stock_id, dbxref_id) VALUES (%d, %d)",
  145. $form_state['values']['db_stock_id'],
  146. $dbxref->dbxref_id
  147. );
  148. drupal_set_message(t('Successfully Added Database Reference'));
  149. }
  150. else {
  151. drupal_set_message(t('Database reference NOT successfully created...'), 'error');
  152. } //end of if dbxref was created successfully
  153. } //end of if valid db reference
  154. }
  155. /**
  156. * Display the EDIT Database References to Stock Page
  157. *
  158. * This page is displayed as a tab on each stock details page (by default)
  159. *
  160. * @param $node
  161. * The stock node to add the database references to
  162. *
  163. * @return
  164. * HTML formatted output
  165. *
  166. * @ingroup tripal_stock
  167. */
  168. function tripal_stock_edit_ALL_dbreferences_page($node) {
  169. $output = '';
  170. $output .= drupal_get_form('tripal_stock_edit_ALL_db_references_form', $node);
  171. $output .= '<br />';
  172. $output .= drupal_get_form('tripal_stock_add_ONE_dbreference_form', $node);
  173. $output .= '<br />';
  174. $output .= drupal_get_form('tripal_stock_back_to_stock_button', $node->nid);
  175. return $output;
  176. }
  177. /**
  178. * Implements Hook_form(): Handles adding of Database References to Stocks
  179. *
  180. * Specifically this adds dbxrefs to a current stock using the stock_dbxref table
  181. *
  182. * @param $form_state
  183. * An array describing the current state of the form
  184. * @param $node
  185. * The stock node whose database references should be edited
  186. *
  187. * @return
  188. * An array describing the form to be rendered
  189. *
  190. * @ingroup tripal_stock
  191. */
  192. function tripal_stock_edit_ALL_db_references_form($form_state, $node) {
  193. $form = array();
  194. // Add database references to the node
  195. $node = tripal_core_expand_chado_vars($node, 'table', 'stock_dbxref');
  196. $form['nid'] = array(
  197. '#type' => 'hidden',
  198. '#value' => $node->nid
  199. );
  200. $i=0;
  201. if (!$node->stock->stock_dbxref) {
  202. $node->stock->stock_dbxref = array();
  203. }
  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(t("Updated all Database References"));
  270. drupal_goto('node/' . $form_state['values']['nid']);
  271. }
  272. elseif ( preg_match('/Delete #(\d+)/', $form_state['clicked_button']['#value'], $matches) ) {
  273. $i = $matches[1];
  274. tripal_stock_delete_db_reference($form_state['values']["id-$i"]);
  275. drupal_set_message(t("Deleted Database Reference"));
  276. }
  277. else {
  278. drupal_set_message(t("Unrecognized Button Pressed"), 'error');
  279. }
  280. }
  281. /**
  282. * Updates a Database Reference
  283. *
  284. * @todo Make this function more generic ie: update all parts of the dbxref and db
  285. *
  286. * @param $dbxref_id
  287. * The unique chado identifier of the database reference to be updated
  288. * @param $database_id
  289. * The new database ID
  290. * @param $accession
  291. * The new accession
  292. *
  293. * @ingroup tripal_stock
  294. */
  295. function tripal_stock_update_db_reference($dbxref_id, $database_id, $accession) {
  296. chado_query(
  297. "UPDATE {dbxref} SET db_id=%d, accession='%s' WHERE dbxref_id=%d",
  298. $database_id,
  299. $accession,
  300. $dbxref_id
  301. );
  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. chado_query(
  316. "DELETE FROM {dbxref} WHERE dbxref_id=%d",
  317. $dbxref_id
  318. );
  319. chado_query(
  320. "DELETE FROM {stock_dbxref} WHERE dbxref_id=%d",
  321. $dbxref_id
  322. );
  323. }
  324. /**
  325. * Themes the Edit All Database References for a stock form
  326. *
  327. * @param $form
  328. * An array describing the form to be themed
  329. *
  330. * @return
  331. * An HTML rendering of the form
  332. *
  333. * @ingroup tripal_stock
  334. */
  335. function theme_tripal_stock_edit_ALL_db_references_form($form) {
  336. $output = '';
  337. $output .= '<br /><fieldset>';
  338. $output .= '<legend>Edit Existing Database References<span class="form-optional" title="This field is optional">(optional)</span></legend>';
  339. $output .= '<p>Below is a list of already existing database references, one per line. When entering a database reference, the accession '
  340. .'is a unique identifier for this stock in the specified database.</p>';
  341. $output .= '<table>';
  342. $output .= '<tr><th>#</th><th>Database</th><th>Accession</th><th></th></tr>';
  343. for ($i=1; $i<=$form['num_db_references']['#value']; $i++) {
  344. $output .= '<tr><td>' . drupal_render($form["num-$i"]) . '</td><td>'
  345. . drupal_render($form["database-$i"]) . '</td><td>'
  346. . drupal_render($form["accession-$i"]) . '</td><td>'
  347. . drupal_render($form["submit-$i"]) . '</td></tr>';
  348. }
  349. $output .= '</table><br />';
  350. $output .= drupal_render($form);
  351. $output .= '</fieldset>';
  352. return $output;
  353. }
  354. /**
  355. * List all database references for a given node
  356. *
  357. * @todo Make this function a theme function
  358. *
  359. * @param $db_references
  360. * An array of database references to be listed
  361. *
  362. * @return
  363. * HTML representation of the list
  364. *
  365. * @ingroup tripal_stock
  366. */
  367. function tripal_stock_list_dbreferences_for_node($db_references) {
  368. if (!empty($db_references) ) {
  369. $output = '<table>';
  370. $output .= '<tr><th>Database</th><th>Accession</th></tr>';
  371. foreach ($db_references as $db) {
  372. $output .= '<tr><td>' . $db->db_name . '</td><td>' . $db->accession . '</td></tr>';
  373. } // end of foreach db reference
  374. $output .= '</table>';
  375. }
  376. else {
  377. $output = 'No Database References Added to the Current Stock';
  378. }
  379. return $output;
  380. }