--- /dev/null
+<?php
+/**
+ * The Horde_Core_Prefs class extends the base Horde_Prefs class by adding
+ * support for the prefs.php configuration file and Horde hooks.
+ *
+ * Copyright 2010 The Horde Project (http://www.horde.org/)
+ *
+ * See the enclosed file COPYING for license information (LGPL). If you
+ * did not receive this file, see http://www.fsf.org/copyleft/lgpl.html.
+ *
+ * @author Michael Slusarz <slusarz@horde.org>
+ * @category Horde
+ * @license http://www.fsf.org/copyleft/lgpl.html LGPL
+ * @package Core
+ */
+class Horde_Core_Prefs extends Horde_Prefs
+{
+ /**
+ * Hash holding preferences with hook functions defined.
+ *
+ * @var array
+ */
+ protected $_hooks = array();
+
+ /**
+ */
+ protected function _setValue($pref, $val, $convert = true)
+ {
+ if (!parent::_setValue($pref, $val, $convert)) {
+ return false;
+ }
+
+ /* If this preference has a change hook, call it now. */
+ try {
+ Horde::callHook('prefs_change', array($pref), $this->_getPrefScope($pref));
+ } catch (Horde_Exception_HookNotSet $e) {}
+
+ return true;
+ }
+
+ /**
+ * Populates the $_scopes hash with the default values as loaded from
+ * an application's prefs.php file.
+ */
+ protected function _loadScopePre($scope)
+ {
+ /* Read the configuration file. The $_prefs array holds the default
+ * values. */
+ try {
+ $result = Horde::loadConfiguration('prefs.php', array('_prefs'), $scope);
+ if (empty($result) || !isset($result['_prefs'])) {
+ return;
+ }
+ } catch (Horde_Exception $e) {
+ return;
+ }
+
+ foreach ($result['_prefs'] as $name => $pref) {
+ if (!isset($pref['value'])) {
+ continue;
+ }
+
+ $name = str_replace('.', '_', $name);
+
+ $mask = self::IS_DEFAULT;
+ if (!empty($pref['locked'])) {
+ $mask |= self::LOCKED;
+ }
+
+ $this->_scopes[$scope][$name] = array(
+ 'm' => $mask,
+ 'v' => $pref['value']
+ );
+
+ if (!empty($pref['hook'])) {
+ $this->_hooks[$scope][] = $name;
+ }
+ }
+ }
+
+ /**
+ * After preferences have been loaded, set any locked or empty
+ * preferences that have hooks to the result of the hook.
+ */
+ protected function _loadScopePost($scope)
+ {
+ if (empty($this->_hooks[$scope])) {
+ return;
+ }
+
+ foreach ($this->_hooks[$scope] as $name) {
+ if ($this->isLocked($name) ||
+ $this->isDefault($name) ||
+ empty($this->_scopes[$scope][$name]['v'])) {
+ try {
+ $val = Horde::callHook('prefs_init', array($name, $this->getUser()), $scope);
+ } catch (Horde_Exception_HookNotSet $e) {
+ continue;
+ }
+
+ $this->_scopes[$scope][$name]['v'] = $this->isDefault($name)
+ ? $val
+ : $this->convertToDriver($val);
+
+ if (!$this->_isLocked($name)) {
+ $this->setDirty($pref, true);
+ }
+ }
+ }
+ }
+
+}
/** Preference value is the application default.
* DEFAULT is a reserved PHP constant. */
- const PREFS_DEFAULT = 2;
+ const IS_DEFAULT = 2;
/**
* Caching object.
protected $_dirty = array();
/**
- * Hash holding preferences with hook functions defined.
- *
- * @var array
- */
- protected $_hooks = array();
-
- /**
* General library options.
*
* @var array
return false;
}
- $result = $this->_setValue($pref, $val, $convert);
-
- if ($result && $this->isDirty($pref)) {
- $this->_cache->store(array(
- $scope => array(
- $pref => $this->_scopes[$scope][$pref]
- )
- ));
-
- /* If this preference has a change hook, call it now. */
- try {
- Horde::callHook('prefs_change', array($pref), $scope);
- } catch (Horde_Exception_HookNotSet $e) {}
+ if (!$this->_setValue($pref, $val, $convert)) {
+ return false;
}
- return $result;
+ $this->_cache->store(array(
+ $scope => array(
+ $pref => $this->_scopes[$scope][$pref]
+ )
+ ));
+
+ return true;
}
/**
*/
public function setDefault($pref, $bool)
{
- $this->_setMask($pref, $bool, self::PREFS_DEFAULT);
+ $this->_setMask($pref, $bool, self::IS_DEFAULT);
}
/**
*/
public function isDefault($pref)
{
- return $this->_getMask($pref, self::PREFS_DEFAULT);
+ return $this->_getMask($pref, self::IS_DEFAULT);
}
/**
/**
* Load a specific preference scope.
+ *
+ * @param string $scope The scope to load.
*/
protected function _loadScope($scope)
{
// Basic initialization so _something_ is always set.
$this->_scopes[$scope] = array();
- $this->_setDefaults($scope);
-
// Now check the prefs cache for existing values.
try {
if (($cached = $this->_cache->get($scope)) !== false) {
}
} catch (Horde_Prefs_Exception $e) {}
+ $this->_loadScopePre($scope);
+
if (($prefs = $this->_storage->get($scope)) !== false) {
foreach ($prefs as $name => $val) {
if ($this->isDefault($name)) {
}
}
- $this->_callHooks($scope);
+ $this->_loadScopePost($scope);
/* Update the cache. */
$this->_cache->store(array($scope => $this->_scopes[$scope]));
}
/**
+ * Actions to perform before a scope is loaded from storage.
+ *
+ * @param string $scope The scope to load.
+ */
+ protected function _loadScopePre($scope)
+ {
+ }
+
+ /**
+ * Actions to perform after a scope is loaded from storage.
+ *
+ * @param string $scope The loaded scope.
+ */
+ protected function _loadScopePost($scope)
+ {
+ }
+
+ /**
* This function will be run at the end of every request as a shutdown
* function (registered by the constructor). All prefs with the
* dirty bit set will be saved to the storage backend.
: Horde_String::convertCharset($value, 'UTF-8', $this->getCharset());
}
- /**
- * Populates the $_scopes hash with new entries and externally
- * defined default values.
- *
- * @param string $scope The scope to load defaults for.
- */
- protected function _setDefaults($scope)
- {
- /* Read the configuration file. The $_prefs array is assumed to hold
- * the default values. */
- try {
- $result = Horde::loadConfiguration('prefs.php', array('_prefs'), $scope);
- if (empty($result) || !isset($result['_prefs'])) {
- return;
- }
- } catch (Horde_Exception $e) {
- return;
- }
-
- foreach ($result['_prefs'] as $name => $pref) {
- if (!isset($pref['value'])) {
- continue;
- }
-
- $name = str_replace('.', '_', $name);
-
- $mask = self::PREFS_DEFAULT;
- if (!empty($pref['locked'])) {
- $mask |= self::LOCKED;
- }
-
- $this->_scopes[$scope][$name] = array(
- 'm' => $mask,
- 'v' => $pref['value']
- );
-
- if (!empty($pref['hook'])) {
- $this->_hooks[$scope][$name] = $scope;
- }
- }
- }
-
- /**
- * After preferences have been loaded, set any locked or empty
- * preferences that have hooks to the result of the hook.
- *
- * @param string $scope The preferences scope to call hooks for.
- */
- protected function _callHooks($scope)
- {
- if (empty($this->_hooks[$scope])) {
- return;
- }
-
- foreach ($this->_hooks[$scope] as $name => $pref_scope) {
- if ($this->_scopes[$pref_scope][$name]['m'] & self::LOCKED ||
- empty($this->_scopes[$pref_scope][$name]['v']) ||
- $this->_scopes[$pref_scope][$name]['m'] & self::PREFS_DEFAULT) {
-
- try {
- $val = Horde::callHook('prefs_init', array($name, $this->getUser()), $scope);
- } catch (Horde_Exception_HookNotSet $e) {
- continue;
- }
-
- if ($this->_scopes[$pref_scope][$name]['m'] & self::PREFS_DEFAULT) {
- $this->_scopes[$pref_scope][$name]['v'] = $val;
- } else {
- $this->_scopes[$pref_scope][$name]['v'] = $this->convertToDriver($val);
- }
- if (!($this->_scopes[$pref_scope][$name]['m'] & self::LOCKED)) {
- $this->_scopes[$pref_scope][$name]['m'] |= self::DIRTY;
- }
- }
- }
- }
-
/* ArrayAccess methods. */
public function offsetExists($offset)