1: <?php
2: /**
3: * XOOPS Kernel Class
4: *
5: * You may not change or alter any portion of this comment or credits
6: * of supporting developers from this source code or any supporting source code
7: * which is considered copyrighted (c) material of the original comment or credit authors.
8: * This program is distributed in the hope that it will be useful,
9: * but WITHOUT ANY WARRANTY; without even the implied warranty of
10: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11: *
12: * @copyright (c) 2000-2016 XOOPS Project (www.xoops.org)
13: * @license GNU GPL 2 (https://www.gnu.org/licenses/gpl-2.0.html)
14: * @package kernel
15: * @since 2.0.0
16: * @author Kazumi Ono (AKA onokazu) http://www.myweb.ne.jp/, http://jp.xoops.org/
17: */
18: defined('XOOPS_ROOT_PATH') || exit('Restricted access');
19:
20: /**
21: * A Config-Option
22: *
23: * @author Kazumi Ono <onokazu@xoops.org>
24: * @copyright (c) 2000-2016 XOOPS Project (www.xoops.org)
25: *
26: * @package kernel
27: */
28: class XoopsConfigOption extends XoopsObject
29: {
30: //PHP 8.2 Dynamic properties deprecated
31: public $confop_id;
32: public $confop_name;
33: public $confop_value;
34: public $conf_id;
35:
36: /**
37: * Constructor
38: */
39: public function __construct()
40: {
41: parent::__construct();
42: $this->initVar('confop_id', XOBJ_DTYPE_INT, null);
43: $this->initVar('confop_name', XOBJ_DTYPE_TXTBOX, null, true, 255);
44: $this->initVar('confop_value', XOBJ_DTYPE_TXTBOX, null, true, 255);
45: $this->initVar('conf_id', XOBJ_DTYPE_INT, 0);
46: }
47:
48: /**
49: * Returns Class Base Variable confop_id
50: * @param string $format
51: * @return mixed
52: */
53: public function id($format = 'N')
54: {
55: return $this->getVar('confop_id', $format);
56: }
57:
58: /**
59: * Returns Class Base Variable confop_id
60: * @param string $format
61: * @return mixed
62: */
63: public function confop_id($format = '')
64: {
65: return $this->getVar('confop_id', $format);
66: }
67:
68: /**
69: * Returns Class Base Variable confop_name
70: * @param string $format
71: * @return mixed
72: */
73: public function confop_name($format = '')
74: {
75: return $this->getVar('confop_name', $format);
76: }
77:
78: /**
79: * Returns Class Base Variable confop_value
80: * @param string $format
81: * @return mixed
82: */
83: public function confop_value($format = '')
84: {
85: return $this->getVar('confop_value', $format);
86: }
87:
88: /**
89: * Returns Class Base Variable conf_id
90: * @param string $format
91: * @return mixed
92: */
93: public function conf_id($format = '')
94: {
95: return $this->getVar('conf_id', $format);
96: }
97: }
98:
99: /**
100: * XOOPS configuration option handler class.
101: * This class is responsible for providing data access mechanisms to the data source
102: * of XOOPS configuration option class objects.
103: *
104: * @copyright (c) 2000-2016 XOOPS Project (www.xoops.org)
105: * @author Kazumi Ono <onokazu@xoops.org>
106: *
107: * @package kernel
108: * @subpackage config
109: */
110: class XoopsConfigOptionHandler extends XoopsObjectHandler
111: {
112: /**
113: * Create a new option
114: *
115: * @param bool $isNew Flag the option as "new"?
116: *
117: * @return XoopsConfigOption {@link XoopsConfigOption}
118: */
119: public function create($isNew = true)
120: {
121: $confoption = new XoopsConfigOption();
122: if ($isNew) {
123: $confoption->setNew();
124: }
125:
126: return $confoption;
127: }
128:
129: /**
130: * Get an option from the database
131: *
132: * @param int $id ID of the option
133: *
134: * @return XoopsConfigOption|false reference to the {@link XoopsConfigOption}, false on fail
135: */
136: public function get($id)
137: {
138: $confoption = false;
139: $id = (int)$id;
140: if ($id > 0) {
141: $sql = 'SELECT * FROM ' . $this->db->prefix('configoption') . ' WHERE confop_id=' . $id;
142: $result = $this->db->query($sql);
143: if (!$this->db->isResultSet($result)) {
144: return $confoption;
145: }
146: $numrows = $this->db->getRowsNum($result);
147: if ($numrows == 1) {
148: $confoption = new XoopsConfigOption();
149: $confoption->assignVars($this->db->fetchArray($result));
150: }
151: }
152:
153: return $confoption;
154: }
155:
156: /**
157: * Insert a new {@link XoopsConfigOption}
158: *
159: * @param XoopsObject|XoopsConfigOption $confoption a XoopsConfigOption object
160: *
161: * @return bool true on success, otherwise false
162: */
163: public function insert(XoopsObject $confoption)
164: {
165: $className = 'XoopsConfigOption';
166: if (!($confoption instanceof $className)) {
167: return false;
168: }
169: if (!$confoption->isDirty()) {
170: return true;
171: }
172: if (!$confoption->cleanVars()) {
173: return false;
174: }
175:
176: $confop_id = $confoption->getVar('confop_id');
177: $confop_name = $confoption->getVar('confop_name');
178: $confop_value = $confoption->getVar('confop_value');
179: $conf_id = $confoption->getVar('conf_id');
180:
181: if ($confoption->isNew()) {
182: $confop_id = $this->db->genId('configoption_confop_id_seq');
183: $sql = sprintf(
184: 'INSERT INTO %s (confop_id, confop_name, confop_value, conf_id) VALUES (%u, %s, %s, %u)',
185: $this->db->prefix('configoption'),
186: $confop_id,
187: $this->db->quote($confop_name),
188: $this->db->quote($confop_value),
189: $conf_id
190: );
191: } else {
192: $sql = sprintf(
193: 'UPDATE %s SET confop_name = %s, confop_value = %s WHERE confop_id = %u',
194: $this->db->prefix('configoption'),
195: $this->db->quote($confop_name),
196: $this->db->quote($confop_value),
197: $confop_id
198: );
199: }
200: if (!$result = $this->db->query($sql)) {
201: return false;
202: }
203: if (empty($confop_id)) {
204: $confop_id = $this->db->getInsertId();
205: }
206: $confoption->assignVar('confop_id', $confop_id);
207:
208: return $confop_id;
209: }
210:
211: /**
212: * Delete a {@link XoopsConfigOption}
213: *
214: * @param XoopsObject|XoopsConfigOption $confoption a XoopsConfigOption object
215: *
216: * @return bool true on success, otherwise false
217: */
218: public function delete(XoopsObject $confoption)
219: {
220: $className = 'XoopsConfigOption';
221: if (!($confoption instanceof $className)) {
222: return false;
223: }
224: $sql = sprintf('DELETE FROM %s WHERE confop_id = %u', $this->db->prefix('configoption'), $confoption->getVar('confop_id'));
225: if (!$result = $this->db->query($sql)) {
226: return false;
227: }
228:
229: return true;
230: }
231:
232: /**
233: * Get some {@link XoopsConfigOption}s
234: *
235: * @param CriteriaElement|CriteriaCompo $criteria {@link CriteriaElement}
236: * @param bool $id_as_key Use the IDs as array-keys?
237: *
238: * @return array Array of {@link XoopsConfigOption}s
239: */
240: public function getObjects(CriteriaElement $criteria = null, $id_as_key = false)
241: {
242: $ret = array();
243: $limit = $start = 0;
244: $sql = 'SELECT * FROM ' . $this->db->prefix('configoption');
245: if (isset($criteria) && \method_exists($criteria, 'renderWhere')) {
246: $sql .= ' ' . $criteria->renderWhere() . ' ORDER BY confop_id ' . $criteria->getOrder();
247: $limit = $criteria->getLimit();
248: $start = $criteria->getStart();
249: }
250: $result = $this->db->query($sql, $limit, $start);
251: if (!$this->db->isResultSet($result)) {
252: return $ret;
253: }
254: /** @var array $myrow */
255: while (false !== ($myrow = $this->db->fetchArray($result))) {
256: $confoption = new XoopsConfigOption();
257: $confoption->assignVars($myrow);
258: if (!$id_as_key) {
259: $ret[] =& $confoption;
260: } else {
261: $ret[$myrow['confop_id']] = &$confoption;
262: }
263: unset($confoption);
264: }
265:
266: return $ret;
267: }
268:
269: /**
270: * get count of matching configoption rows
271: *
272: * @param CriteriaElement $criteria
273: *
274: * @return int Count of matching XoopsConfigOption
275: */
276: public function getCount(CriteriaElement $criteria = null)
277: {
278: $sql = 'SELECT COUNT(*) as `count` FROM ' . $this->db->prefix('configoption');
279: if (isset($criteria) && $criteria instanceof \CriteriaElement) {
280: $sql .= ' ' . $criteria->renderWhere();
281: }
282: $result = $this->db->query($sql);
283: if (!$this->db->isResultSet($result)) {
284: throw new \RuntimeException(
285: \sprintf(_DB_QUERY_ERROR, $sql) . $this->db->error(), E_USER_ERROR
286: );
287: }
288: $row = $this->db->fetchArray($result);
289: $count = $row['count'];
290: $this->db->freeRecordSet($result);
291: return (int)$count;
292: }
293: }
294: