1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18:
19:
20: namespace Xoops\Core\Kernel\Handlers;
21:
22: use Xoops\Core\Kernel\Criteria;
23: use Xoops\Core\Kernel\CriteriaCompo;
24: use Xoops\Core\Kernel\CriteriaElement;
25: use Xoops\Core\Kernel\XoopsObjectHandler;
26:
27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40:
41: class XoopsConfigHandler extends XoopsObjectHandler
42: {
43: 44: 45: 46: 47:
48: private $itemHandler;
49:
50: 51: 52: 53: 54:
55: private $optionHandler;
56:
57: 58: 59: 60: 61: 62:
63: private $cachedConfigs = array();
64:
65: 66: 67:
68: public function __construct()
69: {
70: $this->itemHandler = \Xoops::getInstance()->getHandlerConfigItem();
71: $this->optionHandler = \Xoops::getInstance()->getHandlerConfigOption();
72: }
73:
74: 75: 76: 77: 78:
79: public function createConfig()
80: {
81: $instance = $this->itemHandler->create();
82: return $instance;
83: }
84:
85: 86: 87: 88: 89: 90: 91: 92:
93: public function getConfig($id, $withoptions = false)
94: {
95:
96: $config = $this->itemHandler->get($id);
97: if ($withoptions == true) {
98: $config->setConfOptions($this->getConfigOptions(new Criteria('conf_id', $id)));
99: }
100: return $config;
101: }
102:
103: 104: 105: 106: 107: 108: 109:
110: public function insertConfig(XoopsConfigItem $config)
111: {
112: if (!$this->itemHandler->insert($config)) {
113: return false;
114: }
115: $options = $config->getConfOptions();
116: $count = count($options);
117: $conf_id = $config->getVar('conf_id');
118: for ($i = 0; $i < $count; ++$i) {
119: $options[$i]->setVar('conf_id', $conf_id);
120: if (!$this->optionHandler->insert($options[$i])) {
121: foreach ($options[$i]->getErrors() as $msg) {
122: $config->setErrors($msg);
123: }
124: }
125: }
126: if (!empty($this->cachedConfigs[$config->getVar('conf_modid')][$config->getVar('conf_catid')])) {
127: unset($this->cachedConfigs[$config->getVar('conf_modid')][$config->getVar('conf_catid')]);
128: }
129: return true;
130: }
131:
132: 133: 134: 135: 136: 137: 138:
139: public function deleteConfig(XoopsConfigItem $config)
140: {
141: if (!$this->itemHandler->delete($config)) {
142: return false;
143: }
144: $options = $config->getConfOptions();
145: $count = count($options);
146: if ($count == 0) {
147: $options = $this->getConfigOptions(new Criteria('conf_id', $config->getVar('conf_id')));
148: $count = count($options);
149: }
150: if (is_array($options) && $count > 0) {
151: for ($i = 0; $i < $count; ++$i) {
152: $this->optionHandler->delete($options[$i]);
153: }
154: }
155: if (!empty($this->cachedConfigs[$config->getVar('conf_modid')][$config->getVar('conf_catid')])) {
156: unset($this->cachedConfigs[$config->getVar('conf_modid')][$config->getVar('conf_catid')]);
157: }
158: return true;
159: }
160:
161: 162: 163: 164: 165: 166: 167: 168:
169: public function getConfigs(CriteriaElement $criteria = null, $id_as_key = false)
170: {
171: $criteria2 = new CriteriaCompo();
172: if ($criteria) {
173: $criteria2->add($criteria);
174: if (!$criteria->getSort()) {
175: $criteria2->setSort('conf_order');
176: $criteria2->setOrder('ASC');
177: }
178: } else {
179: $criteria2->setSort('conf_order');
180: $criteria2->setOrder('ASC');
181: }
182: return $this->itemHandler->getObjects($criteria2, $id_as_key);
183: }
184:
185: 186: 187: 188: 189: 190: 191:
192: public function getConfigCount(CriteriaElement $criteria = null)
193: {
194: return $this->itemHandler->getCount($criteria);
195: }
196:
197: 198: 199: 200: 201: 202: 203:
204: public function getConfigsByModule($module = 0)
205: {
206: $ret = array();
207: $criteria = new Criteria('conf_modid', (int)($module));
208: $configs = $this->getConfigs($criteria, true);
209: if (is_array($configs)) {
210: foreach (array_keys($configs) as $i) {
211: $ret[$configs[$i]->getVar('conf_name')] = $configs[$i]->getConfValueForOutput();
212: }
213: }
214: return $ret;
215: }
216:
217: 218: 219: 220: 221: 222: 223: 224: 225: 226:
227: public function getConfigsByCat($category, $module = 0)
228: {
229: static $_cachedConfigs;
230: if (!empty($_cachedConfigs[$module][$category])) {
231: return $_cachedConfigs[$module][$category];
232: } else {
233: $ret = array();
234: $criteria = new CriteriaCompo(new Criteria('conf_modid', (int)($module)));
235: if (!empty($category)) {
236: $criteria->add(new Criteria('conf_catid', (int)($category)));
237: }
238: $configs = $this->getConfigs($criteria, true);
239: if (is_array($configs)) {
240: foreach (array_keys($configs) as $i) {
241: $ret[$configs[$i]->getVar('conf_name')] = $configs[$i]->getConfValueForOutput();
242: }
243: }
244: $_cachedConfigs[$module][$category] = $ret;
245: return $_cachedConfigs[$module][$category];
246: }
247: }
248:
249: 250: 251: 252: 253:
254: public function createConfigOption()
255: {
256: $inst = $this->optionHandler->create();
257: return $inst;
258: }
259:
260: 261: 262: 263: 264: 265: 266:
267: public function getConfigOption($id)
268: {
269: $inst = $this->optionHandler->get($id);
270: return $inst;
271: }
272:
273: 274: 275: 276: 277: 278: 279: 280:
281: public function getConfigOptions(CriteriaElement $criteria = null, $id_as_key = false)
282: {
283: return $this->optionHandler->getObjects($criteria, $id_as_key);
284: }
285:
286: 287: 288: 289: 290: 291: 292:
293: public function getConfigOptionsCount(CriteriaElement $criteria = null)
294: {
295: return $this->optionHandler->getCount($criteria);
296: }
297:
298: 299: 300: 301: 302: 303: 304: 305:
306: public function getConfigList($conf_modid, $conf_catid = 0)
307: {
308: if (!empty($this->cachedConfigs[$conf_modid][$conf_catid])) {
309: return $this->cachedConfigs[$conf_modid][$conf_catid];
310: } else {
311: $criteria = new CriteriaCompo(new Criteria('conf_modid', $conf_modid));
312: if (empty($conf_catid)) {
313: $criteria->add(new Criteria('conf_catid', $conf_catid));
314: }
315: $criteria->setSort('conf_order');
316: $criteria->setOrder('ASC');
317: $configs = $this->itemHandler->getObjects($criteria);
318: $confcount = count($configs);
319: $ret = array();
320: for ($i = 0; $i < $confcount; ++$i) {
321: $ret[$configs[$i]->getVar('conf_name')] = $configs[$i]->getConfValueForOutput();
322: }
323: $this->cachedConfigs[$conf_modid][$conf_catid] = $ret;
324: return $ret;
325: }
326: }
327: }
328: