classes/conference/ConferenceSettingsDAO.inc.php

Go to the documentation of this file.
00001 <?php
00002 
00016 //$Id$
00017 
00018 import('db.SettingsDAO');
00019 class ConferenceSettingsDAO extends SettingsDAO {
00020    function &_getCache($conferenceId) {
00021       static $settingCache;
00022       if (!isset($settingCache)) {
00023          $settingCache = array();
00024       }
00025       if (!isset($settingCache[$conferenceId])) {
00026          $cacheManager =& CacheManager::getManager();
00027          $settingCache[$conferenceId] = $cacheManager->getCache(
00028             'conferenceSettings', $conferenceId,
00029             array($this, '_cacheMiss')
00030          );
00031       }
00032       return $settingCache[$conferenceId];
00033    }
00034 
00042    function &getSetting($conferenceId, $name, $locale = null) {
00043       $cache =& $this->_getCache($conferenceId);
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->getConferenceSettings($cache->getCacheId());
00058       if (!isset($settings[$id])) {
00059          // Make sure that even null values are cached
00060          $cache->setCache($id, null);
00061          return null;
00062       }
00063       return $settings[$id];
00064    }
00065 
00071    function &getConferenceSettings($conferenceId) {
00072       $conferenceSettings = array();
00073 
00074       $result =& $this->retrieve(
00075          'SELECT setting_name, setting_value, setting_type, locale FROM conference_settings WHERE conference_id = ?', $conferenceId
00076       );
00077 
00078       while (!$result->EOF) {
00079          $row =& $result->getRowAssoc(false);
00080          $value = $this->convertFromDB($row['setting_value'], $row['setting_type']);
00081          if ($row['locale'] == '') $conferenceSettings[$row['setting_name']] = $value;
00082          else $conferenceSettings[$row['setting_name']][$row['locale']] = $value;
00083          $result->MoveNext();
00084       }
00085       $result->Close();
00086       unset($result);
00087 
00088       $cache =& $this->_getCache($conferenceId);
00089       $cache->setEntireCache($conferenceSettings);
00090 
00091       return $conferenceSettings;
00092    }
00093 
00102    function updateSetting($conferenceId, $name, $value, $type = null, $isLocalized = false) {
00103       $cache =& $this->_getCache($conferenceId);
00104       $cache->setCache($name, $value);
00105 
00106       $keyFields = array('setting_name', 'locale', 'conference_id');
00107 
00108       if (!$isLocalized) {
00109          $value = $this->convertToDB($value, $type);
00110          $this->replace('conference_settings',
00111             array(
00112                'conference_id' => $conferenceId,
00113                'setting_name' => $name,
00114                'setting_value' => $value,
00115                'setting_type' => $type,
00116                'locale' => ''
00117             ),
00118             $keyFields
00119          );
00120       } else {
00121          if (is_array($value)) foreach ($value as $locale => $localeValue) {
00122             $this->update('DELETE FROM conference_settings WHERE conference_id = ? AND setting_name = ? AND locale = ?', array($conferenceId, $name, $locale));
00123             if (empty($localeValue)) continue;
00124             $type = null;
00125             $this->update('INSERT INTO conference_settings
00126                (conference_id, setting_name, setting_value, setting_type, locale)
00127                VALUES (?, ?, ?, ?, ?)',
00128                array(
00129                   $conferenceId, $name, $this->convertToDB($localeValue, $type), $type, $locale
00130                )
00131             );
00132          }
00133       }
00134    }
00135 
00142    function deleteSetting($conferenceId, $name, $locale = null) {
00143       $cache =& $this->_getCache($conferenceId);
00144       $cache->setCache($name, null);
00145 
00146       $params = array($conferenceId, $name);
00147       $sql = 'DELETE FROM conference_settings WHERE conference_id = ? AND setting_name = ?';
00148       if ($locale !== null) {
00149          $params[] = $locale;
00150          $sql .= ' AND locale = ?';
00151       }
00152 
00153       return $this->update($sql, $params);
00154    }
00155 
00160    function deleteSettingsByConference($conferenceId) {
00161       $cache =& $this->_getCache($conferenceId);
00162       $cache->flush();
00163 
00164       return $this->update(
00165             'DELETE FROM conference_settings WHERE conference_id = ?', $conferenceId
00166       );
00167    }
00168 
00175    function _performReplacement($rawInput, $paramArray = array()) {
00176       $value = preg_replace_callback('{{translate key="([^"]+)"}}', array(&$this, '_installer_regexp_callback'), $rawInput);
00177       foreach ($paramArray as $pKey => $pValue) {
00178          $value = str_replace('{$' . $pKey . '}', $pValue, $value);
00179       }
00180       return $value;
00181    }
00182 
00189    function &_buildObject (&$node, $paramArray = array()) {
00190       $value = array();
00191       foreach ($node->getChildren() as $element) {
00192          $key = $element->getAttribute('key');
00193          $childArray =& $element->getChildByName('array');
00194          if (isset($childArray)) {
00195             $content = $this->_buildObject($childArray, $paramArray);
00196          } else {
00197             $content = $this->_performReplacement($element->getValue(), $paramArray);
00198          }
00199          if (!empty($key)) {
00200             $key = $this->_performReplacement($key, $paramArray);
00201             $value[$key] = $content;
00202          } else $value[] = $content;
00203       }
00204       return $value;
00205    }
00206 
00213    function installSettings($conferenceId, $filename, $paramArray = array()) {
00214       $xmlParser = new XMLParser();
00215       $tree = $xmlParser->parse($filename);
00216 
00217       if (!$tree) {
00218          $xmlParser->destroy();
00219          return false;
00220       }
00221 
00222       foreach ($tree->getChildren() as $setting) {
00223          $nameNode =& $setting->getChildByName('name');
00224          $valueNode =& $setting->getChildByName('value');
00225 
00226          if (isset($nameNode) && isset($valueNode)) {
00227             $type = $setting->getAttribute('type');
00228             $isLocaleField = $setting->getAttribute('locale');
00229             $name =& $nameNode->getValue();
00230 
00231             if ($type == 'object') {
00232                $arrayNode =& $valueNode->getChildByName('array');
00233                $value = $this->_buildObject($arrayNode, $paramArray);
00234             } else {
00235                $value = $this->_performReplacement($valueNode->getValue(), $paramArray);
00236             }
00237 
00238             // Replace translate calls with translated content
00239             $this->updateSetting(
00240                $conferenceId,
00241                $name,
00242                $isLocaleField?array(AppLocale::getLocale() => $value):$value,
00243                $type,
00244                $isLocaleField
00245             );
00246          }
00247       }
00248 
00249       $xmlParser->destroy();
00250 
00251    }
00252 
00260    function _performLocalizedReplacement($rawInput, $paramArray = array(), $locale = null) {
00261       preg_match('{{translate key="([^"]+)"}}', $rawInput, $matches);
00262       if ( isset($matches[1]) ) {
00263          AppLocale::requireComponents(array(LOCALE_COMPONENT_OCS_DEFAULT, LOCALE_COMPONENT_OCS_MANAGER), $locale);
00264          return __($matches[1], $paramArray, $locale);
00265       }
00266 
00267       return $rawInput;
00268    }
00269 
00277    function &_buildLocalizedObject (&$node, $paramArray = array(), $locale = null) {
00278       $value = array();
00279       foreach ($node->getChildren() as $element) {
00280          $key = $element->getAttribute('key');
00281          $childArray =& $element->getChildByName('array');
00282          if (isset($childArray)) {
00283             $content = $this->_buildLocalizedObject($childArray, $paramArray, $locale);
00284          } else {
00285             $content = $this->_performLocalizedReplacement($element->getValue(), $paramArray, $locale);
00286          }
00287          if (!empty($key)) {
00288             $key = $this->_performLocalizedReplacement($key, $paramArray, $locale);
00289             $value[$key] = $content;
00290          } else $value[] = $content;
00291       }
00292       return $value;
00293    }
00294 
00302    function reloadLocalizedDefaultSettings($conferenceId, $filename, $paramArray, $locale) {
00303       $xmlParser = new XMLParser();
00304       $tree = $xmlParser->parse($filename);
00305 
00306       if (!$tree) {
00307          $xmlParser->destroy();
00308          return false;
00309       }
00310 
00311       foreach ($tree->getChildren() as $setting) {
00312          $nameNode =& $setting->getChildByName('name');
00313          $valueNode =& $setting->getChildByName('value');
00314 
00315          if (isset($nameNode) && isset($valueNode)) {
00316             $type = $setting->getAttribute('type');
00317             $isLocaleField = $setting->getAttribute('locale');
00318             $name =& $nameNode->getValue();
00319 
00320             //skip all settings that are not locale fields
00321             if (!$isLocaleField) continue;
00322 
00323             if ($type == 'object') {
00324                $arrayNode =& $valueNode->getChildByName('array');
00325                $value = $this->_buildLocalizedObject($arrayNode, $paramArray, $locale);
00326             } else {
00327                $value = $this->_performLocalizedReplacement($valueNode->getValue(), $paramArray, $locale);
00328             }
00329 
00330             // Replace translate calls with translated content
00331             $this->updateSetting(
00332                $conferenceId,
00333                $name,
00334                array($locale => $value),
00335                $type,
00336                true
00337             );
00338          }
00339       }
00340 
00341       $xmlParser->destroy();
00342 
00343    }
00344 
00348    function _installer_regexp_callback($matches) {
00349       return __($matches[1]);
00350    }
00351 
00352 }
00353 
00354 ?>

Generated on 25 Jul 2013 for Open Conference Systems by  doxygen 1.4.7