Open Journal Systems  2.4.3
 All Classes Namespaces Functions Variables Groups Pages
JournalSettingsDAO.inc.php
1 <?php
2 
16 class JournalSettingsDAO extends DAO {
17  function &_getCache($journalId) {
18  static $settingCache;
19  if (!isset($settingCache)) {
20  $settingCache = array();
21  }
22  if (!isset($settingCache[$journalId])) {
23  $cacheManager =& CacheManager::getManager();
24  $settingCache[$journalId] =& $cacheManager->getFileCache(
25  'journalSettings', $journalId,
26  array($this, '_cacheMiss')
27  );
28  }
29  return $settingCache[$journalId];
30  }
31 
39  function &getSetting($journalId, $name, $locale = null) {
40  $cache =& $this->_getCache($journalId);
41  $returner = $cache->get($name);
42  if ($locale !== null) {
43  if (!isset($returner[$locale]) || !is_array($returner)) {
44  unset($returner);
45  $returner = null;
46  return $returner;
47  }
48  return $returner[$locale];
49  }
50  return $returner;
51  }
52 
53  function _cacheMiss(&$cache, $id) {
54  $settings =& $this->getJournalSettings($cache->getCacheId());
55  if (!isset($settings[$id])) {
56  $cache->setCache($id, null);
57  return null;
58  }
59  return $settings[$id];
60  }
61 
67  function &getJournalSettings($journalId) {
68  $journalSettings = array();
69 
70  $result =& $this->retrieve(
71  'SELECT setting_name, setting_value, setting_type, locale FROM journal_settings WHERE journal_id = ?', $journalId
72  );
73 
74  while (!$result->EOF) {
75  $row =& $result->getRowAssoc(false);
76  $value = $this->convertFromDB($row['setting_value'], $row['setting_type']);
77  if ($row['locale'] == '') {
78  $journalSettings[$row['setting_name']] = $value;
79  } else {
80  if (!isset($journalSettings[$row['setting_name']])) {
81  $journalSettings[$row['setting_name']] = array();
82  }
83  $journalSettings[$row['setting_name']][$row['locale']] = $value;
84  }
85  $result->MoveNext();
86  }
87  $result->Close();
88  unset($result);
89 
90  $cache =& $this->_getCache($journalId);
91  $cache->setEntireCache($journalSettings);
92 
93  return $journalSettings;
94  }
95 
104  function updateSetting($journalId, $name, $value, $type = null, $isLocalized = false) {
105  $cache =& $this->_getCache($journalId);
106  $cache->setCache($name, $value);
107 
108  $keyFields = array('setting_name', 'locale', 'journal_id');
109 
110  if (!$isLocalized) {
111  $value = $this->convertToDB($value, $type);
112  $this->replace('journal_settings',
113  array(
114  'journal_id' => $journalId,
115  'setting_name' => $name,
116  'setting_value' => $value,
117  'setting_type' => $type,
118  'locale' => ''
119  ),
120  $keyFields
121  );
122  } else {
123  if (is_array($value)) foreach ($value as $locale => $localeValue) {
124  $this->update('DELETE FROM journal_settings WHERE journal_id = ? AND setting_name = ? AND locale = ?', array($journalId, $name, $locale));
125  if (empty($localeValue)) continue;
126  $type = null;
127  $this->update('INSERT INTO journal_settings
128  (journal_id, setting_name, setting_value, setting_type, locale)
129  VALUES (?, ?, ?, ?, ?)',
130  array(
131  $journalId, $name, $this->convertToDB($localeValue, $type), $type, $locale
132  )
133  );
134  }
135  }
136  }
137 
143  function deleteSetting($journalId, $name, $locale = null) {
144  $cache =& $this->_getCache($journalId);
145  $cache->setCache($name, null);
146 
147  $params = array($journalId, $name);
148  $sql = 'DELETE FROM journal_settings WHERE journal_id = ? AND setting_name = ?';
149  if ($locale !== null) {
150  $params[] = $locale;
151  $sql .= ' AND locale = ?';
152  }
153 
154  return $this->update($sql, $params);
155  }
156 
161  function deleteSettingsByJournal($journalId) {
162  $cache =& $this->_getCache($journalId);
163  $cache->flush();
164 
165  return $this->update(
166  'DELETE FROM journal_settings WHERE journal_id = ?', $journalId
167  );
168  }
169 
176  function _performReplacement($rawInput, $paramArray = array()) {
177  $value = preg_replace_callback('{{translate key="([^"]+)"}}', array(&$this, '_installer_regexp_callback'), $rawInput);
178  foreach ($paramArray as $pKey => $pValue) {
179  $value = str_replace('{$' . $pKey . '}', $pValue, $value);
180  }
181  return $value;
182  }
183 
190  function &_buildObject (&$node, $paramArray = array()) {
191  $value = array();
192  foreach ($node->getChildren() as $element) {
193  $key = $element->getAttribute('key');
194  $childArray =& $element->getChildByName('array');
195  if (isset($childArray)) {
196  $content = $this->_buildObject($childArray, $paramArray);
197  } else {
198  $content = $this->_performReplacement($element->getValue(), $paramArray);
199  }
200  if (!empty($key)) {
201  $key = $this->_performReplacement($key, $paramArray);
202  $value[$key] = $content;
203  } else $value[] = $content;
204  }
205  return $value;
206  }
207 
214  function installSettings($journalId, $filename, $paramArray = array()) {
215  $xmlParser = new XMLParser();
216  $tree = $xmlParser->parse($filename);
217 
218  if (!$tree) {
219  $xmlParser->destroy();
220  return false;
221  }
222 
223  foreach ($tree->getChildren() as $setting) {
224  $nameNode =& $setting->getChildByName('name');
225  $valueNode =& $setting->getChildByName('value');
226 
227  if (isset($nameNode) && isset($valueNode)) {
228  $type = $setting->getAttribute('type');
229  $isLocaleField = $setting->getAttribute('locale');
230  $name =& $nameNode->getValue();
231 
232  if ($type == 'object') {
233  $arrayNode =& $valueNode->getChildByName('array');
234  $value = $this->_buildObject($arrayNode, $paramArray);
235  } else {
236  $value = $this->_performReplacement($valueNode->getValue(), $paramArray);
237  }
238 
239  // Replace translate calls with translated content
240  $this->updateSetting(
241  $journalId,
242  $name,
243  $isLocaleField?array(AppLocale::getLocale() => $value):$value,
244  $type,
245  $isLocaleField
246  );
247  }
248  }
249 
250  $xmlParser->destroy();
251 
252  }
253 
261  function _performLocalizedReplacement($rawInput, $paramArray = array(), $locale = null) {
262  preg_match('{{translate key="([^"]+)"}}', $rawInput, $matches);
263  if ( isset($matches[1]) ) {
264  AppLocale::requireComponents(LOCALE_COMPONENT_OJS_DEFAULT, LOCALE_COMPONENT_OJS_MANAGER, $locale);
265  return __($matches[1], $paramArray, $locale);
266  }
267 
268  return $rawInput;
269  }
270 
278  function &_buildLocalizedObject (&$node, $paramArray = array(), $locale = null) {
279  $value = array();
280  foreach ($node->getChildren() as $element) {
281  $key = $element->getAttribute('key');
282  $childArray =& $element->getChildByName('array');
283  if (isset($childArray)) {
284  $content = $this->_buildLocalizedObject($childArray, $paramArray, $locale);
285  } else {
286  $content = $this->_performLocalizedReplacement($element->getValue(), $paramArray, $locale);
287  }
288  if (!empty($key)) {
289  $key = $this->_performLocalizedReplacement($key, $paramArray, $locale);
290  $value[$key] = $content;
291  } else $value[] = $content;
292  }
293  return $value;
294  }
295 
303  function reloadLocalizedDefaultSettings($journalId, $filename, $paramArray, $locale) {
304  $xmlParser = new XMLParser();
305  $tree = $xmlParser->parse($filename);
306 
307  if (!$tree) {
308  $xmlParser->destroy();
309  return false;
310  }
311 
312  foreach ($tree->getChildren() as $setting) {
313  $nameNode =& $setting->getChildByName('name');
314  $valueNode =& $setting->getChildByName('value');
315 
316  if (isset($nameNode) && isset($valueNode)) {
317  $type = $setting->getAttribute('type');
318  $isLocaleField = $setting->getAttribute('locale');
319  $name =& $nameNode->getValue();
320 
321  //skip all settings that are not locale fields
322  if (!$isLocaleField) continue;
323 
324  if ($type == 'object') {
325  $arrayNode =& $valueNode->getChildByName('array');
326  $value = $this->_buildLocalizedObject($arrayNode, $paramArray, $locale);
327  } else {
328  $value = $this->_performLocalizedReplacement($valueNode->getValue(), $paramArray, $locale);
329  }
330 
331  // Replace translate calls with translated content
332  $this->updateSetting(
333  $journalId,
334  $name,
335  array($locale => $value),
336  $type,
337  true
338  );
339  }
340  }
341 
342  $xmlParser->destroy();
343 
344  }
345 
349  function _installer_regexp_callback($matches) {
350  return __($matches[1]);
351  }
352 }
353 
354 ?>
Operations for retrieving and modifying journal settings.
reloadLocalizedDefaultSettings($journalId, $filename, $paramArray, $locale)
Operations for retrieving and modifying objects from a database.
Definition: DAO.inc.php:29
_performLocalizedReplacement($rawInput, $paramArray=array(), $locale=null)
& retrieve($sql, $params=false, $callHooks=true)
Definition: DAO.inc.php:83
_performReplacement($rawInput, $paramArray=array())
updateSetting($journalId, $name, $value, $type=null, $isLocalized=false)
installSettings($journalId, $filename, $paramArray=array())
deleteSetting($journalId, $name, $locale=null)
& _buildObject(&$node, $paramArray=array())
convertFromDB($value, $type)
Definition: DAO.inc.php:338
Generic class for parsing an XML document into a data structure.
& getSetting($journalId, $name, $locale=null)
& getJournalSettings($journalId)
convertToDB($value, &$type)
Definition: DAO.inc.php:394
replace($table, $arrFields, $keyCols)
Definition: DAO.inc.php:240
update($sql, $params=false, $callHooks=true, $dieOnError=true)
Definition: DAO.inc.php:211
& _buildLocalizedObject(&$node, $paramArray=array(), $locale=null)