Open Journal Systems  2.4.3
 All Classes Namespaces Functions Variables Groups Pages
PluginSettingsDAO.inc.php
1 <?php
2 
18 class PluginSettingsDAO extends DAO {
19  function &_getCache($journalId, $pluginName) {
20  static $settingCache;
21 
22  if (!isset($settingCache)) {
23  $settingCache = array();
24  }
25  if (!isset($settingCache[$journalId])) {
26  $settingCache[$journalId] = array();
27  }
28  if (!isset($settingCache[$journalId][$pluginName])) {
29  $cacheManager =& CacheManager::getManager();
30  $settingCache[$journalId][$pluginName] =& $cacheManager->getFileCache(
31  'pluginSettings-' . $journalId, $pluginName,
32  array($this, '_cacheMiss')
33  );
34  }
35  return $settingCache[$journalId][$pluginName];
36  }
37 
44  function getSetting($journalId, $pluginName, $name) {
45  // Normalize the plug-in name to lower case.
46  $pluginName = strtolower_codesafe($pluginName);
47 
48  // Retrieve the setting.
49  $cache =& $this->_getCache($journalId, $pluginName);
50  return $cache->get($name);
51  }
52 
53  function _cacheMiss(&$cache, $id) {
54  $contextParts = explode('-', $cache->getContext());
55  $journalId = array_pop($contextParts);
56  $settings =& $this->getPluginSettings($journalId, $cache->getCacheId());
57  if (!isset($settings[$id])) {
58  // Make sure that even null values are cached
59  $cache->setCache($id, null);
60  return null;
61  }
62  return $settings[$id];
63  }
64 
71  function &getPluginSettings($journalId, $pluginName) {
72  // Normalize plug-in name to lower case.
73  $pluginName = strtolower_codesafe($pluginName);
74 
75  $result =& $this->retrieve(
76  'SELECT setting_name, setting_value, setting_type FROM plugin_settings WHERE plugin_name = ? AND journal_id = ?', array($pluginName, $journalId)
77  );
78 
79  $pluginSettings = array();
80  while (!$result->EOF) {
81  $row =& $result->getRowAssoc(false);
82  $pluginSettings[$row['setting_name']] = $this->convertFromDB($row['setting_value'], $row['setting_type']);
83  $result->MoveNext();
84  }
85  $result->Close();
86  unset($result);
87 
88  // Update the cache.
89  $cache =& $this->_getCache($journalId, $pluginName);
90  $cache->setEntireCache($pluginSettings);
91 
92  return $pluginSettings;
93  }
94 
103  function updateSetting($journalId, $pluginName, $name, $value, $type = null) {
104  // Normalize the plug-in name to lower case.
105  $pluginName = strtolower_codesafe($pluginName);
106 
107  $cache =& $this->_getCache($journalId, $pluginName);
108  $cache->setCache($name, $value);
109 
110  $result = $this->retrieve(
111  'SELECT COUNT(*) FROM plugin_settings WHERE plugin_name = ? AND setting_name = ? AND journal_id = ?',
112  array($pluginName, $name, $journalId)
113  );
114 
115  $value = $this->convertToDB($value, $type);
116  if ($result->fields[0] == 0) {
117  $returner = $this->update(
118  'INSERT INTO plugin_settings
119  (plugin_name, journal_id, setting_name, setting_value, setting_type)
120  VALUES
121  (?, ?, ?, ?, ?)',
122  array($pluginName, $journalId, $name, $value, $type)
123  );
124  } else {
125  $returner = $this->update(
126  'UPDATE plugin_settings SET
127  setting_value = ?,
128  setting_type = ?
129  WHERE plugin_name = ? AND setting_name = ? AND journal_id = ?',
130  array($value, $type, $pluginName, $name, $journalId)
131  );
132  }
133 
134  $result->Close();
135  unset($result);
136 
137  return $returner;
138  }
139 
146  function deleteSetting($journalId, $pluginName, $name) {
147  // Normalize the plug-in name to lower case.
148  $pluginName = strtolower_codesafe($pluginName);
149 
150  $cache =& $this->_getCache($journalId, $pluginName);
151  $cache->setCache($name, null);
152 
153  return $this->update(
154  'DELETE FROM plugin_settings WHERE plugin_name = ? AND setting_name = ? AND journal_id = ?',
155  array($pluginName, $name, $journalId)
156  );
157  }
158 
164  function deleteSettingsByPlugin($pluginName, $journalId = null) {
165  // Normalize the plug-in name to lower case.
166  $pluginName = strtolower_codesafe($pluginName);
167 
168  if ( $journalId ) {
169  $cache =& $this->_getCache($journalId, $pluginName);
170  $cache->flush();
171 
172  return $this->update(
173  'DELETE FROM plugin_settings WHERE journal_id = ? AND plugin_name = ?',
174  array($journalId, $pluginName)
175  );
176  } else {
177  $cacheManager =& CacheManager::getManager();
178  // NB: this actually deletes all plugins' settings cache
179  $cacheManager->flush('pluginSettings', CACHE_TYPE_FILE);
180 
181  return $this->update(
182  'DELETE FROM plugin_settings WHERE plugin_name = ?',
183  array($pluginName)
184  );
185  }
186  }
187 
192  function deleteSettingsByJournalId($journalId) {
193  return $this->update(
194  'DELETE FROM plugin_settings WHERE journal_id = ?', $journalId
195  );
196  }
197 
204  function _performReplacement($rawInput, $paramArray = array()) {
205  $value = preg_replace_callback('{{translate key="([^"]+)"}}', '_installer_plugin_regexp_callback', $rawInput);
206  foreach ($paramArray as $pKey => $pValue) {
207  $value = str_replace('{$' . $pKey . '}', $pValue, $value);
208  }
209  return $value;
210  }
211 
218  function &_buildObject (&$node, $paramArray = array()) {
219  $value = array();
220  foreach ($node->getChildren() as $element) {
221  $key = $element->getAttribute('key');
222  $childArray =& $element->getChildByName('array');
223  if (isset($childArray)) {
224  $content = $this->_buildObject($childArray, $paramArray);
225  } else {
226  $content = $this->_performReplacement($element->getValue(), $paramArray);
227  }
228  if (!empty($key)) {
229  $key = $this->_performReplacement($key, $paramArray);
230  $value[$key] = $content;
231  } else $value[] = $content;
232  }
233  return $value;
234  }
235 
243  function installSettings($journalId, $pluginName, $filename, $paramArray = array()) {
244  $xmlParser = new XMLParser();
245  $tree = $xmlParser->parse($filename);
246 
247  if (!$tree) {
248  $xmlParser->destroy();
249  return false;
250  }
251 
252  // Check for existing settings and leave them if they are already in place.
253  $currentSettings =& $this->getPluginSettings($journalId, $pluginName);
254 
255  foreach ($tree->getChildren() as $setting) {
256  $nameNode =& $setting->getChildByName('name');
257  $valueNode =& $setting->getChildByName('value');
258 
259  if (isset($nameNode) && isset($valueNode)) {
260  $type = $setting->getAttribute('type');
261  $name =& $nameNode->getValue();
262 
263  // If the setting already exists, respect it.
264  if (isset($currentSettings[$name])) continue;
265 
266  if ($type == 'object') {
267  $arrayNode =& $valueNode->getChildByName('array');
268  $value = $this->_buildObject($arrayNode, $paramArray);
269  } else {
270  $value = $this->_performReplacement($valueNode->getValue(), $paramArray);
271  }
272 
273  // Replace translate calls with translated content
274  $this->updateSetting($journalId, $pluginName, $name, $value, $type);
275  }
276  }
277 
278  $xmlParser->destroy();
279 
280  }
281 }
282 
286 function _installer_plugin_regexp_callback($matches) {
287  return __($matches[1]);
288 }
289 
290 ?>
Operations for retrieving and modifying objects from a database.
Definition: DAO.inc.php:29
& retrieve($sql, $params=false, $callHooks=true)
Definition: DAO.inc.php:83
deleteSettingsByJournalId($journalId)
Operations for retrieving and modifying plugin settings.
deleteSettingsByPlugin($pluginName, $journalId=null)
updateSetting($journalId, $pluginName, $name, $value, $type=null)
getSetting($journalId, $pluginName, $name)
& getPluginSettings($journalId, $pluginName)
convertFromDB($value, $type)
Definition: DAO.inc.php:338
Generic class for parsing an XML document into a data structure.
& _buildObject(&$node, $paramArray=array())
_performReplacement($rawInput, $paramArray=array())
convertToDB($value, &$type)
Definition: DAO.inc.php:394
deleteSetting($journalId, $pluginName, $name)
update($sql, $params=false, $callHooks=true, $dieOnError=true)
Definition: DAO.inc.php:211
installSettings($journalId, $pluginName, $filename, $paramArray=array())