You are here

function themekey_rule_chain_form_validate in ThemeKey 7.2

Same name and namespace in other branches
  1. 7.3 themekey_admin.inc \themekey_rule_chain_form_validate()
  2. 7 themekey_admin.inc \themekey_rule_chain_form_validate()

Validation of

See also

themekey_rule_chain_form()

File

./themekey_admin.inc, line 240

Code

function themekey_rule_chain_form_validate(&$form, $form_state) {
  module_load_include('inc', 'themekey', 'themekey_validators');
  $attributes = variable_get('themekey_attributes', array());
  $values = $form_state['values'];
  if (!empty($values['old_items'])) {
    foreach ($values['old_items'] as $key_1 => $value_1) {
      if ($value_1['enabled']) {
        if (!themekey_check_theme_enabled($value_1['theme'])) {
          form_set_error('old_items][' . $key_1 . '][theme', check_plain(t('Theme is not activated')));
        }
        if (empty($value_1['property'])) {
          form_set_error('old_items][' . $key_1 . '][property', check_plain(t('Property missing')));
        }
        else {
          if (0 == drupal_strlen($value_1['value'])) {
            form_set_error('old_items][' . $key_1 . '][value', check_plain(t('You must enter a value')));
          }
          elseif (!empty($attributes[$value_1['property']]['validator'])) {

            //validate rule with custom validator
            $validator = $attributes[$value_1['property']]['validator'];
            if (!function_exists($validator) && isset($attributes[$value_1['property']]['file'])) {
              themekey_load_function($validator, $attributes[$value_1['property']]['file'], isset($attributes[$value_1['property']]['path']) ? $attributes[$value_1['property']]['path'] : '');
            }
            if (function_exists($validator)) {
              $rule = array(
                'property' => $value_1['property'],
                'wildcard' => $value_1['wildcard'],
                'operator' => $value_1['operator'],
                'value' => $value_1['value'],
              );
              $errors = $validator($rule);
              foreach ($errors as $element => $msg) {
                form_set_error('old_items][' . $key_1 . '][' . $element, filter_xss($msg, array(
                  'em',
                )));
              }
            }
            else {
              form_set_error('old_items][' . $key_1 . '][property', filter_xss(t('ThemeKey requested an unknown validator called %validator to validate property %property', array(
                '%validator' => $validator,
                '%property' => $value_1['property'],
              )), array(
                'em',
              )));
            }
          }
          elseif ('~' == $value_1['operator'] || '!~' == $value_1['operator']) {
            $rule = array(
              'property' => $value_1['property'],
              'wildcard' => $value_1['wildcard'],
              'operator' => $value_1['operator'],
              'value' => $value_1['value'],
            );
            $errors = themekey_validator_regex($rule);
            foreach ($errors as $element => $msg) {
              form_set_error('old_items][' . $key_1 . '][' . $element, filter_xss($msg, array(
                'em',
              )));
            }
          }
        }
        foreach ($values['old_items'] as $key_2 => $value_2) {
          if ($key_2 == $key_1) {
            continue;
          }
          if ($value_2['enabled'] && !empty($value_1['value']) && $value_2['property'] == $value_1['property'] && $value_2['operator'] == $value_1['operator'] && $value_2['value'] == $value_1['value'] && ($value_2['parent'] == $value_1['parent'] || $value_2['parent'] == $value_1['id'])) {
            if ('drupal:path:wildcard' != $value_2['property'] || 'drupal:path:wildcard' == $value_2['property'] && $value_2['wildcard'] == $value_1['wildcard']) {

              // We have two identical rules with same 'indention' in a chain.
              // This is allowed only if first one has childs and second one has none and one isn't the parent of the other
              if (!$value_2['parent'] == $value_1['id']) {
                $has_childs_1 = FALSE;
                $has_childs_2 = FALSE;
                foreach ($values['old_items'] as $key_3 => $value_3) {
                  if ($value_3['parent'] == $value_1['id']) {
                    $has_childs_1 = TRUE;
                  }
                  if ($value_3['parent'] == $value_2['id']) {
                    $has_childs_2 = TRUE;
                  }
                  if ($has_childs_1 && $has_childs_2) {
                    break;
                  }
                }
                if ($value_1['weight'] < $value_2['weight'] && $has_childs_1 && !$has_childs_2 || $value_1['weight'] > $value_2['weight'] && !$has_childs_1 && $has_childs_2) {

                  // no error
                  continue;
                }
                elseif ($value_1['weight'] > $value_2['weight'] && $has_childs_1 && !$has_childs_2) {
                  form_set_error('old_items][' . $key_1 . '][property', check_plain(t('Theme switching rule could never be reached')));
                  continue;
                }
                elseif ($value_1['weight'] < $value_2['weight'] && !$has_childs_1 && $has_childs_2) {
                  form_set_error('old_items][' . $key_1 . '][property', check_plain(t('Theme switching rule hides a later one')));
                  continue;
                }
                elseif ($value_1['weight'] < $value_2['weight'] && $has_childs_1 && $has_childs_2) {
                  form_set_error('old_items][' . $key_1 . '][property', check_plain(t('Theme switching rule should be combined with an identical one below')));
                  continue;
                }
                elseif ($value_1['weight'] > $value_2['weight'] && $has_childs_1 && $has_childs_2) {
                  form_set_error('old_items][' . $key_1 . '][property', check_plain(t('Theme switching rule should be combined with an identical one above')));
                  continue;
                }
              }
              form_set_error('old_items][' . $key_1 . '][property', check_plain(t('You entered two identical theme switching rules in the chain')));
              form_set_error('old_items][' . $key_2 . '][property', check_plain(t('You entered two identical theme switching rules in the chain')));
            }
          }
        }
      }
    }
  }
  if (!empty($values['new_item']['value'])) {
    if ($values['new_item']['enabled'] && !themekey_check_theme_enabled($values['new_item']['theme'])) {
      form_set_error('new_item][theme', check_plain(t('Theme is not activated')));
    }
    if (empty($values['new_item']['property'])) {
      form_set_error('new_item][property', check_plain(t('Property missing')));
    }
    else {
      if (!empty($attributes[$values['new_item']['property']]['validator'])) {

        //validate rule with custom validator
        $validator = $attributes[$values['new_item']['property']]['validator'];
        if (!function_exists($validator) && isset($attributes[$values['new_item']['property']]['file'])) {
          themekey_load_function($validator, $attributes[$values['new_item']['property']]['file'], isset($attributes[$values['new_item']['property']]['path']) ? $attributes[$values['new_item']['property']]['path'] : '');
        }
        if (function_exists($validator)) {
          $rule = array(
            'property' => $values['new_item']['property'],
            'wildcard' => $values['new_item']['wildcard'],
            'operator' => $values['new_item']['operator'],
            'value' => $values['new_item']['value'],
          );
          $errors = $validator($rule);
          foreach ($errors as $element => $msg) {
            form_set_error('new_item][' . $element, filter_xss($msg));
          }
        }
        else {
          form_set_error('new_item][property', filter_xss(t('ThemeKey requested an unknown validator called %validator to validate the property, %property', array(
            '%validator' => $validator,
            '%property' => $value_1['property'],
          ))), array(
            'em',
          ));
        }
      }
      elseif ('~' == $values['new_item']['operator'] || '!~' == $values['new_item']['operator']) {
        $rule = array(
          'property' => $values['new_item']['property'],
          'wildcard' => $values['new_item']['wildcard'],
          'operator' => $values['new_item']['operator'],
          'value' => $values['new_item']['value'],
        );
        $errors = themekey_validator_regex($rule);
        foreach ($errors as $element => $msg) {
          form_set_error('new_item][' . $element, filter_xss($msg, array(
            'em',
          )));
        }
      }
    }
  }
}