Open Journal Systems  3.0.0
 All Data Structures 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 loadSettings($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  if ($cache) $cache->setEntireCache($settings);
48 
49  return $settings;
50  }
51 
57  function &getSettings($id) {
58  $cache = $this->_getCache($id);
59  if ($cache) return $cache->getContents();
60  return $this->loadSettings($id);
61  }
62 
70  function &getSetting($id, $name, $locale = null) {
71  if ($cache = $this->_getCache($id)) {
72  $returner = $cache->get($name);
73  } else {
74  $settings = $this->loadSettings($id);
75  if (isset($settings[$name])) $returner = $settings[$name];
76  else $returner = null;
77  }
78  if ($locale !== null) {
79  if (!isset($returner[$locale]) || !is_array($returner)) {
80  unset($returner);
81  $returner = null;
82  return $returner;
83  }
84  return $returner[$locale];
85  }
86  return $returner;
87  }
88 
95  function _cacheMiss($cache, $id) {
96  $settings = $this->loadSettings($cache->getCacheId());
97  if (!isset($settings[$id])) {
98  $cache->setCache($id, null);
99  return null;
100  }
101  return $settings[$id];
102  }
103 
110  function _performReplacement($rawInput, $paramArray = array()) {
111  $value = preg_replace_callback('{{translate key="([^"]+)"}}', array($this, '_installer_regexp_callback'), $rawInput);
112  foreach ($paramArray as $pKey => $pValue) {
113  $value = str_replace('{$' . $pKey . '}', $pValue, $value);
114  }
115  return $value;
116  }
117 
124  function _buildObject (&$node, $paramArray = array()) {
125  $value = array();
126  foreach ($node->getChildren() as $element) {
127  $key = $element->getAttribute('key');
128  $childArray = $element->getChildByName('array');
129  if (isset($childArray)) {
130  $content = $this->_buildObject($childArray, $paramArray);
131  } else {
132  $content = $this->_performReplacement($element->getValue(), $paramArray);
133  }
134  if (!empty($key)) {
135  $key = $this->_performReplacement($key, $paramArray);
136  $value[$key] = $content;
137  } else $value[] = $content;
138  }
139  return $value;
140  }
141 
148  function installSettings($id, $filename, $paramArray = array()) {
149  $xmlParser = new XMLParser();
150  $tree = $xmlParser->parse($filename);
151 
152  if (!$tree) {
153  $xmlParser->destroy();
154  return false;
155  }
156 
157  foreach ($tree->getChildren() as $setting) {
158  $nameNode = $setting->getChildByName('name');
159  $valueNode = $setting->getChildByName('value');
160 
161  if (isset($nameNode) && isset($valueNode)) {
162  $type = $setting->getAttribute('type');
163  $isLocaleField = $setting->getAttribute('locale');
164  $name = $nameNode->getValue();
165 
166  if ($type == 'object') {
167  $arrayNode = $valueNode->getChildByName('array');
168  $value = $this->_buildObject($arrayNode, $paramArray);
169  } else {
170  $value = $this->_performReplacement($valueNode->getValue(), $paramArray);
171  }
172 
173  // Replace translate calls with translated content
174  $this->updateSetting(
175  $id,
176  $name,
177  $isLocaleField?array(AppLocale::getLocale() => $value):$value,
178  $type,
179  $isLocaleField
180  );
181  }
182  }
183 
184  $xmlParser->destroy();
185 
186  }
187 
191  function _installer_regexp_callback($matches) {
192  return __($matches[1]);
193  }
194 
203  function updateSetting($id, $name, $value, $type = null, $isLocalized = false) {
204  $cache = $this->_getCache($id);
205  if ($cache) $cache->setCache($name, $value);
206 
207  $keyFields = array('setting_name', 'locale', $this->_getPrimaryKeyColumn());
208 
209  if (!$isLocalized) {
210  $value = $this->convertToDB($value, $type);
211  $this->replace($this->_getTableName(),
212  array(
213  $this->_getPrimaryKeyColumn() => $id,
214  'setting_name' => $name,
215  'setting_value' => $value,
216  'setting_type' => $type,
217  'locale' => ''
218  ),
219  $keyFields
220  );
221  } else {
222  if (is_array($value)) foreach ($value as $locale => $localeValue) {
223  $this->update('DELETE FROM ' . $this->_getTableName() . ' WHERE ' . $this->_getPrimaryKeyColumn() . ' = ? AND setting_name = ? AND locale = ?', array($id, $name, $locale));
224  if (empty($localeValue)) continue;
225  $type = null;
226  $this->update('INSERT INTO ' . $this->_getTableName() . '
227  (' . $this->_getPrimaryKeyColumn() . ', setting_name, setting_value, setting_type, locale)
228  VALUES (?, ?, ?, ?, ?)',
229  array(
230  $id, $name, $this->convertToDB($localeValue, $type), $type, $locale
231  )
232  );
233  }
234  }
235  }
236 
242  function deleteSetting($id, $name, $locale = null) {
243  $cache = $this->_getCache($id);
244  if ($cache) $cache->setCache($name, null);
245 
246  $params = array($id, $name);
247  $sql = 'DELETE FROM ' . $this->_getTableName() . ' WHERE ' . $this->_getPrimaryKeyColumn() . ' = ? AND setting_name = ?';
248  if ($locale !== null) {
249  $params[] = $locale;
250  $sql .= ' AND locale = ?';
251  }
252 
253  return $this->update($sql, $params);
254  }
255 
260  function deleteById($id) {
261  $cache = $this->_getCache($id);
262  if ($cache) $cache->flush();
263 
264  return $this->update(
265  'DELETE FROM ' . $this->_getTableName() . ' WHERE ' . $this->_getPrimaryKeyColumn() . ' = ?',
266  (int) $id
267  );
268  }
269 
277  function _performLocalizedReplacement($rawInput, $paramArray = array(), $locale = null) {
278  $value = preg_replace_callback(
279  '{{translate key="([^"]+)"}}',
280  // this only translates from mail locale file
281  create_function(
282  '$matches',
283  '$locale = "' . $locale . '";'.'$localeFileName = AppLocale::getMainLocaleFilename($locale);' .
284  '$localeFile = new LocaleFile($locale, $localeFileName);'.'return $localeFile->translate($matches[1]);'
285  ),
286  $rawInput
287  );
288 
289  foreach ($paramArray as $pKey => $pValue) {
290  $value = str_replace('{$' . $pKey . '}', $pValue, $value);
291  }
292  return $value;
293  }
294 
302  function &_buildLocalizedObject (&$node, $paramArray = array(), $locale = null) {
303  $value = array();
304  foreach ($node->getChildren() as $element) {
305  $key = $element->getAttribute('key');
306  $childArray = $element->getChildByName('array');
307  if (isset($childArray)) {
308  $content = $this->_buildLocalizedObject($childArray, $paramArray, $locale);
309  } else {
310  $content = $this->_performLocalizedReplacement($element->getValue(), $paramArray, $locale);
311  }
312  if (!empty($key)) {
313  $key = $this->_performLocalizedReplacement($key, $paramArray, $locale);
314  $value[$key] = $content;
315  } else $value[] = $content;
316  }
317  return $value;
318  }
319 
327  function reloadDefaultSetting($id, $filename, $settingName, $paramArray) {
328  $xmlParser = new XMLParser();
329  $tree = $xmlParser->parse($filename);
330 
331  if (!$tree) {
332  $xmlParser->destroy();
333  return false;
334  }
335 
336  foreach ($tree->getChildren() as $setting) {
337  $nameNode = $setting->getChildByName('name');
338  $valueNode = $setting->getChildByName('value');
339 
340  if (isset($nameNode) && isset($valueNode)) {
341 
342  if ($nameNode->getValue() == $settingName) {
343  $type = $setting->getAttribute('type');
344  $isLocaleField = $setting->getAttribute('locale');
345  $name = $nameNode->getValue();
346 
347  if ($type == 'object') {
348  $arrayNode = $valueNode->getChildByName('array');
349  $value = $this->_buildObject($arrayNode, $paramArray);
350  } else {
351  $value = $this->_performReplacement($valueNode->getValue(), $paramArray);
352  }
353 
354  $this->updateSetting(
355  $id,
356  $name,
357  $isLocaleField?array(AppLocale::getLocale() => $value):$value,
358  $type,
359  $isLocaleField
360  );
361 
362  $xmlParser->destroy();
363  return true;
364  }
365  }
366  }
367 
368  $xmlParser->destroy();
369 
370  }
371 
379  function reloadLocalizedDefaultSettings($id, $filename, $paramArray, $locale) {
380  $xmlParser = new XMLParser();
381  $tree = $xmlParser->parse($filename);
382 
383  if (!$tree) {
384  $xmlParser->destroy();
385  return false;
386  }
387 
388  foreach ($tree->getChildren() as $setting) {
389  $nameNode = $setting->getChildByName('name');
390  $valueNode = $setting->getChildByName('value');
391 
392  if (isset($nameNode) && isset($valueNode)) {
393  $type = $setting->getAttribute('type');
394  $isLocaleField = $setting->getAttribute('locale');
395  $name = $nameNode->getValue();
396 
397  //skip all settings that are not locale fields
398  if (!$isLocaleField) continue;
399 
400  if ($type == 'object') {
401  $arrayNode = $valueNode->getChildByName('array');
402  $value = $this->_buildLocalizedObject($arrayNode, $paramArray, $locale);
403  } else {
404  $value = $this->_performLocalizedReplacement($valueNode->getValue(), $paramArray, $locale);
405  }
406 
407  // Replace translate calls with translated content
408  $this->updateSetting(
409  $id,
410  $name,
411  array($locale => $value),
412  $type,
413  true
414  );
415  }
416  }
417 
418  $xmlParser->destroy();
419 
420  }
421 
427  function _getCache($id) {
428  $cacheName = $this->_getCacheName();
429  if ($cacheName === null) return null;
430 
431  static $settingCache;
432  if (!isset($settingCache)) {
433  $settingCache = array();
434  }
435  if (!isset($settingCache[$id])) {
436  $cacheManager = CacheManager::getManager();
437  $settingCache[$id] = $cacheManager->getCache(
438  $cacheName, $id,
439  array($this, '_cacheMiss')
440  );
441  }
442  return $settingCache[$id];
443  }
444 
449  protected function _getTableName() {
450  assert(false); // Must be implemented by subclasses
451  }
452 
456  protected function _getPrimaryKeyColumn() {
457  assert(false); // Must be implemented by subclasses
458  }
459 
464  protected function _getCacheName() {
465  return null;
466  }
467 }
468 
469 ?>
_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)
static getManager()
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())