tripal_feature-db_references.inc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. <?php
  2. /**
  3. * @file
  4. * @todo Add file header description
  5. */
  6. /**
  7. *
  8. *
  9. * @ingroup tripal_feature
  10. */
  11. function tripal_feature_add_ALL_dbreferences_page($node) {
  12. $output = '';
  13. $output .= tripal_feature_implement_add_chado_properties_progress('db_references') . '<br />';
  14. $output .= '<b>All Database References should strictly pertain to THE CURRENT Individual</b><br />';
  15. $output .= '<br /><b>Current Database References</b><br />';
  16. $output .= list_dbreferences_for_node($node->db_references);
  17. $output .= '<br /><br />';
  18. $output .= drupal_get_form('tripal_feature_add_ONE_dbreference_form', $node);
  19. $output .= '<br />';
  20. $output .= drupal_get_form('tripal_feature_implement_add_chado_properties_navigate', 'db_references', $node->nid);
  21. return $output;
  22. }
  23. /**
  24. * Implements Hook_form()
  25. * Handles adding of Database References to features
  26. *
  27. * @ingroup tripal_feature
  28. */
  29. function tripal_feature_add_ONE_dbreference_form($form_state, $node) {
  30. $form['nid'] = array(
  31. '#type' => 'hidden',
  32. '#value' => $node->nid
  33. );
  34. $form['feature_id'] = array(
  35. '#type' => 'hidden',
  36. '#value' => $node->feature->feature_id,
  37. );
  38. $form['add_dbreference'] = array(
  39. '#type' => 'fieldset',
  40. '#title' => t('Add Database References') . '<span class="form-optional" title="This field is optional">(optional)</span>',
  41. );
  42. $form['add_dbreference']['accession'] = array(
  43. '#type' => 'textfield',
  44. '#title' => t('Accession'),
  45. '#required' => TRUE,
  46. );
  47. $form['add_dbreference']['description'] = array(
  48. '#type' => 'textarea',
  49. '#title' => t('Description of Reference') . '<span class="form-optional" title="This field is optional">+</span>',
  50. '#description' => t('Optionally enter a description about the database accession.'),
  51. );
  52. $db_options = tripal_db_get_db_options();
  53. $db_options[0] = 'Select a Database';
  54. ksort($db_options);
  55. $form['add_dbreference']['db_id'] = array(
  56. '#type' => 'select',
  57. '#title' => t('Database'),
  58. '#options' => $db_options,
  59. '#required' => TRUE,
  60. );
  61. $form['add_dbreference']['submit'] = array(
  62. '#type' => 'submit',
  63. '#value' => t('Add Database Reference')
  64. );
  65. return $form;
  66. }
  67. /**
  68. *
  69. *
  70. * @ingroup tripal_feature
  71. */
  72. function tripal_feature_add_ONE_dbreference_form_validate($form, &$form_state) {
  73. $db_id = $form_state['values']['db_id'];
  74. $accession = $form_state['values']['accession'];
  75. $description = $form_state['values']['description'];
  76. $feature_id = $form_state['values']['feature_id'];
  77. $nid = $form_state['values']['nid'];
  78. // Check database is valid db_id in chado
  79. $previous_db = tripal_db_set_active('chado');
  80. $tmp_obj = db_fetch_object(db_query("SELECT count(*) as count FROM {db} WHERE db_id=%d", $db_id));
  81. tripal_db_set_active($previous_db);
  82. if ($tmp_obj->count != 1) {
  83. form_set_error('database', 'The database you selected is not valid. Please choose another one.');
  84. }
  85. // Check Accession is unique for database
  86. $previous_db = tripal_db_set_active('chado');
  87. $sql = "SELECT count(*) as count FROM {dbxref} WHERE accession='%s' and db_id = %d";
  88. $tmp_obj = db_fetch_object(db_query($sql, $accession, $db_id));
  89. tripal_db_set_active($previous_db);
  90. if ($tmp_obj->count > 0) {
  91. form_set_error('accession', 'This accession has already been assigned to another feature in the selected database.');
  92. }
  93. }
  94. /**
  95. *
  96. *
  97. * @ingroup tripal_feature
  98. */
  99. function tripal_feature_add_ONE_dbreference_form_submit($form, &$form_state) {
  100. $db_id = $form_state['values']['db_id'];
  101. $accession = $form_state['values']['accession'];
  102. $description = $form_state['values']['description'];
  103. $feature_id = $form_state['values']['feature_id'];
  104. $nid = $form_state['values']['nid'];
  105. // create dbxref
  106. $previous_db = tripal_db_set_active('chado');
  107. $isql = "INSERT INTO {dbxref} (db_id, accession, description) VALUES (%d, '%s', '%s')";
  108. db_query($isql, $db_id, $accession, $description);
  109. tripal_db_set_active($previous_db);
  110. //create feature_dbxref
  111. $dbxref = tripal_db_get_dbxref( array('db_id' => array('type' => 'INT', 'value' => $form_state['values']['db_id']),
  112. 'accession' => array('type' => 'STRING', 'exact' => TRUE, 'value' => $form_state['values']['accession']) ) );
  113. if (!empty($dbxref->dbxref_id)) {
  114. $previous_db = tripal_db_set_active('chado');
  115. $isql = "INSERT INTO {feature_dbxref} (feature_id, dbxref_id) VALUES (%d, %d)";
  116. db_query($isql, $feature_id, $dbxref->dbxref_id);
  117. tripal_db_set_active($previous_db);
  118. drupal_set_message(t('Successfully Added Database Reference'));
  119. drupal_goto('node/' . $nid);
  120. }
  121. else {
  122. drupal_set_message(t('Database reference NOT successfully created...'), 'error');
  123. } //end of if dbxref was created successfully
  124. }
  125. /**
  126. *
  127. *
  128. * @ingroup tripal_feature
  129. */
  130. function tripal_feature_edit_ALL_dbreferences_page($node) {
  131. $output = '';
  132. $output .= drupal_get_form('tripal_feature_edit_ALL_db_references_form', $node);
  133. $output .= '<br />';
  134. $output .= drupal_get_form('tripal_feature_add_ONE_dbreference_form', $node);
  135. $output .= '<br />';
  136. $output .= drupal_get_form('tripal_feature_implement_back_to_feature_button', $node->nid);
  137. return $output;
  138. }
  139. /**
  140. * Implements Hook_form()
  141. * Handles adding of DB References to Features
  142. *
  143. * @ingroup tripal_feature
  144. */
  145. function tripal_feature_edit_ALL_db_references_form($form_state, $node) {
  146. $form = array();
  147. $form['nid'] = array(
  148. '#type' => 'hidden',
  149. '#value' => $node->nid
  150. );
  151. $i=0;
  152. $feature = $node->feature;
  153. $references = tripal_feature_load_references($feature->feature_id);
  154. // pre populate the database options
  155. $db_options = tripal_db_get_db_options();
  156. $db_options[0] = 'Select a Database';
  157. ksort($db_options);
  158. if (sizeof($references) != 0) {
  159. foreach ($references as $ref) {
  160. $i++;
  161. $form["num-$i"] = array(
  162. '#type' => 'fieldset',
  163. '#title' => t("Database Reference") . " $i"
  164. );
  165. $form["num-$i"]["accession-$i"] = array(
  166. '#type' => 'textfield',
  167. '#title' => t('Accession'),
  168. '#size' => 30,
  169. '#required' => TRUE,
  170. '#default_value' => $ref->accession
  171. );
  172. $form["num-$i"]["db_id-$i"] = array(
  173. '#type' => 'select',
  174. '#title' => t('Database'),
  175. '#options' => $db_options,
  176. '#required' => TRUE,
  177. '#default_value' => $ref->db_id
  178. );
  179. $form["num-$i"]["dbxref_id-$i"] = array(
  180. '#type' => 'hidden',
  181. '#value' => $ref->dbxref_id
  182. );
  183. $form["num-$i"]["delete-$i"] = array(
  184. '#type' => 'submit',
  185. '#value' => t("Delete"),
  186. '#name' => "delete-$i",
  187. );
  188. }
  189. $form['num_db_references'] = array(
  190. '#type' => 'hidden',
  191. '#value' => $i
  192. );
  193. $form["submit-edits"] = array(
  194. '#type' => 'submit',
  195. '#value' => t('Update All References')
  196. );
  197. } //end of foreach db ref
  198. return $form;
  199. }
  200. /**
  201. *
  202. *
  203. * @ingroup tripal_feature
  204. */
  205. function tripal_feature_edit_ALL_db_references_form_submit($form, &$form_state) {
  206. $num_refs = $form_state['values']['num_db_references'];
  207. $action = $form_state['clicked_button']['#value'];
  208. $button = $form_state['clicked_button']['#name'];
  209. $nid = $form_state['values']['nid'];
  210. if (strcmp($action, 'Update All References')==0) {
  211. for ($i=1; $i<=$num_refs; $i++) {
  212. $dbxref_id = $form_state['values']["dbxref_id-$i"];
  213. $db_id = $form_state['values']["db_id-$i"];
  214. $accession = $form_state['values']["accession-$i"];
  215. tripal_feature_update_db_reference($dbxref_id, $db_id, $accession);
  216. }
  217. drupal_set_message(t("Updated all Database References"));
  218. drupal_goto('node/' . $nid);
  219. }
  220. elseif (strcmp($action, 'Delete')==0) {
  221. if (preg_match('/delete-(\d+)/', $button, $matches) ) {
  222. $i = $matches[1];
  223. $dbxref_id = $form_state['values']["dbxref_id-$i"];
  224. tripal_feature_delete_db_reference($dbxref_id);
  225. drupal_set_message(t("Deleted Database Reference"));
  226. drupal_goto('node/' . $nid);
  227. }
  228. else {
  229. drupal_set_message(t("Could not remove database reference:"), 'error');
  230. }
  231. }
  232. else {
  233. drupal_set_message(t("Unrecognized Button Pressed"), 'error');
  234. }
  235. }
  236. /**
  237. *
  238. *
  239. * @ingroup tripal_feature
  240. */
  241. function tripal_feature_update_db_reference($dbxref_id, $db_id, $accession) {
  242. $previous_db = tripal_db_set_active('chado');
  243. $sql = "UPDATE {dbxref} SET db_id=%d, accession='%s' WHERE dbxref_id=%d";
  244. db_query($sql, $db_id, $accession, $dbxref_id);
  245. tripal_db_set_active($previous_db);
  246. }
  247. /**
  248. *
  249. *
  250. * @ingroup tripal_feature
  251. */
  252. function tripal_feature_delete_db_reference($dbxref_id) {
  253. $previous_db = tripal_db_set_active('chado');
  254. db_query(
  255. "DELETE FROM {dbxref} WHERE dbxref_id=%d",
  256. $dbxref_id
  257. );
  258. db_query(
  259. "DELETE FROM {feature_dbxref} WHERE dbxref_id=%d",
  260. $dbxref_id
  261. );
  262. tripal_db_set_active($previous_db);
  263. }
  264. /**
  265. *
  266. *
  267. * @ingroup tripal_feature
  268. */
  269. function theme_tripal_feature_edit_ALL_db_references_form($form) {
  270. $output = '';
  271. $output .= '<br /><fieldset>';
  272. $output .= '<legend>Edit Existing Database References<span class="form-optional" title="This field is optional">(optional)</span></legend>';
  273. $output .= '<p>Below is a list of already existing database references, one per line. When entering a database reference, the accession '
  274. .'is a unique identifier for this feature in the specified database.</p>';
  275. $output .= '<table>';
  276. $output .= '<tr><th>#</th><th>Database</th><th>Accession</th><th></th></tr>';
  277. for ($i=1; $i<=$form['num_db_references']['#value']; $i++) {
  278. $output .= '<tr><td>' . drupal_render($form["num-$i"]) . '</td><td>'
  279. . drupal_render($form["database-$i"]) . '</td><td>'
  280. . drupal_render($form["accession-$i"]) . '</td><td>'
  281. . drupal_render($form["submit-$i"]) . '</td></tr>';
  282. }
  283. $output .= '</table><br />';
  284. $output .= drupal_render($form);
  285. $output .= '</fieldset>';
  286. return $output;
  287. }
  288. /**
  289. *
  290. *
  291. * @ingroup tripal_feature
  292. */
  293. function list_dbreferences_for_node($db_references) {
  294. if (!empty($db_references) ) {
  295. $output = '<table>';
  296. $output .= '<tr><th>Database</th><th>Accession</th></tr>';
  297. foreach ($db_references as $db) {
  298. $output .= '<tr><td>' . $db->db_name . '</td><td>' . $db->accession . '</td></tr>';
  299. } // end of foreach db reference
  300. $output .= '</table>';
  301. }
  302. else {
  303. $output = 'No Database References Added to the Current Feature';
  304. }
  305. return $output;
  306. }