Open Monograph Press  3.3.0
PluginSettingsDAO.inc.php
1 <?php
2 
17 class PluginSettingsDAO extends DAO {
18 
25  function _getCache($contextId, $pluginName) {
26  static $settingCache;
27 
28  if (!isset($settingCache)) {
29  $settingCache = array();
30  }
31  if (!isset($settingCache[$contextId])) {
32  $settingCache[$contextId] = array();
33  }
34  if (!isset($settingCache[$contextId][$pluginName])) {
35  $cacheManager = CacheManager::getManager();
36  $settingCache[$contextId][$pluginName] = $cacheManager->getCache(
37  'pluginSettings-' . $contextId, $pluginName,
38  array($this, '_cacheMiss')
39  );
40  }
41  return $settingCache[$contextId][$pluginName];
42  }
43 
51  function getSetting($contextId, $pluginName, $name) {
52  // Normalize the plug-in name to lower case.
53  $pluginName = strtolower_codesafe($pluginName);
54 
55  // Retrieve the setting.
56  $cache = $this->_getCache($contextId, $pluginName);
57  return $cache->get($name);
58  }
59 
67  function settingExists($contextId, $pluginName, $name) {
68  $pluginName = strtolower_codesafe($pluginName);
69  $result = $this->retrieve(
70  'SELECT COUNT(*) FROM plugin_settings WHERE plugin_name = ? AND context_id = ? AND setting_name = ?', array($pluginName, (int) $contextId, $name)
71  );
72  $returner = $result->fields[0] ? true : false;
73  $result->Close();
74  return $returner;
75  }
76 
83  function _cacheMiss($cache, $id) {
84  $contextParts = explode('-', $cache->getContext());
85  $contextId = array_pop($contextParts);
86  $settings = $this->getPluginSettings($contextId, $cache->getCacheId());
87  if (!isset($settings[$id])) {
88  // Make sure that even null values are cached
89  $cache->setCache($id, null);
90  return null;
91  }
92  return $settings[$id];
93  }
94 
101  function getPluginSettings($contextId, $pluginName) {
102  // Normalize plug-in name to lower case.
103  $pluginName = strtolower_codesafe($pluginName);
104 
105  $result = $this->retrieve(
106  'SELECT setting_name, setting_value, setting_type FROM plugin_settings WHERE plugin_name = ? AND context_id = ?', array($pluginName, (int) $contextId)
107  );
108 
109  $pluginSettings = array();
110  while (!$result->EOF) {
111  $row = $result->getRowAssoc(false);
112  $pluginSettings[$row['setting_name']] = $this->convertFromDB($row['setting_value'], $row['setting_type']);
113  $result->MoveNext();
114  }
115  $result->Close();
116 
117  $cache = $this->_getCache($contextId, $pluginName);
118  $cache->setEntireCache($pluginSettings);
119 
120  return $pluginSettings;
121  }
122 
132  function updateSetting($contextId, $pluginName, $name, $value, $type = null) {
133  // Normalize the plug-in name to lower case.
134  $pluginName = strtolower_codesafe($pluginName);
135 
136  $cache = $this->_getCache($contextId, $pluginName);
137  $cache->setCache($name, $value);
138 
139  $value = $this->convertToDB($value, $type);
140 
141  return $this->replace(
142  'plugin_settings',
143  array(
144  'context_id' => (int) $contextId,
145  'plugin_name' => $pluginName,
146  'setting_name' => $name,
147  'setting_value' => $value,
148  'setting_type' => $type,
149  ),
150  array('context_id', 'plugin_name', 'setting_name')
151  );
152  }
153 
160  function deleteSetting($contextId, $pluginName, $name) {
161  // Normalize the plug-in name to lower case.
162  $pluginName = strtolower_codesafe($pluginName);
163 
164  $cache = $this->_getCache($contextId, $pluginName);
165  $cache->setCache($name, null);
166 
167  return $this->update(
168  'DELETE FROM plugin_settings WHERE plugin_name = ? AND setting_name = ? AND context_id = ?',
169  array($pluginName, $name, (int) $contextId)
170  );
171  }
172 
178  function deleteSettingsByPlugin($contextId, $pluginName) {
179  // Normalize the plug-in name to lower case.
180  $pluginName = strtolower_codesafe($pluginName);
181 
182  $cache = $this->_getCache($contextId, $pluginName);
183  $cache->flush();
184 
185  return $this->update(
186  'DELETE FROM plugin_settings WHERE context_id = ? AND plugin_name = ?',
187  array((int) $contextId, $pluginName)
188  );
189  }
190 
195  function deleteByContextId($contextId) {
196  return $this->update(
197  'DELETE FROM plugin_settings WHERE context_id = ?', (int) $contextId
198  );
199  }
200 
207  function _performReplacement($rawInput, $paramArray = array()) {
208  $value = preg_replace_callback('{{translate key="([^"]+)"}}', '_installer_plugin_regexp_callback', $rawInput);
209  foreach ($paramArray as $pKey => $pValue) {
210  $value = str_replace('{$' . $pKey . '}', $pValue, $value);
211  }
212  return $value;
213  }
214 
221  function _buildObject ($node, $paramArray = array()) {
222  $value = array();
223  foreach ($node->getChildren() as $element) {
224  $key = $element->getAttribute('key');
225  $childArray = $element->getChildByName('array');
226  if (isset($childArray)) {
227  $content = $this->_buildObject($childArray, $paramArray);
228  } else {
229  $content = $this->_performReplacement($element->getValue(), $paramArray);
230  }
231  if (!empty($key)) {
232  $key = $this->_performReplacement($key, $paramArray);
233  $value[$key] = $content;
234  } else $value[] = $content;
235  }
236  return $value;
237  }
238 
245  function installSettings($contextId, $pluginName, $filename, $paramArray = array()) {
246  $xmlParser = new XMLParser();
247  $tree = $xmlParser->parse($filename);
248 
249  if (!$tree) return false;
250 
251  // Check for existing settings and leave them if they are already in place.
252  $currentSettings = $this->getPluginSettings($contextId, $pluginName);
253 
254  foreach ($tree->getChildren() as $setting) {
255  $nameNode = $setting->getChildByName('name');
256  $valueNode = $setting->getChildByName('value');
257 
258  if (isset($nameNode) && isset($valueNode)) {
259  $type = $setting->getAttribute('type');
260  $name = $nameNode->getValue();
261 
262  // If the setting already exists, respect it.
263  if (isset($currentSettings[$name])) continue;
264 
265  if ($type == 'object') {
266  $arrayNode = $valueNode->getChildByName('array');
267  $value = $this->_buildObject($arrayNode, $paramArray);
268  } else {
269  $value = $this->_performReplacement($valueNode->getValue(), $paramArray);
270  }
271 
272  // Replace translate calls with translated content
273  $this->updateSetting($contextId, $pluginName, $name, $value, $type);
274  }
275  }
276  }
277 }
278 
285 function _installer_plugin_regexp_callback($matches) {
286  return __($matches[1]);
287 }
288 
289 
PluginSettingsDAO\getPluginSettings
getPluginSettings($contextId, $pluginName)
Definition: PluginSettingsDAO.inc.php:101
DAO\convertToDB
convertToDB($value, &$type)
Definition: DAO.inc.php:401
PluginSettingsDAO\getSetting
getSetting($contextId, $pluginName, $name)
Definition: PluginSettingsDAO.inc.php:51
PluginSettingsDAO\updateSetting
updateSetting($contextId, $pluginName, $name, $value, $type=null)
Definition: PluginSettingsDAO.inc.php:132
DAO\retrieve
& retrieve($sql, $params=false, $callHooks=true)
Definition: DAO.inc.php:85
DAO\convertFromDB
convertFromDB($value, $type)
Definition: DAO.inc.php:341
PluginSettingsDAO\_cacheMiss
_cacheMiss($cache, $id)
Definition: PluginSettingsDAO.inc.php:83
CacheManager\getManager
static getManager()
Definition: CacheManager.inc.php:27
PluginSettingsDAO\deleteSetting
deleteSetting($contextId, $pluginName, $name)
Definition: PluginSettingsDAO.inc.php:160
PluginSettingsDAO\settingExists
settingExists($contextId, $pluginName, $name)
Definition: PluginSettingsDAO.inc.php:67
DAO\update
update($sql, $params=false, $callHooks=true, $dieOnError=true)
Definition: DAO.inc.php:214
PluginSettingsDAO\_performReplacement
_performReplacement($rawInput, $paramArray=array())
Definition: PluginSettingsDAO.inc.php:207
XMLParser
Generic class for parsing an XML document into a data structure.
Definition: XMLParser.inc.php:28
PluginSettingsDAO\_buildObject
_buildObject($node, $paramArray=array())
Definition: PluginSettingsDAO.inc.php:221
strtolower_codesafe
strtolower_codesafe($str)
Definition: functions.inc.php:280
PluginSettingsDAO
Operations for retrieving and modifying plugin settings.
Definition: PluginSettingsDAO.inc.php:17
PluginSettingsDAO\installSettings
installSettings($contextId, $pluginName, $filename, $paramArray=array())
Definition: PluginSettingsDAO.inc.php:245
PluginSettingsDAO\_getCache
_getCache($contextId, $pluginName)
Definition: PluginSettingsDAO.inc.php:25
DAO\replace
replace($table, $arrFields, $keyCols)
Definition: DAO.inc.php:243
PluginSettingsDAO\deleteByContextId
deleteByContextId($contextId)
Definition: PluginSettingsDAO.inc.php:195
DAO
Operations for retrieving and modifying objects from a database.
Definition: DAO.inc.php:31
PluginSettingsDAO\deleteSettingsByPlugin
deleteSettingsByPlugin($contextId, $pluginName)
Definition: PluginSettingsDAO.inc.php:178