tripal_organism.delete.inc

  1. 2.x tripal_organism/includes/tripal_organism.delete.inc
  2. 3.x legacy/tripal_organism/includes/tripal_organism.delete.inc

Administration Interface for deleting multiple organisms

File

tripal_organism/includes/tripal_organism.delete.inc
View source
  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_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 = array();
  16. while ($organism = $org_rset->fetchObject()) {
  17. $organisms[$organism->organism_id] = "$organism->genus $organism->species ($organism->common_name)";
  18. }
  19. $form['desc'] = array(
  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'] = array(
  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'] = array(
  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_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 = array();
  47. foreach ($organisms 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 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_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 fatureloc 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. // $previous_db = chado_set_active('chado');
  80. // begin the transaction
  81. $transaction = db_transaction();
  82. print "\nNOTE: Deleting organisms is performed using a database transaction. \n" .
  83. "If the load fails or is terminated prematurely then the entire set of \n" .
  84. "deletions is rolled back and will not be found in the database\n\n";
  85. try {
  86. $values = array(
  87. 'organism_id' => $organisms
  88. );
  89. $num_deletes = chado_select_record('organism', array('count(*) as cnt'), $values);
  90. print "Deleting " . $num_deletes[0]->cnt . " organisms\n";
  91. chado_delete_record('organism', $values);
  92. print "Removing orphaned organism pages\n";
  93. chado_cleanup_orphaned_nodes('organism');
  94. }
  95. catch (Exception $e) {
  96. print "\n"; // make sure we start errors on new line
  97. $transaction->rollback();
  98. print "FAILED: Rolling back database changes...\n";
  99. watchdog_exception('tripal_organism', $e);
  100. return 0;
  101. }
  102. // chado_set_active($previous_db);
  103. print "\nDone\n";
  104. }