You are here

private static function Views::viewSwitch in Hook Update Deploy Tools 8

Same name and namespace in other branches
  1. 7 src/Views.php \HookUpdateDeployTools\Views::viewSwitch()

Enable or disable requested Views.

Parameters

mixed $views_names: string - a single machine name of a View to enable/disable. array - an array of View machine names to enable/disable.

string $operation: 'enable' to enable the views. 'disable' to disable the views.

Return value

string Message returned to display.

2 calls to Views::viewSwitch()
Views::disable in src/Views.php
Disable requested Views.
Views::enable in src/Views.php
Enable requested Views.

File

src/Views.php, line 56

Class

Views
Public methods for working with Views.

Namespace

HookUpdateDeployTools

Code

private static function viewSwitch($views_names, $operation) {
  $t = get_t();
  $views_names = (array) $views_names;
  $op_friendly = $operation === 'disable' ? $t('Disabled') : $t('Enabled');
  $enable = $operation === 'disable' ? FALSE : TRUE;

  // Prepare the summary.
  $summary_description = $t('Views @op', array(
    '@op' => $op_friendly,
  ));
  $completed = array();
  $total_requested = count($views_names);
  try {
    self::canSwitch();

    // Enable the Views.
    foreach ($views_names as $view_name) {
      $variables = array(
        '@view' => $view_name,
        '@op' => $op_friendly,
      );
      $view = views_get_view($view_name);

      // Check to see if it exists.
      if (empty($view)) {

        // This View does not exist.
        $message = 'The View:@view does not exist so it could not be @op.';
        throw new HudtException($message, $variables, WATCHDOG_ERROR, TRUE);
      }

      // Does the status already match?
      if (property_exists($view, 'disabled') && $view->disabled == $enable) {

        // The View does not match the requested state.
        // Run the operation on the View.
        ctools_export_crud_set_status('views_view', $view, !$enable);

        // Verify that it worked.
        $view = views_get_view($view_name, TRUE);
        if (property_exists($view, 'disabled') && $view->disabled == $enable) {

          // The status does not match.  Message and fail the update.
          $message = 'The View:@view status does not match the requested state of "@op".';
          $completed[$view_name] = $t($message, $variables);
          throw new HudtException($message, $variables, WATCHDOG_ERROR, TRUE);
        }
        else {

          // The requested operation matches the current state so, success.
          $completed[$view_name] = $op_friendly;
        }
      }
      else {

        // Status matches requested operation, so skip.
        $completed[$view_name] = $t("Skipped. Already @op.", array(
          '@op' => $op_friendly,
        ));
      }
    }
  } catch (\Exception $e) {
    $vars = array(
      '!error' => method_exists($e, 'logMessage') ? $e
        ->logMessage() : $e
        ->getMessage(),
    );
    if (!method_exists($e, 'logMessage')) {

      // Not logged yet, so log it.
      $message = 'View enable/disable denied because: !error';
      Message::make($message, $vars, WATCHDOG_ERROR);
    }

    // Output a summary before shutting this down.
    $done = HudtInternal::getSummary($completed, $total_requested, $summary_description);
    Message::make($done, array(), FALSE, 1);
    throw new HudtException('Caught Exception: Update aborted!  !error', $vars, WATCHDOG_ERROR, FALSE);
  }
  $done = HudtInternal::getSummary($completed, $total_requested, $summary_description);
  $done = Message::make($done, array(), WATCHDOG_INFO, 1);
  return $done;
}