From 4c835c763f0a6d2d17a3d50f61f9cbd211afc095 Mon Sep 17 00:00:00 2001
From: Manuel Garcia <manuel.drupal@gmail.com>
Date: Mon, 7 Mar 2011 18:01:50 +0100
Subject: [PATCH] by Manuel Garcia: Added drush command to revert overridden ctools exported objects.

---
 ctools_revert.drush.inc |  133 +++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 133 insertions(+), 0 deletions(-)
 create mode 100644 ctools_revert.drush.inc

diff --git a/ctools_revert.drush.inc b/ctools_revert.drush.inc
new file mode 100644
index 0000000..20034ec
--- /dev/null
+++ b/ctools_revert.drush.inc
@@ -0,0 +1,133 @@
+<?php
+
+/**
+ * @file
+ * ctools-revert - Drush command to revert ctools exported objects in the system.
+ */
+
+/*
+ * Implement hook_drush_help().
+ */
+function ctools_revert_drush_help($section) {
+  switch ($section) {
+    case 'drush:ctools-revert':
+      return dt('Reverts ctools exported objects in the drupal installation that have been overriden. Careful, use with care.');
+  }
+}
+
+/*
+ * Implement hook_drush_command().
+ */
+function ctools_revert_drush_command() {
+  $items = array();
+
+  $items['ctools-revert'] = array(
+    'drupal dependencies' => array('ctools', ),
+    'description' => dt('Revert overridden views to their default state. Make sure to backup first.'),
+    'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_FULL,
+    'aliases' => array('cr'),
+  );
+
+  return $items;
+}
+
+/*
+ * Callback for the ctools-revert command
+ */
+function drush_ctools_revert() {
+  $i = 0;
+  $specified = _convert_csv_to_array(func_get_args()); // the provided object names specified in the command
+  ctools_include('export');
+
+  // get all necessary info from ctools
+  // 1. get all schemas to find each object later on
+  $schemas = ctools_export_get_schemas();
+  $schemas = array_keys($schemas);
+  $exported = array();
+   // 2. get all objects in those schemas
+  foreach($schemas as $key => $schema) {
+    $exported[$schema] = ctools_export_crud_load_all($schema);
+  }
+  // 3. find the overridden objects in the system
+  $overridden = array();
+  foreach($exported as $key => $object){
+    foreach($object as $key => $value){
+      if($value->export_type == 3) {
+        // put them in our $overridden array
+        $overridden[$key] = $value;
+      }
+    }
+  }
+  // return early if there's nothing to do
+  if(empty($overridden)) {
+    return drush_log('There are no overridden objects in the system to revert.', 'ok');
+  }
+
+  // if the user specified object names while calling the command, clear those
+  if(!empty($specified)) {
+    foreach($specified as $key => $objname) {
+      if(key_exists($objname, $overridden)){
+        _drush_ctools_revert_object($overridden[$objname]->table, $overridden[$objname]);
+        $i++;
+      }
+      else {
+         drush_set_error(dt("The object name specified '!objname' is not overridden or does not exist", array('!objname' => $objname)));
+      }
+    }
+  }
+
+  // no specified object names in the command, provide a list to choose from if possible
+  else {
+    // list of choices for the user
+    $overridden[dt('Revert all overridden objects')] = dt('Revert all overridden objects'); // add a choice at the end
+    $choice = drush_choice($overridden, 'Enter a number to choose which object to revert.', '!key'); // prompt the user
+
+    if ($choice !== FALSE) {
+      // revert all views option
+      if($choice == dt('Revert all overridden objects')) {
+        $i =  _drush_ctools_revertall_objects($overridden);
+      }
+      // else the user specified a single object
+      else {
+        $e = _drush_ctools_revert_object($overridden[$choice]->table, $overridden[$choice]);
+        $i = $i + $e;
+      }
+    }
+
+  }
+
+   // final results output
+  if($i == 0) {
+    drush_log(dt('No objects were reverted.'), 'ok');
+  }
+  else {
+    drush_log(dt('Reverted a total of !count objects.', array('!count' => $i)), 'ok');
+  }
+}
+
+
+/*
+ * Function to revert a single object
+ */
+function _drush_ctools_revert_object($tablename, $object) {
+  $i = 0;
+  if($object->export_type == 3) {
+    ctools_export_crud_delete($tablename, $object);
+    drush_log("Reverted the object '". $object->name ."'", 'success');
+    $i++;
+  }
+  return $i;
+}
+
+
+/*
+ * Function to revert all objects
+ */
+function _drush_ctools_revertall_objects($overridden) {
+  $i = 0;
+  foreach($overridden as $objname => $object) {
+    $e = _drush_ctools_revert_object($object->table, $object);
+    $i = $i + $e;
+  }
+  return $i;
+}
\ No newline at end of file
-- 
1.7.1

