Open Journal Systems  3.0.0
 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  $returner = null;
49  return $returner;
50  }
51  return $returner[$locale];
52  }
53  return $returner;
54  }
55 
56  function _cacheMiss($cache, $id) {
57  $settings = $this->getSiteSettings();
58  if (!isset($settings[$id])) {
59  $cache->setCache($id, null);
60  return null;
61  }
62  return $settings[$id];
63  }
64 
69  function &getSiteSettings() {
70  $siteSettings = array();
71 
72  $result = $this->retrieve(
73  'SELECT setting_name, setting_value, setting_type, locale FROM site_settings'
74  );
75 
76  if ($result->RecordCount() == 0) {
77  $returner = null;
78  $result->Close();
79  return $returner;
80 
81  } else {
82  while (!$result->EOF) {
83  $row = $result->getRowAssoc(false);
84  $value = $this->convertFromDB($row['setting_value'], $row['setting_type']);
85  if ($row['locale'] == '') $siteSettings[$row['setting_name']] = $value;
86  else $siteSettings[$row['setting_name']][$row['locale']] = $value;
87  $result->MoveNext();
88  }
89  $result->Close();
90 
91  $cache = $this->_getCache();
92  $cache->setEntireCache($siteSettings);
93 
94  return $siteSettings;
95  }
96  }
97 
106  function updateSetting($name, $value, $type = null, $isLocalized = false) {
107  $returner = null;
108  $cache = $this->_getCache();
109  $cache->setCache($name, $value);
110 
111  $keyFields = array('setting_name', 'locale');
112 
113  if (!$isLocalized) {
114  $value = $this->convertToDB($value, $type);
115  $this->replace('site_settings',
116  array(
117  'setting_name' => $name,
118  'setting_value' => $value,
119  'setting_type' => $type,
120  'locale' => ''
121  ),
122  $keyFields
123  );
124  $returner = true;
125  } else {
126  if (is_array($value)) foreach ($value as $locale => $localeValue) {
127  $this->update('DELETE FROM site_settings WHERE setting_name = ? AND locale = ?', array($name, $locale));
128  if (empty($localeValue)) continue;
129  $type = null;
130  $returner = $this->update('INSERT INTO site_settings
131  (setting_name, setting_value, setting_type, locale)
132  VALUES (?, ?, ?, ?)',
133  array(
134  $name, $this->convertToDB($localeValue, $type), $type, $locale
135  )
136  );
137  }
138  }
139  return $returner;
140  }
141 
146  function deleteSetting($name, $locale = null) {
147  $cache = $this->_getCache();
148  $cache->setCache($name, null);
149 
150  $params = array($name);
151  $sql = 'DELETE FROM site_settings WHERE setting_name = ?';
152  if ($locale !== null) {
153  $params[] = $locale;
154  $sql .= ' AND locale = ?';
155  }
156 
157  return $this->update($sql, $params);
158  }
159 
166  function _performReplacement($rawInput, $paramArray = array()) {
167  $value = preg_replace_callback('{{translate key="([^"]+)"}}', array($this, '_installer_regexp_callback'), $rawInput);
168  foreach ($paramArray as $pKey => $pValue) {
169  $value = str_replace('{$' . $pKey . '}', $pValue, $value);
170  }
171  return $value;
172  }
173 
180  function &_buildObject (&$node, $paramArray = array()) {
181  $value = array();
182  foreach ($node->getChildren() as $element) {
183  $key = $element->getAttribute('key');
184  $childArray =& $element->getChildByName('array');
185  if (isset($childArray)) {
186  $content = $this->_buildObject($childArray, $paramArray);
187  } else {
188  $content = $this->_performReplacement($element->getValue(), $paramArray);
189  }
190  if (!empty($key)) {
191  $key = $this->_performReplacement($key, $paramArray);
192  $value[$key] = $content;
193  } else $value[] = $content;
194  }
195  return $value;
196  }
197 
203  function installSettings($filename, $paramArray = array()) {
204  $xmlParser = new XMLParser();
205  $tree = $xmlParser->parse($filename);
206 
207  if (!$tree) {
208  $xmlParser->destroy();
209  return false;
210  }
211 
212  foreach ($tree->getChildren() as $setting) {
213  $nameNode =& $setting->getChildByName('name');
214  $valueNode =& $setting->getChildByName('value');
215 
216  if (isset($nameNode) && isset($valueNode)) {
217  $type = $setting->getAttribute('type');
218  $isLocaleField = $setting->getAttribute('locale');
219  $name =& $nameNode->getValue();
220 
221  if ($type == 'object') {
222  $arrayNode =& $valueNode->getChildByName('array');
223  $value = $this->_buildObject($arrayNode, $paramArray);
224  } else {
225  $value = $this->_performReplacement($valueNode->getValue(), $paramArray);
226  }
227 
228  // Replace translate calls with translated content
229  $this->updateSetting(
230  $name,
231  $isLocaleField?array(AppLocale::getLocale() => $value):$value,
232  $type,
233  $isLocaleField
234  );
235  }
236  }
237 
238  $xmlParser->destroy();
239 
240  }
241 
245  function _installer_regexp_callback($matches) {
246  return __($matches[1]);
247  }
248 }
249 
250 ?>
Operations for retrieving and modifying objects from a database.
Definition: DAO.inc.php:30
installSettings($filename, $paramArray=array())
& retrieve($sql, $params=false, $callHooks=true)
Definition: DAO.inc.php:84
& _buildObject(&$node, $paramArray=array())
_performReplacement($rawInput, $paramArray=array())
updateSetting($name, $value, $type=null, $isLocalized=false)
static getLocale()
_installer_regexp_callback($matches)
convertFromDB($value, $type)
Definition: DAO.inc.php:335
Generic class for parsing an XML document into a data structure.
convertToDB($value, &$type)
Definition: DAO.inc.php:391
static & get($key, $createIfEmpty=false, $createWithDefault=null)
deleteSetting($name, $locale=null)
replace($table, $arrFields, $keyCols)
Definition: DAO.inc.php:237
update($sql, $params=false, $callHooks=true, $dieOnError=true)
Definition: DAO.inc.php:208
& getSetting($name, $locale=null)