tripal_organism.delete.inc 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. /**
  3. * @file
  4. * Administration Interface for deleting multiple organisms
  5. */
  6. /**
  7. * A form for indicating the organisms to delete
  8. *
  9. * @ingroup tripal_legacy_organism
  10. */
  11. function tripal_organism_delete_form() {
  12. // get the list of organisms
  13. $sql = "SELECT * FROM {organism} ORDER BY genus, species";
  14. $org_rset = chado_query($sql);
  15. $organisms = [];
  16. while ($organism = $org_rset->fetchObject()) {
  17. $organisms[$organism->organism_id] = "$organism->genus $organism->species ($organism->common_name)";
  18. }
  19. $form['desc'] = [
  20. '#markup' => t("Use the following form to delete organisms which may or may
  21. not be synced. Please be cautious. Deleting an organism will cause all
  22. data associated with the organism to also be deleted."),
  23. ];
  24. $form['organisms'] = [
  25. '#title' => t('Organism'),
  26. '#type' => 'checkboxes',
  27. '#options' => $organisms,
  28. '#prefix' => '<div style="height: 400px; overflow: scroll">',
  29. '#suffix' => '</div><br>',
  30. ];
  31. $form['button'] = [
  32. '#type' => 'submit',
  33. '#value' => t('Delete Organisms'),
  34. ];
  35. return $form;
  36. }
  37. /**
  38. * Submit for the delete features form
  39. *
  40. * @ingroup tripal_legacy_organism
  41. */
  42. function tripal_organism_delete_form_submit($form, &$form_state) {
  43. global $user;
  44. // Convert the organisms input into an array of just ids.
  45. $organisms = $form_state['values']['organisms'];
  46. $org_ids = [];
  47. foreach ($organisms as $id => $val) {
  48. if ($val != 0) {
  49. $org_ids[] = $id;
  50. }
  51. }
  52. if (count($org_ids) > 0) {
  53. $args = [$org_ids];
  54. tripal_add_job("Delete Organism", 'tripal_organism',
  55. 'tripal_organism_delete_organisms', $args, $user->uid);
  56. drupal_set_message(t("Organisms will disappear from the list below once the job completes."));
  57. }
  58. else {
  59. drupal_set_message(t("Please select at least one organism to delete."), "error");
  60. }
  61. }
  62. /**
  63. * Function to actually delete the features indicated
  64. *
  65. * @param $organism_id
  66. * The list of organism_id of the features to delete
  67. * @param $job
  68. * The tripal_job id
  69. *
  70. * @ingroup tripal_legacy_organism
  71. */
  72. function tripal_organism_delete_organisms($organisms, $job = NULL) {
  73. global $user;
  74. // Deleting of organisms 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 organisms 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 = [
  86. 'organism_id' => $organisms,
  87. ];
  88. $num_deletes = chado_select_record('organism', ['count(*) as cnt'], $values);
  89. print "Deleting " . $num_deletes[0]->cnt . " organisms\n";
  90. chado_delete_record('organism', $values);
  91. print "Removing orphaned organism pages\n";
  92. chado_cleanup_orphaned_nodes('organism');
  93. } catch (Exception $e) {
  94. print "\n"; // make sure we start errors on new line
  95. $transaction->rollback();
  96. print "FAILED: Rolling back database changes...\n";
  97. watchdog_exception('tripal_organism', $e);
  98. return 0;
  99. }
  100. print "\nDone\n";
  101. }