XCL Web Application Platform 2.5.0
The XoopsCube Legacy Project
Loading...
Searching...
No Matches
configitem.php
1<?php
12
13if (!defined('XOOPS_ROOT_PATH')) {
14 exit();
15}
16
17define('XOOPS_CONF', 1);
18define('XOOPS_CONF_USER', 2);
19define('XOOPS_CONF_METAFOOTER', 3);
20define('XOOPS_CONF_CENSOR', 4);
21define('XOOPS_CONF_SEARCH', 5);
22define('XOOPS_CONF_MAILER', 6);
24
32{
33
40 public $_confOptions = [];
41
45 public function __construct()
46 {
47 static $initVars;
48 if (isset($initVars)) {
49 $this->vars = $initVars;
50 return;
51 }
52 $this->initVar('conf_id', XOBJ_DTYPE_INT, null, false);
53 $this->initVar('conf_modid', XOBJ_DTYPE_INT, null, false);
54 $this->initVar('conf_catid', XOBJ_DTYPE_INT, null, false);
55 $this->initVar('conf_name', XOBJ_DTYPE_OTHER);
56 $this->initVar('conf_title', XOBJ_DTYPE_TXTBOX);
57 $this->initVar('conf_value', XOBJ_DTYPE_TXTAREA);
58 $this->initVar('conf_desc', XOBJ_DTYPE_OTHER);
59 $this->initVar('conf_formtype', XOBJ_DTYPE_OTHER);
60 $this->initVar('conf_valuetype', XOBJ_DTYPE_OTHER);
61 $this->initVar('conf_order', XOBJ_DTYPE_INT);
62 $initVars = $this->vars;
63 }
64
68 public function getTitle()
69 {
70 return defined($this->get('conf_title')) ? constant($this->get('conf_title')) : $this->get('conf_title');
71 }
72
76 public function getDesc()
77 {
78 return defined($this->get('conf_desc')) ? constant($this->get('conf_desc')) : null;
79 }
80
84 public function &getOptionItems()
85 {
86 $handler = xoops_gethandler('config');
87 $optionArr =& $handler->getConfigOptions(new Criteria('conf_id', $this->get('conf_id')));
88
89 return $optionArr;
90 }
91
95 public function getRoledModuleList()
96 {
97 $handler = xoops_gethandler('config');
98 $optionArr =& $handler->getConfigOptions(new Criteria('conf_id', $this->get('conf_id')));
99 $list = [];
100 foreach ($optionArr as $opt) {
101 if ('none' == $opt->get('confop_value')) {
102 $list[] = '';
103 } else {
104 $list = array_merge($list, Legacy_Utils::getCommonModuleList($opt->get('confop_value')));
105 }
106 }
107 return $list;
108 }
109
115 public function getConfValueForOutput()
116 {
117 switch ($this->getVar('conf_valuetype')) {
118 case 'int':
119 return (int)$this->getVar('conf_value', 'N');
120 case 'array':
121 return unserialize($this->getVar('conf_value', 'N'));
122 case 'float':
123 return (float)$this->getVar('conf_value', 'N');
124 case 'textarea':
125 return $this->getVar('conf_value');
126 case 'encrypt':
127 return XCube_Utils::decrypt($this->getVar('conf_value', 'N'));
128 default:
129 return $this->getVar('conf_value', 'N');
130 }
131 }
132
139 public function setConfValueForInput(&$value, $force_slash = false)
140 {
141 switch ($this->getVar('conf_valuetype')) {
142 case 'array':
143 if (!is_array($value)) {
144 $value = explode('|', trim($value));
145 }
146 $this->setVar('conf_value', serialize($value), $force_slash);
147 break;
148 case 'text':
149 $this->setVar('conf_value', trim($value), $force_slash);
150 break;
151 case 'encrypt':
152 $this->setVar('conf_value', XCube_Utils::encrypt(trim($value)), $force_slash);
153 break;
154 default:
155 $this->setVar('conf_value', $value, $force_slash);
156 break;
157 }
158 }
159
165 public function setConfOptions($option)
166 {
167 if (is_array($option)) {
168 $count = count($option);
169 for ($i = 0; $i < $count; $i++) {
170 $this->setConfOptions($option[$i]);
171 }
172 } else {
173 if (is_object($option)) {
174 $this->_confOptions[] =& $option;
175 }
176 }
177 }
178
184 public function &getConfOptions()
185 {
186 return $this->_confOptions;
187 }
188
196 public function isEqual(&$config)
197 {
198 $flag = true;
199
200 $flag &= ($this->get('conf_modid') == $config->get('conf_modid'));
201 $flag &= ($this->get('conf_catid') == $config->get('conf_catid'));
202 $flag &= ($this->get('conf_name') == $config->get('conf_name'));
203 $flag &= ($this->get('conf_title') == $config->get('conf_title'));
204 $flag &= ($this->get('conf_desc') == $config->get('conf_desc'));
205 $flag &= ($this->get('conf_formtype') == $config->get('conf_formtype'));
206 $flag &= ($this->get('conf_valuetype') == $config->get('conf_valuetype'));
207
208 //
209 // Compare options
210 //
211 $thisOptions =& $this->getOptionItems();
212 $hisOptions =& $config->getConfOptions();
213
214 if (count($thisOptions) == count($hisOptions)) {
215 foreach (array_keys($thisOptions) as $t_thiskey) {
216 $t_okFlag = false;
217 foreach (array_keys($hisOptions) as $t_hiskey) {
218 if ($thisOptions[$t_thiskey]->isEqual($hisOptions[$t_hiskey])) {
219 $t_okFlag = true;
220 }
221 }
222
223 if (!$t_okFlag) {
224 $flag = false;
225 break;
226 }
227 }
228 } else {
229 $flag = false;
230 }
231
232 return $flag;
233 }
234
242 public function loadFromConfigInfo($mid, &$configInfo, $order = null)
243 {
244 $this->set('conf_modid', $mid);
245 $this->set('conf_catid', 0);
246 $this->set('conf_name', $configInfo['name']);
247 $this->set('conf_title', $configInfo['title'], true);
248 if (isset($configInfo['description'])) {
249 $this->set('conf_desc', $configInfo['description'], true);
250 }
251 $this->set('conf_formtype', $configInfo['formtype'], true);
252 $this->set('conf_valuetype', $configInfo['valuetype'], true);
253 $this->setConfValueForInput($configInfo['default'], true);
254 if (isset($configInfo['order'])) {
255 $this->set('conf_order', $configInfo['order']);
256 } else {
257 $this->set('conf_order', $order);
258 }
259
260 if (isset($configInfo['options']) && is_array($configInfo['options'])) {
261 $configHandler = xoops_gethandler('config');
262 foreach ($configInfo['options'] as $key => $value) {
263 $configOption =& $configHandler->createConfigOption();
264 $configOption->setVar('confop_name', $key, true);
265 $configOption->setVar('confop_value', $value, true);
266 $this->setConfOptions($configOption);
267 unset($configOption);
268 }
269 }
270 }
271}
272
273
284{
285
293 public function &create($isNew = true)
294 {
295 $config =new XoopsConfigItem();
296 if ($isNew) {
297 $config->setNew();
298 }
299 return $config;
300 }
301
308 public function &get($id)
309 {
310 $ret = false;
311 $id = (int)$id;
312 if ($id > 0) {
313 $db = &$this->db;
314 $sql = 'SELECT * FROM '.$db->prefix('config').' WHERE conf_id='.$id;
315 if ($result = $db->query($sql)) {
316 $numrows = $db->getRowsNum($result);
317 if (1 == $numrows) {
318 $myrow = $db->fetchArray($result);
319 $config =new XoopsConfigItem();
320 $config->assignVars($myrow);
321 $ret =& $config;
322 }
323 }
324 }
325 return $ret;
326 }
327
334 public function insert(&$config)
335 {
336 $conf_modid = null;
337 $conf_catid = null;
338 $conf_name = null;
339 $conf_title = null;
340 $conf_value = null;
341 $conf_desc = null;
342 $conf_formtype = null;
343 $conf_valuetype = null;
344 $conf_order = null;
345 $conf_id = null;
346 if ('xoopsconfigitem' != strtolower(get_class($config))) {
347 return false;
348 }
349 if (!$config->isDirty()) {
350 return true;
351 }
352 if (!$config->cleanVars()) {
353 return false;
354 }
355 foreach ($config->cleanVars as $k => $v) {
356 ${$k} = $v;
357 }
358 $db = &$this->db;
359 if ($config->isNew()) {
360 $conf_id = $db->genId('config_conf_id_seq');
361 $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);
362 } else {
363 $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);
364 }
365 if (!$result = $db->query($sql)) {
366 return false;
367 }
368 if (empty($conf_id)) {
369 $conf_id = $db->getInsertId();
370 }
371 $config->assignVar('conf_id', $conf_id);
372 return true;
373 }
374
381 public function delete(&$config)
382 {
383 if ('xoopsconfigitem' != strtolower(get_class($config))) {
384 return false;
385 }
386 $sql = sprintf('DELETE FROM %s WHERE conf_id = %u', $this->db->prefix('config'), $config->getVar('conf_id'));
387 if (!$result = $this->db->query($sql)) {
388 return false;
389 }
390 return true;
391 }
392
400 public function &getObjects($criteria = null, $id_as_key = false)
401 {
402 $ret = [];
403 $limit = $start = 0;
404 $db = $this->db;
405 $sql = 'SELECT * FROM '.$db->prefix('config');
406 if (isset($criteria) && $criteria instanceof \criteriaelement) {
407 $sql .= ' '.$criteria->renderWhere();
408 $sql .= ' ORDER BY conf_order ASC';
409 $limit = $criteria->getLimit();
410 $start = $criteria->getStart();
411 }
412 $result = $db->query($sql, $limit, $start);
413 if (!$result) {
414 return $ret;
415 }
416 while ($myrow = $db->fetchArray($result)) {
417 $config =new XoopsConfigItem();
418 $config->assignVars($myrow);
419 if (!$id_as_key) {
420 $ret[] =& $config;
421 } else {
422 $ret[$myrow['conf_id']] =& $config;
423 }
424 unset($config);
425 }
426 return $ret;
427 }
428
435 public function getCount($criteria = null)
436 {
437 $limit = $start = 0;
438 $db = &$this->db;
439 $sql = 'SELECT * FROM '.$db->prefix('config');
440 if (isset($criteria) && $criteria instanceof \criteriaelement) {
441 $sql .= ' '.$criteria->renderWhere();
442 }
443 $result = $db->query($sql);
444 if (!$result) {
445 return false;
446 }
447 [$count] = $db->fetchRow($result);
448 return $count;
449 }
450}
static getCommonModuleList( $role)
static encrypt(string $plain_text, string $key=null)
[Static] To encrypt strings by "DES-ECB".
static decrypt(string $crypt_text, string $key=null)
[Static] To decrypt strings by "DES-ECB".
& getObjects($criteria=null, $id_as_key=false)
getCount($criteria=null)
& create($isNew=true)
isEqual(&$config)
setConfValueForInput(&$value, $force_slash=false)
setConfOptions($option)
setVar($key, $value, $not_gpc=false)
Definition object.php:250
initVar($key, $data_type, $value=null, $required=false, $maxlength=null, $options='')
Definition object.php:206
& getVar($key, $format='s')
Definition object.php:317