You are here

class context_var_context_condition in Context: Variable 7

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

@file This class extends the context_condition

Hierarchy

Expanded class hierarchy of context_var_context_condition

2 string references to 'context_var_context_condition'
context_var_context_plugins in ./context_var.module
Implements hook_context_plugins().
context_var_context_registry in ./context_var.module
Implements hook_context_registry().

File

plugins/context_var_context_condition.inc, line 7
This class extends the context_condition

View source
class context_var_context_condition extends context_condition {

  /**
   * Omit condition values. We will provide a custom input form for our conditions.
   */
  function condition_values() {
    return array();
  }

  /**
   * Condition form.
   */
  function condition_form($context) {
    $form = parent::condition_form($context);
    unset($form['#options']);
    $form['#type'] = 'textarea';
    $form['#description'] = 'Map values as database variable|value. This will also accept variable|index:value in the case of arrays that are stored in the drupal variable table. %theme can be used to replace it with the current theme in use, accounting for og_theme';
    $form['#default_value'] = implode("\n", $this
      ->fetch_from_context($context, 'values'));
    return $form;
  }

  /**
   * Condition form submit handler.
   */
  function condition_form_submit($values) {
    $parsed = array();
    $items = explode("\n", $values);

    // pull together the values that are of the form name|value and store them in an array
    if (!empty($items)) {
      foreach ($items as $v) {
        $v = trim($v);
        if (!empty($v)) {
          $parsed[$v] = $v;
        }
      }
    }
    return $parsed;
  }

  /**
   * Execute.
   */
  function execute() {
    foreach (context_enabled_contexts() as $context) {
      if ($vars = $this
        ->fetch_from_context($context, 'values')) {

        // scan for the %theme value for replacement as a custom token
        if (module_exists('og_context')) {
          $group = og_context();

          // we are in the context of a group, grab theme from there
          if (isset($group->type)) {
            $theme = $group->og_theme;
          }
          else {
            $theme = variable_get('default_theme', 'garland');
          }
        }
        else {
          $theme = variable_get('default_theme', 'garland');
        }
        $vars = str_replace('%theme', $theme, $vars);

        // split all values that we recieve
        $all_conditions_met = TRUE;
        foreach ($vars as $var) {

          // split based on | safely
          $values = explode('|', filter_xss($var));

          // blow the array up based on | and convert it back into a key => value pair
          $sys_array = array();

          // reset the sys_array so that variables don't carry over from other contexts
          for ($i = 0; $i < count($values) - 1;) {
            $sys_array[$values[$i]] = $values[$i + 1];
            $i = $i + 2;
          }

          // go through each value pair and compare to drupal's current values
          // because we use variable_get this will take into account strongarm / spaces
          foreach ($sys_array as $key => $value) {
            if (module_exists('variable')) {
              $sys_var = variable_get_value($key);
            }
            else {
              $sys_var = variable_get($key, array());
            }

            // check to see if this is an even deeper array
            if (strpos($value, ':') === FALSE) {

              // if the database value doesn't equal the context value, return FALSE
              if ($sys_var != $value) {
                $all_conditions_met = FALSE;
              }
            }
            else {

              // drupal values can store arrays, we need to check for an array value
              $tmp_val = explode(':', $value);

              // if the database value doesn't equal the context value, return FALSE
              if (!isset($sys_var[$tmp_val[0]]) || $sys_var[$tmp_val[0]] != $tmp_val[1]) {
                $all_conditions_met = FALSE;
              }
            }
          }
        }

        // if all the conditions are met, pass ahead that they are met
        if ($all_conditions_met) {
          $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_var_context_condition::condition_form function Condition form. Overrides context_condition::condition_form
context_var_context_condition::condition_form_submit function Condition form submit handler. Overrides context_condition::condition_form_submit
context_var_context_condition::condition_values function Omit condition values. We will provide a custom input form for our conditions. Overrides context_condition::condition_values
context_var_context_condition::execute function Execute.