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: * Xmf\Module\Helper\AbstractHelper defines the basis for various
16: * helpers that simplify routine module tasks.
17: *
18: * @category Xmf\Module\Helper\AbstractHelper
19: * @package Xmf
20: * @author trabis <lusopoemas@gmail.com>
21: * @author Richard Griffith <richard@geekwright.com>
22: * @copyright 2011-2016 XOOPS Project (http://xoops.org)
23: * @license GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
24: * @link http://xoops.org
25: */
26: abstract class AbstractHelper
27: {
28: /**
29: * @var XoopsModule
30: */
31: protected $module;
32:
33: /**
34: * @var bool true if debug is enabled
35: */
36: protected $debug;
37:
38: /**
39: * Instantiate a XoopsModule object for the helper to use.
40: * The module is determined as follows:
41: * - if null is passed, use the current module
42: * - if a string is passed, use as dirname to load
43: *
44: * @param string|null $dirname dirname
45: */
46: public function __construct($dirname = null)
47: {
48: $this->module = null;
49:
50: if (empty($dirname)) {
51: // nothing specified, use current module
52: // check if we are running in 2.6
53: if (class_exists('Xoops', false)) {
54: $xoops = \Xoops::getInstance();
55: if ($xoops->isModule()) {
56: $this->module = $xoops->module;
57: }
58: } else {
59: $this->module = $GLOBALS['xoopsModule'];
60: }
61: } else {
62: // assume dirname specified, try to get a module object
63: /* @var $moduleHandler \XoopsModuleHandler */
64: $moduleHandler = xoops_getHandler('module');
65: $this->module = $moduleHandler->getByDirname($dirname);
66: }
67: if (is_object($this->module)) {
68: $this->init();
69: }
70: }
71:
72: /**
73: * init() is called once/if __construct has a module object.
74: * $this->module will have a module object that any further
75: * initialization can use.
76: *
77: * @return void
78: */
79: abstract public function init();
80:
81: /**
82: * Set debug option on or off
83: *
84: * @param bool $bool true to turn on debug logging, false for off
85: *
86: * @return void
87: */
88: public function setDebug($bool = true)
89: {
90: $this->debug = (bool) $bool;
91: }
92:
93: /**
94: * Add a message to the module log
95: *
96: * @param string $log log message
97: *
98: * @return void
99: */
100: public function addLog($log)
101: {
102: if ($this->debug) {
103: if (is_object($GLOBALS['xoopsLogger'])) {
104: $GLOBALS['xoopsLogger']->addExtra(get_called_class(), $log);
105: }
106: }
107: }
108: }
109: