You are here

function themekey_match_condition in ThemeKey 7.3

Same name and namespace in other branches
  1. 6.4 themekey_base.inc \themekey_match_condition()
  2. 6.2 themekey_base.inc \themekey_match_condition()
  3. 6.3 themekey_base.inc \themekey_match_condition()
  4. 7 themekey_base.inc \themekey_match_condition()
  5. 7.2 themekey_base.inc \themekey_match_condition()

Detects if a ThemeKey rule matches to the current page request.

Parameters

$condition: ThemeKey rule as object:

  • property
  • operator
  • value

$parameters: reference to an array containing all ThemeKey properties an their values

Return value

boolean

2 calls to themekey_match_condition()
themekey_cron_clear_page_cache in ./themekey_cron.inc
Checks rules containing time-based properties when cron runs. ThemeKey will carefully clean up the page cache, if necessary, to provide the right theme to anonymous users automatically, for example, a Christmas theme.
themekey_match_rule_childs in ./themekey_base.inc
Helper function of

File

./themekey_base.inc, line 408
The functions in this file are the back end of ThemeKey.

Code

function themekey_match_condition($condition, &$parameters) {
  $custom_theme =& drupal_static('themekey_custom_theme', '');
  if (is_object($condition)) {

    // Default operator is 'equal'
    if (empty($condition->operator)) {
      $condition->operator = '=';
    }
    if ('drupal:path' == $condition->property) {
      $match_path = themekey_match_path($condition, $parameters);
      if ($condition->operator == '=') {
        return $match_path;
      }

      // only '=' and '!' are allowed
      // @see themekey_validator_drupal_path()
      return !$match_path;
    }
    $value = themekey_property_value($parameters, $condition->property);
    if ('static' === $value && $custom_theme) {
      if (variable_get('themekey_debug_trace_rule_switching', FALSE)) {
        themekey_set_debug_message('A static rule set custom theme %custom_theme', array(
          '%custom_theme' => $custom_theme,
        ));
      }
      return TRUE;
    }
    if (!is_array($value)) {
      $value = array(
        $value,
      );
    }
    if (!empty($value)) {
      foreach ($value as $single_value) {
        if (!is_null($single_value)) {

          // Supported operators for condition check:
          // smaller ('<'), greater ('>'), equal ('='), not equal ('!'), regex match ('~')
          if ($condition->operator == '<' && $single_value >= $condition->value) {
            return FALSE;

            // all values need to not match
          }
          elseif ($condition->operator == '>' && $single_value <= $condition->value) {
            return FALSE;

            // all values need to not match
          }
          elseif ($condition->operator == '<=' && $single_value > $condition->value) {
            return FALSE;

            // all values need to not match
          }
          elseif ($condition->operator == '>=' && $single_value < $condition->value) {
            return FALSE;

            // all values need to not match
          }
          elseif ($condition->operator == '=' && $single_value == $condition->value) {
            return TRUE;
          }
          elseif ($condition->operator == '!' && $single_value == $condition->value) {
            return FALSE;

            // all values need to not match
          }
          elseif ($condition->operator == '*' && strpos($single_value, $condition->value) !== FALSE) {
            return TRUE;
          }
          elseif ($condition->operator == '!*' && strpos($single_value, $condition->value) !== FALSE) {
            return FALSE;

            // all values need to not match
          }
          elseif ($condition->operator == '~' && preg_match($condition->value, $single_value)) {
            return TRUE;
          }
          elseif ($condition->operator == '!~' && preg_match($condition->value, $single_value)) {
            return FALSE;

            // all values need to not match
          }
        }
        else {

          // value is NULL
          return FALSE;
        }
      }
      if ($condition->operator == '=' || $condition->operator == '~' || $condition->operator == '*') {

        // no value matched
        return FALSE;
      }
      else {

        // condition matched for all values
        return TRUE;
      }
    }
    else {

      // value array is empty => value is NULL
      return FALSE;
    }
  }
  else {
    trigger_error(t('Function themekey_match_condition() called with illegal parameters'), E_USER_ERROR);
  }
  return FALSE;
}