XOOPS  2.6.0
config.php
Go to the documentation of this file.
1 <?php
2 /*
3  You may not change or alter any portion of this comment or credits
4  of supporting developers from this source code or any supporting source code
5  which is considered copyrighted (c) material of the original comment or credit authors.
6 
7  This program is distributed in the hope that it will be useful,
8  but WITHOUT ANY WARRANTY; without even the implied warranty of
9  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10  */
11 
16 
27 {
34  private $_iHandler;
35 
42  private $_oHandler;
43 
51  private $_cachedConfigs = array();
52 
56  public function __construct()
57  {
59  $this->_iHandler = $helper->getHandlerItem();
60  $this->_oHandler = $helper->getHandlerOption();
61  }
62 
69  public function createConfig()
70  {
71  $instance = $this->_iHandler->create();
72  return $instance;
73  }
74 
83  public function getConfig($id, $withoptions = false)
84  {
85  /* @var $config UserconfigsItem */
86  $config = $this->_iHandler->get($id);
87  if ($withoptions == true) {
88  $config->setConfOptions($this->getConfigOptions(new Criteria('conf_id', $id)));
89  }
90  return $config;
91  }
92 
101  {
102  if (!$this->_iHandler->insert($config)) {
103  return false;
104  }
105  $options = $config->getConfOptions();
106  $count = count($options);
107  $conf_id = $config->getVar('conf_id');
108  for ($i = 0; $i < $count; ++$i) {
109  $options[$i]->setVar('conf_id', $conf_id);
110  if (!$this->_oHandler->insert($options[$i])) {
111  foreach ($options[$i]->getErrors() as $msg) {
112  $config->setErrors($msg);
113  }
114  }
115  }
116  if (!empty($this->_cachedConfigs[$config->getVar('conf_modid')][$config->getVar('conf_uid')])) {
117  unset($this->_cachedConfigs[$config->getVar('conf_modid')][$config->getVar('conf_uid')]);
118  }
119  return true;
120  }
121 
130  {
131  if (!$this->_iHandler->delete($config, true)) {
132  return false;
133  }
134  $options = $config->getConfOptions();
135  $count = count($options);
136  if ($count == 0) {
137  $options = $this->getConfigOptions(new Criteria('conf_id', $config->getVar('conf_id')));
138  $count = count($options);
139  }
140  if (is_array($options) && $count > 0) {
141  for ($i = 0; $i < $count; ++$i) {
142  $this->_oHandler->delete($options[$i], true);
143  }
144  }
145  if (!empty($this->_cachedConfigs[$config->getVar('conf_modid')][$config->getVar('conf_uid')])) {
146  unset($this->_cachedConfigs[$config->getVar('conf_modid')][$config->getVar('conf_uid')]);
147  }
148  return true;
149  }
150 
159  public function getConfigs(CriteriaElement $criteria = null, $id_as_key = false)
160  {
161  $criteria2 = new CriteriaCompo();
162  if ($criteria) {
163  $criteria2->add($criteria);
164  if (!$criteria->getSort()) {
165  $criteria2->setSort('conf_order');
166  $criteria2->setOrder('ASC');
167  }
168  } else {
169  $criteria2->setSort('conf_order');
170  $criteria2->setOrder('ASC');
171  }
172  return $this->_iHandler->getObjects($criteria2, $id_as_key);
173  }
174 
182  public function getConfigCount(CriteriaElement $criteria = null)
183  {
184  return $this->_iHandler->getCount($criteria);
185  }
186 
194  public function getConfigsByModule($module = 0)
195  {
196  $ret = array();
197  $criteria = new Criteria('conf_modid', intval($module));
198  $configs = $this->getConfigs($criteria, true);
199  if (is_array($configs)) {
200  foreach (array_keys($configs) as $i) {
201  $ret[$configs[$i]->getVar('conf_name')] = $configs[$i]->getConfValueForOutput();
202  }
203  }
204  $_cachedConfigs[$module] = $ret;
205  return $_cachedConfigs[$module];
206  }
207 
215  public function deleteConfigsByModule($module = 0)
216  {
217  $criteria = new Criteria('conf_modid', intval($module));
218  $configs = $this->getConfigs($criteria, true);
219  if (is_array($configs)) {
220  foreach (array_keys($configs) as $i) {
221  $this->deleteConfig($configs[$i]);
222  }
223  return true;
224  }
225  return false;
226  }
227 
236  public function getConfigsByUser($uid, $moduleId)
237  {
238  static $_cachedConfigs;
239  if (!empty($_cachedConfigs[$moduleId][$uid])) {
240  return $_cachedConfigs[$moduleId][$uid];
241  } else {
242  $ret = array();
243  $criteria = new CriteriaCompo(new Criteria('conf_modid', intval($moduleId)));
244  $criteria->add(new Criteria('conf_uid', intval($uid)));
245  $configs = $this->getConfigs($criteria, true);
246  if (is_array($configs)) {
247  foreach (array_keys($configs) as $i) {
248  $ret[$configs[$i]->getVar('conf_name')] = $configs[$i]->getConfValueForOutput();
249  }
250  }
251  $_cachedConfigs[$moduleId][$uid] = $ret;
252  return $_cachedConfigs[$moduleId][$uid];
253  }
254  }
255 
261  public function createConfigOption()
262  {
263  $inst = $this->_oHandler->create();
264  return $inst;
265  }
266 
274  public function getConfigOption($id)
275  {
276  $inst = $this->_oHandler->get($id);
277  return $inst;
278  }
279 
288  public function getConfigOptions(CriteriaElement $criteria = null, $id_as_key = false)
289  {
290  return $this->_oHandler->getObjects($criteria, $id_as_key);
291  }
292 
301  {
302  return $this->_oHandler->getCount($criteria);
303  }
304 
313  public function getConfigList($conf_modid, $conf_uid = 0)
314  {
315  if (!empty($this->_cachedConfigs[$conf_modid][$conf_uid])) {
316  return $this->_cachedConfigs[$conf_modid][$conf_uid];
317  } else {
318  $criteria = new CriteriaCompo(new Criteria('conf_modid', $conf_modid));
319  if (empty($conf_uid)) {
320  $criteria->add(new Criteria('conf_uid', $conf_uid));
321  }
322  $criteria->setSort('conf_order');
323  $criteria->setOrder('ASC');
324  $configs = $this->_iHandler->getObjects($criteria);
325  $confcount = count($configs);
326  $ret = array();
327  for ($i = 0; $i < $confcount; ++$i) {
328  $ret[$configs[$i]->getVar('conf_name')] = $configs[$i]->getConfValueForOutput();
329  }
330  $this->_cachedConfigs[$conf_modid][$conf_uid] = $ret;
331  return $ret;
332  }
333  }
334 
336  {
337  /* @var $plugin UserconfigsPluginInterface */
338  if ($plugin = \Xoops\Module\Plugin::getPlugin($module->getVar('dirname'), 'userconfigs')) {
339  // now reinsert them with the new settings
340  $configs = $plugin->configs();
341  if (!is_array($configs)) {
342  $configs = array();
343  }
344 
345  if (is_array($configs) && count($configs) > 0) {
346  $order = 0;
347  foreach ($configs as $config) {
348  $confobj = $this->createConfig();
349  $confobj->setVar('conf_modid', $module->getVar('mid'));
350  $confobj->setVar('conf_uid', $uid);
351  $confobj->setVar('conf_name', $config['name']);
352  $confobj->setVar('conf_title', $config['title'], true);
353  $confobj->setVar('conf_desc', $config['description'], true);
354  $confobj->setVar('conf_formtype', $config['formtype']);
355  $confobj->setVar('conf_valuetype', $config['valuetype']);
356  $confobj->setConfValueForInput($config['default'], true);
357  $confobj->setVar('conf_order', $order);
358  if (isset($config['options']) && is_array($config['options'])) {
359  foreach ($config['options'] as $key => $value) {
360  $confop = $this->createConfigOption();
361  $confop->setVar('confop_name', $key, true);
362  $confop->setVar('confop_value', $value, true);
363  $confobj->setConfOptions($confop);
364  unset($confop);
365  }
366  }
367  ++$order;
368  $this->insertConfig($confobj);
369  unset($confobj);
370  }
371  unset($configs);
372  }
373  }
374  }
375 }
getConfigList($conf_modid, $conf_uid=0)
Definition: config.php:313
getConfig($id, $withoptions=false)
Definition: config.php:83
$i
Definition: dialog.php:68
getConfigOptionsCount(CriteriaElement $criteria=null)
Definition: config.php:300
$options['editor']
getConfigsByModule($module=0)
Definition: config.php:194
deleteConfig(UserconfigsItem $config)
Definition: config.php:129
insertConfig(UserconfigsItem $config)
Definition: config.php:100
getConfigsByUser($uid, $moduleId)
Definition: config.php:236
createDefaultUserConfigs($uid, XoopsModule $module)
Definition: config.php:335
static getInstance()
Definition: helper.php:36
deleteConfigsByModule($module=0)
Definition: config.php:215
getConfigs(CriteriaElement $criteria=null, $id_as_key=false)
Definition: config.php:159
getVar($key, $format= 's')
getConfigCount(CriteriaElement $criteria=null)
Definition: config.php:182
getConfOptions()
Definition: item.php:230
$id
Definition: admin_menu.php:36
return $config
Definition: config.php:31
$module
Definition: main.php:52
if(!$xoops->isUser()) $uid
Definition: index.php:31
$configs
Definition: config.php:27
$criteria2
getConfigOptions(CriteriaElement $criteria=null, $id_as_key=false)
Definition: config.php:288
$helper
$criteria
if(!is_object($module)||!$module->getVar('isactive')) $msg
Definition: groupperm.php:38