1: <?php
2: // $Id: configoption.php,v 1.1 2007/05/15 02:34:38 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: /**
37: *
38: *
39: * @package kernel
40: *
41: * @author Kazumi Ono <onokazu@xoops.org>
42: * @copyright copyright (c) 2000-2003 XOOPS.org
43: */
44:
45: /**
46: * A Config-Option
47: *
48: * @author Kazumi Ono <onokazu@xoops.org>
49: * @copyright copyright (c) 2000-2003 XOOPS.org
50: *
51: * @package kernel
52: */
53: class XoopsConfigOption extends XoopsObject
54: {
55: /**
56: * Constructor
57: */
58: function XoopsConfigOption()
59: {
60: static $initVars;
61: if (isset($initVars)) {
62: $this->vars = $initVars;
63: return;
64: }
65: $this->XoopsObject();
66: $this->initVar('confop_id', XOBJ_DTYPE_INT, null);
67: $this->initVar('confop_name', XOBJ_DTYPE_TXTBOX, null, true, 255);
68: $this->initVar('confop_value', XOBJ_DTYPE_TXTBOX, null, true, 255);
69: $this->initVar('conf_id', XOBJ_DTYPE_INT, 0);
70: $initVars = $this->vars;
71: }
72:
73: /**
74: * Get a constract of confop_value
75: */
76: function getOptionKey()
77: {
78: return defined($this->get('confop_value')) ? constant($this->get('confop_value')) : $this->get('confop_value');
79: }
80:
81: /**
82: * Get a constract of confop_name
83: */
84: function getOptionLabel()
85: {
86: return defined($this->get('confop_name')) ? constant($this->get('confop_name')) : $this->get('confop_name');
87: }
88: /**
89: * Compare with contents of $config object. If it's equal, return true.
90: * This member function doesn't use 'conf_id' & 'conf_order' to compare.
91: *
92: * @param XoopsConfigItem $config
93: * @return bool
94: */
95: function isEqual(&$option)
96: {
97: $flag = true;
98:
99: $flag &= ($this->get('confop_name') == $option->get('confop_name'));
100: $flag &= ($this->get('confop_value') == $option->get('confop_value'));
101:
102: return $flag;
103: }
104: }
105:
106: /**
107: * XOOPS configuration option handler class.
108: * This class is responsible for providing data access mechanisms to the data source
109: * of XOOPS configuration option class objects.
110: *
111: * @copyright copyright (c) 2000-2003 XOOPS.org
112: * @author Kazumi Ono <onokazu@xoops.org>
113: *
114: * @package kernel
115: * @subpackage config
116: */
117: class XoopsConfigOptionHandler extends XoopsObjectHandler
118: {
119:
120: /**
121: * Create a new option
122: *
123: * @param bool $isNew Flag the option as "new"?
124: *
125: * @return object {@link XoopsConfigOption}
126: */
127: function &create($isNew = true)
128: {
129: $confoption =new XoopsConfigOption();
130: if ($isNew) {
131: $confoption->setNew();
132: }
133: return $confoption;
134: }
135:
136: /**
137: * Get an option from the database
138: *
139: * @param int $id ID of the option
140: *
141: * @return object reference to the {@link XoopsConfigOption}, FALSE on fail
142: */
143: function &get($id)
144: {
145: $ret = false;
146: $id = (int)$id;
147: if ($id > 0) {
148: $sql = 'SELECT * FROM '.$this->db->prefix('configoption').' WHERE confop_id='.$id;
149: if ($result = $this->db->query($sql)) {
150: $numrows = $this->db->getRowsNum($result);
151: if ($numrows == 1) {
152: $confoption =new XoopsConfigOption();
153: $confoption->assignVars($this->db->fetchArray($result));
154: $ret =& $confoption;
155: }
156: }
157: }
158: return $ret;
159: }
160:
161: /**
162: * Insert a new option in the database
163: *
164: * @param object &$confoption reference to a {@link XoopsConfigOption}
165: * @return bool TRUE if successfull.
166: */
167: function insert(&$confoption)
168: {
169: if (strtolower(get_class($confoption)) != 'xoopsconfigoption') {
170: return false;
171: }
172: if (!$confoption->isDirty()) {
173: return true;
174: }
175: if (!$confoption->cleanVars()) {
176: return false;
177: }
178: foreach ($confoption->cleanVars as $k => $v) {
179: ${$k} = $v;
180: }
181: if ($confoption->isNew()) {
182: $confop_id = $this->db->genId('configoption_confop_id_seq');
183: $sql = sprintf('INSERT INTO %s (confop_id, confop_name, confop_value, conf_id) VALUES (%u, %s, %s, %u)', $this->db->prefix('configoption'), $confop_id, $this->db->quoteString($confop_name), $this->db->quoteString($confop_value), $conf_id);
184: } else {
185: $sql = sprintf('UPDATE %s SET confop_name = %s, confop_value = %s WHERE confop_id = %u', $this->db->prefix('configoption'), $this->db->quoteString($confop_name), $this->db->quoteString($confop_value), $confop_id);
186: }
187: if (!$result = $this->db->query($sql)) {
188: return false;
189: }
190: if (empty($confop_id)) {
191: $confop_id = $this->db->getInsertId();
192: }
193: $confoption->assignVar('confop_id', $confop_id);
194: return $confop_id;
195: }
196:
197: /**
198: * Delete an option
199: *
200: * @param object &$confoption reference to a {@link XoopsConfigOption}
201: * @return bool TRUE if successful
202: */
203: function delete(&$confoption)
204: {
205: if (strtolower(get_class($confoption)) != 'xoopsconfigoption') {
206: return false;
207: }
208: $sql = sprintf('DELETE FROM %s WHERE confop_id = %u', $this->db->prefix('configoption'), $confoption->getVar('confop_id', 'n'));
209: if (!$result = $this->db->query($sql)) {
210: return false;
211: }
212: return true;
213: }
214:
215: /**
216: * Get some {@link XoopsConfigOption}s
217: *
218: * @param object $criteria {@link CriteriaElement}
219: * @param bool $id_as_key Use the IDs as array-keys?
220: *
221: * @return array Array of {@link XoopsConfigOption}s
222: */
223: function &getObjects($criteria = null, $id_as_key = false)
224: {
225: $ret = array();
226: $limit = $start = 0;
227: $sql = 'SELECT * FROM '.$this->db->prefix('configoption');
228: if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
229: $sql .= ' '.$criteria->renderWhere().' ORDER BY confop_id '.$criteria->getOrder();
230: $limit = $criteria->getLimit();
231: $start = $criteria->getStart();
232: }
233: $result = $this->db->query($sql, $limit, $start);
234: if (!$result) {
235: return $ret;
236: }
237: while ($myrow = $this->db->fetchArray($result)) {
238: $confoption =new XoopsConfigOption();
239: $confoption->assignVars($myrow);
240: if (!$id_as_key) {
241: $ret[] =& $confoption;
242: } else {
243: $ret[$myrow['confop_id']] =& $confoption;
244: }
245: unset($confoption);
246: }
247: return $ret;
248: }
249: }
250: ?>
251: