Open Journal Systems  3.0.0
 All Classes Namespaces Functions Variables Groups Pages
PluginSettingsDAO.inc.php
1 <?php
2 
17 class PluginSettingsDAO extends DAO {
21  function PluginSettingsDAO() {
22  parent::DAO();
23  }
24 
31  function _getCache($contextId, $pluginName) {
32  static $settingCache;
33 
34  if (!isset($settingCache)) {
35  $settingCache = array();
36  }
37  if (!isset($settingCache[$contextId])) {
38  $settingCache[$contextId] = array();
39  }
40  if (!isset($settingCache[$contextId][$pluginName])) {
41  $cacheManager = CacheManager::getManager();
42  $settingCache[$contextId][$pluginName] = $cacheManager->getCache(
43  'pluginSettings-' . $contextId, $pluginName,
44  array($this, '_cacheMiss')
45  );
46  }
47  return $settingCache[$contextId][$pluginName];
48  }
49 
57  function getSetting($contextId, $pluginName, $name) {
58  // Normalize the plug-in name to lower case.
59  $pluginName = strtolower_codesafe($pluginName);
60 
61  // Retrieve the setting.
62  $cache = $this->_getCache($contextId, $pluginName);
63  return $cache->get($name);
64  }
65 
72  function _cacheMiss($cache, $id) {
73  $contextParts = explode('-', $cache->getContext());
74  $contextId = array_pop($contextParts);
75  $settings = $this->getPluginSettings($contextId, $cache->getCacheId());
76  if (!isset($settings[$id])) {
77  // Make sure that even null values are cached
78  $cache->setCache($id, null);
79  return null;
80  }
81  return $settings[$id];
82  }
83 
90  function getPluginSettings($contextId, $pluginName) {
91  // Normalize plug-in name to lower case.
92  $pluginName = strtolower_codesafe($pluginName);
93 
95  $result = $this->retrieve(
96  'SELECT setting_name, setting_value, setting_type FROM plugin_settings WHERE plugin_name = ? AND ' . $contextColumn . ' = ?', array($pluginName, (int) $contextId)
97  );
98 
99  $pluginSettings = array();
100  while (!$result->EOF) {
101  $row = $result->getRowAssoc(false);
102  $pluginSettings[$row['setting_name']] = $this->convertFromDB($row['setting_value'], $row['setting_type']);
103  $result->MoveNext();
104  }
105  $result->Close();
106 
107  $cache = $this->_getCache($contextId, $pluginName);
108  $cache->setEntireCache($pluginSettings);
109 
110  return $pluginSettings;
111  }
112 
122  function updateSetting($contextId, $pluginName, $name, $value, $type = null) {
123  // Normalize the plug-in name to lower case.
124  $pluginName = strtolower_codesafe($pluginName);
125 
126  $cache = $this->_getCache($contextId, $pluginName);
127  $cache->setCache($name, $value);
128 
129  $value = $this->convertToDB($value, $type);
130 
131  return $this->replace(
132  'plugin_settings',
133  array(
134  'context_id' => (int) $contextId,
135  'plugin_name' => $pluginName,
136  'setting_name' => $name,
137  'setting_value' => $value,
138  'setting_type' => $type,
139  ),
140  array('context_id', 'plugin_name', 'setting_name')
141  );
142  }
143 
150  function deleteSetting($contextId, $pluginName, $name) {
151  // Normalize the plug-in name to lower case.
152  $pluginName = strtolower_codesafe($pluginName);
153 
154  $cache = $this->_getCache($contextId, $pluginName);
155  $cache->setCache($name, null);
156 
157  return $this->update(
158  'DELETE FROM plugin_settings WHERE plugin_name = ? AND setting_name = ? AND context_id = ?',
159  array($pluginName, $name, (int) $contextId)
160  );
161  }
162 
168  function deleteSettingsByPlugin($contextId, $pluginName) {
169  // Normalize the plug-in name to lower case.
170  $pluginName = strtolower_codesafe($pluginName);
171 
172  $cache = $this->_getCache($contextId, $pluginName);
173  $cache->flush();
174 
175  return $this->update(
176  'DELETE FROM plugin_settings WHERE context_id = ? AND plugin_name = ?',
177  array((int) $contextId, $pluginName)
178  );
179  }
180 
185  function deleteByContextId($contextId) {
186  return $this->update(
187  'DELETE FROM plugin_settings WHERE context_id = ?', (int) $contextId
188  );
189  }
190 
197  function _performReplacement($rawInput, $paramArray = array()) {
198  $value = preg_replace_callback('{{translate key="([^"]+)"}}', '_installer_plugin_regexp_callback', $rawInput);
199  foreach ($paramArray as $pKey => $pValue) {
200  $value = str_replace('{$' . $pKey . '}', $pValue, $value);
201  }
202  return $value;
203  }
204 
211  function _buildObject ($node, $paramArray = array()) {
212  $value = array();
213  foreach ($node->getChildren() as $element) {
214  $key = $element->getAttribute('key');
215  $childArray = $element->getChildByName('array');
216  if (isset($childArray)) {
217  $content = $this->_buildObject($childArray, $paramArray);
218  } else {
219  $content = $this->_performReplacement($element->getValue(), $paramArray);
220  }
221  if (!empty($key)) {
222  $key = $this->_performReplacement($key, $paramArray);
223  $value[$key] = $content;
224  } else $value[] = $content;
225  }
226  return $value;
227  }
228 
235  function installSettings($contextId, $pluginName, $filename, $paramArray = array()) {
236  $xmlParser = new XMLParser();
237  $tree = $xmlParser->parse($filename);
238 
239  if (!$tree) {
240  $xmlParser->destroy();
241  return false;
242  }
243 
244  // Check for existing settings and leave them if they are already in place.
245  $currentSettings = $this->getPluginSettings($contextId, $pluginName);
246 
247  foreach ($tree->getChildren() as $setting) {
248  $nameNode = $setting->getChildByName('name');
249  $valueNode = $setting->getChildByName('value');
250 
251  if (isset($nameNode) && isset($valueNode)) {
252  $type = $setting->getAttribute('type');
253  $name = $nameNode->getValue();
254 
255  // If the setting already exists, respect it.
256  if (isset($currentSettings[$name])) continue;
257 
258  if ($type == 'object') {
259  $arrayNode = $valueNode->getChildByName('array');
260  $value = $this->_buildObject($arrayNode, $paramArray);
261  } else {
262  $value = $this->_performReplacement($valueNode->getValue(), $paramArray);
263  }
264 
265  // Replace translate calls with translated content
266  $this->updateSetting($contextId, $pluginName, $name, $value, $type);
267  }
268  }
269 
270  $xmlParser->destroy();
271  }
272 }
273 
280 function _installer_plugin_regexp_callback($matches) {
281  return __($matches[1]);
282 }
283 
284 ?>
deleteSettingsByPlugin($contextId, $pluginName)
Operations for retrieving and modifying objects from a database.
Definition: DAO.inc.php:30
getPluginSettings($contextId, $pluginName)
& retrieve($sql, $params=false, $callHooks=true)
Definition: DAO.inc.php:84
getSetting($contextId, $pluginName, $name)
Operations for retrieving and modifying plugin settings.
updateSetting($contextId, $pluginName, $name, $value, $type=null)
deleteSetting($contextId, $pluginName, $name)
static getPluginSettingsContextColumnName()
convertFromDB($value, $type)
Definition: DAO.inc.php:335
installSettings($contextId, $pluginName, $filename, $paramArray=array())
_getCache($contextId, $pluginName)
Generic class for parsing an XML document into a data structure.
_performReplacement($rawInput, $paramArray=array())
convertToDB($value, &$type)
Definition: DAO.inc.php:391
_buildObject($node, $paramArray=array())
replace($table, $arrFields, $keyCols)
Definition: DAO.inc.php:237
update($sql, $params=false, $callHooks=true, $dieOnError=true)
Definition: DAO.inc.php:208