1 | <?php |
||
33 | abstract class GenericHelper |
||
34 | { |
||
35 | /** |
||
36 | * @var string module directory name |
||
37 | */ |
||
38 | protected $dirname; |
||
39 | |||
40 | /** |
||
41 | * @var \XoopsModule |
||
42 | */ |
||
43 | protected $object; |
||
44 | |||
45 | /** |
||
46 | * @var array of XoopsObjectHandler|XoopsPersistableObjectHandler |
||
47 | */ |
||
48 | protected $handlers; |
||
49 | |||
50 | /** |
||
51 | * @var array config items |
||
52 | */ |
||
53 | protected $configs; |
||
54 | |||
55 | /** |
||
56 | * @var bool true if debug is enabled |
||
57 | */ |
||
58 | protected $debug; |
||
59 | |||
60 | /** |
||
61 | * class constructor |
||
62 | * |
||
63 | * @param string $dirname a module directory name |
||
64 | */ |
||
65 | protected function __construct($dirname) |
||
69 | |||
70 | /** |
||
71 | * get the module object |
||
72 | * |
||
73 | * @return XoopsModule |
||
74 | */ |
||
75 | public function getModule() |
||
76 | { |
||
77 | if ($this->object == null) { |
||
78 | $this->initObject(); |
||
79 | } |
||
80 | if (!is_object($this->object)) { |
||
81 | $this->addLog("ERROR :: Module '{$this->dirname}' does not exist"); |
||
82 | } |
||
83 | |||
84 | return $this->object; |
||
85 | } |
||
86 | |||
87 | /** |
||
88 | * get a module config item |
||
89 | * |
||
90 | * @param string $name name of config item, or blank for all items |
||
91 | * @param mixed $default default value to return if config $name is not set |
||
92 | * |
||
93 | * @return mixed string config item, array of config items, |
||
94 | * or null if config not found |
||
95 | */ |
||
96 | public function getConfig($name = null, $default = null) |
||
97 | { |
||
98 | if ($this->configs == null) { |
||
99 | $this->initConfig(); |
||
100 | } |
||
101 | if (empty($name)) { |
||
102 | $this->addLog("Getting all config"); |
||
103 | |||
104 | return $this->configs; |
||
105 | } |
||
106 | |||
107 | if (!isset($this->configs[$name])) { |
||
108 | $this->addLog("ERROR :: Config '{$name}' does not exist"); |
||
109 | return $default; |
||
110 | } |
||
111 | |||
112 | $this->addLog("Getting config '{$name}' : " . $this->configs[$name]); |
||
113 | |||
114 | return $this->configs[$name]; |
||
115 | } |
||
116 | |||
117 | /** |
||
118 | * Get an Object Handler |
||
119 | * |
||
120 | * @param string $name name of handler to load |
||
121 | * |
||
122 | * @return bool|XoopsObjectHandler|XoopsPersistableObjectHandler |
||
123 | */ |
||
124 | public function getHandler($name) |
||
125 | { |
||
126 | $ret = false; |
||
127 | $name = strtolower($name); |
||
128 | if (!isset($this->handlers[$name])) { |
||
129 | $this->initHandler($name); |
||
130 | } |
||
131 | |||
132 | if (!isset($this->handlers[$name])) { |
||
133 | $this->addLog("ERROR :: Handler '{$name}' does not exist"); |
||
134 | } else { |
||
135 | $this->addLog("Getting handler '{$name}'"); |
||
136 | $ret = $this->handlers[$name]; |
||
137 | } |
||
138 | |||
139 | return $ret; |
||
140 | } |
||
141 | |||
142 | /** |
||
143 | * get a module object |
||
144 | * |
||
145 | * @return void |
||
146 | */ |
||
147 | protected function initObject() |
||
148 | { |
||
149 | global $xoopsModule; |
||
150 | if (isset($xoopsModule) && is_object($xoopsModule) |
||
151 | && $xoopsModule->getVar('dirname') == $this->dirname |
||
152 | ) { |
||
153 | $this->object = $xoopsModule; |
||
154 | } else { |
||
155 | /* @var $module_handler XoopsModuleHandler */ |
||
156 | $module_handler = xoops_getHandler('module'); |
||
|
|||
157 | $this->object = $module_handler->getByDirname($this->dirname); |
||
158 | } |
||
159 | $this->addLog('INIT MODULE OBJECT'); |
||
160 | } |
||
161 | |||
162 | /** |
||
163 | * get module configs |
||
164 | * |
||
165 | * @return void |
||
166 | */ |
||
167 | protected function initConfig() |
||
168 | { |
||
169 | $this->addLog('INIT CONFIG'); |
||
170 | global $xoopsModule; |
||
171 | if (isset($xoopsModule) && is_object($xoopsModule) |
||
172 | && $xoopsModule->getVar('dirname') == $this->dirname |
||
173 | ) { |
||
174 | global $xoopsModuleConfig; |
||
175 | $this->configs = $xoopsModuleConfig; |
||
176 | } else { |
||
177 | /* @var $config_handler XoopsConfigHandler */ |
||
178 | $config_handler = xoops_getHandler('config'); |
||
179 | $this->configs = $config_handler->getConfigsByCat(0, $this->getModule()->getVar('mid')); |
||
180 | } |
||
181 | } |
||
182 | |||
183 | /** |
||
184 | * get a handler instance and store in $this->_handlers |
||
185 | * |
||
186 | * @param string $name name of handler to load |
||
187 | * |
||
188 | * @return void |
||
189 | */ |
||
190 | protected function initHandler($name) |
||
191 | { |
||
192 | $this->addLog('INIT ' . $name . ' HANDLER'); |
||
193 | |||
194 | if (!isset($this->handlers[$name])) { |
||
195 | $hnd_file = XOOPS_ROOT_PATH . "/modules/{$this->dirname}/class/{$name}.php"; |
||
196 | if (file_exists($hnd_file)) { |
||
197 | include_once $hnd_file; |
||
198 | } |
||
199 | $class = ucfirst(strtolower($this->dirname)) |
||
200 | . ucfirst(strtolower($name)) . 'Handler'; |
||
201 | if (class_exists($class)) { |
||
202 | $db = \XoopsDatabaseFactory::getDatabaseConnection(); |
||
203 | $this->handlers[$name] = new $class($db); |
||
204 | $this->addLog("Loading class '{$class}'"); |
||
205 | } else { |
||
206 | $this->addLog("ERROR :: Class '{$class}' could not be loaded"); |
||
207 | } |
||
208 | } |
||
209 | } |
||
210 | |||
211 | /** |
||
212 | * load a language file for this module |
||
213 | * |
||
214 | * @param string $name basename of language file (i.e. 'admin') |
||
215 | * |
||
216 | * @return bool |
||
217 | */ |
||
218 | public function loadLanguage($name) |
||
219 | { |
||
220 | if ($ret = Language::load($name, $this->dirname)) { |
||
221 | $this->addLog("Loading language '{$name}'"); |
||
222 | } else { |
||
223 | $this->addLog("ERROR :: Language '{$name}' could not be loaded"); |
||
224 | } |
||
225 | |||
226 | return $ret; |
||
227 | } |
||
228 | |||
229 | /** |
||
230 | * Set debug option on or off |
||
231 | * |
||
232 | * @param bool $bool true to turn on debug logging, false for off |
||
233 | * |
||
234 | * @return void |
||
235 | */ |
||
236 | public function setDebug($bool = true) |
||
240 | |||
241 | /** |
||
242 | * Add a message to the module log |
||
243 | * |
||
244 | * @param string $log log message |
||
245 | * |
||
246 | * @return void |
||
247 | */ |
||
248 | public function addLog($log) |
||
249 | { |
||
250 | if ($this->debug) { |
||
251 | if (is_object($GLOBALS['xoopsLogger'])) { |
||
252 | if (!is_scalar($log)) { |
||
253 | $log = serialize($log); |
||
254 | } |
||
255 | $GLOBALS['xoopsLogger']->addExtra( |
||
256 | is_object($this->object) ? $this->object->name() : $this->dirname, |
||
257 | $log |
||
258 | ); |
||
259 | } |
||
260 | } |
||
261 | } |
||
262 | |||
263 | /** |
||
264 | * Is this the currently active module? |
||
265 | * |
||
266 | * @return bool |
||
267 | */ |
||
268 | public function isCurrentModule() |
||
276 | |||
277 | /** |
||
278 | * Does user have admin rights to this module? |
||
279 | * |
||
280 | * @return bool true is user has admin right, else false |
||
281 | */ |
||
282 | public function isUserAdmin() |
||
286 | |||
287 | /** |
||
288 | * Return absolute URL for a module relative URL |
||
289 | * |
||
290 | * @param string $url module relative URL |
||
291 | * |
||
292 | * @return string |
||
293 | */ |
||
294 | public function url($url = '') |
||
298 | |||
299 | /** |
||
300 | * Return absolute filesystem path for a module relative path |
||
301 | * |
||
302 | * @param string $path module relative file system path |
||
303 | * |
||
304 | * @return string |
||
305 | */ |
||
306 | public function path($path = '') |
||
310 | |||
311 | /** |
||
312 | * Redirect the user to a page within this module |
||
313 | * |
||
314 | * @param string $url module relative url (i.e. index.php) |
||
315 | * @param int $time time in seconds to show redirect message |
||
316 | * @param string $message redirect message |
||
317 | * |
||
318 | * @return void |
||
319 | */ |
||
320 | public function redirect($url, $time = 3, $message = '') |
||
324 | } |
||
325 |
This function has been deprecated.