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