tripal_analysis.delete.inc 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. /**
  3. * @file
  4. * Administration Interface for deleting multiple analyses
  5. */
  6. /**
  7. * A form for indicating the analyses to delete
  8. *
  9. * @ingroup tripal_legacy_analysis
  10. */
  11. function tripal_analysis_delete_form() {
  12. // get the list of analyses
  13. $sql = "SELECT * FROM {analysis} ORDER BY name";
  14. $org_rset = chado_query($sql);
  15. $analyses = array();
  16. while ($analysis = $org_rset->fetchObject()) {
  17. $analyses[$analysis->analysis_id] = "$analysis->name";
  18. }
  19. $form['desc'] = array(
  20. '#markup' => t("Use the following form to delete analyses which may or may
  21. not be synced. Please be cautious. Deleting an analysis will cause all
  22. data associated with the analysis to also be deleted."),
  23. );
  24. $form['analyses'] = array(
  25. '#title' => t('analysis'),
  26. '#type' => 'checkboxes',
  27. '#options' => $analyses,
  28. '#prefix' => '<div style="height: 400px; overflow: scroll">',
  29. '#suffix' => '</div><br>',
  30. );
  31. $form['button'] = array(
  32. '#type' => 'submit',
  33. '#value' => t('Delete analyses'),
  34. );
  35. return $form;
  36. }
  37. /**
  38. * Submit for the delete features form
  39. *
  40. * @ingroup tripal_legacy_analysis
  41. */
  42. function tripal_analysis_delete_form_submit($form, &$form_state) {
  43. global $user;
  44. // Convert the analyses input into an array of just ids.
  45. $analyses = $form_state['values']['analyses'];
  46. $org_ids = array();
  47. foreach ($analyses as $id => $val) {
  48. if ($val != 0) {
  49. $org_ids[] = $id;
  50. }
  51. }
  52. if (count($org_ids) > 0) {
  53. $args = array($org_ids);
  54. tripal_add_job("Delete analysis", 'tripal_analysis',
  55. 'tripal_analysis_delete_analyses', $args, $user->uid);
  56. drupal_set_message(t("analyses will disappear from the list below once the job completes."));
  57. }
  58. else {
  59. drupal_set_message(t("Please select at least one analysis to delete."), "error");
  60. }
  61. }
  62. /**
  63. * Function to actually delete the features indicated
  64. *
  65. * @param $analysis_id
  66. * The list of analysis_id of the features to delete
  67. * @param $job
  68. * The tripal_job id
  69. *
  70. * @ingroup tripal_legacy_analysis
  71. */
  72. function tripal_analysis_delete_analyses($analyses, $job = NULL) {
  73. global $user;
  74. // Deleting of analyses will cause a cascade delete on the
  75. // fassociated tables which may include the featureloc table. The create_point
  76. // function which is not prefix with the schema, and an error occurs.
  77. // Therefore, we set the active database to chado to get around that
  78. // problem.
  79. // begin the transaction
  80. $transaction = db_transaction();
  81. print "\nNOTE: Deleting analyses is performed using a database transaction. \n" .
  82. "If the load fails or is terminated prematurely then the entire set of \n" .
  83. "deletions is rolled back and will not be found in the database\n\n";
  84. try {
  85. $values = array(
  86. 'analysis_id' => $analyses
  87. );
  88. $num_deletes = chado_select_record('analysis', array('count(*) as cnt'), $values);
  89. print "Deleting " . $num_deletes[0]->cnt . " analyses\n";
  90. chado_delete_record('analysis', $values);
  91. print "Removing orphaned analysis pages\n";
  92. chado_cleanup_orphaned_nodes('analysis');
  93. }
  94. catch (Exception $e) {
  95. print "\n"; // make sure we start errors on new line
  96. $transaction->rollback();
  97. print "FAILED: Rolling back database changes...\n";
  98. watchdog_exception('tripal_analysis', $e);
  99. return 0;
  100. }
  101. print "\nDone\n";
  102. }