You are here

function views_revert_views in Views (for Drupal 7) 6.2

Same name and namespace in other branches
  1. 7.3 drush/views.drush.inc \views_revert_views()

Callback function for views-revert command.

1 string reference to 'views_revert_views'
views_revert_drush_command in ./views_revert.drush.inc
Implement hook_drush_command().

File

./views_revert.drush.inc, line 37
views-revert - Drush command to revert views overridden in the system.

Code

function views_revert_views() {
  $views = views_get_all_views();
  $i = 0;

  // The provided view names specified in the command.
  $viewnames = _convert_csv_to_array(func_get_args());

  // Find all overridden views.
  foreach ($views as $view) {
    if ($view->disabled) {
      continue;
    }
    if ($view->type == dt('Overridden')) {
      $overridden[$view->name] = $view->name;
    }
  }

  // Return early if there are no views overridden in the system.
  if (empty($overridden)) {
    return drush_set_error(dt('There are no overridden views in the system.'));
  }

  // If the user specified in the command the views to be overridden.
  if (!empty($viewnames)) {
    foreach ($viewnames as $key => $viewname) {
      $is_overridden = key_exists($viewname, $overridden);

      // Check if the provided view name is in the system
      if ($viewname && !key_exists($viewname, $views)) {
        drush_set_error(dt("'@viewname' view is not present in the system.", array(
          '@viewname' => $viewname,
        )));
      }
      elseif (!$is_overridden) {
        drush_set_error(dt("The view specified '@viewname' is not overridden.", array(
          '@viewname' => $viewname,
        )));
      }
      elseif ($is_overridden) {
        views_revert_view($views[$viewname]);
        $i++;
      }
      else {
        drush_set_error(dt("The view specified '@viewname' is not provided in code, and thus cannot be reverted.", array(
          '@viewname' => $viewname,
        )));
      }
    }
  }
  else {

    // list of choices for the user
    $overridden['all'] = dt('Revert all overridden views');

    // add a choice at the end
    $choice = drush_choice($overridden, 'Enter a number to choose which view to revert.', '!key');

    // prompt the user
    if ($choice !== FALSE) {

      // revert all views option
      if ($choice == 'all') {
        $i = views_revert_allviews($views);
      }
      else {
        views_revert_view($views[$choice]);
        $i++;
      }
    }
  }

  // final results output
  if ($i == 0) {
    drush_log(dt('No views were reverted.'), 'ok');
  }
  else {
    drush_log(dt('Reverted a total of @count views.', array(
      '@count' => $i,
    )), 'ok');
  }
}