You are here

function views_revert_views in Views (for Drupal 7) 7.3

Same name and namespace in other branches
  1. 6.2 views_revert.drush.inc \views_revert_views()

Callback function for views-revert command.

1 string reference to 'views_revert_views'
views_drush_command in drush/views.drush.inc
Implements hook_drush_command().

File

drush/views.drush.inc, line 121
Drush integration for Views.

Code

function views_revert_views() {
  $args = func_get_args();

  // The provided views names specified in the command.
  $viewnames = _convert_csv_to_array($args);
  $views = views_get_all_views();
  $i = 0;

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

  // If there are no overridden views in the system, report it.
  if (empty($overridden)) {
    drush_log(dt('There are no overridden views in the system.'), 'ok');
  }

  // If the user provided the "--all" option, revert all views.
  if (drush_get_option('all')) {
    $i = views_revert_allviews($views);
  }
  elseif (!empty($viewnames)) {
    foreach ($viewnames as $key => $viewname) {
      $is_overridden = array_key_exists($viewname, $overridden);

      // Check if the provided view name is in the system.
      if ($viewname && !array_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');
  }
}