You are here

class VariableRealmDefaultController in Variable 7.2

Same name and namespace in other branches
  1. 7 variable_realm/variable_realm.class.inc \VariableRealmDefaultController

Base class, keeps static list of variables.

Hierarchy

Expanded class hierarchy of VariableRealmDefaultController

2 string references to 'VariableRealmDefaultController'
variable_realm_variable_realm_info in variable_realm/variable_realm.module
Implements hook_variable_realm_info().
_variable_realm_controller in variable_realm/variable_realm.module
Create realm controller object.

File

variable_realm/variable_realm.class.inc, line 215
Variable realm controller

View source
class VariableRealmDefaultController implements VariableRealmControllerInterface {

  // Unique realm name (language, domain..)
  public $realm_name;

  // Current realm key.
  public $current_key;

  // Current realm weight.
  public $current_weight;

  // Array of variable stores indexed by realm key.
  protected $store;

  /**
   * Implementation of VariableRealmControllerInterface::__construct().
   */
  public function __construct($realm_name) {
    $this->realm_name = $realm_name;
    $this->current_weight = $this
      ->getDefaultWeight();
  }

  /**
   * Implementation of VariableRealmControllerInterface::isEnabled()
   */
  public function isEnabled() {
    return isset($this->current_key);
  }

  /**
   * Implementation of VariableRealmControllerInterface::isActive()
   */
  public function isActive() {
    return $this
      ->isEnabled() && $this->current_key !== FALSE;
  }

  /**
   * Implementation of VariableRealmControllerInterface::enable().
   */
  public function enable($realm_key = NULL) {
    if (!isset($this->current_key)) {
      return $this->current_key = isset($realm_key) ? $realm_key : $this
        ->getRequestKey();
    }
  }

  /**
   * Implementation of VariableRealmControllerInterface::getTitle().
   */
  public function getTitle() {
    return $this
      ->getInfo('title');
  }

  /**
   * Implementation of VariableRealmControllerInterface::getVariableName().
   */
  public function getVariableName() {
    return $this
      ->getInfo('variable name');
  }

  /**
   * Implementation of VariableRealmControllerInterface::getStore().
   */
  public function getStore($realm_key) {
    if (isset($this->store[$realm_key])) {
      return $this->store[$realm_key];
    }
    else {
      return $this
        ->addStore($realm_key);
    }
  }

  /**
   * Implementation of VariableRealmControllerInterface::addStore().
   */
  public function addStore($realm_key, $variables = NULL) {
    $store = $this
      ->createStore($realm_key, $variables);
    $this
      ->setStore($realm_key, $store);
    return $store;
  }

  /**
   * Create Store for key.
   */
  protected function createStore($realm_key, $variables = NULL) {
    $class = $this
      ->getInfo('store class');
    $class = $class && class_exists($class) ? $class : 'VariableRealmDefaultStore';
    return new $class($this->realm_name, $realm_key, $variables);
  }

  /**
   * Set store for realm key.
   */
  public function setStore($realm_key, $realm_store) {
    $this->store[$realm_key] = $realm_store;
  }

  /**
   * Implementation of VariableRealmControllerInterface::setKey().
   */
  public function setKey($realm_key) {
    $this->current_key = $realm_key;
  }

  /**
   * Implementation of VariableRealmControllerInterface::getKey().
   */
  public function getKey() {
    return isset($this->current_key) ? $this->current_key : FALSE;
  }

  /**
   * Implementation of VariableRealmControllerInterface::getAllKeys().
   */
  public function getAllKeys() {
    return $this
      ->getInfo('keys', array());
  }

  /**
   * Implementation of VariableRealmControllerInterface::getDefaultKey().
   */
  public function getDefaultKey() {
    return $this
      ->getInfo('default key', FALSE);
  }

  /**
   * Implementation of VariableRealmControllerInterface::getRequestKey().
   */
  public function getRequestKey() {
    return FALSE;
  }

  /**
   * Implementation of VariableRealmControllerInterface::getWeight().
   */
  public function getWeight() {
    return isset($this->current_weight) ? $this->current_weight : $this->controller_data['weight'];
  }

  /**
   * Implementation of VariableRealmControllerInterface::setWeight().
   */
  public function setWeight($weight) {
    $this->current_weight = $weight;
  }

  /**
   * Implementation of VariableRealmControllerInterface::getWeight().
   */
  public function getDefaultWeight() {
    return $this
      ->getRealmVariable('weight', $this
      ->getInfo('weight', 0));
  }

  /**
   * Implementation of VariableRealmControllerInterface::getParentRealms().
   */
  public function getParentRealms() {
    return array();
  }

  /**
   * Implementation of VariableRealmControllerInterface::getCurrentVariables().
   */
  public function getCurrentVariables() {
    if ($store = $this
      ->getCurrentStore()) {
      return $store
        ->variable_list();
    }
    else {
      return array();
    }
  }

  /**
   * Implementation of VariableRealmControllerInterface::getCurrentStore().
   */
  public function getCurrentStore() {
    if ($this
      ->isActive()) {
      return $this
        ->getStore($this
        ->getKey());
    }
    else {
      return NULL;
    }
  }

  /**
   * Implementation of VariableRealmControllerInterface::getAvailableVariables().
   */
  public function getAvailableVariables() {
    if ($options = $this
      ->getInfo('options')) {
      return $options;
    }
    else {

      // Defaults to all available variables.
      return array_keys(variable_get_info());
    }
  }

  /**
   * Implementation of VariableRealmControllerInterface::getEnabledVariables().
   */
  public function getEnabledVariables() {
    if ($this
      ->getInfo('select')) {
      return $this
        ->getRealmVariable('list', array());
    }
    else {

      // If the variable is not set it will default to all variables
      return $this
        ->getAvailableVariables();
    }
  }

  /**
   * Implementation of VariableRealmControllerInterface::getInfo().
   */
  public function getInfo($property = NULL, $default = NULL) {
    $info = variable_realm_info($this->realm_name);
    if ($property) {
      return isset($info[$property]) ? $info[$property] : $default;
    }
    else {
      return $info;
    }
  }

  /**
   * Implementation of VariableRealmControllerInterface::getRealmVariable().
   */
  public function getRealmVariable($name, $default = NULL) {
    return variable_get('variable_realm_' . $name . '_' . $this->realm_name, $default);
  }

  /**
   * Implementation of VariableRealmControllerInterface::setRealmVariable().
   */
  public function setRealmVariable($name, $value) {
    variable_realm_global_set('variable_realm_' . $name . '_' . $this->realm_name, $value);
  }

  /**
   * Implementation of VariableRealmControllerInterface::deleteVariable().
   */
  public function deleteVariable($variable_name) {
    foreach ($this
      ->getAllKeys() as $key => $name) {
      variable_realm_del($this->realm_name, $key, $variable_name, FALSE);
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
VariableRealmDefaultController::$current_key public property
VariableRealmDefaultController::$current_weight public property
VariableRealmDefaultController::$realm_name public property
VariableRealmDefaultController::$store protected property
VariableRealmDefaultController::addStore public function Implementation of VariableRealmControllerInterface::addStore(). Overrides VariableRealmControllerInterface::addStore
VariableRealmDefaultController::createStore protected function Create Store for key.
VariableRealmDefaultController::deleteVariable public function Implementation of VariableRealmControllerInterface::deleteVariable(). Overrides VariableRealmControllerInterface::deleteVariable
VariableRealmDefaultController::enable public function Implementation of VariableRealmControllerInterface::enable(). Overrides VariableRealmControllerInterface::enable
VariableRealmDefaultController::getAllKeys public function Implementation of VariableRealmControllerInterface::getAllKeys(). Overrides VariableRealmControllerInterface::getAllKeys 1
VariableRealmDefaultController::getAvailableVariables public function Implementation of VariableRealmControllerInterface::getAvailableVariables(). Overrides VariableRealmControllerInterface::getAvailableVariables 1
VariableRealmDefaultController::getCurrentStore public function Implementation of VariableRealmControllerInterface::getCurrentStore(). Overrides VariableRealmControllerInterface::getCurrentStore
VariableRealmDefaultController::getCurrentVariables public function Implementation of VariableRealmControllerInterface::getCurrentVariables(). Overrides VariableRealmControllerInterface::getCurrentVariables
VariableRealmDefaultController::getDefaultKey public function Implementation of VariableRealmControllerInterface::getDefaultKey(). Overrides VariableRealmControllerInterface::getDefaultKey 1
VariableRealmDefaultController::getDefaultWeight public function Implementation of VariableRealmControllerInterface::getWeight(). Overrides VariableRealmControllerInterface::getDefaultWeight
VariableRealmDefaultController::getEnabledVariables public function Implementation of VariableRealmControllerInterface::getEnabledVariables(). Overrides VariableRealmControllerInterface::getEnabledVariables 1
VariableRealmDefaultController::getInfo public function Implementation of VariableRealmControllerInterface::getInfo(). Overrides VariableRealmControllerInterface::getInfo
VariableRealmDefaultController::getKey public function Implementation of VariableRealmControllerInterface::getKey(). Overrides VariableRealmControllerInterface::getKey
VariableRealmDefaultController::getParentRealms public function Implementation of VariableRealmControllerInterface::getParentRealms(). Overrides VariableRealmControllerInterface::getParentRealms 1
VariableRealmDefaultController::getRealmVariable public function Implementation of VariableRealmControllerInterface::getRealmVariable(). Overrides VariableRealmControllerInterface::getRealmVariable
VariableRealmDefaultController::getRequestKey public function Implementation of VariableRealmControllerInterface::getRequestKey(). Overrides VariableRealmControllerInterface::getRequestKey 1
VariableRealmDefaultController::getStore public function Implementation of VariableRealmControllerInterface::getStore(). Overrides VariableRealmControllerInterface::getStore
VariableRealmDefaultController::getTitle public function Implementation of VariableRealmControllerInterface::getTitle(). Overrides VariableRealmControllerInterface::getTitle
VariableRealmDefaultController::getVariableName public function Implementation of VariableRealmControllerInterface::getVariableName(). Overrides VariableRealmControllerInterface::getVariableName
VariableRealmDefaultController::getWeight public function Implementation of VariableRealmControllerInterface::getWeight(). Overrides VariableRealmControllerInterface::getWeight
VariableRealmDefaultController::isActive public function Implementation of VariableRealmControllerInterface::isActive() Overrides VariableRealmControllerInterface::isActive
VariableRealmDefaultController::isEnabled public function Implementation of VariableRealmControllerInterface::isEnabled() Overrides VariableRealmControllerInterface::isEnabled
VariableRealmDefaultController::setKey public function Implementation of VariableRealmControllerInterface::setKey(). Overrides VariableRealmControllerInterface::setKey
VariableRealmDefaultController::setRealmVariable public function Implementation of VariableRealmControllerInterface::setRealmVariable(). Overrides VariableRealmControllerInterface::setRealmVariable
VariableRealmDefaultController::setStore public function Set store for realm key. Overrides VariableRealmControllerInterface::setStore
VariableRealmDefaultController::setWeight public function Implementation of VariableRealmControllerInterface::setWeight(). Overrides VariableRealmControllerInterface::setWeight
VariableRealmDefaultController::__construct public function Implementation of VariableRealmControllerInterface::__construct(). Overrides VariableRealmControllerInterface::__construct 1