You are here

class contexturl_condition_url in Context URL 7

URL as a Context condition.

Hierarchy

Expanded class hierarchy of contexturl_condition_url

2 string references to 'contexturl_condition_url'
contexturl_context_plugins in ./contexturl.module
Implements hook_context_plugins().
contexturl_context_registry in ./contexturl.module
Implements hook_context_registry().

File

plugins/contexturl_condition_url.inc, line 11
Adds url argument condition to Context

View source
class contexturl_condition_url extends context_condition {

  /**
   * condition_values function
   */
  function condition_values() {
    return array(
      1 => t('True'),
    );
  }

  /**
   * condition_form function
   */
  function condition_form($context) {
    $form['#type'] = 'value';
    $form['#value'] = TRUE;
    return $form;
  }

  /**
   * condition_form_submit function
   */
  function condition_form_submit($values) {
    return array(
      $values,
    );
  }

  /**
   * options_form function
   */
  function options_form($context) {
    $defaults = $this
      ->fetch_from_context($context, 'options');
    return array(
      'evaluationtype' => array(
        '#type' => 'select',
        '#title' => t('Select the function for URL pattern evaluation'),
        '#description' => t('Select the function for URL pattern evaluation.'),
        '#options' => array(
          'fnmatch' => t('fnmatch'),
          'preg_match' => t('preg_match'),
        ),
        '#default_value' => isset($defaults['evaluationtype']) ? $defaults['evaluationtype'] : 1,
      ),
      'pageurl' => array(
        '#type' => 'textarea',
        '#title' => t('Page URL'),
        '#description' => t('Enter the URL formulas. Put each formula on a separate line. You can use the * character (asterisk) and any other pattern of the PHP fnmatch function and the ~ character (tilde) to exlcude one or more URL.'),
        '#default_value' => isset($defaults['pageurl']) ? $defaults['pageurl'] : '',
      ),
    );
  }

  /**
   * execute function
   */
  function execute() {
    foreach (context_enabled_contexts() as $context) {
      $options = $this
        ->fetch_from_context($context, 'options');
      if (!empty($options['pageurl'])) {
        $current_url = $_SERVER['HTTP_HOST'] . request_uri();
        $evaluationtype = explode("\n", isset($options['evaluationtype']) ? $options['evaluationtype'] : 1);
        $patterns = explode("\n", $options['pageurl']);
        $return = FALSE;
        foreach ($patterns as $pattern) {
          $pattern = trim($pattern);
          if ($pattern) {
            $negate = FALSE;
            if (strpos($pattern, '~') === 0) {
              $negate = TRUE;
              $pattern = substr($pattern, 1);
            }
            if ($evaluationtype == 'preg_match') {
              if ($negate && !preg_match($pattern, $current_url) || !$negate && preg_match($pattern, $current_url)) {
                $return = TRUE;
                break;
              }
            }
            else {
              if ($negate && !fnmatch($pattern, $current_url) || !$negate && fnmatch($pattern, $current_url)) {
                $return = TRUE;
                break;
              }
            }
          }
        }
        if ($return == TRUE) {
          $this
            ->condition_met($context, $return);
        }
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
contexturl_condition_url::condition_form function condition_form function Overrides context_condition::condition_form
contexturl_condition_url::condition_form_submit function condition_form_submit function Overrides context_condition::condition_form_submit
contexturl_condition_url::condition_values function condition_values function Overrides context_condition::condition_values
contexturl_condition_url::execute function execute function
contexturl_condition_url::options_form function options_form function Overrides context_condition::options_form
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_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.