ソースを参照

Added delete tabs for organisms and analyses

Stephen Ficklin 10 年 前
コミット
804e7229dc

+ 119 - 0
tripal_analysis/includes/tripal_analysis.delete.inc

@@ -0,0 +1,119 @@
+<?php
+
+/**
+ * @file
+* Administration Interface for deleting multiple analyses
+*/
+
+/**
+ * A form for indicating the analyses to delete
+*
+* @ingroup tripal_analysis
+*/
+function tripal_analysis_delete_form() {
+  // get the list of analyses
+  $sql = "SELECT * FROM {analysis} ORDER BY name";
+  $org_rset = chado_query($sql);
+  $analyses = array();
+  while ($analysis = $org_rset->fetchObject()) {
+    $analyses[$analysis->analysis_id] = "$analysis->name";
+  }
+
+  $form['desc'] = array(
+      '#markup' => t("Use the following form to delete analyses which may or may
+        not be synced. Please be cautious. Deleting an analysis will cause all
+        data associated with the analysis to also be deleted."),
+  );
+
+  $form['analyses'] = array(
+      '#title'       => t('analysis'),
+      '#type'        => 'checkboxes',
+      '#options'     => $analyses,
+      '#prefix'      => '<div style="height: 400px; overflow: scroll">',
+      '#suffix'      => '</div><br>',
+  );
+
+  $form['button'] = array(
+      '#type' => 'submit',
+      '#value' => t('Delete analyses'),
+  );
+  return $form;
+}
+
+/**
+ * Submit for the delete features form
+ *
+ * @ingroup tripal_analysis
+ */
+function tripal_analysis_delete_form_submit($form, &$form_state) {
+  global $user;
+
+  // Convert the analyses input into an array of just ids.
+  $analyses   = $form_state['values']['analyses'];
+  $org_ids = array();
+  foreach ($analyses as $id => $val) {
+    if ($val != 0) {
+      $org_ids[] = $id;
+    }
+  }
+
+  if (count($org_ids) > 0) {
+    $args = array($org_ids);
+
+    tripal_add_job("Delete analysis", 'tripal_analysis',
+    'tripal_analysis_delete_analyses', $args, $user->uid);
+    drupal_set_message(t("analyses will disappear from the list below once the job completes."));
+  }
+  else {
+    drupal_set_message(t("Please select at least one analysis to delete."), "error");
+  }
+}
+
+/**
+ * Function to actually delete the features indicated
+ *
+ * @param $analysis_id
+ *   The list of analysis_id of the features to delete
+ * @param $job
+ *   The tripal_job id
+ *
+ * @ingroup tripal_analysis
+ */
+function tripal_analysis_delete_analyses($analyses, $job = NULL) {
+
+  global $user;
+
+  // Deleting of analyses will cause a cascade delete on the
+  // fassociated tables which may include the fatureloc table. The create_point
+  // function which is not prefix with the schema, and an error occurs.
+  // Therefore, we set the active database to chado to get around that
+  // problem.
+  $previous_db = chado_set_active('chado');
+
+  // begin the transaction
+  $transaction = db_transaction();
+  print "\nNOTE: Deleting analyses is performed using a database transaction. \n" .
+      "If the load fails or is terminated prematurely then the entire set of \n" .
+      "deletions is rolled back and will not be found in the database\n\n";
+
+  try {
+    $values = array(
+        analysis_id => $analyses
+    );
+    $num_deletes = chado_select_record('analysis', array('count(*) as cnt'), $values);
+    print "Deleting " . $num_deletes[0]->cnt . " analyses\n";
+    chado_delete_record('analysis', $values);
+
+    print "Removing orphaned analysis pages\n";
+    chado_cleanup_orphaned_nodes('analysis');
+  }
+  catch (Exception $e) {
+    print "\n"; // make sure we start errors on new line
+    $transaction->rollback();
+    print "FAILED: Rolling back database changes...\n";
+    watchdog_exception('tripal_analysis', $e);
+    return 0;
+  }
+  chado_set_active($previous_db);
+  print "\nDone\n";
+}

+ 13 - 2
tripal_analysis/tripal_analysis.module

@@ -11,6 +11,7 @@ require_once 'api/tripal_analysis.api.inc';
 require_once 'api/tripal_analysis.schema.api.inc';
 require_once 'api/tripal_analysis.DEPRECATED.inc';
 
+require_once 'includes/tripal_analysis.delete.inc';
 require_once 'includes/tripal_analysis_privacy.inc';
 require_once 'includes/tripal_analysis.admin.inc';
 require_once 'includes/tripal_analysis.chado_node.inc';
@@ -78,9 +79,19 @@ function tripal_analysis_menu() {
     'page arguments' => array('chado_node_sync_form', 'tripal_analysis', 'chado_analysis'),
     'access arguments' => array('administer tripal analysis'),
     'type' => MENU_LOCAL_TASK,
-    'weight' => 2
+    'weight' => 1
+  );
+  $items['admin/tripal/chado/tripal_analysis/delete'] = array(
+      'title' => ' Delete',
+      'description' => 'Delete multiple analyses from Chado',
+      'page callback' => 'drupal_get_form',
+      'page arguments' => array('tripal_analysis_delete_form'),
+      'access arguments' => array('administer tripal feature'),
+      'type' => MENU_LOCAL_TASK,
+      'file path' => drupal_get_path('module', 'tripal_analysis'),
+      'file' =>  'includes/tripal_analysis.delete.inc',
+      'weight' => 2
   );
-
   $items['admin/tripal/chado/tripal_analysis/chado_analysis_toc'] = array(
     'title' => ' TOC',
     'description' => 'Manage the table of contents for analysis nodes.',

+ 4 - 6
tripal_feature/includes/tripal_feature.delete.inc

@@ -43,7 +43,7 @@ function tripal_feature_delete_form() {
 
   $form['organism_id'] = array(
    '#title'       => t('Organism'),
-   '#type'        => t('select'),
+   '#type'        => 'select',
    '#description' => t("Choose the organism for which features will be deleted."),
    '#options'     => $organisms,
   );
@@ -162,13 +162,11 @@ function tripal_feature_delete_features($organism_id, $analysis_id, $seq_type,
   
   // begin the transaction
   $transaction = db_transaction();
-  print "\nNOTE: Loading of this GFF file is performed using a database transaction. \n" .
+  print "\nNOTE: Deleting features is performed using a database transaction. \n" .
       "If the load fails or is terminated prematurely then the entire set of \n" .
-      "insertions/updates is rolled back and will not be found in the database\n\n";
-
+      "deletions is rolled back and will not be found in the database\n\n";
   try {
-    
-    
+
     // if feature names have been provided then handle that separately
     if ($feature_names) {
       $names = preg_split('/\s+/', $feature_names);

+ 118 - 0
tripal_organism/includes/tripal_organism.delete.inc

@@ -0,0 +1,118 @@
+<?php
+/**
+ * @file
+ * Administration Interface for deleting multiple organisms
+ */
+
+/**
+ * A form for indicating the organisms to delete
+ *
+ * @ingroup tripal_organism
+ */
+function tripal_organism_delete_form() {
+  // get the list of organisms
+  $sql = "SELECT * FROM {organism} ORDER BY genus, species";
+  $org_rset = chado_query($sql);
+  $organisms = array();
+  while ($organism = $org_rset->fetchObject()) {
+    $organisms[$organism->organism_id] = "$organism->genus $organism->species ($organism->common_name)";
+  }
+
+  $form['desc'] = array(
+    '#markup' => t("Use the following form to delete organisms which may or may 
+        not be synced. Please be cautious. Deleting an organism will cause all 
+        data associated with the organism to also be deleted."),
+  );
+  
+  $form['organisms'] = array(
+    '#title'       => t('Organism'),
+    '#type'        => 'checkboxes',
+    '#options'     => $organisms,
+    '#prefix'      => '<div style="height: 400px; overflow: scroll">',
+    '#suffix'      => '</div><br>',
+  );
+
+  $form['button'] = array(
+    '#type' => 'submit',
+    '#value' => t('Delete Organisms'),
+  );
+  return $form;
+}
+
+/**
+ * Submit for the delete features form
+ *
+ * @ingroup tripal_organism
+ */
+function tripal_organism_delete_form_submit($form, &$form_state) {
+  global $user;
+
+  // Convert the organisms input into an array of just ids.
+  $organisms   = $form_state['values']['organisms'];
+  $org_ids = array();
+  foreach ($organisms as $id => $val) {
+    if ($val != 0) {
+      $org_ids[] = $id;
+    }
+  }
+  
+  if (count($org_ids) > 0) {
+    $args = array($org_ids);
+  
+    tripal_add_job("Delete Organism", 'tripal_organism',
+      'tripal_organism_delete_organisms', $args, $user->uid);
+    drupal_set_message(t("Organisms will disappear from the list below once the job completes."));
+  }
+  else {
+    drupal_set_message(t("Please select at least one organism to delete."), "error");
+  }
+}
+
+/**
+ * Function to actually delete the features indicated
+ *
+ * @param $organism_id
+ *   The list of organism_id of the features to delete
+ * @param $job
+ *   The tripal_job id
+ *
+ * @ingroup tripal_organism
+ */
+function tripal_organism_delete_organisms($organisms, $job = NULL) {
+
+  global $user;
+
+  // Deleting of organisms will cause a cascade delete on the
+  // fassociated tables which may include the fatureloc table. The create_point
+  // function which is not prefix with the schema, and an error occurs.
+  // Therefore, we set the active database to chado to get around that
+  // problem.
+  $previous_db = chado_set_active('chado');
+  
+  // begin the transaction
+  $transaction = db_transaction();
+  print "\nNOTE: Deleting organisms is performed using a database transaction. \n" .
+      "If the load fails or is terminated prematurely then the entire set of \n" .
+      "deletions is rolled back and will not be found in the database\n\n";
+
+  try {
+    $values = array(
+      organism_id => $organisms
+    );
+    $num_deletes = chado_select_record('organism', array('count(*) as cnt'), $values);
+    print "Deleting " . $num_deletes[0]->cnt . " organisms\n";
+    chado_delete_record('organism', $values);
+  
+    print "Removing orphaned organism pages\n";
+    chado_cleanup_orphaned_nodes('organism');
+  }
+  catch (Exception $e) {
+    print "\n"; // make sure we start errors on new line
+    $transaction->rollback();
+    print "FAILED: Rolling back database changes...\n";
+    watchdog_exception('tripal_organism', $e);
+    return 0;
+  }
+  chado_set_active($previous_db);
+  print "\nDone\n";
+}

+ 34 - 26
tripal_organism/tripal_organism.module

@@ -8,6 +8,7 @@ require_once 'api/tripal_organism.api.inc';
 require_once 'api/tripal_organism.DEPRECATED.inc';
 
 require_once 'includes/tripal_organism.admin.inc';
+require_once 'includes/tripal_organism.delete.inc';
 require_once 'includes/tripal_organism.chado_node.inc';
 
 /**
@@ -39,36 +40,26 @@ function tripal_organism_menu() {
     'type' => MENU_NORMAL_ITEM,
   );
 
-  $items['admin/tripal/chado/tripal_organism/help'] = array(
-    'title' => 'Help',
-    'description' => "A description of the Tripal Organism module including a short description of it's usage.",
-    'page callback' => 'theme',
-    'page arguments' => array('tripal_organism_help'),
-    'access arguments' => array('administer tripal organism'),
-    'type' => MENU_LOCAL_TASK,
-    'weight' => 10
-  );
-
-  $items['admin/tripal/chado/tripal_organism/configuration'] = array(
-    'title' => 'Settings',
-    'description' => 'Manage integration of Chado organisms including associated features',
-    'page callback' => 'drupal_get_form',
-    'page arguments' => array('tripal_organism_admin'),
-    'access arguments' => array('administer tripal organism'),
-    'type' => MENU_LOCAL_TASK,
-    'weight' => 5
-  );
-
   $items['admin/tripal/chado/tripal_organism/sync'] = array(
-    'title' => ' Sync',
-    'description' => 'Create pages on this site for organisms stored in Chado',
+      'title' => ' Sync',
+      'description' => 'Create pages on this site for organisms stored in Chado',
+      'page callback' => 'drupal_get_form',
+      'page arguments' => array('chado_node_sync_form', 'tripal_organism', 'chado_organism'),
+      'access arguments' => array('administer tripal organism'),
+      'type' => MENU_LOCAL_TASK,
+      'weight' => 1
+  );
+  $items['admin/tripal/chado/tripal_organism/delete'] = array(
+    'title' => ' Delete',
+    'description' => 'Delete multiple organisms from Chado',
     'page callback' => 'drupal_get_form',
-    'page arguments' => array('chado_node_sync_form', 'tripal_organism', 'chado_organism'),
-    'access arguments' => array('administer tripal organism'),
+    'page arguments' => array('tripal_organism_delete_form'),
+    'access arguments' => array('administer tripal feature'),
     'type' => MENU_LOCAL_TASK,
+    'file path' => drupal_get_path('module', 'tripal_organism'),
+    'file' =>  'includes/tripal_organism.delete.inc',
     'weight' => 2
   );
-  
   $items['admin/tripal/chado/tripal_organism/chado_organism_toc'] = array(
     'title' => ' TOC',
     'description' => 'Manage the table of contents for organism nodes.',
@@ -80,7 +71,24 @@ function tripal_organism_menu() {
     'file path' => drupal_get_path('module', 'tripal_core'),
     'weight' => 3
   );
-
+  $items['admin/tripal/chado/tripal_organism/configuration'] = array(
+      'title' => 'Settings',
+      'description' => 'Manage integration of Chado organisms including associated features',
+      'page callback' => 'drupal_get_form',
+      'page arguments' => array('tripal_organism_admin'),
+      'access arguments' => array('administer tripal organism'),
+      'type' => MENU_LOCAL_TASK,
+      'weight' => 5
+  );
+  $items['admin/tripal/chado/tripal_organism/help'] = array(
+      'title' => 'Help',
+      'description' => "A description of the Tripal Organism module including a short description of it's usage.",
+      'page callback' => 'theme',
+      'page arguments' => array('tripal_organism_help'),
+      'access arguments' => array('administer tripal organism'),
+      'type' => MENU_LOCAL_TASK,
+      'weight' => 10
+  );
   $items['admin/tripal/chado/tripal_organism/views/organisms/enable'] = array(
     'title' => 'Enable Organism Administrative View',
     'page callback' => 'tripal_enable_view',