Open Journal Systems  2.4.4
 All Classes Namespaces Functions Variables Groups Pages
SiteSettingsDAO.inc.php
1 <?php
2 
17 class SiteSettingsDAO extends DAO {
21  function SiteSettingsDAO() {
22  parent::DAO();
23  }
24 
25  function &_getCache() {
26  $settingCache =& Registry::get('siteSettingCache', true, null);
27  if ($settingCache === null) {
28  $cacheManager =& CacheManager::getManager();
29  $settingCache = $cacheManager->getFileCache(
30  'siteSettings', 'site',
31  array($this, '_cacheMiss')
32  );
33  }
34  return $settingCache;
35  }
36 
43  function &getSetting($name, $locale = null) {
44  $cache =& $this->_getCache();
45  $returner = $cache->get($name);
46  if ($locale !== null) {
47  if (!isset($returner[$locale]) || !is_array($returner)) {
48  unset($returner);
49  $returner = null;
50  return $returner;
51  }
52  return $returner[$locale];
53  }
54  return $returner;
55  }
56 
57  function _cacheMiss(&$cache, $id) {
58  $settings =& $this->getSiteSettings();
59  if (!isset($settings[$id])) {
60  $cache->setCache($id, null);
61  return null;
62  }
63  return $settings[$id];
64  }
65 
70  function &getSiteSettings() {
71  $siteSettings = array();
72 
73  $result =& $this->retrieve(
74  'SELECT setting_name, setting_value, setting_type, locale FROM site_settings'
75  );
76 
77  if ($result->RecordCount() == 0) {
78  $returner = null;
79  $result->Close();
80  return $returner;
81 
82  } else {
83  while (!$result->EOF) {
84  $row =& $result->getRowAssoc(false);
85  $value = $this->convertFromDB($row['setting_value'], $row['setting_type']);
86  if ($row['locale'] == '') $siteSettings[$row['setting_name']] = $value;
87  else $siteSettings[$row['setting_name']][$row['locale']] = $value;
88  $result->MoveNext();
89  }
90  $result->Close();
91  unset($result);
92 
93  $cache =& $this->_getCache();
94  $cache->setEntireCache($siteSettings);
95 
96  return $siteSettings;
97  }
98  }
99 
108  function updateSetting($name, $value, $type = null, $isLocalized = false) {
109  $returner = null;
110  $cache =& $this->_getCache();
111  $cache->setCache($name, $value);
112 
113  $keyFields = array('setting_name', 'locale');
114 
115  if (!$isLocalized) {
116  $value = $this->convertToDB($value, $type);
117  $this->replace('site_settings',
118  array(
119  'setting_name' => $name,
120  'setting_value' => $value,
121  'setting_type' => $type,
122  'locale' => ''
123  ),
124  $keyFields
125  );
126  $returner = true;
127  } else {
128  if (is_array($value)) foreach ($value as $locale => $localeValue) {
129  $this->update('DELETE FROM site_settings WHERE setting_name = ? AND locale = ?', array($name, $locale));
130  if (empty($localeValue)) continue;
131  $type = null;
132  $returner = $this->update('INSERT INTO site_settings
133  (setting_name, setting_value, setting_type, locale)
134  VALUES (?, ?, ?, ?)',
135  array(
136  $name, $this->convertToDB($localeValue, $type), $type, $locale
137  )
138  );
139  }
140  }
141  return $returner;
142  }
143 
148  function deleteSetting($name, $locale = null) {
149  $cache =& $this->_getCache();
150  $cache->setCache($name, null);
151 
152  $params = array($name);
153  $sql = 'DELETE FROM site_settings WHERE setting_name = ?';
154  if ($locale !== null) {
155  $params[] = $locale;
156  $sql .= ' AND locale = ?';
157  }
158 
159  return $this->update($sql, $params);
160  }
161 
168  function _performReplacement($rawInput, $paramArray = array()) {
169  $value = preg_replace_callback('{{translate key="([^"]+)"}}', array(&$this, '_installer_regexp_callback'), $rawInput);
170  foreach ($paramArray as $pKey => $pValue) {
171  $value = str_replace('{$' . $pKey . '}', $pValue, $value);
172  }
173  return $value;
174  }
175 
182  function &_buildObject (&$node, $paramArray = array()) {
183  $value = array();
184  foreach ($node->getChildren() as $element) {
185  $key = $element->getAttribute('key');
186  $childArray =& $element->getChildByName('array');
187  if (isset($childArray)) {
188  $content = $this->_buildObject($childArray, $paramArray);
189  } else {
190  $content = $this->_performReplacement($element->getValue(), $paramArray);
191  }
192  if (!empty($key)) {
193  $key = $this->_performReplacement($key, $paramArray);
194  $value[$key] = $content;
195  } else $value[] = $content;
196  }
197  return $value;
198  }
199 
205  function installSettings($filename, $paramArray = array()) {
206  $xmlParser = new XMLParser();
207  $tree = $xmlParser->parse($filename);
208 
209  if (!$tree) {
210  $xmlParser->destroy();
211  return false;
212  }
213 
214  foreach ($tree->getChildren() as $setting) {
215  $nameNode =& $setting->getChildByName('name');
216  $valueNode =& $setting->getChildByName('value');
217 
218  if (isset($nameNode) && isset($valueNode)) {
219  $type = $setting->getAttribute('type');
220  $isLocaleField = $setting->getAttribute('locale');
221  $name =& $nameNode->getValue();
222 
223  if ($type == 'object') {
224  $arrayNode =& $valueNode->getChildByName('array');
225  $value = $this->_buildObject($arrayNode, $paramArray);
226  } else {
227  $value = $this->_performReplacement($valueNode->getValue(), $paramArray);
228  }
229 
230  // Replace translate calls with translated content
231  $this->updateSetting(
232  $name,
233  $isLocaleField?array(AppLocale::getLocale() => $value):$value,
234  $type,
235  $isLocaleField
236  );
237  }
238  }
239 
240  $xmlParser->destroy();
241 
242  }
243 
247  function _installer_regexp_callback($matches) {
248  return __($matches[1]);
249  }
250 }
251 
252 ?>
Operations for retrieving and modifying objects from a database.
Definition: DAO.inc.php:29
installSettings($filename, $paramArray=array())
& retrieve($sql, $params=false, $callHooks=true)
Definition: DAO.inc.php:83
& _buildObject(&$node, $paramArray=array())
_performReplacement($rawInput, $paramArray=array())
updateSetting($name, $value, $type=null, $isLocalized=false)
_installer_regexp_callback($matches)
convertFromDB($value, $type)
Definition: DAO.inc.php:338
Generic class for parsing an XML document into a data structure.
convertToDB($value, &$type)
Definition: DAO.inc.php:394
deleteSetting($name, $locale=null)
replace($table, $arrFields, $keyCols)
Definition: DAO.inc.php:240
& get($key, $createIfEmpty=false, $createWithDefault=null)
update($sql, $params=false, $callHooks=true, $dieOnError=true)
Definition: DAO.inc.php:211
& getSetting($name, $locale=null)