Completed
Push — master ( fe4c2e...b05117 )
by Michael
12s
created

GenericHelper::addLog()   B

Complexity

Conditions 5
Paths 4

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 14
rs 8.8571
ccs 0
cts 8
cp 0
cc 5
eloc 8
nc 4
nop 1
crap 30
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-2016 XOOPS Project (http://xoops.org)
28
 * @license   GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
29
 * @version   Release: 1.0
30
 * @link      http://xoops.org
31
 * @since     1.0
32
 */
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)
66
    {
67
        $this->dirname = $dirname;
68
    }
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;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
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');
0 ignored issues
show
Deprecated Code introduced by
The function xoops_getHandler() has been deprecated.

This function has been deprecated.

Loading history...
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;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
171
        if (isset($xoopsModule) && is_object($xoopsModule)
172
            && $xoopsModule->getVar('dirname') == $this->dirname
173
        ) {
174
            global $xoopsModuleConfig;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
175
            $this->configs = $xoopsModuleConfig;
176
        } else {
177
            /* @var $config_handler XoopsConfigHandler */
178
            $config_handler = xoops_getHandler('config');
0 ignored issues
show
Deprecated Code introduced by
The function xoops_getHandler() has been deprecated.

This function has been deprecated.

Loading history...
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)
237
    {
238
        $this->debug = (bool) $bool;
239
    }
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)
0 ignored issues
show
Coding Style introduced by
addLog uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
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()
0 ignored issues
show
Coding Style introduced by
isCurrentModule uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
269
    {
270
        if ($GLOBALS['xoopsModule']->getVar('dirname') == $this->dirname) {
271
            return true;
272
        }
273
274
        return false;
275
    }
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()
0 ignored issues
show
Coding Style introduced by
isUserAdmin uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
283
    {
284
        return $GLOBALS['xoopsUser']->isAdmin($this->getModule()->getVar('mid'));
285
    }
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 = '')
295
    {
296
        return XOOPS_URL . '/modules/' . $this->dirname . '/' . $url;
297
    }
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 = '')
307
    {
308
        return XOOPS_ROOT_PATH . '/modules/' . $this->dirname . '/' . $path;
309
    }
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 = '')
321
    {
322
        redirect_header($this->url($url), $time, $message);
0 ignored issues
show
Deprecated Code introduced by
The function redirect_header() has been deprecated.

This function has been deprecated.

Loading history...
323
    }
324
}
325