1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17:
18: defined('XOOPS_ROOT_PATH') || exit('Restricted access');
19:
20: 21: 22:
23: define('XOOPS_CONF', 1);
24: define('XOOPS_CONF_USER', 2);
25: define('XOOPS_CONF_METAFOOTER', 3);
26: define('XOOPS_CONF_CENSOR', 4);
27: define('XOOPS_CONF_SEARCH', 5);
28: define('XOOPS_CONF_MAILER', 6);
29: define('XOOPS_CONF_AUTH', 7);
30:
31:
32: 33: 34: 35: 36: 37:
38: class XoopsConfigItem extends XoopsObject
39: {
40: 41: 42: 43: 44: 45:
46: public $_confOptions = array();
47:
48: 49: 50:
51: public function __construct()
52: {
53: $this->initVar('conf_id', XOBJ_DTYPE_INT, null, false);
54: $this->initVar('conf_modid', XOBJ_DTYPE_INT, null, false);
55: $this->initVar('conf_catid', XOBJ_DTYPE_INT, null, false);
56: $this->initVar('conf_name', XOBJ_DTYPE_OTHER);
57: $this->initVar('conf_title', XOBJ_DTYPE_TXTBOX);
58: $this->initVar('conf_value', XOBJ_DTYPE_TXTAREA);
59: $this->initVar('conf_desc', XOBJ_DTYPE_OTHER);
60: $this->initVar('conf_formtype', XOBJ_DTYPE_OTHER);
61: $this->initVar('conf_valuetype', XOBJ_DTYPE_OTHER);
62: $this->initVar('conf_order', XOBJ_DTYPE_INT);
63: }
64:
65: 66: 67: 68: 69:
70: public function id($format = 'N')
71: {
72: return $this->getVar('conf_id', $format);
73: }
74:
75: 76: 77: 78: 79:
80: public function conf_id($format = '')
81: {
82: return $this->getVar('conf_id', $format);
83: }
84:
85: 86: 87: 88: 89:
90: public function conf_modid($format = '')
91: {
92: return $this->getVar('conf_modid', $format);
93: }
94:
95: 96: 97: 98: 99:
100: public function conf_catid($format = '')
101: {
102: return $this->getVar('conf_catid', $format);
103: }
104:
105: 106: 107: 108: 109:
110: public function conf_name($format = '')
111: {
112: return $this->getVar('conf_name', $format);
113: }
114:
115: 116: 117: 118: 119:
120: public function conf_title($format = '')
121: {
122: return $this->getVar('conf_title', $format);
123: }
124:
125: 126: 127: 128: 129:
130: public function conf_value($format = '')
131: {
132: return $this->getVar('conf_value', $format);
133: }
134:
135: 136: 137: 138: 139:
140: public function conf_desc($format = '')
141: {
142: return $this->getVar('conf_desc', $format);
143: }
144:
145: 146: 147: 148: 149:
150: public function conf_formtype($format = '')
151: {
152: return $this->getVar('conf_formtype', $format);
153: }
154:
155: 156: 157: 158: 159:
160: public function conf_valuetype($format = '')
161: {
162: return $this->getVar('conf_valuetype', $format);
163: }
164:
165: 166: 167: 168: 169:
170: public function conf_order($format = '')
171: {
172: return $this->getVar('conf_order', $format);
173: }
174:
175: 176: 177: 178: 179:
180: public function getConfValueForOutput()
181: {
182: switch ($this->getVar('conf_valuetype')) {
183: case 'int':
184: return (int)$this->getVar('conf_value', 'N');
185: break;
186: case 'array':
187: $value = @unserialize($this->getVar('conf_value', 'N'));
188:
189: return $value ?: array();
190: case 'float':
191: $value = $this->getVar('conf_value', 'N');
192:
193: return (float)$value;
194: break;
195: case 'textarea':
196: return $this->getVar('conf_value');
197: default:
198: return $this->getVar('conf_value', 'N');
199: break;
200: }
201: }
202:
203: 204: 205: 206: 207: 208:
209: public function setConfValueForInput(&$value, $force_slash = false)
210: {
211: switch ($this->getVar('conf_valuetype')) {
212: case 'array':
213: if (!is_array($value)) {
214: $value = explode('|', trim($value));
215: }
216: $this->setVar('conf_value', serialize($value), $force_slash);
217: break;
218: case 'text':
219: $this->setVar('conf_value', trim($value), $force_slash);
220: break;
221: default:
222: $this->setVar('conf_value', $value, $force_slash);
223: break;
224: }
225: }
226:
227: 228: 229: 230: 231:
232: public function setConfOptions($option)
233: {
234: if (is_array($option)) {
235: $count = count($option);
236: for ($i = 0; $i < $count; ++$i) {
237: $this->setConfOptions($option[$i]);
238: }
239: } else {
240: if (is_object($option)) {
241: $this->_confOptions[] =& $option;
242: }
243: }
244: }
245:
246: 247: 248: 249: 250:
251: public function &getConfOptions()
252: {
253: return $this->_confOptions;
254: }
255:
256: 257: 258: 259: 260:
261: public function clearConfOptions()
262: {
263: $this->_confOptions = array();
264: }
265: }
266:
267: 268: 269: 270: 271: 272: 273: 274: 275:
276: class XoopsConfigItemHandler extends XoopsObjectHandler
277: {
278: 279: 280: 281: 282: 283: 284:
285: public function create($isNew = true)
286: {
287: $config = new XoopsConfigItem();
288: if ($isNew) {
289: $config->setNew();
290: }
291:
292: return $config;
293: }
294:
295: 296: 297: 298: 299: 300:
301: public function get($id)
302: {
303: $config = false;
304: $id = (int)$id;
305: if ($id > 0) {
306: $sql = 'SELECT * FROM ' . $this->db->prefix('config') . ' WHERE conf_id=' . $id;
307: if (!$result = $this->db->query($sql)) {
308: return $config;
309: }
310: $numrows = $this->db->getRowsNum($result);
311: if ($numrows == 1) {
312: $myrow = $this->db->fetchArray($result);
313: $config = new XoopsConfigItem();
314: $config->assignVars($myrow);
315: }
316: }
317:
318: return $config;
319: }
320:
321: 322: 323: 324: 325: 326: 327:
328: public function insert(XoopsObject $config)
329: {
330: $className = 'XoopsConfigItem';
331: if (!($config instanceof $className)) {
332: return false;
333: }
334: if (!$config->isDirty()) {
335: return true;
336: }
337: if (!$config->cleanVars()) {
338: return false;
339: }
340:
341: $conf_id = $config->getVar('conf_id', 'n');
342: $conf_modid = $config->getVar('conf_modid', 'n');
343: $conf_catid = $config->getVar('conf_catid', 'n');
344: $conf_name = $config->getVar('conf_name', 'n');
345: $conf_title = $config->getVar('conf_title', 'n');
346: $conf_value = $config->getVar('conf_value', 'n');
347: $conf_desc = $config->getVar('conf_desc', 'n');
348: $conf_formtype = $config->getVar('conf_formtype', 'n');
349: $conf_valuetype = $config->getVar('conf_valuetype', 'n');
350: $conf_order = $config->getVar('conf_order', 'n');
351:
352: if ($config->isNew()) {
353: $conf_id = $this->db->genId('config_conf_id_seq');
354: $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);
355: } else {
356: $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);
357: }
358: if (!$result = $this->db->query($sql)) {
359: return false;
360: }
361: if (empty($conf_id)) {
362: $conf_id = $this->db->getInsertId();
363: }
364: $config->assignVar('conf_id', $conf_id);
365:
366: return true;
367: }
368:
369: 370: 371: 372: 373: 374: 375:
376: public function delete(XoopsObject $config)
377: {
378: $className = 'XoopsConfigItem';
379: if (!($config instanceof $className)) {
380: return false;
381: }
382: $sql = sprintf('DELETE FROM %s WHERE conf_id = %u', $this->db->prefix('config'), $config->getVar('conf_id'));
383: if (!$result = $this->db->query($sql)) {
384: return false;
385: }
386:
387: return true;
388: }
389:
390: 391: 392: 393: 394: 395: 396:
397: public function getObjects(CriteriaElement $criteria = null, $id_as_key = false)
398: {
399: $ret = array();
400: $limit = $start = 0;
401: $sql = 'SELECT * FROM ' . $this->db->prefix('config');
402: if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
403: $sql .= ' ' . $criteria->renderWhere();
404: $sql .= ' ORDER BY conf_order ASC';
405: $limit = $criteria->getLimit();
406: $start = $criteria->getStart();
407: }
408: $result = $this->db->query($sql, $limit, $start);
409: if (!$result) {
410: return false;
411: }
412: while ($myrow = $this->db->fetchArray($result)) {
413: $config = new XoopsConfigItem();
414: $config->assignVars($myrow);
415: if (!$id_as_key) {
416: $ret[] =& $config;
417: } else {
418: $ret[$myrow['conf_id']] = &$config;
419: }
420: unset($config);
421: }
422:
423: return $ret;
424: }
425:
426: 427: 428: 429: 430: 431:
432: public function getCount(CriteriaElement $criteria = null)
433: {
434: $sql = 'SELECT * FROM ' . $this->db->prefix('config');
435: if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) {
436: $sql .= ' ' . $criteria->renderWhere();
437: }
438: $result = $this->db->query($sql);
439: if (!$result) {
440: return false;
441: }
442: list($count) = $this->db->fetchRow($result);
443:
444: return $count;
445: }
446: }
447: