• Main Page
  • Modules
  • Classes
  • Files
  • File List

classes/press/PressSettingsDAO.inc.php

00001 <?php
00002 
00016 class PressSettingsDAO extends DAO {
00020    function PressSettingsDAO() {
00021       parent::DAO();
00022    }
00023 
00029    function &_getCache($pressId) {
00030       static $settingCache;
00031       if (!isset($settingCache)) {
00032          $settingCache = array();
00033       }
00034       if (!isset($settingCache[$pressId])) {
00035          $cacheManager =& CacheManager::getManager();
00036          $settingCache[$pressId] = $cacheManager->getCache(
00037             'pressSettings', $pressId,
00038             array($this, '_cacheMiss')
00039          );
00040       }
00041       return $settingCache[$pressId];
00042    }
00043 
00051    function &getSetting($pressId, $name, $locale = null) {
00052       $cache =& $this->_getCache($pressId);
00053       $returner = $cache->get($name);
00054       if ($locale !== null) {
00055          if (!isset($returner[$locale]) || !is_array($returner)) {
00056             unset($returner);
00057             $returner = null;
00058             return $returner;
00059          }
00060          return $returner[$locale];
00061       }
00062       return $returner;
00063    }
00064 
00071    function _cacheMiss(&$cache, $id) {
00072       $settings =& $this->getPressSettings($cache->getCacheId());
00073       if (!isset($settings[$id])) {
00074          $cache->setCache($id, null);
00075          return null;
00076       }
00077       return $settings[$id];
00078    }
00079 
00085    function &getPressSettings($pressId) {
00086       $pressSettings = array();
00087 
00088       $result =& $this->retrieve(
00089          'SELECT setting_name, setting_value, setting_type, locale FROM press_settings WHERE press_id = ?', $pressId
00090       );
00091 
00092       while (!$result->EOF) {
00093          $row =& $result->getRowAssoc(false);
00094          $value = $this->convertFromDB($row['setting_value'], $row['setting_type']);
00095          if ($row['locale'] == '') $pressSettings[$row['setting_name']] = $value;
00096          else $pressSettings[$row['setting_name']][$row['locale']] = $value;
00097          $result->MoveNext();
00098       }
00099       $result->Close();
00100       unset($result);
00101 
00102       $cache =& $this->_getCache($pressId);
00103       $cache->setEntireCache($pressSettings);
00104 
00105       return $pressSettings;
00106    }
00107 
00116    function updateSetting($pressId, $name, $value, $type = null, $isLocalized = false) {
00117       $cache =& $this->_getCache($pressId);
00118       $cache->setCache($name, $value);
00119 
00120       $keyFields = array('setting_name', 'locale', 'press_id');
00121 
00122       if (!$isLocalized) {
00123          $value = $this->convertToDB($value, $type);
00124          $this->replace('press_settings',
00125             array(
00126                'press_id' => $pressId,
00127                'setting_name' => $name,
00128                'setting_value' => $value,
00129                'setting_type' => $type,
00130                'locale' => ''
00131             ),
00132             $keyFields
00133          );
00134       } else {
00135          if (is_array($value)) foreach ($value as $locale => $localeValue) {
00136             $this->update('DELETE FROM press_settings WHERE press_id = ? AND setting_name = ? AND locale = ?', array($pressId, $name, $locale));
00137             if (empty($localeValue)) continue;
00138             $type = null;
00139             $this->update('INSERT INTO press_settings
00140                (press_id, setting_name, setting_value, setting_type, locale)
00141                VALUES (?, ?, ?, ?, ?)',
00142                array(
00143                   $pressId, $name, $this->convertToDB($localeValue, $type), $type, $locale
00144                )
00145             );
00146          }
00147       }
00148    }
00149 
00155    function deleteSetting($pressId, $name, $locale = null) {
00156       $cache =& $this->_getCache($pressId);
00157       $cache->setCache($name, null);
00158 
00159       $params = array($pressId, $name);
00160       $sql = 'DELETE FROM press_settings WHERE press_id = ? AND setting_name = ?';
00161       if ($locale !== null) {
00162          $params[] = $locale;
00163          $sql .= ' AND locale = ?';
00164       }
00165 
00166       return $this->update($sql, $params);
00167    }
00168 
00173    function deleteSettingsByPress($pressId) {
00174       $cache =& $this->_getCache($pressId);
00175       $cache->flush();
00176 
00177       return $this->update(
00178             'DELETE FROM press_settings WHERE press_id = ?', $pressId
00179       );
00180    }
00181 
00188    function _performReplacement($rawInput, $paramArray = array()) {
00189       $value = preg_replace_callback('{{translate key="([^"]+)"}}', '_installer_regexp_callback', $rawInput);
00190       foreach ($paramArray as $pKey => $pValue) {
00191          $value = str_replace('{$' . $pKey . '}', $pValue, $value);
00192       }
00193       return $value;
00194    }
00195 
00202    function &_buildObject (&$node, $paramArray = array()) {
00203       $value = array();
00204       foreach ($node->getChildren() as $element) {
00205          $key = $element->getAttribute('key');
00206          $childArray =& $element->getChildByName('array');
00207          if (isset($childArray)) {
00208             $content = $this->_buildObject($childArray, $paramArray);
00209          } else {
00210             $content = $this->_performReplacement($element->getValue(), $paramArray);
00211          }
00212          if (!empty($key)) {
00213             $key = $this->_performReplacement($key, $paramArray);
00214             $value[$key] = $content;
00215          } else $value[] = $content;
00216       }
00217       return $value;
00218    }
00219 
00226    function installSettings($pressId, $filename, $paramArray = array()) {
00227       $xmlParser = new XMLParser();
00228       $tree = $xmlParser->parse($filename);
00229 
00230       if (!$tree) {
00231          $xmlParser->destroy();
00232          return false;
00233       }
00234 
00235       foreach ($tree->getChildren() as $setting) {
00236          $nameNode =& $setting->getChildByName('name');
00237          $valueNode =& $setting->getChildByName('value');
00238 
00239          if (isset($nameNode) && isset($valueNode)) {
00240             $type = $setting->getAttribute('type');
00241             $isLocaleField = $setting->getAttribute('locale');
00242             $name =& $nameNode->getValue();
00243 
00244             if ($type == 'object') {
00245                $arrayNode =& $valueNode->getChildByName('array');
00246                $value = $this->_buildObject($arrayNode, $paramArray);
00247             } else {
00248                $value = $this->_performReplacement($valueNode->getValue(), $paramArray);
00249             }
00250 
00251             // Replace translate calls with translated content
00252             $this->updateSetting(
00253                $pressId,
00254                $name,
00255                $isLocaleField?array(AppLocale::getLocale() => $value):$value,
00256                $type,
00257                $isLocaleField
00258             );
00259          }
00260       }
00261 
00262       $xmlParser->destroy();
00263 
00264    }
00265 
00273    function _performLocalizedReplacement($rawInput, $paramArray = array(), $locale = null) {
00274       $value = preg_replace_callback(
00275          '{{translate key="([^"]+)"}}',
00276          // this only translates from mail locale file
00277          create_function(
00278             '$matches',
00279             '$locale = "' . $locale . '";'.'$localeFileName = AppLocale::getMainLocaleFilename($locale);' .
00280             '$localeFile = new LocaleFile($locale, $localeFileName);'.'return $localeFile->translate($matches[1]);'
00281          ),
00282          $rawInput
00283       );
00284 
00285       foreach ($paramArray as $pKey => $pValue) {
00286          $value = str_replace('{$' . $pKey . '}', $pValue, $value);
00287       }
00288       return $value;
00289    }
00290 
00298    function &_buildLocalizedObject (&$node, $paramArray = array(), $locale = null) {
00299       $value = array();
00300       foreach ($node->getChildren() as $element) {
00301          $key = $element->getAttribute('key');
00302          $childArray =& $element->getChildByName('array');
00303          if (isset($childArray)) {
00304             $content = $this->_buildLocalizedObject($childArray, $paramArray, $locale);
00305          } else {
00306             $content = $this->_performLocalizedReplacement($element->getValue(), $paramArray, $locale);
00307          }
00308          if (!empty($key)) {
00309             $key = $this->_performLocalizedReplacement($key, $paramArray, $locale);
00310             $value[$key] = $content;
00311          } else $value[] = $content;
00312       }
00313       return $value;
00314    }
00315 
00323    function reloadDefaultSetting($pressId, $filename, $settingName, $paramArray) {
00324       $xmlParser = new XMLParser();
00325       $tree = $xmlParser->parse($filename);
00326 
00327       if (!$tree) {
00328          $xmlParser->destroy();
00329          return false;
00330       }
00331 
00332       foreach ($tree->getChildren() as $setting) {
00333          $nameNode =& $setting->getChildByName('name');
00334          $valueNode =& $setting->getChildByName('value');
00335 
00336          if (isset($nameNode) && isset($valueNode)) {
00337 
00338             if ($nameNode->getValue() == $settingName) {
00339                $type = $setting->getAttribute('type');
00340                $isLocaleField = $setting->getAttribute('locale');
00341                $name =& $nameNode->getValue();
00342 
00343                if ($type == 'object') {
00344                   $arrayNode =& $valueNode->getChildByName('array');
00345                   $value = $this->_buildObject($arrayNode, $paramArray);
00346                } else {
00347                   $value = $this->_performReplacement($valueNode->getValue(), $paramArray);
00348                }
00349 
00350                $this->updateSetting(
00351                   $pressId,
00352                   $name,
00353                   $isLocaleField?array(AppLocale::getLocale() => $value):$value,
00354                   $type,
00355                   $isLocaleField
00356                );
00357 
00358                $xmlParser->destroy();
00359                return true;
00360             }
00361          }
00362       }
00363 
00364       $xmlParser->destroy();
00365 
00366    }
00367 
00375    function reloadLocalizedDefaultSettings($pressId, $filename, $paramArray, $locale) {
00376       $xmlParser = new XMLParser();
00377       $tree = $xmlParser->parse($filename);
00378 
00379       if (!$tree) {
00380          $xmlParser->destroy();
00381          return false;
00382       }
00383 
00384       foreach ($tree->getChildren() as $setting) {
00385          $nameNode =& $setting->getChildByName('name');
00386          $valueNode =& $setting->getChildByName('value');
00387 
00388          if (isset($nameNode) && isset($valueNode)) {
00389             $type = $setting->getAttribute('type');
00390             $isLocaleField = $setting->getAttribute('locale');
00391             $name =& $nameNode->getValue();
00392 
00393             //skip all settings that are not locale fields
00394             if (!$isLocaleField) continue;
00395 
00396             if ($type == 'object') {
00397                $arrayNode =& $valueNode->getChildByName('array');
00398                $value = $this->_buildLocalizedObject($arrayNode, $paramArray, $locale);
00399             } else {
00400                $value = $this->_performLocalizedReplacement($valueNode->getValue(), $paramArray, $locale);
00401             }
00402 
00403             // Replace translate calls with translated content
00404             $this->updateSetting(
00405                $pressId,
00406                $name,
00407                array($locale => $value),
00408                $type,
00409                true
00410             );
00411          }
00412       }
00413 
00414       $xmlParser->destroy();
00415 
00416    }
00417 }
00418 
00425 function _installer_regexp_callback($matches) {
00426    return __($matches[1]);
00427 }
00428 
00429 ?>

Generated on Mon Sep 17 2012 13:58:55 for Open Monograph Press by  doxygen 1.7.1