[ Index ]

PHP Cross Reference of Drupal 6 (yi-drupal)

title

Body

[close]

/sites/all/modules/views/ -> views_revert.drush.inc (source)

   1  <?php
   2  /**
   3   * @file
   4   * views-revert - Drush command to revert views overridden in the system.
   5   */
   6  
   7  /**
   8   * Implement hook_drush_help().
   9   */
  10  function views_revert_drush_help($section) {
  11    switch ($section) {
  12      case 'drush:revert-views':
  13        return dt('Reverts all views in the drupal installation that have been overriden. Careful, use with care.');
  14    }
  15  }
  16  
  17  /**
  18   * Implement hook_drush_command().
  19   */
  20  function views_revert_drush_command() {
  21    $items = array();
  22  
  23    $items['views-revert'] = array(
  24      'callback' => 'views_revert_views',
  25      'drupal dependencies' => array('views'),
  26      'description' => dt('Revert overridden views to their default state. Make sure to backup first.'),
  27      'bootstrap' => DRUSH_BOOTSTRAP_DRUPAL_FULL,
  28      'aliases' => array('vr'),
  29    );
  30  
  31    return $items;
  32  }
  33  
  34  /**
  35   * Callback function for views-revert command.
  36   */
  37  function views_revert_views() {
  38    $views = views_get_all_views();
  39    $i = 0;
  40    // The provided view names specified in the command.
  41    $viewnames = _convert_csv_to_array(func_get_args());
  42  
  43    // Find all overridden views.
  44    foreach ($views as $view) {
  45      if ($view->disabled) {
  46        continue;
  47      }
  48      if ($view->type == dt('Overridden')) {
  49        $overridden[$view->name] = $view->name;
  50      }
  51    }
  52  
  53    // Return early if there are no views overridden in the system.
  54    if (empty($overridden)) {
  55      return drush_set_error(dt('There are no overridden views in the system.'));
  56    }
  57  
  58    // If the user specified in the command the views to be overridden.
  59    if (!empty($viewnames)) {
  60      foreach ($viewnames as $key => $viewname) {
  61        $is_overridden = key_exists($viewname, $overridden);
  62        // Check if the provided view name is in the system
  63        if ($viewname && !key_exists($viewname, $views)) {
  64          drush_set_error(dt("'@viewname' view is not present in the system.", array('@viewname' => $viewname)));
  65        }
  66        // Check if the provided view is overridden.
  67        elseif (!$is_overridden) {
  68          drush_set_error(dt("The view specified '@viewname' is not overridden.", array('@viewname' => $viewname)));
  69        }
  70        // If the view is overriden, revert it.
  71        elseif ($is_overridden){
  72          views_revert_view($views[$viewname]);
  73          $i++;
  74        }
  75        // We should never get here but well...
  76        else {
  77          drush_set_error(dt("The view specified '@viewname' is not provided in code, and thus cannot be reverted.", array('@viewname' => $viewname)));
  78        }
  79      }
  80    }
  81  
  82    // The user did not specify any views in the command, prompt the user
  83    else {
  84      // list of choices for the user
  85      $overridden['all'] = dt('Revert all overridden views'); // add a choice at the end
  86      $choice = drush_choice($overridden, 'Enter a number to choose which view to revert.', '!key'); // prompt the user
  87  
  88      if ($choice !== FALSE) {
  89        // revert all views option
  90        if ($choice == 'all') {
  91          $i = views_revert_allviews($views);
  92        }
  93        // else the user specified a single view
  94        else {
  95          views_revert_view($views[$choice]);
  96          $i++;
  97        }
  98      }
  99  
 100    }
 101  
 102    // final results output
 103    if ($i == 0) {
 104      drush_log(dt('No views were reverted.'), 'ok');
 105    }
 106    else {
 107      drush_log(dt('Reverted a total of @count views.', array('@count' => $i)), 'ok');
 108    }
 109  }
 110  
 111  /**
 112   * Reverts all views
 113   * @param $views
 114   * All views in the system as provided by views_get_all_views().
 115   */
 116  function views_revert_allviews($views) {
 117    $i = 0;
 118    foreach ($views as $view) {
 119      if ($view->disabled) {
 120        continue;
 121      }
 122  
 123      if ($view->type == t('Overridden')) {
 124        views_revert_view($view);
 125        $i++;
 126      }
 127    }
 128    return $i;
 129  }
 130  
 131  /**
 132   * Revert a specified view
 133   * @param $view
 134   * The view object to be reverted
 135   *
 136   * Checks on wether or not the view is overridden is handled in views_revert_views_revert()
 137   * We perform a check here anyway in case someone somehow calls this function on their own...
 138   */
 139  function views_revert_view($view) {
 140    // check anyway just in case
 141    if ($view->type == t('Overridden')) {
 142      // Revert the view.
 143      $view->delete();
 144      // Clear its cache.
 145      views_object_cache_clear('view', $view->name);
 146      // Give feedback.
 147      $message = dt("Reverted the view '@viewname'", array('@viewname' => $view->name));
 148      drush_log($message, 'success');
 149      // Reverted one more view.
 150    }
 151    else {
 152      drush_set_error(dt("The view '@viewname' is not overridden.", array('@viewname' => $view->name)));
 153    }
 154  }


Generated: Mon Jul 9 18:01:44 2012 Cross-referenced by PHPXref 0.7