Completed
Pull Request — master (#573)
by Richard
08:56 queued 27s
created

GenericHelper::init()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
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\Language;
15
16
/**
17
 * GenericHelper implements a Xoops 2.6 Xoops\Module\Helper\HelperAbstract.
18
 * We use it pre 2.6 systems so we can encapsulate many of the changes
19
 * needed to make modules more compatible with 2.6 in these methods.
20
 * The most common deprecated warnings can be avoided by using module
21
 * helper methods.
22
 *
23
 * @category  Xmf\Module\Helper\GenericHelper
24
 * @package   Xmf
25
 * @author    trabis <[email protected]>
26
 * @author    Richard Griffith <[email protected]>
27
 * @copyright 2011-2018 XOOPS Project (http://xoops.org)
28
 * @license   GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
29
 * @link      http://xoops.org
30
 */
31
abstract class GenericHelper extends AbstractHelper
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
    protected $handlers;
47
48
    /**
49
     * @var array config items
50
     */
51
    protected $configs;
52
53
    /**
54
     * Initialize parent::__construct calls this after verifying module object.
55
     *
56
     * @return void
57
     */
58 1
    public function init()
59
    {
60 1
        $this->object = $this->module;
61 1
        $this->dirname = $this->object->getVar('dirname');
62 1
    }
63
64
    /**
65
     * get the module object
66
     *
67
     * @return \XoopsModule
68
     */
69 1
    public function getModule()
70
    {
71 1
        if ($this->object == null) {
72
            $this->initObject();
73
        }
74 1
        if (!is_object($this->object)) {
75
            $this->addLog("ERROR :: Module '{$this->dirname}' does not exist");
76
        }
77
78 1
        return $this->object;
79
    }
80
81
    /**
82
     * get a module config item
83
     *
84
     * @param string $name    name of config item, or blank for all items
85
     * @param mixed  $default default value to return if config $name is not set
86
     *
87
     * @return mixed string config item, array of config items,
88
     *                or null if config not found
89
     */
90
    public function getConfig($name = null, $default = null)
91
    {
92
        if ($this->configs == null) {
93
            $this->initConfig();
94
        }
95
        if (empty($name)) {
96
            $this->addLog("Getting all config");
97
98
            return $this->configs;
99
        }
100
101
        if (!isset($this->configs[$name])) {
102
            $this->addLog("ERROR :: Config '{$name}' does not exist");
103
            return $default;
104
        }
105
106
        $this->addLog("Getting config '{$name}' : " . $this->serializeForHelperLog($this->configs[$name]));
107
108
        return $this->configs[$name];
109
    }
110
111
    /**
112
     * Get an Object Handler
113
     *
114
     * @param string $name name of handler to load
115
     *
116
     * @return bool|XoopsObjectHandler|XoopsPersistableObjectHandler
0 ignored issues
show
Bug introduced by
The type Xmf\Module\Helper\XoopsObjectHandler was not found. Did you mean XoopsObjectHandler? If so, make sure to prefix the type with \.
Loading history...
Bug introduced by
The type Xmf\Module\Helper\XoopsPersistableObjectHandler was not found. Did you mean XoopsPersistableObjectHandler? If so, make sure to prefix the type with \.
Loading history...
117
     */
118
    public function getHandler($name)
119
    {
120
        $ret = false;
121
        $name = strtolower($name);
122
        if (!isset($this->handlers[$name])) {
123
            $this->initHandler($name);
124
        }
125
126
        if (!isset($this->handlers[$name])) {
127
            $this->addLog("ERROR :: Handler '{$name}' does not exist");
128
        } else {
129
            $this->addLog("Getting handler '{$name}'");
130
            $ret = $this->handlers[$name];
131
        }
132
133
        return $ret;
134
    }
135
136
    /**
137
     * get a module object
138
     *
139
     * @return void
140
     */
141
    protected function initObject()
142
    {
143
        global $xoopsModule;
144
        if (isset($xoopsModule) && is_object($xoopsModule)
145
            && $xoopsModule->getVar('dirname') == $this->dirname
146
        ) {
147
            $this->object = $xoopsModule;
148
        } else {
149
            /* @var $module_handler \XoopsModuleHandler */
150
            $module_handler = xoops_getHandler('module');
0 ignored issues
show
Deprecated Code introduced by
The function xoops_getHandler() has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

150
            $module_handler = /** @scrutinizer ignore-deprecated */ xoops_getHandler('module');
Loading history...
151
            $this->object = $module_handler->getByDirname($this->dirname);
152
        }
153
        $this->addLog('INIT MODULE OBJECT');
154
    }
155
156
    /**
157
     * get module configs
158
     *
159
     * @return void
160
     */
161
    protected function initConfig()
162
    {
163
        $this->addLog('INIT CONFIG');
164
        global $xoopsModule;
165
        if (isset($xoopsModule) && is_object($xoopsModule)
166
            && $xoopsModule->getVar('dirname') == $this->dirname
167
        ) {
168
            global $xoopsModuleConfig;
169
            $this->configs = $xoopsModuleConfig;
170
        } else {
171
            /* @var $config_handler \XoopsConfigHandler */
172
            $config_handler = xoops_getHandler('config');
0 ignored issues
show
Deprecated Code introduced by
The function xoops_getHandler() has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

172
            $config_handler = /** @scrutinizer ignore-deprecated */ xoops_getHandler('config');
Loading history...
173
            $this->configs = $config_handler->getConfigsByCat(0, $this->getModule()->getVar('mid'));
0 ignored issues
show
Deprecated Code introduced by
The function Xoops\Core\Kernel\Handle...dler::getConfigsByCat() has been deprecated: Use getConfigsByModule instead ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

173
            $this->configs = /** @scrutinizer ignore-deprecated */ $config_handler->getConfigsByCat(0, $this->getModule()->getVar('mid'));

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
174
        }
175
    }
176
177
    /**
178
     * get a handler instance and store in $this->_handlers
179
     *
180
     * @param string $name name of handler to load
181
     *
182
     * @return void
183
     */
184
    protected function initHandler($name)
185
    {
186
        $this->addLog('INIT ' . $name . ' HANDLER');
187
188
        if (!isset($this->handlers[$name])) {
189
            $hnd_file = XOOPS_ROOT_PATH . "/modules/{$this->dirname}/class/{$name}.php";
190
            if (file_exists($hnd_file)) {
191
                include_once $hnd_file;
192
            }
193
            $class = ucfirst(strtolower($this->dirname))
194
                . ucfirst(strtolower($name)) . 'Handler';
195
            if (class_exists($class)) {
196
                $db = \XoopsDatabaseFactory::getDatabaseConnection();
197
                $this->handlers[$name] = new $class($db);
198
                $this->addLog("Loading class '{$class}'");
199
            } else {
200
                $this->addLog("ERROR :: Class '{$class}' could not be loaded");
201
            }
202
        }
203
    }
204
205
    /**
206
     * load a language file for this module
207
     *
208
     * @param string $name basename of language file (i.e. 'admin')
209
     *
210
     * @return bool
211
     */
212
    public function loadLanguage($name)
213
    {
214
        if ($ret = Language::load($name, $this->dirname)) {
215
            $this->addLog("Loading language '{$name}'");
216
        } else {
217
            $this->addLog("ERROR :: Language '{$name}' could not be loaded");
218
        }
219
220
        return $ret;
221
    }
222
223
    /**
224
     * Is this the currently active module?
225
     *
226
     * @return bool
227
     */
228
    public function isCurrentModule()
229
    {
230
        if ($GLOBALS['xoopsModule']->getVar('dirname') == $this->dirname) {
231
            return true;
232
        }
233
234
        return false;
235
    }
236
237
    /**
238
     * Does user have admin rights to this module?
239
     *
240
     * @return bool true is user has admin right, else false
241
     */
242 1
    public function isUserAdmin()
243
    {
244 1
        return (isset($GLOBALS['xoopsUser']) && $GLOBALS['xoopsUser'] instanceof \XoopsUser)
245 1
            ? $GLOBALS['xoopsUser']->isAdmin($this->getModule()->getVar('mid')) : false;
246
    }
247
248
    /**
249
     * Return absolute URL for a module relative URL
250
     *
251
     * @param string $url module relative URL
252
     *
253
     * @return string
254
     */
255
    public function url($url = '')
256
    {
257
        return XOOPS_URL . '/modules/' . $this->dirname . '/' . $url;
258
    }
259
260
    /**
261
     * Return absolute filesystem path for a module relative path
262
     *
263
     * @param string $path module relative file system path
264
     *
265
     * @return string
266
     */
267
    public function path($path = '')
268
    {
269
        return XOOPS_ROOT_PATH . '/modules/' . $this->dirname . '/' . $path;
270
    }
271
272
    /**
273
     * Redirect the user to a page within this module
274
     *
275
     * @param string $url     module relative url (i.e. index.php)
276
     * @param int    $time    time in seconds to show redirect message
277
     * @param string $message redirect message
278
     *
279
     * @return void
280
     */
281
    public function redirect($url, $time = 3, $message = '')
282
    {
283
        redirect_header($this->url($url), $time, $message);
0 ignored issues
show
Deprecated Code introduced by
The function redirect_header() has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

283
        /** @scrutinizer ignore-deprecated */ redirect_header($this->url($url), $time, $message);
Loading history...
284
    }
285
}
286