00001 <?php
00002
00015
00016
00017
00018 class JournalSettingsDAO extends DAO {
00019 function &_getCache($journalId) {
00020 static $settingCache;
00021 if (!isset($settingCache)) {
00022 $settingCache = array();
00023 }
00024 if (!isset($settingCache[$journalId])) {
00025 import('cache.CacheManager');
00026 $cacheManager =& CacheManager::getManager();
00027 $settingCache[$journalId] = $cacheManager->getCache(
00028 'journalSettings', $journalId,
00029 array($this, '_cacheMiss')
00030 );
00031 }
00032 return $settingCache[$journalId];
00033 }
00034
00042 function &getSetting($journalId, $name, $locale = null) {
00043 $cache =& $this->_getCache($journalId);
00044 $returner = $cache->get($name);
00045 if ($locale !== null) {
00046 if (!isset($returner[$locale]) || !is_array($returner)) {
00047 unset($returner);
00048 $returner = null;
00049 return $returner;
00050 }
00051 return $returner[$locale];
00052 }
00053 return $returner;
00054 }
00055
00056 function _cacheMiss(&$cache, $id) {
00057 $settings =& $this->getJournalSettings($cache->getCacheId());
00058 if (!isset($settings[$id])) {
00059
00060 $cache->setCache($id, null);
00061 return null;
00062 }
00063 return $settings[$id];
00064 }
00065
00071 function &getJournalSettings($journalId) {
00072 $journalSettings = array();
00073
00074 $result = &$this->retrieve(
00075 'SELECT setting_name, setting_value, setting_type, locale FROM journal_settings WHERE journal_id = ?', $journalId
00076 );
00077
00078 if ($result->RecordCount() == 0) {
00079 $returner = null;
00080 $result->Close();
00081 return $returner;
00082
00083 } else {
00084 while (!$result->EOF) {
00085 $row = &$result->getRowAssoc(false);
00086 $value = $this->convertFromDB($row['setting_value'], $row['setting_type']);
00087 if ($row['locale'] == '') $journalSettings[$row['setting_name']] = $value;
00088 else $journalSettings[$row['setting_name']][$row['locale']] = $value;
00089 $result->MoveNext();
00090 }
00091 $result->close();
00092 unset($result);
00093
00094 $cache =& $this->_getCache($journalId);
00095 $cache->setEntireCache($journalSettings);
00096
00097 return $journalSettings;
00098 }
00099 }
00100
00109 function updateSetting($journalId, $name, $value, $type = null, $isLocalized = false) {
00110 $cache =& $this->_getCache($journalId);
00111 $cache->setCache($name, $value);
00112
00113 $keyFields = array('setting_name', 'locale', 'journal_id');
00114
00115 if (!$isLocalized) {
00116 $value = $this->convertToDB($value, $type);
00117 $this->replace('journal_settings',
00118 array(
00119 'journal_id' => $journalId,
00120 'setting_name' => $name,
00121 'setting_value' => $value,
00122 'setting_type' => $type,
00123 'locale' => ''
00124 ),
00125 $keyFields
00126 );
00127 } else {
00128 if (is_array($value)) foreach ($value as $locale => $localeValue) {
00129 $this->update('DELETE FROM journal_settings WHERE journal_id = ? AND setting_name = ? AND locale = ?', array($journalId, $name, $locale));
00130 if (empty($localeValue)) continue;
00131 $type = null;
00132 $this->update('INSERT INTO journal_settings
00133 (journal_id, setting_name, setting_value, setting_type, locale)
00134 VALUES (?, ?, ?, ?, ?)',
00135 array(
00136 $journalId, $name, $this->convertToDB($localeValue, $type), $type, $locale
00137 )
00138 );
00139 }
00140 }
00141 }
00142
00148 function deleteSetting($journalId, $name, $locale = null) {
00149 $cache =& $this->_getCache($journalId);
00150 $cache->setCache($name, null);
00151
00152 $params = array($journalId, $name);
00153 $sql = 'DELETE FROM journal_settings WHERE journal_id = ? AND setting_name = ?';
00154 if ($locale !== null) {
00155 $params[] = $locale;
00156 $sql .= ' AND locale = ?';
00157 }
00158
00159 return $this->update($sql, $params);
00160 }
00161
00166 function deleteSettingsByJournal($journalId) {
00167 $cache =& $this->_getCache($journalId);
00168 $cache->flush();
00169
00170 return $this->update(
00171 'DELETE FROM journal_settings WHERE journal_id = ?', $journalId
00172 );
00173 }
00174
00181 function _performReplacement($rawInput, $paramArray = array()) {
00182 $value = preg_replace_callback('{{translate key="([^"]+)"}}', '_installer_regexp_callback', $rawInput);
00183 foreach ($paramArray as $pKey => $pValue) {
00184 $value = str_replace('{$' . $pKey . '}', $pValue, $value);
00185 }
00186 return $value;
00187 }
00188
00195 function &_buildObject (&$node, $paramArray = array()) {
00196 $value = array();
00197 foreach ($node->getChildren() as $element) {
00198 $key = $element->getAttribute('key');
00199 $childArray = &$element->getChildByName('array');
00200 if (isset($childArray)) {
00201 $content = $this->_buildObject($childArray, $paramArray);
00202 } else {
00203 $content = $this->_performReplacement($element->getValue(), $paramArray);
00204 }
00205 if (!empty($key)) {
00206 $key = $this->_performReplacement($key, $paramArray);
00207 $value[$key] = $content;
00208 } else $value[] = $content;
00209 }
00210 return $value;
00211 }
00212
00219 function installSettings($journalId, $filename, $paramArray = array()) {
00220 $xmlParser = &new XMLParser();
00221 $tree = $xmlParser->parse($filename);
00222
00223 if (!$tree) {
00224 $xmlParser->destroy();
00225 return false;
00226 }
00227
00228 foreach ($tree->getChildren() as $setting) {
00229 $nameNode = &$setting->getChildByName('name');
00230 $valueNode = &$setting->getChildByName('value');
00231
00232 if (isset($nameNode) && isset($valueNode)) {
00233 $type = $setting->getAttribute('type');
00234 $isLocaleField = $setting->getAttribute('locale');
00235 $name = &$nameNode->getValue();
00236
00237 if ($type == 'object') {
00238 $arrayNode = &$valueNode->getChildByName('array');
00239 $value = $this->_buildObject($arrayNode, $paramArray);
00240 } else {
00241 $value = $this->_performReplacement($valueNode->getValue(), $paramArray);
00242 }
00243
00244
00245 $this->updateSetting(
00246 $journalId,
00247 $name,
00248 $isLocaleField?array(Locale::getLocale() => $value):$value,
00249 $type,
00250 $isLocaleField
00251 );
00252 }
00253 }
00254
00255 $xmlParser->destroy();
00256
00257 }
00258
00266 function _performLocalizedReplacement($rawInput, $paramArray = array(), $locale = null) {
00267 $value = preg_replace_callback('{{translate key="([^"]+)"}}',
00268
00269 create_function('$matches',
00270 '$locale = "' . $locale . '";'.
00271 '$localeFileName = Locale::getMainLocaleFilename($locale);'.
00272 '$localeFile =& new LocaleFile($locale, $localeFileName);'.
00273 'return $localeFile->translate($matches[1]);'
00274 ),
00275 $rawInput);
00276 foreach ($paramArray as $pKey => $pValue) {
00277 $value = str_replace('{$' . $pKey . '}', $pValue, $value);
00278 }
00279 return $value;
00280 }
00281
00289 function &_buildLocalizedObject (&$node, $paramArray = array(), $locale = null) {
00290 $value = array();
00291 foreach ($node->getChildren() as $element) {
00292 $key = $element->getAttribute('key');
00293 $childArray = &$element->getChildByName('array');
00294 if (isset($childArray)) {
00295 $content = $this->_buildLocalizedObject($childArray, $paramArray, $locale);
00296 } else {
00297 $content = $this->_performLocalizedReplacement($element->getValue(), $paramArray, $locale);
00298 }
00299 if (!empty($key)) {
00300 $key = $this->_performLocalizedReplacement($key, $paramArray, $locale);
00301 $value[$key] = $content;
00302 } else $value[] = $content;
00303 }
00304 return $value;
00305 }
00306
00314 function reloadLocalizedDefaultSettings($journalId, $filename, $paramArray, $locale) {
00315 $xmlParser = &new XMLParser();
00316 $tree = $xmlParser->parse($filename);
00317
00318 if (!$tree) {
00319 $xmlParser->destroy();
00320 return false;
00321 }
00322
00323 foreach ($tree->getChildren() as $setting) {
00324 $nameNode = &$setting->getChildByName('name');
00325 $valueNode = &$setting->getChildByName('value');
00326
00327 if (isset($nameNode) && isset($valueNode)) {
00328 $type = $setting->getAttribute('type');
00329 $isLocaleField = $setting->getAttribute('locale');
00330 $name = &$nameNode->getValue();
00331
00332
00333 if (!$isLocaleField) continue;
00334
00335 if ($type == 'object') {
00336 $arrayNode = &$valueNode->getChildByName('array');
00337 $value = $this->_buildLocalizedObject($arrayNode, $paramArray, $locale);
00338 } else {
00339 $value = $this->_performLocalizedReplacement($valueNode->getValue(), $paramArray, $locale);
00340 }
00341
00342
00343 $this->updateSetting(
00344 $journalId,
00345 $name,
00346 array($locale => $value),
00347 $type,
00348 true
00349 );
00350 }
00351 }
00352
00353 $xmlParser->destroy();
00354
00355 }
00356 }
00357
00361 function _installer_regexp_callback($matches) {
00362 return Locale::translate($matches[1]);
00363 }
00364
00365 ?>