XOOPS  2.6.0
GenericHelper.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 
12 namespace Xmf\Module\Helper;
13 
32 {
36  protected $dirname;
37 
41  protected $object;
42 
46  private $_handlers;
47 
51  protected $configs;
52 
56  protected $debug;
57 
63  private function __construct($dirname)
64  {
65  $this->dirname = $dirname;
66  }
67 
75  public static function getInstance($dirname = 'notsetyet')
76  {
77  static $instance = array();
78  if (!isset($instance[$dirname])) {
79  $class = __CLASS__;
80  $instance[$dirname] = new $class($dirname);
81  }
82 
83  return $instance[$dirname];
84 
85  }
86 
92  public function getModule()
93  {
94  if ($this->object == null) {
95  $this->_initObject();
96  }
97  if (!is_object($this->object)) {
98  $this->addLog("ERROR :: Module '{$this->dirname}' does not exist");
99  }
100 
101  return $this->object;
102  }
103 
112  public function getConfig($name)
113  {
114  if ($this->configs == null) {
115  $this->_initConfig();
116  }
117  if (!$name) {
118  $this->addLog("Getting all config");
119 
120  return $this->configs;
121  }
122 
123  if (!isset($this->configs[$name])) {
124  $this->addLog("ERROR :: Config '{$name}' does not exist");
125  $ret = null;
126 
127  return $ret;
128  }
129 
130  $this->addLog("Getting config '{$name}' : " . $this->configs[$name]);
131 
132  return $this->configs[$name];
133  }
134 
142  public function getHandler($name)
143  {
144  $ret = false;
145  $name = strtolower($name);
146  if (!isset($this->_handlers[$name])) {
147  $this->_initHandler($name);
148  }
149 
150  if (!isset($this->_handlers[$name])) {
151  $this->addLog("ERROR :: Handler '{$name}' does not exist");
152  } else {
153  $this->addLog("Getting handler '{$name}'");
154  $ret = $this->_handlers[$name];
155  }
156 
157  return $ret;
158  }
159 
165  private function _initObject()
166  {
167  global $xoopsModule;
168  if (isset($xoopsModule) && is_object($xoopsModule)
169  && $xoopsModule->getVar('dirname') == $this->dirname
170  ) {
171  $this->object = $xoopsModule;
172  } else {
173  /* @var $module_handler XoopsModuleHandler */
174  $module_handler = xoops_getHandler('module');
175  $this->object = $module_handler->getByDirname($this->dirname);
176  }
177  $this->addLog('INIT MODULE OBJECT');
178  }
179 
185  private function _initConfig()
186  {
187  $this->addLog('INIT CONFIG');
188  global $xoopsModule;
189  if (isset($xoopsModule) && is_object($xoopsModule)
190  && $xoopsModule->getVar('dirname') == $this->dirname
191  ) {
192  global $xoopsModuleConfig;
193  $this->configs =& $xoopsModuleConfig;
194  } else {
195  /* @var $config_handler XoopsConfigHandler */
196  $config_handler = xoops_gethandler('config');
197  $this->configs = $config_handler->getConfigsByCat(
198  0, $this->getModule()->getVar('mid')
199  );
200  }
201  }
202 
210  private function _initHandler($name)
211  {
212  $this->addLog('INIT ' . $name . ' HANDLER');
213 
214  if (!isset($this->_handlers[$name])) {
215  $hnd_file = \XoopsBaseConfig::get('root-path') .
216  "/modules/{$this->dirname}/class/{$name}.php";
217  if (file_exists($hnd_file)) {
218  include_once $hnd_file;
219  }
220  $class = ucfirst(strtolower($this->dirname))
221  . ucfirst(strtolower($name)) . 'Handler';
222  if (class_exists($class)) {
224  $this->_handlers[$name] = new $class($db);
225  $this->addLog("Loading class '{$class}'");
226  } else {
227  $this->addLog("ERROR :: Class '{$class}' could not be loaded");
228  }
229  }
230  }
231 
239  public function loadLanguage($name)
240  {
241  if ($ret = \Xmf\Language::load($name, $this->dirname)) {
242  $this->addLog("Loading language '{$name}'");
243  } else {
244  $this->addLog("ERROR :: Language '{$name}' could not be loaded");
245  }
246 
247  return $ret;
248  }
249 
257  public function setDebug($bool = true)
258  {
259  $this->debug = (bool) $bool;
260  }
261 
269  public function addLog($log)
270  {
271  if ($this->debug) {
272  if (!is_scalar($log)) {
273  $log = serialize($log);
274  }
275  $name = is_object($this->object) ? $this->object->name() : $this->dirname;
276  \Xoops::getInstance()->logger()->debug($log, array('channel'=>'Extra', 'name'=>$name));
277  }
278  }
279 
280  // these added to mimic 2.6 Xoops\Module\Helper\HelperAbstract
281 
287  public function isCurrentModule()
288  {
289  if ($GLOBALS['xoopsModule']->getVar('dirname') == $this->dirname) {
290  return true;
291  }
292 
293  return false;
294  }
295 
301  public function isUserAdmin()
302  {
303  return $GLOBALS['xoopsUser']->isAdmin($this->getModule()->getVar('mid'));
304  }
305 
313  public function url($url = '')
314  {
315  return \XoopsBaseConfig::get('url') . '/modules/' . $this->dirname . '/' . $url;
316  }
317 
325  public function path($path = '')
326  {
327  return \XoopsBaseConfig::get('root-path') . '/modules/' . $this->dirname . '/' . $path;
328  }
329 
339  public function redirect($url, $time = 3, $message = '')
340  {
341  redirect_header($this->url($url), $time, $message);
342  exit;
343  }
344 
345 }
$path
Definition: execute.php:31
$xoopsModuleConfig
Definition: common.php:266
$xoopsModule
Definition: common.php:264
static getInstance()
Definition: Xoops.php:160
static getInstance($dirname= 'notsetyet')
xoops_getHandler($name, $optional=false)
Definition: functions.php:25
exit
Definition: browse.php:104
if($_SERVER['REQUEST_METHOD']== 'POST') $config_handler
static get($name)
static getConnection($options=null)
Definition: Factory.php:51
static load($name, $domain= '', $language=null)
Definition: Language.php:54
$url
Definition: register.php:72
$GLOBALS['xoops']
Definition: common.php:33
$module_handler
Definition: main.php:55
redirect($url, $time=3, $message= '')
redirect_header($url, $time=3, $message= '', $addredirect=true, $allowExternalLink=false)
Definition: functions.php:271