tripal_feature-db_references.inc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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. } else {
  117. drupal_set_message('Database reference NOT successfully created...','error');
  118. } //end of if dbxref was created successfully
  119. }
  120. /**
  121. *
  122. *
  123. * @ingroup tripal_feature
  124. */
  125. function tripal_feature_edit_ALL_dbreferences_page($node) {
  126. $output = '';
  127. $output .= drupal_get_form('tripal_feature_edit_ALL_db_references_form', $node);
  128. $output .= '<br>';
  129. $output .= drupal_get_form('tripal_feature_add_ONE_dbreference_form', $node);
  130. $output .= '<br>';
  131. $output .= drupal_get_form('tripal_feature_implement_back_to_feature_button', $node->nid);
  132. return $output;
  133. }
  134. /**
  135. * Implements Hook_form()
  136. * Handles adding of DB References to Features
  137. *
  138. * @ingroup tripal_feature
  139. */
  140. function tripal_feature_edit_ALL_db_references_form($form_state, $node) {
  141. $form = array();
  142. $form['nid'] = array(
  143. '#type' => 'hidden',
  144. '#value' => $node->nid
  145. );
  146. $i=0;
  147. $feature = $node->feature;
  148. $references = tripal_feature_load_references ($feature->feature_id);
  149. // pre populate the database options
  150. $db_options = tripal_db_get_db_options();
  151. $db_options[0] = 'Select a Database';
  152. ksort($db_options);
  153. if (sizeof($references) != 0) {
  154. foreach ($references as $ref) {
  155. $i++;
  156. $form["num-$i"] = array(
  157. '#type' => 'fieldset',
  158. '#title' => t("Database Reference")." $i"
  159. );
  160. $form["num-$i"]["accession-$i"] = array(
  161. '#type' => 'textfield',
  162. '#title' => t('Accession'),
  163. '#size' => 30,
  164. '#required' => TRUE,
  165. '#default_value' => $ref->accession
  166. );
  167. $form["num-$i"]["db_id-$i"] = array(
  168. '#type' => 'select',
  169. '#title' => t('Database'),
  170. '#options' => $db_options,
  171. '#required' => TRUE,
  172. '#default_value' => $ref->db_id
  173. );
  174. $form["num-$i"]["dbxref_id-$i"] = array(
  175. '#type' => 'hidden',
  176. '#value' => $ref->dbxref_id
  177. );
  178. $form["num-$i"]["delete-$i"] = array(
  179. '#type' => 'submit',
  180. '#value' => t("Delete"),
  181. '#name' => "delete-$i",
  182. );
  183. }
  184. $form['num_db_references'] = array(
  185. '#type' => 'hidden',
  186. '#value' => $i
  187. );
  188. $form["submit-edits"] = array(
  189. '#type' => 'submit',
  190. '#value' => t('Update All References')
  191. );
  192. } //end of foreach db ref
  193. return $form;
  194. }
  195. /**
  196. *
  197. *
  198. * @ingroup tripal_feature
  199. */
  200. function tripal_feature_edit_ALL_db_references_form_submit($form, &$form_state) {
  201. $num_refs = $form_state['values']['num_db_references'];
  202. $action = $form_state['clicked_button']['#value'];
  203. $button = $form_state['clicked_button']['#name'];
  204. $nid = $form_state['values']['nid'];
  205. if (strcmp($action,'Update All References')==0) {
  206. for ($i=1; $i<=$num_refs; $i++) {
  207. $dbxref_id = $form_state['values']["dbxref_id-$i"];
  208. $db_id = $form_state['values']["db_id-$i"];
  209. $accession = $form_state['values']["accession-$i"];
  210. tripal_feature_update_db_reference($dbxref_id,$db_id,$accession);
  211. }
  212. drupal_set_message("Updated all Database References");
  213. drupal_goto('node/'.$nid);
  214. }
  215. elseif (strcmp($action,'Delete')==0){
  216. if(preg_match('/delete-(\d+)/', $button, $matches) ) {
  217. $i = $matches[1];
  218. $dbxref_id = $form_state['values']["dbxref_id-$i"];
  219. tripal_feature_delete_db_reference($dbxref_id);
  220. drupal_set_message("Deleted Database Reference");
  221. drupal_goto('node/'.$nid);
  222. } else {
  223. drupal_set_message("Could not remove database reference: ",'error');
  224. }
  225. }
  226. else {
  227. drupal_set_message("Unrecognized Button Pressed",'error');
  228. }
  229. }
  230. /**
  231. *
  232. *
  233. * @ingroup tripal_feature
  234. */
  235. function tripal_feature_update_db_reference($dbxref_id, $db_id, $accession) {
  236. $previous_db = tripal_db_set_active('chado');
  237. $sql = "UPDATE dbxref SET db_id=%d, accession='%s' WHERE dbxref_id=%d";
  238. db_query($sql,$db_id,$accession,$dbxref_id);
  239. tripal_db_set_active($previous_db);
  240. }
  241. /**
  242. *
  243. *
  244. * @ingroup tripal_feature
  245. */
  246. function tripal_feature_delete_db_reference($dbxref_id) {
  247. $previous_db = tripal_db_set_active('chado');
  248. db_query(
  249. "DELETE FROM dbxref WHERE dbxref_id=%d",
  250. $dbxref_id
  251. );
  252. db_query(
  253. "DELETE FROM feature_dbxref WHERE dbxref_id=%d",
  254. $dbxref_id
  255. );
  256. tripal_db_set_active($previous_db);
  257. }
  258. /**
  259. *
  260. *
  261. * @ingroup tripal_feature
  262. */
  263. function theme_tripal_feature_edit_ALL_db_references_form ($form) {
  264. $output = '';
  265. $output .= '<br><fieldset>';
  266. $output .= '<legend>Edit Existing Database References<span class="form-optional" title="This field is optional">(optional)</span></legend>';
  267. $output .= '<p>Below is a list of already existing database references, one per line. When entering a database reference, the accession '
  268. .'is a unique identifier for this feature in the specified database.</p>';
  269. $output .= '<table>';
  270. $output .= '<tr><th>#</th><th>Database</th><th>Accession</th><th></th></tr>';
  271. for ($i=1; $i<=$form['num_db_references']['#value']; $i++) {
  272. $output .= '<tr><td>'.drupal_render($form["num-$i"]).'</td><td>'
  273. .drupal_render($form["database-$i"]).'</td><td>'
  274. .drupal_render($form["accession-$i"]).'</td><td>'
  275. .drupal_render($form["submit-$i"]).'</td></tr>';
  276. }
  277. $output .= '</table><br>';
  278. $output .= drupal_render($form);
  279. $output .= '</fieldset>';
  280. return $output;
  281. }
  282. /**
  283. *
  284. *
  285. * @ingroup tripal_feature
  286. */
  287. function list_dbreferences_for_node($db_references) {
  288. if (!empty($db_references) ) {
  289. $output = '<table>';
  290. $output .= '<tr><th>Database</th><th>Accession</th></tr>';
  291. foreach ($db_references as $db) {
  292. $output .= '<tr><td>'.$db->db_name.'</td><td>'.$db->accession.'</td></tr>';
  293. } // end of foreach db reference
  294. $output .= '</table>';
  295. } else {
  296. $output = 'No Database References Added to the Current Feature';
  297. }
  298. return $output;
  299. }