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: use Xmf\Module\Helper;
15:
16: /**
17: * Xmf\Module\Helper\AbstractHelper defines the basis for various
18: * helpers that simplfy routine module tasks.
19: * uses.
20: *
21: * @category Xmf\Module\Helper\AbstractHelper
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: abstract class AbstractHelper
32: {
33: /**
34: * @var XoopsModule
35: */
36: protected $module;
37:
38: /**
39: * @var bool true if debug is enabled
40: */
41: protected $debug;
42:
43: /**
44: * Instantiate a XoopsModule object for the helper to use.
45: * This occurs in one of three ways
46: * - if null is passed, use the current module
47: * - if a string is passed, use as dirname for a module
48: * - if an object is passed, use it as the module object
49: *
50: * @param mixed $module string dirname | object XoopsModule
51: */
52: public function __construct($module = null)
53: {
54: $this->module = null;
55:
56: if (empty($module)) {
57: // nothing specified, use current module
58: // check if we are running in 2.6
59: if (class_exists('Xoops', false)) {
60: $xoops=\Xoops::getInstance();
61: if ($xoops->isModule()) {
62: $this->module = $xoops->module;
63: }
64: } else {
65: $this->module = $GLOBALS['xoopsModule'];
66: }
67: } elseif (is_scalar($module)) {
68: // dirname specified, get a module object
69: $helper = Helper::getHelper($module);
70: if ($helper) {
71: $this->module = $helper->getModule();
72: }
73: } else {
74: // assume a passed object is appropriate
75: if (is_object($module)) {
76: $this->module = $module;
77: }
78: }
79: if (is_object($this->module)) {
80: $this->init();
81: }
82: }
83:
84: /**
85: * init() is called once/if __construct has a module object.
86: * $this->module will have a module object that any further
87: * initialization can use.
88: *
89: * @return void
90: */
91: abstract public function init();
92:
93: /**
94: * Set debug option on or off
95: *
96: * @param bool $bool true to turn on debug logging, false for off
97: *
98: * @return void
99: */
100: public function setDebug($bool = true)
101: {
102: $this->debug = (bool) $bool;
103: }
104:
105: /**
106: * Add a message to the module log
107: *
108: * @param string $log log message
109: *
110: * @return void
111: */
112: public function addLog($log)
113: {
114: if ($this->debug) {
115: \Xoops::getInstance()->logger()->debug($log, array('channel'=>'Extra'));
116: }
117: }
118: }
119: