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