Open Monograph Press  3.3.0
SettingsDAO.inc.php
1 <?php
2 
16 abstract class SettingsDAO extends DAO {
22  function loadSettings($id) {
23  $settings = array();
24 
25  $result = $this->retrieve(
26  'SELECT setting_name, setting_value, setting_type, locale FROM ' . $this->_getTableName() . ' WHERE ' . $this->_getPrimaryKeyColumn() . ' = ?',
27  (int) $id
28  );
29 
30  while (!$result->EOF) {
31  $row = $result->getRowAssoc(false);
32  $value = $this->convertFromDB($row['setting_value'], $row['setting_type']);
33  if ($row['locale'] == '') $settings[$row['setting_name']] = $value;
34  else $settings[$row['setting_name']][$row['locale']] = $value;
35  $result->MoveNext();
36  }
37  $result->Close();
38 
39  $cache = $this->_getCache($id);
40  if ($cache) $cache->setEntireCache($settings);
41 
42  return $settings;
43  }
44 
50  function &getSettings($id) {
51  $cache = $this->_getCache($id);
52  if ($cache) return $cache->getContents();
53  return $this->loadSettings($id);
54  }
55 
63  function &getSetting($id, $name, $locale = null) {
64  if ($cache = $this->_getCache($id)) {
65  $returner = $cache->get($name);
66  } else {
67  $settings = $this->loadSettings($id);
68  if (isset($settings[$name])) $returner = $settings[$name];
69  else $returner = null;
70  }
71  if ($locale !== null) {
72  if (!isset($returner[$locale]) || !is_array($returner)) {
73  unset($returner);
74  $returner = null;
75  return $returner;
76  }
77  return $returner[$locale];
78  }
79  return $returner;
80  }
81 
88  function _cacheMiss($cache, $id) {
89  $settings = $this->loadSettings($cache->getCacheId());
90  if (!isset($settings[$id])) {
91  $cache->setCache($id, null);
92  return null;
93  }
94  return $settings[$id];
95  }
96 
103  function _performReplacement($rawInput, $paramArray = array()) {
104  $value = preg_replace_callback('{{translate key="([^"]+)"}}', array($this, '_installer_regexp_callback'), $rawInput);
105  foreach ($paramArray as $pKey => $pValue) {
106  $value = str_replace('{$' . $pKey . '}', $pValue, $value);
107  }
108  return $value;
109  }
110 
117  function _buildObject (&$node, $paramArray = array()) {
118  $value = array();
119  foreach ($node->getChildren() as $element) {
120  $key = $element->getAttribute('key');
121  $childArray = $element->getChildByName('array');
122  if (isset($childArray)) {
123  $content = $this->_buildObject($childArray, $paramArray);
124  } else {
125  $content = $this->_performReplacement($element->getValue(), $paramArray);
126  }
127  if (!empty($key)) {
128  $key = $this->_performReplacement($key, $paramArray);
129  $value[$key] = $content;
130  } else $value[] = $content;
131  }
132  return $value;
133  }
134 
141  function installSettings($id, $filename, $paramArray = array()) {
142  $xmlParser = new XMLParser();
143  $tree = $xmlParser->parse($filename);
144  if (!$tree) return false;
145 
146  foreach ($tree->getChildren() as $setting) {
147  $nameNode = $setting->getChildByName('name');
148  $valueNode = $setting->getChildByName('value');
149 
150  if (isset($nameNode) && isset($valueNode)) {
151  $type = $setting->getAttribute('type');
152  $isLocaleField = $setting->getAttribute('locale');
153  $name = $nameNode->getValue();
154 
155  if ($type == 'object') {
156  $arrayNode = $valueNode->getChildByName('array');
157  $value = $this->_buildObject($arrayNode, $paramArray);
158  } else {
159  $value = $this->_performReplacement($valueNode->getValue(), $paramArray);
160  }
161 
162  // Replace translate calls with translated content
163  $this->updateSetting(
164  $id,
165  $name,
166  $isLocaleField?array(AppLocale::getLocale() => $value):$value,
167  $type,
168  $isLocaleField
169  );
170  }
171  }
172  }
173 
177  function _installer_regexp_callback($matches) {
178  return __($matches[1]);
179  }
180 
189  function updateSetting($id, $name, $value, $type = null, $isLocalized = false) {
190  $keyFields = array('setting_name', 'locale', $this->_getPrimaryKeyColumn());
191  if (!$isLocalized) {
192  $value = $this->convertToDB($value, $type);
193  $this->replace($this->_getTableName(),
194  array(
195  $this->_getPrimaryKeyColumn() => $id,
196  'setting_name' => $name,
197  'setting_value' => $value,
198  'setting_type' => $type,
199  'locale' => ''
200  ),
201  $keyFields
202  );
203  } else {
204  if (is_array($value)) foreach ($value as $locale => $localeValue) {
205  $this->update('DELETE FROM ' . $this->_getTableName() . ' WHERE ' . $this->_getPrimaryKeyColumn() . ' = ? AND setting_name = ? AND locale = ?', array($id, $name, $locale));
206  if (empty($localeValue)) continue;
207  $type = null;
208  $this->update('INSERT INTO ' . $this->_getTableName() . '
209  (' . $this->_getPrimaryKeyColumn() . ', setting_name, setting_value, setting_type, locale)
210  VALUES (?, ?, ?, ?, ?)',
211  array(
212  $id, $name, $this->convertToDB($localeValue, $type), $type, $locale
213  )
214  );
215  }
216  }
217 
218  $cache = $this->_getCache($id);
219  if ($cache) $cache->setCache($name, $value);
220  }
221 
227  function deleteSetting($id, $name, $locale = null) {
228  $cache = $this->_getCache($id);
229  if ($cache) $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  if ($cache) $cache->flush();
248 
249  return $this->update(
250  'DELETE FROM ' . $this->_getTableName() . ' WHERE ' . $this->_getPrimaryKeyColumn() . ' = ?',
251  (int) $id
252  );
253  }
254 
260  function _getCache($id) {
261  $cacheName = $this->_getCacheName();
262  if ($cacheName === null) return null;
263 
264  static $settingCache;
265  if (!isset($settingCache)) {
266  $settingCache = array();
267  }
268  if (!isset($settingCache[$id])) {
269  $cacheManager = CacheManager::getManager();
270  $settingCache[$id] = $cacheManager->getCache(
271  $cacheName, $id,
272  array($this, '_cacheMiss')
273  );
274  }
275  return $settingCache[$id];
276  }
277 
282  abstract protected function _getTableName();
283 
287  abstract protected function _getPrimaryKeyColumn();
288 
293  protected function _getCacheName() {
294  return null;
295  }
296 }
297 
298 
SettingsDAO\deleteById
deleteById($id)
Definition: SettingsDAO.inc.php:245
SettingsDAO\installSettings
installSettings($id, $filename, $paramArray=array())
Definition: SettingsDAO.inc.php:141
SettingsDAO\_performReplacement
_performReplacement($rawInput, $paramArray=array())
Definition: SettingsDAO.inc.php:103
SettingsDAO\_installer_regexp_callback
_installer_regexp_callback($matches)
Definition: SettingsDAO.inc.php:177
SettingsDAO\_cacheMiss
_cacheMiss($cache, $id)
Definition: SettingsDAO.inc.php:88
DAO\convertToDB
convertToDB($value, &$type)
Definition: DAO.inc.php:401
SettingsDAO\_getCacheName
_getCacheName()
Definition: SettingsDAO.inc.php:293
SettingsDAO\_getPrimaryKeyColumn
_getPrimaryKeyColumn()
SettingsDAO\getSettings
& getSettings($id)
Definition: SettingsDAO.inc.php:50
DAO\retrieve
& retrieve($sql, $params=false, $callHooks=true)
Definition: DAO.inc.php:85
SettingsDAO\_getCache
_getCache($id)
Definition: SettingsDAO.inc.php:260
SettingsDAO\deleteSetting
deleteSetting($id, $name, $locale=null)
Definition: SettingsDAO.inc.php:227
DAO\convertFromDB
convertFromDB($value, $type)
Definition: DAO.inc.php:341
SettingsDAO\getSetting
& getSetting($id, $name, $locale=null)
Definition: SettingsDAO.inc.php:63
SettingsDAO
Operations for retrieving and modifying settings.
Definition: SettingsDAO.inc.php:16
SettingsDAO\_buildObject
_buildObject(&$node, $paramArray=array())
Definition: SettingsDAO.inc.php:117
CacheManager\getManager
static getManager()
Definition: CacheManager.inc.php:27
DAO\update
update($sql, $params=false, $callHooks=true, $dieOnError=true)
Definition: DAO.inc.php:214
XMLParser
Generic class for parsing an XML document into a data structure.
Definition: XMLParser.inc.php:28
SettingsDAO\loadSettings
loadSettings($id)
Definition: SettingsDAO.inc.php:22
SettingsDAO\_getTableName
_getTableName()
DAO\replace
replace($table, $arrFields, $keyCols)
Definition: DAO.inc.php:243
AppLocale\getLocale
static getLocale()
Definition: env1/MockAppLocale.inc.php:40
SettingsDAO\updateSetting
updateSetting($id, $name, $value, $type=null, $isLocalized=false)
Definition: SettingsDAO.inc.php:189
DAO
Operations for retrieving and modifying objects from a database.
Definition: DAO.inc.php:31