You are here

class OgVariableStoreRealmStore in OG Variables 7

@file Variable realm controller

Hierarchy

Expanded class hierarchy of OgVariableStoreRealmStore

1 string reference to 'OgVariableStoreRealmStore'
og_variables_variable_realm_info in ./og_variables.module
Implements hook_variable_realm_info().

File

./og_variables.class.inc, line 60
Variable Realm controller.

View source
class OgVariableStoreRealmStore extends VariableStoreRealmStore {
  private $parent_variables = array();
  private $deleted = array();

  /**
   * Default to global to get around form fields default to hard default.
   */
  public function variable_get($name, $default = NULL) {
    $this
      ->variable_init();

    // This gets around the hiccup that $conf has the old value right after
    // delete os variable_realm_refresh resets it to that old value.
    $was_deleted = FALSE;
    if (!empty($this->deleted[$name])) {
      $was_deleted = TRUE;
      unset($this->deleted[$name]);
    }
    return isset($this->variables[$name]) ? $this->variables[$name] : (isset($this->parent_variables[$name]) ? $this->parent_variables[$name] : ($was_deleted ? $default : variable_get($name, $default)));
  }

  /**
   * Delete single variable.
   *
   * @param $name
   *   Variable name
   */
  public function variable_del($name) {
    if (isset($this->variables[$name])) {
      $this->deleted[$name] = $name;

      // Since $variables is a reference we just need to delete the store value.
      variable_store_del($this->realm, $this->key, $name);
    }
  }

  /**
   * Initialize realm.
   */
  public function variable_init() {
    if (!isset($this->variables)) {

      // We need to traverse the key to get parent ones also.
      $keys = $this
        ->getAllKeys();

      // We need to keep this by reference so changes are reflected.
      $this->variables =& variable_store($this->realm, array_shift($keys));
      $this->parent_variables = array();
      foreach ($keys as $key) {

        // variable_store is cached so likely fetching cached info.
        $this->parent_variables += variable_store($this->realm, $key);
      }
    }
  }

  /**
   * List all current variable values.
   */
  public function variable_list() {
    $this
      ->variable_init();
    return $this->variables + $this->parent_variables;
  }

  /**
   * Get all the keys that should fetch for set key.
   */
  private function getAllKeys() {
    $keys = array(
      $this->key,
    );
    if (module_exists('og_subgroups')) {
      list(, $nid) = explode('_', $this->key);

      // We fetch one level at time to perserve hierarchy.
      $parents = og_subgroups_parents_load('node', $nid, TRUE, FALSE);
      while (!empty($parents['node'])) {
        $parent = reset($parents['node']);
        $keys[] = 'node_' . $parent;

        // og_subgroups_parents_load is cached well.
        $parents = og_subgroups_parents_load('node', $parent, TRUE, FALSE);
      }
    }
    return $keys;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
OgVariableStoreRealmStore::$deleted private property
OgVariableStoreRealmStore::$parent_variables private property
OgVariableStoreRealmStore::getAllKeys private function Get all the keys that should fetch for set key.
OgVariableStoreRealmStore::variable_del public function Delete single variable. Overrides VariableStoreRealmStore::variable_del
OgVariableStoreRealmStore::variable_get public function Default to global to get around form fields default to hard default. Overrides VariableRealmDefaultStore::variable_get
OgVariableStoreRealmStore::variable_init public function Initialize realm. Overrides VariableStoreRealmStore::variable_init
OgVariableStoreRealmStore::variable_list public function List all current variable values. Overrides VariableRealmDefaultStore::variable_list
VariableRealmDefaultStore::$key public property
VariableRealmDefaultStore::$realm public property
VariableRealmDefaultStore::$variables protected property
VariableRealmDefaultStore::variable_add public function Implementation of VariableRealmStoreInterface::variable_add(). Overrides VariableRealmStoreInterface::variable_add
VariableRealmDefaultStore::variable_exists public function Implementation of VariableRealmStoreInterface::variable_exists(). Overrides VariableRealmStoreInterface::variable_exists
VariableRealmDefaultStore::__construct public function Class constructor. Overrides VariableRealmStoreInterface::__construct
VariableStoreRealmStore::variable_set public function Set single variable. Overrides VariableRealmDefaultStore::variable_set
VariableStoreRealmStore::__sleep public function Implements 'magic' _sleep method.