| 1 | <?php |
|---|
| 2 | // $Id: config.php,v 1.3 2006/05/01 02:37:28 onokazu Exp $ |
|---|
| 3 | // ------------------------------------------------------------------------ // |
|---|
| 4 | // XOOPS - PHP Content Management System // |
|---|
| 5 | // Copyright (c) 2000 XOOPS.org // |
|---|
| 6 | // <http://www.xoops.org/> // |
|---|
| 7 | // ------------------------------------------------------------------------ // |
|---|
| 8 | // This program is free software; you can redistribute it and/or modify // |
|---|
| 9 | // it under the terms of the GNU General Public License as published by // |
|---|
| 10 | // the Free Software Foundation; either version 2 of the License, or // |
|---|
| 11 | // (at your option) any later version. // |
|---|
| 12 | // // |
|---|
| 13 | // You may not change or alter any portion of this comment or credits // |
|---|
| 14 | // of supporting developers from this source code or any supporting // |
|---|
| 15 | // source code which is considered copyrighted (c) material of the // |
|---|
| 16 | // original comment or credit authors. // |
|---|
| 17 | // // |
|---|
| 18 | // This program is distributed in the hope that it will be useful, // |
|---|
| 19 | // but WITHOUT ANY WARRANTY; without even the implied warranty of // |
|---|
| 20 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // |
|---|
| 21 | // GNU General Public License for more details. // |
|---|
| 22 | // // |
|---|
| 23 | // You should have received a copy of the GNU General Public License // |
|---|
| 24 | // along with this program; if not, write to the Free Software // |
|---|
| 25 | // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA // |
|---|
| 26 | // ------------------------------------------------------------------------ // |
|---|
| 27 | // Author: Kazumi Ono (AKA onokazu) // |
|---|
| 28 | // URL: http://www.myweb.ne.jp/, http://www.xoops.org/, http://jp.xoops.org/ // |
|---|
| 29 | // Project: The XOOPS Project // |
|---|
| 30 | // ------------------------------------------------------------------------- // |
|---|
| 31 | |
|---|
| 32 | if (!defined('XOOPS_ROOT_PATH')) { |
|---|
| 33 | exit(); |
|---|
| 34 | } |
|---|
| 35 | |
|---|
| 36 | require_once XOOPS_ROOT_PATH.'/kernel/configoption.php'; |
|---|
| 37 | require_once XOOPS_ROOT_PATH.'/kernel/configitem.php'; |
|---|
| 38 | |
|---|
| 39 | /** |
|---|
| 40 | * @package kernel |
|---|
| 41 | * |
|---|
| 42 | * @author Kazumi Ono <[email protected]> |
|---|
| 43 | * @copyright copyright (c) 2000-2003 XOOPS.org |
|---|
| 44 | */ |
|---|
| 45 | |
|---|
| 46 | |
|---|
| 47 | /** |
|---|
| 48 | * XOOPS configuration handling class. |
|---|
| 49 | * This class acts as an interface for handling general configurations of XOOPS |
|---|
| 50 | * and its modules. |
|---|
| 51 | * |
|---|
| 52 | * |
|---|
| 53 | * @author Kazumi Ono <[email protected]> |
|---|
| 54 | * @todo Tests that need to be made: |
|---|
| 55 | * - error handling |
|---|
| 56 | * @access public |
|---|
| 57 | */ |
|---|
| 58 | |
|---|
| 59 | class XoopsConfigHandler |
|---|
| 60 | { |
|---|
| 61 | |
|---|
| 62 | /** |
|---|
| 63 | * holds reference to config item handler(DAO) class |
|---|
| 64 | * |
|---|
| 65 | * @var object |
|---|
| 66 | * @access private |
|---|
| 67 | */ |
|---|
| 68 | var $_cHandler; |
|---|
| 69 | |
|---|
| 70 | /** |
|---|
| 71 | * holds reference to config option handler(DAO) class |
|---|
| 72 | * |
|---|
| 73 | * @var object |
|---|
| 74 | * @access private |
|---|
| 75 | */ |
|---|
| 76 | var $_oHandler; |
|---|
| 77 | |
|---|
| 78 | /** |
|---|
| 79 | * holds an array of cached references to config value arrays, |
|---|
| 80 | * indexed on module id and category id |
|---|
| 81 | * |
|---|
| 82 | * @var array |
|---|
| 83 | * @access private |
|---|
| 84 | */ |
|---|
| 85 | var $_cachedConfigs = array(); |
|---|
| 86 | |
|---|
| 87 | /** |
|---|
| 88 | * Constructor |
|---|
| 89 | * |
|---|
| 90 | * @param object &$db reference to database object |
|---|
| 91 | */ |
|---|
| 92 | function XoopsConfigHandler(&$db) |
|---|
| 93 | { |
|---|
| 94 | $this->_cHandler =& new XoopsConfigItemHandler($db); |
|---|
| 95 | $this->_oHandler =& new XoopsConfigOptionHandler($db); |
|---|
| 96 | } |
|---|
| 97 | |
|---|
| 98 | /** |
|---|
| 99 | * Create a config |
|---|
| 100 | * |
|---|
| 101 | * @see XoopsConfigItem |
|---|
| 102 | * @return object reference to the new {@link XoopsConfigItem} |
|---|
| 103 | */ |
|---|
| 104 | function &createConfig() |
|---|
| 105 | { |
|---|
| 106 | $ret =& $this->_cHandler->create(); |
|---|
| 107 | return $ret; |
|---|
| 108 | } |
|---|
| 109 | |
|---|
| 110 | /** |
|---|
| 111 | * Get a config |
|---|
| 112 | * |
|---|
| 113 | * @param int $id ID of the config |
|---|
| 114 | * @param bool $withoptions load the config's options now? |
|---|
| 115 | * @return object reference to the {@link XoopsConfig} |
|---|
| 116 | */ |
|---|
| 117 | function &getConfig($id, $withoptions = false) |
|---|
| 118 | { |
|---|
| 119 | $config =& $this->_cHandler->get($id); |
|---|
| 120 | if ($withoptions == true) { |
|---|
| 121 | $config->setConfOptions($this->getConfigOptions(new Criteria('conf_id', $id))); |
|---|
| 122 | } |
|---|
| 123 | return $config; |
|---|
| 124 | } |
|---|
| 125 | |
|---|
| 126 | /** |
|---|
| 127 | * insert a new config in the database |
|---|
| 128 | * |
|---|
| 129 | * @param object &$config reference to the {@link XoopsConfigItem} |
|---|
| 130 | */ |
|---|
| 131 | function insertConfig(&$config) |
|---|
| 132 | { |
|---|
| 133 | if (!$this->_cHandler->insert($config)) { |
|---|
| 134 | return false; |
|---|
| 135 | } |
|---|
| 136 | $options =& $config->getConfOptions(); |
|---|
| 137 | $count = count($options); |
|---|
| 138 | $conf_id = $config->getVar('conf_id'); |
|---|
| 139 | for ($i = 0; $i < $count; $i++) { |
|---|
| 140 | $options[$i]->setVar('conf_id', $conf_id); |
|---|
| 141 | if (!$this->_oHandler->insert($options[$i])) { |
|---|
| 142 | echo $options[$i]->getErrors(); |
|---|
| 143 | } |
|---|
| 144 | } |
|---|
| 145 | if (!empty($this->_cachedConfigs[$config->getVar('conf_modid')][$config->getVar('conf_catid')])) { |
|---|
| 146 | unset ($this->_cachedConfigs[$config->getVar('conf_modid')][$config->getVar('conf_catid')]); |
|---|
| 147 | } |
|---|
| 148 | return true; |
|---|
| 149 | } |
|---|
| 150 | |
|---|
| 151 | /** |
|---|
| 152 | * Delete a config from the database |
|---|
| 153 | * |
|---|
| 154 | * @param object &$config reference to a {@link XoopsConfigItem} |
|---|
| 155 | */ |
|---|
| 156 | function deleteConfig(&$config) |
|---|
| 157 | { |
|---|
| 158 | if (!$this->_cHandler->delete($config)) { |
|---|
| 159 | return false; |
|---|
| 160 | } |
|---|
| 161 | $options =& $config->getConfOptions(); |
|---|
| 162 | $count = count($options); |
|---|
| 163 | if ($count == 0) { |
|---|
| 164 | $options =& $this->getConfigOptions(new Criteria('conf_id', $config->getVar('conf_id'))); |
|---|
| 165 | $count = count($options); |
|---|
| 166 | } |
|---|
| 167 | if (is_array($options) && $count > 0) { |
|---|
| 168 | for ($i = 0; $i < $count; $i++) { |
|---|
| 169 | $this->_oHandler->delete($options[$i]); |
|---|
| 170 | } |
|---|
| 171 | } |
|---|
| 172 | if (!empty($this->_cachedConfigs[$config->getVar('conf_modid')][$config->getVar('conf_catid')])) { |
|---|
| 173 | unset ($this->_cachedConfigs[$config->getVar('conf_modid')][$config->getVar('conf_catid')]); |
|---|
| 174 | } |
|---|
| 175 | return true; |
|---|
| 176 | } |
|---|
| 177 | |
|---|
| 178 | /** |
|---|
| 179 | * get one or more Configs |
|---|
| 180 | * |
|---|
| 181 | * @param object $criteria {@link CriteriaElement} |
|---|
| 182 | * @param bool $id_as_key Use the configs' ID as keys? |
|---|
| 183 | * @param bool $with_options get the options now? |
|---|
| 184 | * |
|---|
| 185 | * @return array Array of {@link XoopsConfigItem} objects |
|---|
| 186 | */ |
|---|
| 187 | function &getConfigs($criteria = null, $id_as_key = false, $with_options = false) |
|---|
| 188 | { |
|---|
| 189 | $ret =& $this->_cHandler->getObjects($criteria, $id_as_key); |
|---|
| 190 | return $ret; |
|---|
| 191 | } |
|---|
| 192 | |
|---|
| 193 | /** |
|---|
| 194 | * Count some configs |
|---|
| 195 | * |
|---|
| 196 | * @param object $criteria {@link CriteriaElement} |
|---|
| 197 | */ |
|---|
| 198 | function getConfigCount($criteria = null) |
|---|
| 199 | { |
|---|
| 200 | return $this->_cHandler->getCount($criteria); |
|---|
| 201 | } |
|---|
| 202 | |
|---|
| 203 | /** |
|---|
| 204 | * Get configs from a certain category |
|---|
| 205 | * |
|---|
| 206 | * @param int $category ID of a category |
|---|
| 207 | * @param int $module ID of a module |
|---|
| 208 | * |
|---|
| 209 | * @return array array of {@link XoopsConfig}s |
|---|
| 210 | */ |
|---|
| 211 | function &getConfigsByCat($category, $module = 0) |
|---|
| 212 | { |
|---|
| 213 | static $_cachedConfigs; |
|---|
| 214 | if (!empty($_cachedConfigs[$module][$category])) { |
|---|
| 215 | return $_cachedConfigs[$module][$category]; |
|---|
| 216 | } else { |
|---|
| 217 | $ret = array(); |
|---|
| 218 | $criteria = new CriteriaCompo(new Criteria('conf_modid', intval($module))); |
|---|
| 219 | if (!empty($category)) { |
|---|
| 220 | $criteria->add(new Criteria('conf_catid', intval($category))); |
|---|
| 221 | } |
|---|
| 222 | $configs =& $this->getConfigs($criteria, true); |
|---|
| 223 | if (is_array($configs)) { |
|---|
| 224 | foreach (array_keys($configs) as $i) { |
|---|
| 225 | $ret[$configs[$i]->getVar('conf_name')] = $configs[$i]->getConfValueForOutput(); |
|---|
| 226 | } |
|---|
| 227 | } |
|---|
| 228 | $_cachedConfigs[$module][$category] =& $ret; |
|---|
| 229 | return $ret; |
|---|
| 230 | } |
|---|
| 231 | } |
|---|
| 232 | |
|---|
| 233 | /** |
|---|
| 234 | * Make a new {@link XoopsConfigOption} |
|---|
| 235 | * |
|---|
| 236 | * @return object {@link XoopsConfigOption} |
|---|
| 237 | */ |
|---|
| 238 | function &createConfigOption(){ |
|---|
| 239 | $ret =& $this->_oHandler->create(); |
|---|
| 240 | return $ret; |
|---|
| 241 | } |
|---|
| 242 | |
|---|
| 243 | /** |
|---|
| 244 | * Get a {@link XoopsConfigOption} |
|---|
| 245 | * |
|---|
| 246 | * @param int $id ID of the config option |
|---|
| 247 | * |
|---|
| 248 | * @return object {@link XoopsConfigOption} |
|---|
| 249 | */ |
|---|
| 250 | function &getConfigOption($id) |
|---|
| 251 | { |
|---|
| 252 | $ret =& $this->_oHandler->get($id); |
|---|
| 253 | return $ret; |
|---|
| 254 | } |
|---|
| 255 | |
|---|
| 256 | /** |
|---|
| 257 | * Get one or more {@link XoopsConfigOption}s |
|---|
| 258 | * |
|---|
| 259 | * @param object $criteria {@link CriteriaElement} |
|---|
| 260 | * @param bool $id_as_key Use IDs as keys in the array? |
|---|
| 261 | * |
|---|
| 262 | * @return array Array of {@link XoopsConfigOption}s |
|---|
| 263 | */ |
|---|
| 264 | function &getConfigOptions($criteria = null, $id_as_key = false) |
|---|
| 265 | { |
|---|
| 266 | $ret =& $this->_oHandler->getObjects($criteria, $id_as_key); |
|---|
| 267 | return $ret; |
|---|
| 268 | } |
|---|
| 269 | |
|---|
| 270 | /** |
|---|
| 271 | * Count some {@link XoopsConfigOption}s |
|---|
| 272 | * |
|---|
| 273 | * @param object $criteria {@link CriteriaElement} |
|---|
| 274 | * |
|---|
| 275 | * @return int Count of {@link XoopsConfigOption}s matching $criteria |
|---|
| 276 | */ |
|---|
| 277 | function getConfigOptionsCount($criteria = null) |
|---|
| 278 | { |
|---|
| 279 | return $this->_oHandler->getCount($criteria); |
|---|
| 280 | } |
|---|
| 281 | |
|---|
| 282 | /** |
|---|
| 283 | * Get a list of configs |
|---|
| 284 | * |
|---|
| 285 | * @param int $conf_modid ID of the modules |
|---|
| 286 | * @param int $conf_catid ID of the category |
|---|
| 287 | * |
|---|
| 288 | * @return array Associative array of name=>value pairs. |
|---|
| 289 | */ |
|---|
| 290 | function &getConfigList($conf_modid, $conf_catid = 0) |
|---|
| 291 | { |
|---|
| 292 | if (!empty($this->_cachedConfigs[$conf_modid][$conf_catid])) { |
|---|
| 293 | return $this->_cachedConfigs[$conf_modid][$conf_catid]; |
|---|
| 294 | } else { |
|---|
| 295 | $criteria = new CriteriaCompo(new Criteria('conf_modid', $conf_modid)); |
|---|
| 296 | if (empty($conf_catid)) { |
|---|
| 297 | $criteria->add(new Criteria('conf_catid', $conf_catid)); |
|---|
| 298 | } |
|---|
| 299 | $configs =& $this->_cHandler->getObjects($criteria); |
|---|
| 300 | $confcount = count($configs); |
|---|
| 301 | $ret = array(); |
|---|
| 302 | for ($i = 0; $i < $confcount; $i++) { |
|---|
| 303 | $ret[$configs[$i]->getVar('conf_name')] = $configs[$i]->getConfValueForOutput(); |
|---|
| 304 | } |
|---|
| 305 | $this->_cachedConfigs[$conf_modid][$conf_catid] =& $ret; |
|---|
| 306 | return $ret; |
|---|
| 307 | } |
|---|
| 308 | } |
|---|
| 309 | } |
|---|
| 310 | ?> |
|---|