You are here

class context_error_context_condition_error in Context error 7

Same name and namespace in other branches
  1. 6 plugins/context_error_context_conditions.inc \context_error_context_condition_error

Expose the 404 error page as the context condition.

Hierarchy

Expanded class hierarchy of context_error_context_condition_error

2 string references to 'context_error_context_condition_error'
context_error_context_plugins in ./context_error.module
Implements hook_context_plugins().
context_error_context_registry in ./context_error.module
Implements hook_context_registry().

File

plugins/context_error_context_conditions.inc, line 30
Context condition plugin to provide a trigger for 404 and 403 error pages.

View source
class context_error_context_condition_error extends context_condition {
  private $known_errors = array(
    CONTEXT_ERROR_403 => '403 Forbidden',
    CONTEXT_ERROR_404 => '404 Not Found',
  );
  function condition_values() {
    $values = array();
    $values[CONTEXT_ERROR_403] = t('403 only');
    $values[CONTEXT_ERROR_404] = t('404 only');
    $values[CONTEXT_ERROR_403_404] = t('404 or 403 pages');
    $values[CONTEXT_ERROR_NOT_ERROR] = t('Not an error page');
    return $values;
  }

  /**
   * Condition form.
   */
  function condition_form($context) {
    return array(
      '#title' => $this->title,
      '#description' => t('Select which error case should this condition activate.'),
      '#options' => $this
        ->condition_values(),
      '#type' => 'radios',
      '#default_value' => count($this
        ->fetch_from_context($context, 'values')) > 1 ? CONTEXT_ERROR_403_404 : key($this
        ->fetch_from_context($context, 'values')),
    );
  }

  /**
   * Condition form submit handler.
   *
   * Convert the values to an array, otherwise only string is submitted.
   * Store the 403 AND 404 case as an array with both values stored.
   */
  function condition_form_submit($values) {
    if ($values == CONTEXT_ERROR_403_404) {
      return array(
        CONTEXT_ERROR_403 => CONTEXT_ERROR_403,
        CONTEXT_ERROR_404 => CONTEXT_ERROR_404,
      );
    }
    return array(
      $values => $values,
    );
  }

  /**
   * Helper function to decide wether we are on a 404 or 403 page or not.
   */
  function is_error($header_status) {
    foreach ($this->known_errors as $error => $status) {
      if ($status == $header_status) {
        return TRUE;
      }
    }
    return FALSE;
  }

  /**
   * Execute.
   */
  function execute() {
    if ($this
      ->condition_used()) {
      $headers = drupal_get_http_header();
      foreach ($this
        ->get_contexts() as $context) {
        $errors = $this
          ->fetch_from_context($context, 'values');
        if (isset($headers['status']) && $this
          ->is_error($headers['status'])) {
          foreach ($errors as $error) {
            if (isset($this->known_errors[$error]) && $this->known_errors[$error] == $headers['status']) {
              $this
                ->condition_met($context);
            }
          }
        }
        else {
          if (array_key_exists(CONTEXT_ERROR_NOT_ERROR, $this
            ->fetch_from_context($context, 'values'))) {
            $this
              ->condition_met($context);
          }
        }
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
context_condition::$description property
context_condition::$plugin property
context_condition::$title property
context_condition::$values property
context_condition::condition_met function Marks a context as having met this particular condition.
context_condition::condition_used function Check whether this condition is used by any contexts. Can be used to prevent expensive condition checks from being triggered when no contexts use this condition.
context_condition::editor_form function Context editor form for conditions. 2
context_condition::editor_form_submit function Context editor form submit handler.
context_condition::fetch_from_context function Retrieve options from the context provided.
context_condition::get_contexts function Retrieve all contexts with the condition value provided. 2
context_condition::options_form function Options form. Provide additional options for your condition. 4
context_condition::options_form_submit function Options form submit handler.
context_condition::settings_form function Settings form. Provide variable settings for your condition.
context_condition::__clone function Clone our references when we're being cloned.
context_condition::__construct function Constructor. Do not override.
context_error_context_condition_error::$known_errors private property
context_error_context_condition_error::condition_form function Condition form. Overrides context_condition::condition_form
context_error_context_condition_error::condition_form_submit function Condition form submit handler. Overrides context_condition::condition_form_submit
context_error_context_condition_error::condition_values function Condition values. Overrides context_condition::condition_values
context_error_context_condition_error::execute function Execute.
context_error_context_condition_error::is_error function Helper function to decide wether we are on a 404 or 403 page or not.