source: temp/test-xoops.ec-cube.net/html/kernel/configitem.php @ 405

Revision 405, 11.4 KB checked in by root, 20 years ago (diff)
Line 
1<?php
2// $Id: configitem.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
32if (!defined('XOOPS_ROOT_PATH')) {
33    exit();
34}
35
36/**
37 * @package     kernel
38 *
39 * @author      Kazumi Ono  <[email protected]>
40 * @copyright   copyright (c) 2000-2003 XOOPS.org
41 */
42
43/**#@+
44 * Config type
45 */
46define('XOOPS_CONF', 1);
47define('XOOPS_CONF_USER', 2);
48define('XOOPS_CONF_METAFOOTER', 3);
49define('XOOPS_CONF_CENSOR', 4);
50define('XOOPS_CONF_SEARCH', 5);
51define('XOOPS_CONF_MAILER', 6);
52/**#@-*/
53
54/**
55 *
56 *
57 * @author      Kazumi Ono  <[email protected]>
58 * @copyright   copyright (c) 2000-2003 XOOPS.org
59 */
60class XoopsConfigItem extends XoopsObject
61{
62
63    /**
64     * Config options
65     *
66     * @var array
67     * @access  private
68     */
69    var $_confOptions = array();
70
71    /**
72     * Constructor
73     */
74    function XoopsConfigItem()
75    {
76        $this->initVar('conf_id', XOBJ_DTYPE_INT, null, false);
77        $this->initVar('conf_modid', XOBJ_DTYPE_INT, null, false);
78        $this->initVar('conf_catid', XOBJ_DTYPE_INT, null, false);
79        $this->initVar('conf_name', XOBJ_DTYPE_OTHER);
80        $this->initVar('conf_title', XOBJ_DTYPE_TXTBOX);
81        $this->initVar('conf_value', XOBJ_DTYPE_TXTAREA);
82        $this->initVar('conf_desc', XOBJ_DTYPE_OTHER);
83        $this->initVar('conf_formtype', XOBJ_DTYPE_OTHER);
84        $this->initVar('conf_valuetype', XOBJ_DTYPE_OTHER);
85        $this->initVar('conf_order', XOBJ_DTYPE_INT);
86    }
87
88    /**
89     * Get a config value in a format ready for output
90     *
91     * @return  string
92     */
93    function &getConfValueForOutput()
94    {
95        switch ($this->getVar('conf_valuetype')) {
96        case 'int':
97            $ret = intval($this->getVar('conf_value', 'N'));
98            break;
99        case 'array':
100            $ret = unserialize($this->getVar('conf_value', 'N'));
101            break;
102        case 'float':
103            $value = $this->getVar('conf_value', 'N');
104            $ret = (float)$value;
105            break;
106        case 'textarea':
107            $ret = $this->getVar('conf_value');
108            break;
109        default:
110            $ret = $this->getVar('conf_value', 'N');
111            break;
112        }
113        return $ret;
114    }
115
116    /**
117     * Set a config value
118     *
119     * @param   mixed   &$value Value
120     * @param   bool    $force_slash
121     */
122    function setConfValueForInput(&$value, $force_slash = false)
123    {
124        switch($this->getVar('conf_valuetype')) {
125        case 'array':
126            if (!is_array($value)) {
127                $value = explode('|', trim($value));
128            }
129            $this->setVar('conf_value', serialize($value), $force_slash);
130            break;
131        case 'text':
132            $this->setVar('conf_value', trim($value), $force_slash);
133            break;
134        default:
135            $this->setVar('conf_value', $value, $force_slash);
136            break;
137        }
138    }
139
140    /**
141     * Assign one or more {@link XoopsConfigItemOption}s
142     *
143     * @param   mixed   $option either a {@link XoopsConfigItemOption} object or an array of them
144     */
145    function setConfOptions($option)
146    {
147        if (is_array($option)) {
148            $count = count($option);
149            for ($i = 0; $i < $count; $i++) {
150                $this->setConfOptions($option[$i]);
151            }
152        } else {
153            if(is_object($option)) {
154                $this->_confOptions[] =& $option;
155            }
156        }
157    }
158
159    /**
160     * Get the {@link XoopsConfigItemOption}s of this Config
161     *
162     * @return  array   array of {@link XoopsConfigItemOption}
163     */
164    function &getConfOptions()
165    {
166        return $this->_confOptions;
167    }
168}
169
170
171/**
172* XOOPS configuration handler class. 
173*
174* This class is responsible for providing data access mechanisms to the data source
175* of XOOPS configuration class objects.
176*
177* @author       Kazumi Ono <[email protected]>
178* @copyright    copyright (c) 2000-2003 XOOPS.org
179*/
180class XoopsConfigItemHandler extends XoopsObjectHandler
181{
182
183    /**
184     * Create a new {@link XoopsConfigItem}
185     *
186     * @see     XoopsConfigItem
187     * @param   bool    $isNew  Flag the config as "new"?
188     * @return  object  reference to the new config
189     */
190    function &create($isNew = true)
191    {
192        $config =& new XoopsConfigItem();
193        if ($isNew) {
194            $config->setNew();
195        }
196        return $config;
197    }
198
199    /**
200     * Load a config from the database
201     *
202     * @param   int $id ID of the config
203     * @return  object  reference to the config, FALSE on fail
204     */
205    function &get($id)
206    {
207        $ret = false;
208        $id = intval($id);
209        if ($id > 0) {
210            $sql = 'SELECT * FROM '.$this->db->prefix('config').' WHERE conf_id='.$id;
211            if ($result = $this->db->query($sql)) {
212                $numrows = $this->db->getRowsNum($result);
213                if ($numrows == 1) {
214                    $myrow = $this->db->fetchArray($result);
215                    $config =& new XoopsConfigItem();
216                    $config->assignVars($myrow);
217                    $ret =& $config;
218                }
219            }
220        }
221        return $ret;
222    }
223
224    /**
225     * Write a config to the database
226     *
227     * @param   object  &$config    {@link XoopsConfigItem} object
228     * @return  mixed   FALSE on fail.
229     */
230    function insert(&$config)
231    {
232        if (strtolower(get_class($config)) != 'xoopsconfigitem') {
233            return false;
234        }
235        if (!$config->isDirty()) {
236            return true;
237        }
238        if (!$config->cleanVars()) {
239            return false;
240        }
241        foreach ($config->cleanVars as $k => $v) {
242            ${$k} = $v;
243        }
244        if ($config->isNew()) {
245            $conf_id = $this->db->genId('config_conf_id_seq');
246            $sql = sprintf("INSERT INTO %s (conf_id, conf_modid, conf_catid, conf_name, conf_title, conf_value, conf_desc, conf_formtype, conf_valuetype, conf_order) VALUES (%u, %u, %u, %s, %s, %s, %s, %s, %s, %u)", $this->db->prefix('config'), $conf_id, $conf_modid, $conf_catid, $this->db->quoteString($conf_name), $this->db->quoteString($conf_title), $this->db->quoteString($conf_value), $this->db->quoteString($conf_desc), $this->db->quoteString($conf_formtype), $this->db->quoteString($conf_valuetype), $conf_order);
247        } else {
248            $sql = sprintf("UPDATE %s SET conf_modid = %u, conf_catid = %u, conf_name = %s, conf_title = %s, conf_value = %s, conf_desc = %s, conf_formtype = %s, conf_valuetype = %s, conf_order = %u WHERE conf_id = %u", $this->db->prefix('config'), $conf_modid, $conf_catid, $this->db->quoteString($conf_name), $this->db->quoteString($conf_title), $this->db->quoteString($conf_value), $this->db->quoteString($conf_desc), $this->db->quoteString($conf_formtype), $this->db->quoteString($conf_valuetype), $conf_order, $conf_id);
249        }
250        if (!$result = $this->db->query($sql)) {
251            return false;
252        }
253        if (empty($conf_id)) {
254            $conf_id = $this->db->getInsertId();
255        }
256        $config->assignVar('conf_id', $conf_id);
257        return true;
258    }
259
260    /**
261     * Delete a config from the database
262     *
263     * @param   object  &$config    Config to delete
264     * @return  bool    Successful?
265     */
266    function delete(&$config)
267    {
268        if (strtolower(get_class($config)) != 'xoopsconfigitem') {
269            return false;
270        }
271        $sql = sprintf("DELETE FROM %s WHERE conf_id = %u", $this->db->prefix('config'), $config->getVar('conf_id'));
272        if (!$result = $this->db->query($sql)) {
273            return false;
274        }
275        return true;
276    }
277
278    /**
279     * Get configs from the database
280     *
281     * @param   object  $criteria   {@link CriteriaElement}
282     * @param   bool    $id_as_key  return the config's id as key?
283     * @return  array   Array of {@link XoopsConfigItem} objects
284     */
285    function &getObjects($criteria = null, $id_as_key = false)
286    {
287        $ret = array();
288        $limit = $start = 0;
289        $sql = 'SELECT * FROM '.$this->db->prefix('config');
290        if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
291            $sql .= ' '.$criteria->renderWhere();
292            $sql .= ' ORDER BY conf_order ASC';
293            $limit = $criteria->getLimit();
294            $start = $criteria->getStart();
295        }
296        $result = $this->db->query($sql, $limit, $start);
297        if (!$result) {
298            return $ret;
299        }
300        while ($myrow = $this->db->fetchArray($result)) {
301            $config =& new XoopsConfigItem();
302            $config->assignVars($myrow);
303            if (!$id_as_key) {
304                $ret[] =& $config;
305            } else {
306                $ret[$myrow['conf_id']] =& $config;
307            }
308            unset($config);
309        }
310        return $ret;
311    }
312
313    /**
314     * Count configs
315     *
316     * @param   object  $criteria   {@link CriteriaElement}
317     * @return  int     Count of configs matching $criteria
318     */
319    function getCount($criteria = null)
320    {
321        $limit = $start = 0;
322        $sql = 'SELECT * FROM '.$this->db->prefix('config');
323        if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
324            $sql .= ' '.$criteria->renderWhere();
325        }
326        $result =& $this->db->query($sql);
327        if (!$result) {
328            return false;
329        }
330        list($count) = $this->db->fetchRow($result);
331        return $count;
332    }
333}
334
335?>
Note: See TracBrowser for help on using the repository browser.