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:
14: /**
15: * GenericHelper implements a Xoops 2.6 Xoops\Module\Helper\HelperAbstract.
16: * We use it pre 2.6 systems so we can encapsulate many of the changes
17: * needed to make modules more compatable with 2.6 in these methods.
18: * The most common deprecated warnings can be avoided by using module
19: * helper methods.
20: *
21: * @category Xmf\Module\Helper\GenericHelper
22: * @package Xmf
23: * @author trabis <lusopoemas@gmail.com>
24: * @author Richard Griffith <richard@geekwright.com>
25: * @copyright 2011-2013 XOOPS Project (http://xoops.org)
26: * @license GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
27: * @version Release: 1.0
28: * @link http://xoops.org
29: * @since 1.0
30: */
31: class GenericHelper
32: {
33: /**
34: * @var string module directory name
35: */
36: protected $dirname;
37:
38: /**
39: * @var XoopsModule
40: */
41: protected $object;
42:
43: /**
44: * @var array of XoopsObjectHandler|XoopsPersistableObjectHandler
45: */
46: private $_handlers;
47:
48: /**
49: * @var array config items
50: */
51: protected $configs;
52:
53: /**
54: * @var bool true if debug is enabled
55: */
56: protected $debug;
57:
58: /**
59: * class constuctor
60: *
61: * @param string $dirname a module directory name
62: */
63: private function __construct($dirname)
64: {
65: $this->dirname = $dirname;
66: }
67:
68: /**
69: * Return instance of module Xmf\Module\GenericHelper for dirname
70: *
71: * @param string $dirname module directory name
72: *
73: * @return Xmf\Module\GenericHelper
74: */
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:
87: /**
88: * get the module object
89: *
90: * @return XoopsModule
91: */
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:
104: /**
105: * get a module config item
106: *
107: * @param string $name name of config item, or blank for all items
108: *
109: * @return mixed string config item, array of config items,
110: * or null if config not found
111: */
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:
135: /**
136: * Get an Object Handler
137: *
138: * @param string $name name of handler to load
139: *
140: * @return bool|XoopsObjectHandler|XoopsPersistableObjectHandler
141: */
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:
160: /**
161: * get a module object
162: *
163: * @return void
164: */
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:
180: /**
181: * get module configs
182: *
183: * @return void
184: */
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:
203: /**
204: * get a handler instance and store in $this->_handlers
205: *
206: * @param string $name name of handler to load
207: *
208: * @return void
209: */
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)) {
223: $db = \XoopsDatabaseFactory::getConnection();
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:
232: /**
233: * load a language file for this module
234: *
235: * @param string $name basename of language file (i.e. 'admin')
236: *
237: * @return bool
238: */
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:
250: /**
251: * Set debug option on or off
252: *
253: * @param bool $bool true to turn on debug logging, false for off
254: *
255: * @return void
256: */
257: public function setDebug($bool = true)
258: {
259: $this->debug = (bool) $bool;
260: }
261:
262: /**
263: * Add a message to the module log
264: *
265: * @param string $log log message
266: *
267: * @return void
268: */
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:
282: /**
283: * Is this the currently active module?
284: *
285: * @return bool
286: */
287: public function isCurrentModule()
288: {
289: if ($GLOBALS['xoopsModule']->getVar('dirname') == $this->dirname) {
290: return true;
291: }
292:
293: return false;
294: }
295:
296: /**
297: * Does user have admin rights to this module?
298: *
299: * @return bool true is user has admin right, else false
300: */
301: public function isUserAdmin()
302: {
303: return $GLOBALS['xoopsUser']->isAdmin($this->getModule()->getVar('mid'));
304: }
305:
306: /**
307: * Return absolute URL for a module relative URL
308: *
309: * @param string $url module relative URL
310: *
311: * @return string
312: */
313: public function url($url = '')
314: {
315: return \XoopsBaseConfig::get('url') . '/modules/' . $this->dirname . '/' . $url;
316: }
317:
318: /**
319: * Return absolute filesystem path for a module relative path
320: *
321: * @param string $path module relative file system path
322: *
323: * @return string
324: */
325: public function path($path = '')
326: {
327: return \XoopsBaseConfig::get('root-path') . '/modules/' . $this->dirname . '/' . $path;
328: }
329:
330: /**
331: * Redirect the user to a page within this module
332: *
333: * @param string $url module relative url (i.e. index.php)
334: * @param int $time time in seconds to show redirect message
335: * @param string $message redirect message
336: *
337: * @return void
338: */
339: public function redirect($url, $time = 3, $message = '')
340: {
341: redirect_header($this->url($url), $time, $message);
342: exit;
343: }
344:
345: }
346: