View source
<?php
abstract class spaces_controller {
var $controller;
var $space_type;
var $space_id;
var $values;
protected $loaded;
protected $loaded_all;
function __construct($controller, $space_type, $space_id) {
$this->controller = $controller;
$this->space_type = $space_type;
$this->space_id = $space_id;
$this->values = array(
'original' => array(),
'preset' => array(),
'space' => array(),
);
$this->loaded = array(
'original' => array(),
'preset' => array(),
'space' => array(),
);
$this->loaded_all = array(
'original' => FALSE,
'preset' => FALSE,
'space' => FALSE,
);
}
function init_overrides() {
$this
->reset_values('original');
$this
->reset_values('preset');
$this
->reset_values('space');
}
protected function reset_values($environment) {
$this->loaded[$environment] = array();
$this->loaded_all[$environment] = FALSE;
}
protected function load_values($environment = NULL, $id = NULL) {
switch ($environment) {
case 'original':
return $this
->load_original_values($id);
case 'preset':
return $this
->load_preset_values($id);
case 'space':
return $this
->load_space_values($id);
default:
$this
->load_original_values();
$this
->load_preset_values();
$this
->load_space_values();
break;
}
}
protected function load_space_values($id = NULL) {
if (!$this->loaded_all['space']) {
if (!isset($id)) {
$result = db_query("\n SELECT object_id AS id, value\n FROM {spaces_overrides}\n WHERE type = :type\n AND id = :id\n AND object_type = :otype", array(
':type' => $this->space_type,
':id' => $this->space_id,
':otype' => $this->controller,
));
foreach ($result as $row) {
$this->values['space'][$row->id] = unserialize($row->value);
}
$this->loaded_all['space'] = TRUE;
}
else {
if (!isset($this->loaded['space'][$id])) {
$result = db_query("\n SELECT object_id AS id, value\n FROM {spaces_overrides}\n WHERE type = :type\n AND id = :id\n AND object_type = :otype\n AND object_id = :oid", array(
':type' => $this->space_type,
':id' => $this->space_id,
':otype' => $this->controller,
':oid' => $id,
));
foreach ($result as $row) {
$this->values['space'][$row->id] = unserialize($row->value);
}
$this->loaded['space'][$id] = TRUE;
}
}
}
}
protected function load_preset_values($id = NULL) {
if (!$this->loaded_all['preset']) {
$preset_name = variable_get("spaces_preset_{$this->space_type}", NULL);
if ($preset_name && ($preset = spaces_preset_load($preset_name))) {
if (isset($preset->value[$this->controller])) {
$this->values['preset'] = $preset->value[$this->controller];
}
}
$this->loaded_all['preset'] = TRUE;
}
}
protected abstract function load_original_values($id = NULL);
function get($id = NULL, $environment = NULL) {
if (isset($environment, $id)) {
$this
->load_values($environment, $id);
return isset($this->values[$environment][$id]) ? $this->values[$environment][$id] : NULL;
}
else {
if (isset($environment)) {
$this
->load_values($environment);
return isset($this->values[$environment]) ? $this->values[$environment] : NULL;
}
else {
if (isset($id)) {
$environments = array(
'space',
'preset',
'original',
);
$environment = array_shift($environments);
while (isset($this->values[$environment])) {
$this
->load_values($environment, $id);
if (isset($this->values[$environment][$id])) {
return $this->values[$environment][$id];
}
$environment = array_shift($environments);
}
return array();
}
}
}
$this
->load_values();
return array_merge($this->values['original'], $this->values['preset'], $this->values['space']);
}
function set($id, $value) {
$override = array(
'type' => $this->space_type,
'id' => $this->space_id,
'object_type' => $this->controller,
'object_id' => $id,
);
$exists = db_query("SELECT id\n FROM {spaces_overrides}\n WHERE type = :type AND id = :id AND object_type = :otype AND object_id = :oid", array(
':type' => $override['type'],
':id' => $override['id'],
':otype' => $override['object_type'],
':oid' => $override['object_id'],
))
->fetchField();
$keys = array_keys($override);
$override['value'] = $value;
if ($exists) {
drupal_write_record('spaces_overrides', $override, $keys);
}
else {
drupal_write_record('spaces_overrides', $override);
}
$this->values['space'][$id] = $value;
return TRUE;
}
function del($id = NULL) {
$query = "{spaces_overrides} WHERE type = :type AND id = :id AND object_type = :object_type";
$override = array(
':type' => $this->space_type,
':id' => $this->space_id,
':object_type' => $this->controller,
);
if (isset($id)) {
$query .= " AND object_id = :object_id";
$override[':object_id'] = $id;
}
$exists = db_query("SELECT id FROM {$query}", $override)
->fetchField();
if ($exists) {
db_query("DELETE FROM {$query}", $override);
if (isset($this->values['space'][$id])) {
unset($this->values['space'][$id]);
}
return TRUE;
}
return FALSE;
}
function summary($id, $value = NULL) {
switch (gettype($value)) {
case 'string':
case 'integer':
case 'boolean':
case 'double':
return filter_xss_admin($value);
case 'array':
case 'object':
return filter_xss_admin(var_export($value, TRUE));
}
}
}