4 * @file classes/plugins/PluginSettingsDAO.inc.php
6 * Copyright (c) 2003-2008 John Willinsky
7 * Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
9 * @class PluginSettingsDAO
13 * @brief Operations for retrieving and modifying plugin settings.
16 // $Id: PluginSettingsDAO.inc.php,v 1.3 2009/09/22 19:22:09 asmecher Exp $
19 class PluginSettingsDAO
extends DAO
{
20 function &_getCache($pressId, $pluginName) {
22 if (!isset($settingCache)) {
23 $settingCache = array();
25 if (!isset($this->settingCache
[$pressId])) {
26 $this->settingCache
[$pressId] = array();
28 if (!isset($this->settingCache
[$pressId][$pluginName])) {
29 import('cache.CacheManager');
30 $cacheManager =& CacheManager
::getManager();
31 $this->settingCache
[$pressId][$pluginName] = $cacheManager->getCache(
32 'pluginSettings-' . $pressId, $pluginName,
33 array($this, '_cacheMiss')
36 return $this->settingCache
[$pressId][$pluginName];
40 * Retrieve a plugin setting value.
41 * @param $pluginName string
45 function getSetting($pressId, $pluginName, $name) {
46 $cache =& $this->_getCache($pressId, $pluginName);
47 return $cache->get($name);
50 function _cacheMiss(&$cache, $id) {
51 $contextParts = explode('-', $cache->getContext());
52 $pressId = array_pop($contextParts);
53 $settings =& $this->getPluginSettings($pressId, $cache->getCacheId());
54 if (!isset($settings[$id])) {
57 return $settings[$id];
61 * Retrieve and cache all settings for a plugin.
63 * @param $pluginName string
66 function &getPluginSettings($pressId, $pluginName) {
67 $pluginSettings = array();
69 $result =& $this->retrieve(
70 'SELECT setting_name, setting_value, setting_type FROM plugin_settings WHERE plugin_name = ? AND press_id = ?', array($pluginName, $pressId)
73 while (!$result->EOF
) {
74 $row =& $result->getRowAssoc(false);
75 $pluginSettings[$row['setting_name']] = $this->convertFromDB($row['setting_value'], $row['setting_type']);
81 $cache =& $this->_getCache($pressId, $pluginName);
82 $cache->setEntireCache($pluginSettings);
84 return $pluginSettings;
88 * Add/update a plugin setting.
90 * @param $pluginName string
93 * @param $type string data type of the setting. If omitted, type will be guessed
95 function updateSetting($pressId, $pluginName, $name, $value, $type = null) {
96 $cache =& $this->_getCache($pressId, $pluginName);
97 $cache->setCache($name, $value);
99 $result = $this->retrieve(
100 'SELECT COUNT(*) FROM plugin_settings WHERE plugin_name = ? AND setting_name = ? AND press_id = ?',
101 array($pluginName, $name, $pressId)
104 $value = $this->convertToDB($value, $type);
105 if ($result->fields
[0] == 0) {
106 $returner = $this->update(
107 'INSERT INTO plugin_settings
108 (plugin_name, press_id, setting_name, setting_value, setting_type)
111 array($pluginName, $pressId, $name, $value, $type)
114 $returner = $this->update(
115 'UPDATE plugin_settings SET
118 WHERE plugin_name = ? AND setting_name = ? AND press_id = ?',
119 array($value, $type, $pluginName, $name, $pressId)
130 * Delete a plugin setting.
131 * @param $pressId int
132 * @param $pluginName int
133 * @param $name string
135 function deleteSetting($pressId, $pluginName, $name) {
136 $cache =& $this->_getCache($pressId, $pluginName);
137 $cache->setCache($name, null);
139 return $this->update(
140 'DELETE FROM plugin_settings WHERE plugin_name = ? AND setting_name = ? AND press_id = ?',
141 array($pluginName, $name, $pressId)
146 * Delete all settings for a plugin.
147 * @param $pressId int
148 * @param $pluginName string
150 function deleteSettingsByPlugin($pressId, $pluginName) {
151 $cache =& $this->_getCache($pressId, $pluginName);
154 return $this->update(
155 'DELETE FROM plugin_settings WHERE press_id = ? AND plugin_name = ?',
156 array($pressId, $pluginName)
161 * Delete all settings for a press.
162 * @param $pressId int
164 function deleteSettingsByPressId($pressId) {
165 return $this->update(
166 'DELETE FROM plugin_settings WHERE press_id = ?', $pressId
171 * Used internally by installSettings to perform variable and translation replacements.
172 * @param $rawInput string contains text including variable and/or translate replacements.
173 * @param $paramArray array contains variables for replacement
176 function _performReplacement($rawInput, $paramArray = array()) {
177 $value = preg_replace_callback('{{translate key="([^"]+)"}}', '_installer_plugin_regexp_callback', $rawInput);
178 foreach ($paramArray as $pKey => $pValue) {
179 $value = str_replace('{$' . $pKey . '}', $pValue, $value);
185 * Used internally by installSettings to recursively build nested arrays.
186 * Deals with translation and variable replacement calls.
187 * @param $node object XMLNode <array> tag
188 * @param $paramArray array Parameters to be replaced in key/value contents
190 function &_buildObject (&$node, $paramArray = array()) {
192 foreach ($node->getChildren() as $element) {
193 $key = $element->getAttribute('key');
194 $childArray =& $element->getChildByName('array');
195 if (isset($childArray)) {
196 $content = $this->_buildObject($childArray, $paramArray);
198 $content = $this->_performReplacement($element->getValue(), $paramArray);
201 $key = $this->_performReplacement($key, $paramArray);
202 $value[$key] = $content;
203 } else $value[] = $content;
209 * Install plugin settings from an XML file.
210 * @param $pluginName name of plugin for settings to apply to
211 * @param $filename string Name of XML file to parse and install
212 * @param $paramArray array Optional parameters for variable replacement in settings
214 function installSettings($pressId, $pluginName, $filename, $paramArray = array()) {
215 $xmlParser = new XMLParser();
216 $tree = $xmlParser->parse($filename);
219 $xmlParser->destroy();
223 foreach ($tree->getChildren() as $setting) {
224 $nameNode =& $setting->getChildByName('name');
225 $valueNode =& $setting->getChildByName('value');
227 if (isset($nameNode) && isset($valueNode)) {
228 $type = $setting->getAttribute('type');
229 $name =& $nameNode->getValue();
231 if ($type == 'object') {
232 $arrayNode =& $valueNode->getChildByName('array');
233 $value = $this->_buildObject($arrayNode, $paramArray);
235 $value = $this->_performReplacement($valueNode->getValue(), $paramArray);
238 // Replace translate calls with translated content
239 $this->updateSetting($pressId, $pluginName, $name, $value, $type);
243 $xmlParser->destroy();
249 * Used internally by plugin setting installation code to perform translation function.
251 function _installer_plugin_regexp_callback($matches) {
252 return Locale
::translate($matches[1]);