ocs  2.3.6
About: OCS (Open Conference System) is a Web publishing tool that will create a complete Web presence for your scholarly conference.
  Fossies Dox: ocs-2.3.6.tar.gz  ("inofficial" and yet experimental doxygen-generated source code documentation)  

SiteSettingsDAO.inc.php
Go to the documentation of this file.
1 <?php
2 
18 class SiteSettingsDAO extends DAO {
19  function &_getCache() {
20  $settingCache =& Registry::get('siteSettingCache', true, null);
21  if ($settingCache === null) {
22  $cacheManager =& CacheManager::getManager();
23  $settingCache = $cacheManager->getFileCache(
24  'siteSettings', 'site',
25  array($this, '_cacheMiss')
26  );
27  }
28  return $settingCache;
29  }
30 
37  function &getSetting($name, $locale = null) {
38  $cache =& $this->_getCache();
39  $returner = $cache->get($name);
40  if ($locale !== null) {
41  if (!isset($returner[$locale]) || !is_array($returner)) {
42  unset($returner);
43  $returner = null;
44  return $returner;
45  }
46  return $returner[$locale];
47  }
48  return $returner;
49  }
50 
51  function _cacheMiss(&$cache, $id) {
52  $settings =& $this->getSiteSettings();
53  if (!isset($settings[$id])) {
54  $cache->setCache($id, null);
55  return null;
56  }
57  return $settings[$id];
58  }
59 
64  function &getSiteSettings() {
65  $siteSettings = array();
66 
67  $result =& $this->retrieve(
68  'SELECT setting_name, setting_value, setting_type, locale FROM site_settings'
69  );
70 
71  if ($result->RecordCount() == 0) {
72  $returner = null;
73  $result->Close();
74  return $returner;
75 
76  } else {
77  while (!$result->EOF) {
78  $row =& $result->getRowAssoc(false);
79  $value = $this->convertFromDB($row['setting_value'], $row['setting_type']);
80  if ($row['locale'] == '') $siteSettings[$row['setting_name']] = $value;
81  else $siteSettings[$row['setting_name']][$row['locale']] = $value;
82  $result->MoveNext();
83  }
84  $result->close();
85  unset($result);
86 
87  $cache =& $this->_getCache();
88  $cache->setEntireCache($siteSettings);
89 
90  return $siteSettings;
91  }
92  }
93 
102  function updateSetting($name, $value, $type = null, $isLocalized = false) {
103  $returner = null;
104  $cache =& $this->_getCache();
105  $cache->setCache($name, $value);
106 
107  $keyFields = array('setting_name', 'locale');
108 
109  if (!$isLocalized) {
110  $value = $this->convertToDB($value, $type);
111  $this->replace('site_settings',
112  array(
113  'setting_name' => $name,
114  'setting_value' => $value,
115  'setting_type' => $type,
116  'locale' => ''
117  ),
118  $keyFields
119  );
120  $returner = true;
121  } else {
122  if (is_array($value)) foreach ($value as $locale => $localeValue) {
123  $this->update('DELETE FROM site_settings WHERE setting_name = ? AND locale = ?', array($name, $locale));
124  if (empty($localeValue)) continue;
125  $type = null;
126  $returner = $this->update('INSERT INTO site_settings
127  (setting_name, setting_value, setting_type, locale)
128  VALUES (?, ?, ?, ?)',
129  array(
130  $name, $this->convertToDB($localeValue, $type), $type, $locale
131  )
132  );
133  }
134  }
135  return $returner;
136  }
137 
142  function deleteSetting($name, $locale = null) {
143  $cache =& $this->_getCache();
144  $cache->setCache($name, null);
145 
146  $params = array($name);
147  $sql = 'DELETE FROM site_settings WHERE setting_name = ?';
148  if ($locale !== null) {
149  $params[] = $locale;
150  $sql .= ' AND locale = ?';
151  }
152 
153  return $this->update($sql, $params);
154  }
155 
162  function _performReplacement($rawInput, $paramArray = array()) {
163  $value = preg_replace_callback('{{translate key="([^"]+)"}}', array(&$this, '_installer_regexp_callback'), $rawInput);
164  foreach ($paramArray as $pKey => $pValue) {
165  $value = str_replace('{$' . $pKey . '}', $pValue, $value);
166  }
167  return $value;
168  }
169 
176  function &_buildObject (&$node, $paramArray = array()) {
177  $value = array();
178  foreach ($node->getChildren() as $element) {
179  $key = $element->getAttribute('key');
180  $childArray =& $element->getChildByName('array');
181  if (isset($childArray)) {
182  $content = $this->_buildObject($childArray, $paramArray);
183  } else {
184  $content = $this->_performReplacement($element->getValue(), $paramArray);
185  }
186  if (!empty($key)) {
187  $key = $this->_performReplacement($key, $paramArray);
188  $value[$key] = $content;
189  } else $value[] = $content;
190  }
191  return $value;
192  }
193 
199  function installSettings($filename, $paramArray = array()) {
200  $xmlParser = new XMLParser();
201  $tree = $xmlParser->parse($filename);
202 
203  if (!$tree) {
204  $xmlParser->destroy();
205  return false;
206  }
207 
208  foreach ($tree->getChildren() as $setting) {
209  $nameNode =& $setting->getChildByName('name');
210  $valueNode =& $setting->getChildByName('value');
211 
212  if (isset($nameNode) && isset($valueNode)) {
213  $type = $setting->getAttribute('type');
214  $isLocalized = $setting->getAttribute('localized') == 'true';
215  $name =& $nameNode->getValue();
216 
217  if ($type == 'object') {
218  $arrayNode =& $valueNode->getChildByName('array');
219  $value = $this->_buildObject($arrayNode, $paramArray);
220  } else {
221  $value = $this->_performReplacement($valueNode->getValue(), $paramArray);
222  }
223 
224  // Replace translate calls with translated content
225  if ($isLocalized) $value = array(AppLocale::getLocale() => $value);
226  $this->updateSetting($name, $value, $type, $isLocalized);
227  }
228  }
229 
230  $xmlParser->destroy();
231 
232  }
233 
237  function _installer_regexp_callback($matches) {
238  return __($matches[1]);
239  }
240 }
241 
242 ?>
config TEMPLATE inc php
Definition: config.inc.php:4
Operations for retrieving and modifying objects from a database.
Definition: DAO.inc.php:30
installSettings($filename, $paramArray=array())
& retrieve($sql, $params=false, $callHooks=true)
Definition: DAO.inc.php:62
& _buildObject(&$node, $paramArray=array())
_performReplacement($rawInput, $paramArray=array())
$content
updateSetting($name, $value, $type=null, $isLocalized=false)
_cacheMiss(&$cache, $id)
__($key, $params=array(), $locale=null)
_installer_regexp_callback($matches)
convertFromDB($value, $type)
Definition: DAO.inc.php:296
Generic class for parsing an XML document into a data structure.
convertToDB($value, &$type)
Definition: DAO.inc.php:352
deleteSetting($name, $locale=null)
replace($table, $arrFields, $keyCols)
Definition: DAO.inc.php:214
& get($key, $createIfEmpty=false, $createWithDefault=null)
update($sql, $params=false, $callHooks=true, $dieOnError=true)
Definition: DAO.inc.php:186
& getSetting($name, $locale=null)