Open Journal Systems  3.0.0
 All Classes Namespaces Functions Variables Groups Pages
SettingsDAO.inc.php
1 <?php
2 
16 class SettingsDAO extends DAO {
20  function SettingsDAO() {
21  parent::DAO();
22  }
23 
29  function &getSettings($id) {
30  $settings = array();
31 
32  $result = $this->retrieve(
33  'SELECT setting_name, setting_value, setting_type, locale FROM ' . $this->_getTableName() . ' WHERE ' . $this->_getPrimaryKeyColumn() . ' = ?',
34  (int) $id
35  );
36 
37  while (!$result->EOF) {
38  $row = $result->getRowAssoc(false);
39  $value = $this->convertFromDB($row['setting_value'], $row['setting_type']);
40  if ($row['locale'] == '') $settings[$row['setting_name']] = $value;
41  else $settings[$row['setting_name']][$row['locale']] = $value;
42  $result->MoveNext();
43  }
44  $result->Close();
45 
46  $cache = $this->_getCache($id);
47  $cache->setEntireCache($settings);
48 
49  return $settings;
50  }
51 
59  function &getSetting($id, $name, $locale = null) {
60  $cache = $this->_getCache($id);
61  $returner = $cache->get($name);
62  if ($locale !== null) {
63  if (!isset($returner[$locale]) || !is_array($returner)) {
64  unset($returner);
65  $returner = null;
66  return $returner;
67  }
68  return $returner[$locale];
69  }
70  return $returner;
71  }
72 
79  function _cacheMiss($cache, $id) {
80  $settings = $this->getSettings($cache->getCacheId());
81  if (!isset($settings[$id])) {
82  $cache->setCache($id, null);
83  return null;
84  }
85  return $settings[$id];
86  }
87 
94  function _performReplacement($rawInput, $paramArray = array()) {
95  $value = preg_replace_callback('{{translate key="([^"]+)"}}', array($this, '_installer_regexp_callback'), $rawInput);
96  foreach ($paramArray as $pKey => $pValue) {
97  $value = str_replace('{$' . $pKey . '}', $pValue, $value);
98  }
99  return $value;
100  }
101 
108  function _buildObject (&$node, $paramArray = array()) {
109  $value = array();
110  foreach ($node->getChildren() as $element) {
111  $key = $element->getAttribute('key');
112  $childArray = $element->getChildByName('array');
113  if (isset($childArray)) {
114  $content = $this->_buildObject($childArray, $paramArray);
115  } else {
116  $content = $this->_performReplacement($element->getValue(), $paramArray);
117  }
118  if (!empty($key)) {
119  $key = $this->_performReplacement($key, $paramArray);
120  $value[$key] = $content;
121  } else $value[] = $content;
122  }
123  return $value;
124  }
125 
132  function installSettings($id, $filename, $paramArray = array()) {
133  $xmlParser = new XMLParser();
134  $tree = $xmlParser->parse($filename);
135 
136  if (!$tree) {
137  $xmlParser->destroy();
138  return false;
139  }
140 
141  foreach ($tree->getChildren() as $setting) {
142  $nameNode = $setting->getChildByName('name');
143  $valueNode = $setting->getChildByName('value');
144 
145  if (isset($nameNode) && isset($valueNode)) {
146  $type = $setting->getAttribute('type');
147  $isLocaleField = $setting->getAttribute('locale');
148  $name = $nameNode->getValue();
149 
150  if ($type == 'object') {
151  $arrayNode = $valueNode->getChildByName('array');
152  $value = $this->_buildObject($arrayNode, $paramArray);
153  } else {
154  $value = $this->_performReplacement($valueNode->getValue(), $paramArray);
155  }
156 
157  // Replace translate calls with translated content
158  $this->updateSetting(
159  $id,
160  $name,
161  $isLocaleField?array(AppLocale::getLocale() => $value):$value,
162  $type,
163  $isLocaleField
164  );
165  }
166  }
167 
168  $xmlParser->destroy();
169 
170  }
171 
175  function _installer_regexp_callback($matches) {
176  return __($matches[1]);
177  }
178 
187  function updateSetting($id, $name, $value, $type = null, $isLocalized = false) {
188  $cache = $this->_getCache($id);
189  $cache->setCache($name, $value);
190 
191  $keyFields = array('setting_name', 'locale', $this->_getPrimaryKeyColumn());
192 
193  if (!$isLocalized) {
194  $value = $this->convertToDB($value, $type);
195  $this->replace($this->_getTableName(),
196  array(
197  $this->_getPrimaryKeyColumn() => $id,
198  'setting_name' => $name,
199  'setting_value' => $value,
200  'setting_type' => $type,
201  'locale' => ''
202  ),
203  $keyFields
204  );
205  } else {
206  if (is_array($value)) foreach ($value as $locale => $localeValue) {
207  $this->update('DELETE FROM ' . $this->_getTableName() . ' WHERE ' . $this->_getPrimaryKeyColumn() . ' = ? AND setting_name = ? AND locale = ?', array($id, $name, $locale));
208  if (empty($localeValue)) continue;
209  $type = null;
210  $this->update('INSERT INTO ' . $this->_getTableName() . '
211  (' . $this->_getPrimaryKeyColumn() . ', setting_name, setting_value, setting_type, locale)
212  VALUES (?, ?, ?, ?, ?)',
213  array(
214  $id, $name, $this->convertToDB($localeValue, $type), $type, $locale
215  )
216  );
217  }
218  }
219  }
220 
226  function deleteSetting($id, $name, $locale = null) {
227  $cache = $this->_getCache($id);
228  $cache->setCache($name, null);
229 
230  $params = array($id, $name);
231  $sql = 'DELETE FROM ' . $this->_getTableName() . ' WHERE ' . $this->_getPrimaryKeyColumn() . ' = ? AND setting_name = ?';
232  if ($locale !== null) {
233  $params[] = $locale;
234  $sql .= ' AND locale = ?';
235  }
236 
237  return $this->update($sql, $params);
238  }
239 
244  function deleteById($id) {
245  $cache = $this->_getCache($id);
246  $cache->flush();
247 
248  return $this->update(
249  'DELETE FROM ' . $this->_getTableName() . ' WHERE ' . $this->_getPrimaryKeyColumn() . ' = ?',
250  (int) $id
251  );
252  }
253 
261  function _performLocalizedReplacement($rawInput, $paramArray = array(), $locale = null) {
262  $value = preg_replace_callback(
263  '{{translate key="([^"]+)"}}',
264  // this only translates from mail locale file
265  create_function(
266  '$matches',
267  '$locale = "' . $locale . '";'.'$localeFileName = AppLocale::getMainLocaleFilename($locale);' .
268  '$localeFile = new LocaleFile($locale, $localeFileName);'.'return $localeFile->translate($matches[1]);'
269  ),
270  $rawInput
271  );
272 
273  foreach ($paramArray as $pKey => $pValue) {
274  $value = str_replace('{$' . $pKey . '}', $pValue, $value);
275  }
276  return $value;
277  }
278 
286  function &_buildLocalizedObject (&$node, $paramArray = array(), $locale = null) {
287  $value = array();
288  foreach ($node->getChildren() as $element) {
289  $key = $element->getAttribute('key');
290  $childArray = $element->getChildByName('array');
291  if (isset($childArray)) {
292  $content = $this->_buildLocalizedObject($childArray, $paramArray, $locale);
293  } else {
294  $content = $this->_performLocalizedReplacement($element->getValue(), $paramArray, $locale);
295  }
296  if (!empty($key)) {
297  $key = $this->_performLocalizedReplacement($key, $paramArray, $locale);
298  $value[$key] = $content;
299  } else $value[] = $content;
300  }
301  return $value;
302  }
303 
311  function reloadDefaultSetting($id, $filename, $settingName, $paramArray) {
312  $xmlParser = new XMLParser();
313  $tree = $xmlParser->parse($filename);
314 
315  if (!$tree) {
316  $xmlParser->destroy();
317  return false;
318  }
319 
320  foreach ($tree->getChildren() as $setting) {
321  $nameNode = $setting->getChildByName('name');
322  $valueNode = $setting->getChildByName('value');
323 
324  if (isset($nameNode) && isset($valueNode)) {
325 
326  if ($nameNode->getValue() == $settingName) {
327  $type = $setting->getAttribute('type');
328  $isLocaleField = $setting->getAttribute('locale');
329  $name = $nameNode->getValue();
330 
331  if ($type == 'object') {
332  $arrayNode = $valueNode->getChildByName('array');
333  $value = $this->_buildObject($arrayNode, $paramArray);
334  } else {
335  $value = $this->_performReplacement($valueNode->getValue(), $paramArray);
336  }
337 
338  $this->updateSetting(
339  $id,
340  $name,
341  $isLocaleField?array(AppLocale::getLocale() => $value):$value,
342  $type,
343  $isLocaleField
344  );
345 
346  $xmlParser->destroy();
347  return true;
348  }
349  }
350  }
351 
352  $xmlParser->destroy();
353 
354  }
355 
363  function reloadLocalizedDefaultSettings($id, $filename, $paramArray, $locale) {
364  $xmlParser = new XMLParser();
365  $tree = $xmlParser->parse($filename);
366 
367  if (!$tree) {
368  $xmlParser->destroy();
369  return false;
370  }
371 
372  foreach ($tree->getChildren() as $setting) {
373  $nameNode = $setting->getChildByName('name');
374  $valueNode = $setting->getChildByName('value');
375 
376  if (isset($nameNode) && isset($valueNode)) {
377  $type = $setting->getAttribute('type');
378  $isLocaleField = $setting->getAttribute('locale');
379  $name = $nameNode->getValue();
380 
381  //skip all settings that are not locale fields
382  if (!$isLocaleField) continue;
383 
384  if ($type == 'object') {
385  $arrayNode = $valueNode->getChildByName('array');
386  $value = $this->_buildLocalizedObject($arrayNode, $paramArray, $locale);
387  } else {
388  $value = $this->_performLocalizedReplacement($valueNode->getValue(), $paramArray, $locale);
389  }
390 
391  // Replace translate calls with translated content
392  $this->updateSetting(
393  $id,
394  $name,
395  array($locale => $value),
396  $type,
397  true
398  );
399  }
400  }
401 
402  $xmlParser->destroy();
403 
404  }
405 
411  function _getCache($id) {
412  static $settingCache;
413  if (!isset($settingCache)) {
414  $settingCache = array();
415  }
416  if (!isset($settingCache[$id])) {
417  $cacheManager = CacheManager::getManager();
418  $settingCache[$id] = $cacheManager->getCache(
419  $this->_getCacheName(), $id,
420  array($this, '_cacheMiss')
421  );
422  }
423  return $settingCache[$id];
424  }
425 
430  protected function _getTableName() {
431  assert(false); // Must be implemented by subclasses
432  }
433 
437  protected function _getPrimaryKeyColumn() {
438  assert(false); // Must be implemented by subclasses
439  }
440 
444  protected function _getCacheName() {
445  assert(false); // Must be implemented by subclasses
446  }
447 }
448 
449 ?>
_performReplacement($rawInput, $paramArray=array())
Operations for retrieving and modifying objects from a database.
Definition: DAO.inc.php:30
updateSetting($id, $name, $value, $type=null, $isLocalized=false)
& retrieve($sql, $params=false, $callHooks=true)
Definition: DAO.inc.php:84
reloadDefaultSetting($id, $filename, $settingName, $paramArray)
deleteSetting($id, $name, $locale=null)
& getSettings($id)
_cacheMiss($cache, $id)
_installer_regexp_callback($matches)
& getSetting($id, $name, $locale=null)
_buildObject(&$node, $paramArray=array())
& _buildLocalizedObject(&$node, $paramArray=array(), $locale=null)
static getLocale()
convertFromDB($value, $type)
Definition: DAO.inc.php:335
Generic class for parsing an XML document into a data structure.
_performLocalizedReplacement($rawInput, $paramArray=array(), $locale=null)
convertToDB($value, &$type)
Definition: DAO.inc.php:391
Operations for retrieving and modifying settings.
reloadLocalizedDefaultSettings($id, $filename, $paramArray, $locale)
replace($table, $arrFields, $keyCols)
Definition: DAO.inc.php:237
update($sql, $params=false, $callHooks=true, $dieOnError=true)
Definition: DAO.inc.php:208
installSettings($id, $filename, $paramArray=array())