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