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