/classes/site/SiteSettingsDAO.inc.php

https://github.com/davekisly/pkp-lib · PHP · 240 lines · 157 code · 28 blank · 55 comment · 29 complexity · 2ccccf34334a4b41655b002baaad8286 MD5 · raw file

  1. <?php
  2. /**
  3. * @file classes/site/SiteSettingsDAO.inc.php
  4. *
  5. * Copyright (c) 2000-2011 John Willinsky
  6. * Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
  7. *
  8. * @package site
  9. * @class SiteSettingsDAO
  10. *
  11. * Class for Site Settings DAO.
  12. * Operations for retrieving and modifying site settings.
  13. */
  14. class SiteSettingsDAO extends DAO {
  15. function &_getCache() {
  16. $settingCache =& Registry::get('siteSettingCache', true, null);
  17. if ($settingCache === null) {
  18. $cacheManager =& CacheManager::getManager();
  19. $settingCache = $cacheManager->getFileCache(
  20. 'siteSettings', 'site',
  21. array($this, '_cacheMiss')
  22. );
  23. }
  24. return $settingCache;
  25. }
  26. /**
  27. * Retrieve a site setting value.
  28. * @param $name string
  29. * @param $locale string optional
  30. * @return mixed
  31. */
  32. function &getSetting($name, $locale = null) {
  33. $cache =& $this->_getCache();
  34. $returner = $cache->get($name);
  35. if ($locale !== null) {
  36. if (!isset($returner[$locale]) || !is_array($returner)) {
  37. unset($returner);
  38. $returner = null;
  39. return $returner;
  40. }
  41. return $returner[$locale];
  42. }
  43. return $returner;
  44. }
  45. function _cacheMiss(&$cache, $id) {
  46. $settings =& $this->getSiteSettings();
  47. if (!isset($settings[$id])) {
  48. $cache->setCache($id, null);
  49. return null;
  50. }
  51. return $settings[$id];
  52. }
  53. /**
  54. * Retrieve and cache all settings for a site.
  55. * @return array
  56. */
  57. function &getSiteSettings() {
  58. $siteSettings = array();
  59. $result =& $this->retrieve(
  60. 'SELECT setting_name, setting_value, setting_type, locale FROM site_settings'
  61. );
  62. if ($result->RecordCount() == 0) {
  63. $returner = null;
  64. $result->Close();
  65. return $returner;
  66. } else {
  67. while (!$result->EOF) {
  68. $row =& $result->getRowAssoc(false);
  69. $value = $this->convertFromDB($row['setting_value'], $row['setting_type']);
  70. if ($row['locale'] == '') $siteSettings[$row['setting_name']] = $value;
  71. else $siteSettings[$row['setting_name']][$row['locale']] = $value;
  72. $result->MoveNext();
  73. }
  74. $result->close();
  75. unset($result);
  76. $cache =& $this->_getCache();
  77. $cache->setEntireCache($siteSettings);
  78. return $siteSettings;
  79. }
  80. }
  81. /**
  82. * Add/update a site setting.
  83. * @param $name string
  84. * @param $value mixed
  85. * @param $type string data type of the setting. If omitted, type will be guessed
  86. * @param $isLocalized boolean
  87. * @return boolean
  88. */
  89. function updateSetting($name, $value, $type = null, $isLocalized = false) {
  90. $returner = null;
  91. $cache =& $this->_getCache();
  92. $cache->setCache($name, $value);
  93. $keyFields = array('setting_name', 'locale');
  94. if (!$isLocalized) {
  95. $value = $this->convertToDB($value, $type);
  96. $this->replace('site_settings',
  97. array(
  98. 'setting_name' => $name,
  99. 'setting_value' => $value,
  100. 'setting_type' => $type,
  101. 'locale' => ''
  102. ),
  103. $keyFields
  104. );
  105. $returner = true;
  106. } else {
  107. if (is_array($value)) foreach ($value as $locale => $localeValue) {
  108. $this->update('DELETE FROM site_settings WHERE setting_name = ? AND locale = ?', array($name, $locale));
  109. if (empty($localeValue)) continue;
  110. $type = null;
  111. $returner = $this->update('INSERT INTO site_settings
  112. (setting_name, setting_value, setting_type, locale)
  113. VALUES (?, ?, ?, ?)',
  114. array(
  115. $name, $this->convertToDB($localeValue, $type), $type, $locale
  116. )
  117. );
  118. }
  119. }
  120. return $returner;
  121. }
  122. /**
  123. * Delete a site setting.
  124. * @param $name string
  125. */
  126. function deleteSetting($name, $locale = null) {
  127. $cache =& $this->_getCache();
  128. $cache->setCache($name, null);
  129. $params = array($name);
  130. $sql = 'DELETE FROM site_settings WHERE setting_name = ?';
  131. if ($locale !== null) {
  132. $params[] = $locale;
  133. $sql .= ' AND locale = ?';
  134. }
  135. return $this->update($sql, $params);
  136. }
  137. /**
  138. * Used internally by installSettings to perform variable and translation replacements.
  139. * @param $rawInput string contains text including variable and/or translate replacements.
  140. * @param $paramArray array contains variables for replacement
  141. * @returns string
  142. */
  143. function _performReplacement($rawInput, $paramArray = array()) {
  144. $value = preg_replace_callback('{{translate key="([^"]+)"}}', array(&$this, '_installer_regexp_callback'), $rawInput);
  145. foreach ($paramArray as $pKey => $pValue) {
  146. $value = str_replace('{$' . $pKey . '}', $pValue, $value);
  147. }
  148. return $value;
  149. }
  150. /**
  151. * Used internally by installSettings to recursively build nested arrays.
  152. * Deals with translation and variable replacement calls.
  153. * @param $node object XMLNode <array> tag
  154. * @param $paramArray array Parameters to be replaced in key/value contents
  155. */
  156. function &_buildObject (&$node, $paramArray = array()) {
  157. $value = array();
  158. foreach ($node->getChildren() as $element) {
  159. $key = $element->getAttribute('key');
  160. $childArray =& $element->getChildByName('array');
  161. if (isset($childArray)) {
  162. $content = $this->_buildObject($childArray, $paramArray);
  163. } else {
  164. $content = $this->_performReplacement($element->getValue(), $paramArray);
  165. }
  166. if (!empty($key)) {
  167. $key = $this->_performReplacement($key, $paramArray);
  168. $value[$key] = $content;
  169. } else $value[] = $content;
  170. }
  171. return $value;
  172. }
  173. /**
  174. * Install site settings from an XML file.
  175. * @param $filename string Name of XML file to parse and install
  176. * @param $paramArray array Optional parameters for variable replacement in settings
  177. */
  178. function installSettings($filename, $paramArray = array()) {
  179. $xmlParser = new XMLParser();
  180. $tree = $xmlParser->parse($filename);
  181. if (!$tree) {
  182. $xmlParser->destroy();
  183. return false;
  184. }
  185. foreach ($tree->getChildren() as $setting) {
  186. $nameNode =& $setting->getChildByName('name');
  187. $valueNode =& $setting->getChildByName('value');
  188. if (isset($nameNode) && isset($valueNode)) {
  189. $type = $setting->getAttribute('type');
  190. $isLocalized = $setting->getAttribute('localized') == 'true';
  191. $name =& $nameNode->getValue();
  192. if ($type == 'object') {
  193. $arrayNode =& $valueNode->getChildByName('array');
  194. $value = $this->_buildObject($arrayNode, $paramArray);
  195. } else {
  196. $value = $this->_performReplacement($valueNode->getValue(), $paramArray);
  197. }
  198. // Replace translate calls with translated content
  199. if ($isLocalized) $value = array(AppLocale::getLocale() => $value);
  200. $this->updateSetting($name, $value, $type, $isLocalized);
  201. }
  202. }
  203. $xmlParser->destroy();
  204. }
  205. /**
  206. * Used internally by site setting installation code to perform translation function.
  207. */
  208. function _installer_regexp_callback($matches) {
  209. return __($matches[1]);
  210. }
  211. }
  212. ?>