tripal_feature-db_references.inc 10 KB

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