00001 <?php
00002
00016
00017
00018
00019 class PluginSettingsDAO extends DAO {
00020 function &_getCache($journalId, $pluginName) {
00021 static $settingCache;
00022 if (!isset($settingCache)) {
00023 $settingCache = array();
00024 }
00025 if (!isset($this->settingCache[$journalId])) {
00026 $this->settingCache[$journalId] = array();
00027 }
00028 if (!isset($this->settingCache[$journalId][$pluginName])) {
00029 import('cache.CacheManager');
00030 $cacheManager =& CacheManager::getManager();
00031 $this->settingCache[$journalId][$pluginName] = $cacheManager->getCache(
00032 'pluginSettings-' . $journalId, $pluginName,
00033 array($this, '_cacheMiss')
00034 );
00035 }
00036 return $this->settingCache[$journalId][$pluginName];
00037 }
00038
00045 function getSetting($journalId, $pluginName, $name) {
00046 $cache =& $this->_getCache($journalId, $pluginName);
00047 return $cache->get($name);
00048 }
00049
00050 function _cacheMiss(&$cache, $id) {
00051 $contextParts = explode('-', $cache->getContext());
00052 $journalId = array_pop($contextParts);
00053 $settings =& $this->getPluginSettings($journalId, $cache->getCacheId());
00054 if (!isset($settings[$id])) {
00055
00056 $cache->setCache($id, null);
00057 return null;
00058 }
00059 return $settings[$id];
00060 }
00061
00068 function &getPluginSettings($journalId, $pluginName) {
00069 $pluginSettings[$pluginName] = array();
00070
00071 $result = &$this->retrieve(
00072 'SELECT setting_name, setting_value, setting_type FROM plugin_settings WHERE plugin_name = ? AND journal_id = ?', array($pluginName, $journalId)
00073 );
00074
00075 if ($result->RecordCount() == 0) {
00076 $returner = null;
00077 $result->Close();
00078 return $returner;
00079
00080 } else {
00081 while (!$result->EOF) {
00082 $row = &$result->getRowAssoc(false);
00083 $value = $this->convertFromDB($row['setting_value'], $row['setting_type']);
00084 $pluginSettings[$pluginName][$row['setting_name']] = $value;
00085 $result->MoveNext();
00086 }
00087 $result->close();
00088 unset($result);
00089
00090 $cache =& $this->_getCache($journalId, $pluginName);
00091 $cache->setEntireCache($pluginSettings[$pluginName]);
00092
00093 return $pluginSettings[$pluginName];
00094 }
00095 }
00096
00105 function updateSetting($journalId, $pluginName, $name, $value, $type = null) {
00106 $cache =& $this->_getCache($journalId, $pluginName);
00107 $cache->setCache($name, $value);
00108
00109 $result = $this->retrieve(
00110 'SELECT COUNT(*) FROM plugin_settings WHERE plugin_name = ? AND setting_name = ? AND journal_id = ?',
00111 array($pluginName, $name, $journalId)
00112 );
00113
00114 $value = $this->convertToDB($value, $type);
00115 if ($result->fields[0] == 0) {
00116 $returner = $this->update(
00117 'INSERT INTO plugin_settings
00118 (plugin_name, journal_id, setting_name, setting_value, setting_type)
00119 VALUES
00120 (?, ?, ?, ?, ?)',
00121 array($pluginName, $journalId, $name, $value, $type)
00122 );
00123 } else {
00124 $returner = $this->update(
00125 'UPDATE plugin_settings SET
00126 setting_value = ?,
00127 setting_type = ?
00128 WHERE plugin_name = ? AND setting_name = ? AND journal_id = ?',
00129 array($value, $type, $pluginName, $name, $journalId)
00130 );
00131 }
00132
00133 $result->Close();
00134 unset($result);
00135
00136 return $returner;
00137 }
00138
00145 function deleteSetting($journalId, $pluginName, $name) {
00146 $cache =& $this->_getCache($pluginName);
00147 $cache->setCache($name, null);
00148
00149 return $this->update(
00150 'DELETE FROM plugin_settings WHERE plugin_name = ? AND setting_name = ? AND journal_id = ?',
00151 array($pluginName, $name, $journalId)
00152 );
00153 }
00154
00159 function deleteSettingsByPlugin($pluginName) {
00160 $cache =& $this->_getCache($pluginName);
00161 $cache->flush();
00162
00163 return $this->update(
00164 'DELETE FROM plugin_settings WHERE plugin_name = ?', $pluginName
00165 );
00166 }
00167
00172 function deleteSettingsByJournalId($journalId) {
00173 return $this->update(
00174 'DELETE FROM plugin_settings WHERE journal_id = ?', $journalId
00175 );
00176 }
00177
00184 function _performReplacement($rawInput, $paramArray = array()) {
00185 $value = preg_replace_callback('{{translate key="([^"]+)"}}', '_installer_plugin_regexp_callback', $rawInput);
00186 foreach ($paramArray as $pKey => $pValue) {
00187 $value = str_replace('{$' . $pKey . '}', $pValue, $value);
00188 }
00189 return $value;
00190 }
00191
00198 function &_buildObject (&$node, $paramArray = array()) {
00199 $value = array();
00200 foreach ($node->getChildren() as $element) {
00201 $key = $element->getAttribute('key');
00202 $childArray = &$element->getChildByName('array');
00203 if (isset($childArray)) {
00204 $content = $this->_buildObject($childArray, $paramArray);
00205 } else {
00206 $content = $this->_performReplacement($element->getValue(), $paramArray);
00207 }
00208 if (!empty($key)) {
00209 $key = $this->_performReplacement($key, $paramArray);
00210 $value[$key] = $content;
00211 } else $value[] = $content;
00212 }
00213 return $value;
00214 }
00215
00222 function installSettings($journalId, $pluginName, $filename, $paramArray = array()) {
00223 $xmlParser = &new XMLParser();
00224 $tree = $xmlParser->parse($filename);
00225
00226 if (!$tree) {
00227 $xmlParser->destroy();
00228 return false;
00229 }
00230
00231 foreach ($tree->getChildren() as $setting) {
00232 $nameNode = &$setting->getChildByName('name');
00233 $valueNode = &$setting->getChildByName('value');
00234
00235 if (isset($nameNode) && isset($valueNode)) {
00236 $type = $setting->getAttribute('type');
00237 $name = &$nameNode->getValue();
00238
00239 if ($type == 'object') {
00240 $arrayNode = &$valueNode->getChildByName('array');
00241 $value = $this->_buildObject($arrayNode, $paramArray);
00242 } else {
00243 $value = $this->_performReplacement($valueNode->getValue(), $paramArray);
00244 }
00245
00246
00247 $this->updateSetting($journalId, $pluginName, $name, $value, $type);
00248 }
00249 }
00250
00251 $xmlParser->destroy();
00252
00253 }
00254 }
00255
00259 function _installer_plugin_regexp_callback($matches) {
00260 return Locale::translate($matches[1]);
00261 }
00262
00263 ?>