/classes/plugins/PluginSettingsDAO.inc.php

https://github.com/mbehiels/omp · PHP · 276 lines · 159 code · 39 blank · 78 comment · 19 complexity · 7ef900c8688e2d5680e73cacfe75ae9b MD5 · raw file

  1. <?php
  2. /**
  3. * @file classes/plugins/PluginSettingsDAO.inc.php
  4. *
  5. * Copyright (c) 2003-2011 John Willinsky
  6. * Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
  7. *
  8. * @class PluginSettingsDAO
  9. * @ingroup plugins
  10. * @see Plugin
  11. *
  12. * @brief Operations for retrieving and modifying plugin settings.
  13. */
  14. class PluginSettingsDAO extends DAO {
  15. function &_getCache($pressId, $pluginName) {
  16. static $settingCache;
  17. if (!isset($settingCache)) {
  18. $settingCache = array();
  19. }
  20. if (!isset($settingCache[$pressId])) {
  21. $settingCache[$pressId] = array();
  22. }
  23. if (!isset($settingCache[$pressId][$pluginName])) {
  24. $cacheManager =& CacheManager::getManager();
  25. $settingCache[$pressId][$pluginName] = $cacheManager->getCache(
  26. 'pluginSettings-' . $pressId, $pluginName,
  27. array($this, '_cacheMiss')
  28. );
  29. }
  30. return $settingCache[$pressId][$pluginName];
  31. }
  32. /**
  33. * Retrieve a plugin setting value.
  34. * @param $pluginName string
  35. * @param $name
  36. * @return mixed
  37. */
  38. function getSetting($pressId, $pluginName, $name) {
  39. // Normalize the plug-in name to lower case.
  40. $pluginName = strtolower($pluginName);
  41. // Retrieve the setting.
  42. $cache =& $this->_getCache($pressId, $pluginName);
  43. return $cache->get($name);
  44. }
  45. function _cacheMiss(&$cache, $id) {
  46. $contextParts = explode('-', $cache->getContext());
  47. $pressId = array_pop($contextParts);
  48. $settings =& $this->getPluginSettings($pressId, $cache->getCacheId());
  49. if (!isset($settings[$id])) {
  50. // Make sure that even null values are cached
  51. $cache->setCache($id, null);
  52. return null;
  53. }
  54. return $settings[$id];
  55. }
  56. /**
  57. * Retrieve and cache all settings for a plugin.
  58. * @param $pressId int
  59. * @param $pluginName string
  60. * @return array
  61. */
  62. function &getPluginSettings($pressId, $pluginName) {
  63. // Normalize plug-in name to lower case.
  64. $pluginName = strtolower($pluginName);
  65. $result =& $this->retrieve(
  66. 'SELECT setting_name, setting_value, setting_type FROM plugin_settings WHERE plugin_name = ? AND press_id = ?', array($pluginName, $pressId)
  67. );
  68. $pluginSettings = array();
  69. while (!$result->EOF) {
  70. $row =& $result->getRowAssoc(false);
  71. $pluginSettings[$row['setting_name']] = $this->convertFromDB($row['setting_value'], $row['setting_type']);
  72. $result->MoveNext();
  73. }
  74. $result->Close();
  75. unset($result);
  76. $cache =& $this->_getCache($pressId, $pluginName);
  77. $cache->setEntireCache($pluginSettings);
  78. return $pluginSettings;
  79. }
  80. /**
  81. * Add/update a plugin setting.
  82. * @param $pressId int
  83. * @param $pluginName string
  84. * @param $name string
  85. * @param $value mixed
  86. * @param $type string data type of the setting. If omitted, type will be guessed
  87. */
  88. function updateSetting($pressId, $pluginName, $name, $value, $type = null) {
  89. // Normalize the plug-in name to lower case.
  90. $pluginName = strtolower($pluginName);
  91. $cache =& $this->_getCache($pressId, $pluginName);
  92. $cache->setCache($name, $value);
  93. $result = $this->retrieve(
  94. 'SELECT COUNT(*) FROM plugin_settings WHERE plugin_name = ? AND setting_name = ? AND press_id = ?',
  95. array($pluginName, $name, $pressId)
  96. );
  97. $value = $this->convertToDB($value, $type);
  98. if ($result->fields[0] == 0) {
  99. $returner = $this->update(
  100. 'INSERT INTO plugin_settings
  101. (plugin_name, press_id, setting_name, setting_value, setting_type)
  102. VALUES
  103. (?, ?, ?, ?, ?)',
  104. array($pluginName, $pressId, $name, $value, $type)
  105. );
  106. } else {
  107. $returner = $this->update(
  108. 'UPDATE plugin_settings SET
  109. setting_value = ?,
  110. setting_type = ?
  111. WHERE plugin_name = ? AND setting_name = ? AND press_id = ?',
  112. array($value, $type, $pluginName, $name, $pressId)
  113. );
  114. }
  115. $result->Close();
  116. unset($result);
  117. return $returner;
  118. }
  119. /**
  120. * Delete a plugin setting.
  121. * @param $pressId int
  122. * @param $pluginName int
  123. * @param $name string
  124. */
  125. function deleteSetting($pressId, $pluginName, $name) {
  126. // Normalize the plug-in name to lower case.
  127. $pluginName = strtolower($pluginName);
  128. $cache =& $this->_getCache($pressId, $pluginName);
  129. $cache->setCache($name, null);
  130. return $this->update(
  131. 'DELETE FROM plugin_settings WHERE plugin_name = ? AND setting_name = ? AND press_id = ?',
  132. array($pluginName, $name, $pressId)
  133. );
  134. }
  135. /**
  136. * Delete all settings for a plugin.
  137. * @param $pressId int
  138. * @param $pluginName string
  139. */
  140. function deleteSettingsByPlugin($pressId, $pluginName) {
  141. // Normalize the plug-in name to lower case.
  142. $pluginName = strtolower($pluginName);
  143. $cache =& $this->_getCache($pressId, $pluginName);
  144. $cache->flush();
  145. return $this->update(
  146. 'DELETE FROM plugin_settings WHERE press_id = ? AND plugin_name = ?',
  147. array($pressId, $pluginName)
  148. );
  149. }
  150. /**
  151. * Delete all settings for a press.
  152. * @param $pressId int
  153. */
  154. function deleteSettingsByPressId($pressId) {
  155. return $this->update(
  156. 'DELETE FROM plugin_settings WHERE press_id = ?', $pressId
  157. );
  158. }
  159. /**
  160. * Used internally by installSettings to perform variable and translation replacements.
  161. * @param $rawInput string contains text including variable and/or translate replacements.
  162. * @param $paramArray array contains variables for replacement
  163. * @returns string
  164. */
  165. function _performReplacement($rawInput, $paramArray = array()) {
  166. $value = preg_replace_callback('{{translate key="([^"]+)"}}', '_installer_plugin_regexp_callback', $rawInput);
  167. foreach ($paramArray as $pKey => $pValue) {
  168. $value = str_replace('{$' . $pKey . '}', $pValue, $value);
  169. }
  170. return $value;
  171. }
  172. /**
  173. * Used internally by installSettings to recursively build nested arrays.
  174. * Deals with translation and variable replacement calls.
  175. * @param $node object XMLNode <array> tag
  176. * @param $paramArray array Parameters to be replaced in key/value contents
  177. */
  178. function &_buildObject (&$node, $paramArray = array()) {
  179. $value = array();
  180. foreach ($node->getChildren() as $element) {
  181. $key = $element->getAttribute('key');
  182. $childArray =& $element->getChildByName('array');
  183. if (isset($childArray)) {
  184. $content = $this->_buildObject($childArray, $paramArray);
  185. } else {
  186. $content = $this->_performReplacement($element->getValue(), $paramArray);
  187. }
  188. if (!empty($key)) {
  189. $key = $this->_performReplacement($key, $paramArray);
  190. $value[$key] = $content;
  191. } else $value[] = $content;
  192. }
  193. return $value;
  194. }
  195. /**
  196. * Install plugin settings from an XML file.
  197. * @param $pluginName name of plugin for settings to apply to
  198. * @param $filename string Name of XML file to parse and install
  199. * @param $paramArray array Optional parameters for variable replacement in settings
  200. */
  201. function installSettings($pressId, $pluginName, $filename, $paramArray = array()) {
  202. $xmlParser = new XMLParser();
  203. $tree = $xmlParser->parse($filename);
  204. if (!$tree) {
  205. $xmlParser->destroy();
  206. return false;
  207. }
  208. // Check for existing settings and leave them if they are already in place.
  209. $currentSettings =& $this->getPluginSettings($pressId, $pluginName);
  210. foreach ($tree->getChildren() as $setting) {
  211. $nameNode =& $setting->getChildByName('name');
  212. $valueNode =& $setting->getChildByName('value');
  213. if (isset($nameNode) && isset($valueNode)) {
  214. $type = $setting->getAttribute('type');
  215. $name =& $nameNode->getValue();
  216. // If the setting already exists, respect it.
  217. if (isset($currentSettings[$name])) continue;
  218. if ($type == 'object') {
  219. $arrayNode =& $valueNode->getChildByName('array');
  220. $value = $this->_buildObject($arrayNode, $paramArray);
  221. } else {
  222. $value = $this->_performReplacement($valueNode->getValue(), $paramArray);
  223. }
  224. // Replace translate calls with translated content
  225. $this->updateSetting($pressId, $pluginName, $name, $value, $type);
  226. }
  227. }
  228. $xmlParser->destroy();
  229. }
  230. }
  231. /**
  232. * Used internally by plugin setting installation code to perform translation function.
  233. */
  234. function _installer_plugin_regexp_callback($matches) {
  235. return Locale::translate($matches[1]);
  236. }
  237. ?>