1: <?php
2: // $Id: config.php,v 1.1 2007/05/15 02:34:37 minahito 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://xoopscube.jp/ //
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 <onokazu@xoops.org>
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 <webmaster@myweb.ne.jp>
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: $config =& $this->_cHandler->getObjects($criteria, $id_as_key);
190: return $config;
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: * @todo This method keeps cache for categories. This may be problem...
211: */
212: function &getConfigsByCat($category, $module = 0)
213: {
214: static $_cachedConfigs=array();
215: if (!empty($_cachedConfigs[$module][$category])) {
216: return $_cachedConfigs[$module][$category];
217: } else {
218: $ret = array();
219: $criteria = new CriteriaCompo(new Criteria('conf_modid', (int)$module));
220: if (!empty($category)) {
221: $criteria->add(new Criteria('conf_catid', (int)$category));
222: }
223:
224: // get config values
225: $configs = array();
226: $db = $this->_cHandler->db;
227: $result = $db->query('SELECT conf_name,conf_value,conf_valuetype FROM '.$db->prefix('config').' '.$criteria->renderWhere().' ORDER BY conf_order ASC');
228: if ($result) {
229: while (list($name, $value, $type) = $db->fetchRow($result)) {
230: $ret[$name] = $type == 'array'?unserialize($value):$value;
231: }
232: $_cachedConfigs[$module][$category] =& $ret;
233: }
234: return $ret;
235: }
236: }
237:
238: /**
239: * Get configs by dirname.
240: *
241: * @param string $dirname
242: * @param int $category ID of a category. (Reserved)
243: */
244: function &getConfigsByDirname($dirname, $category = 0)
245: {
246: $ret = null;;
247: $handler = xoops_gethandler('module');;
248: $module =& $handler->getByDirname($dirname);
249: if (!is_object($module)) {
250: return $ret;
251: }
252:
253: $ret =& $this->getConfigsByCat($category, $module->get('mid'));
254:
255: return $ret;
256: }
257:
258: /**
259: * Make a new {@link XoopsConfigOption}
260: *
261: * @return object {@link XoopsConfigOption}
262: */
263: function &createConfigOption(){
264: $ret =& $this->_oHandler->create();
265: return $ret;
266: }
267:
268: /**
269: * Get a {@link XoopsConfigOption}
270: *
271: * @param int $id ID of the config option
272: *
273: * @return object {@link XoopsConfigOption}
274: */
275: function &getConfigOption($id)
276: {
277: $ret =& $this->_oHandler->get($id);
278: return $ret;
279: }
280:
281: /**
282: * Get one or more {@link XoopsConfigOption}s
283: *
284: * @param object $criteria {@link CriteriaElement}
285: * @param bool $id_as_key Use IDs as keys in the array?
286: *
287: * @return array Array of {@link XoopsConfigOption}s
288: */
289: function &getConfigOptions($criteria = null, $id_as_key = false)
290: {
291: $ret =& $this->_oHandler->getObjects($criteria, $id_as_key);
292: return $ret;
293: }
294:
295: /**
296: * Count some {@link XoopsConfigOption}s
297: *
298: * @param object $criteria {@link CriteriaElement}
299: *
300: * @return int Count of {@link XoopsConfigOption}s matching $criteria
301: */
302: function getConfigOptionsCount($criteria = null)
303: {
304: return $this->_oHandler->getCount($criteria);
305: }
306:
307: /**
308: * Get a list of configs
309: *
310: * @param int $conf_modid ID of the modules
311: * @param int $conf_catid ID of the category
312: *
313: * @return array Associative array of name=>value pairs.
314: */
315: function &getConfigList($conf_modid, $conf_catid = 0)
316: {
317: if (!empty($this->_cachedConfigs[$conf_modid][$conf_catid])) {
318: return $this->_cachedConfigs[$conf_modid][$conf_catid];
319: } else {
320: $criteria = new CriteriaCompo(new Criteria('conf_modid', $conf_modid));
321: if (empty($conf_catid)) {
322: $criteria->add(new Criteria('conf_catid', $conf_catid));
323: }
324: $configs =& $this->_cHandler->getObjects($criteria);
325: $confcount = count($configs);
326: $ret = array();
327: for ($i = 0; $i < $confcount; $i++) {
328: $ret[$configs[$i]->getVar('conf_name')] = $configs[$i]->getConfValueForOutput();
329: }
330: $this->_cachedConfigs[$conf_modid][$conf_catid] =& $ret;
331: return $ret;
332: }
333: }
334: }
335: ?>
336: