Completed
Pull Request — master (#594)
by Richard
15:59
created

XoopsModule::loadAdminMenu()   A

Complexity

Conditions 4
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 6

Importance

Changes 0
Metric Value
cc 4
eloc 5
nc 2
nop 0
dl 0
loc 7
rs 10
c 0
b 0
f 0
ccs 3
cts 6
cp 0.5
crap 6
1
<?php
2
/**
3
 * XOOPS Kernel Class
4
 *
5
 * You may not change or alter any portion of this comment or credits
6
 * of supporting developers from this source code or any supporting source code
7
 * which is considered copyrighted (c) material of the original comment or credit authors.
8
 * This program is distributed in the hope that it will be useful,
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11
 *
12
 * @copyright       XOOPS Project (http://xoops.org)
13
 * @license         GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
14
 * @package         kernel
15
 * @since           2.0.0
16
 * @version         $Id$
17
 */
18
19
namespace Xoops\Core\Kernel\Handlers;
20
21
use Xoops\Core\XoopsArray;
22
use Xoops\Core\Kernel\Dtype;
23
use Xoops\Core\Kernel\XoopsObject;
24
25
/**
26
 * A Module
27
 *
28
 * @category  Xoops\Core\Kernel\XoopsModule
29
 * @package   Xoops\Core\Kernel
30
 * @author    Kazumi Ono <[email protected]>
31
 * @author    Taiwen Jiang <[email protected]>
32
 * @copyright 2000-2019 XOOPS Project (https://xoops.org)
33
 * @license   GNU GPL 2 or later (https://www.gnu.org/licenses/gpl-2.0.html)
34
 */
35
class XoopsModule extends XoopsObject
36
{
37
    /**
38
     * @var array
39
     */
40
    public $modinfo;
41
42
    /**
43
     * @var array
44
     */
45
    public $adminmenu;
46
    /**
47
    *
48
    * @var array
49
    */
50
    private $internalMessages = array();
51
52
    protected $xoops_url;
53
    protected $xoops_root_path;
54
55
    /**
56
     * Constructor
57
     */
58 58
    public function __construct()
59
    {
60 58
        $this->initVar('mid', Dtype::TYPE_INTEGER, null, false);
61 58
        $this->initVar('name', Dtype::TYPE_TEXT_BOX, null, true, 150);
62 58
        $this->initVar('version', Dtype::TYPE_INTEGER, 100, false);
63 58
        $this->initVar('last_update', Dtype::TYPE_INTEGER, null, false);
64 58
        $this->initVar('weight', Dtype::TYPE_INTEGER, 0, false);
65 58
        $this->initVar('isactive', Dtype::TYPE_INTEGER, 1, false);
66 58
        $this->initVar('dirname', Dtype::TYPE_TEXT_BOX, null, true);
67 58
        $this->initVar('hasmain', Dtype::TYPE_INTEGER, 0, false);
68 58
        $this->initVar('hasadmin', Dtype::TYPE_INTEGER, 0, false);
69 58
        $this->initVar('hassearch', Dtype::TYPE_INTEGER, 0, false);
70 58
        $this->initVar('hasconfig', Dtype::TYPE_INTEGER, 0, false);
71 58
        $this->initVar('hascomments', Dtype::TYPE_INTEGER, 0, false);
72 58
        $this->initVar('hasnotification', Dtype::TYPE_INTEGER, 0, false);
73 58
        $this->initVar('namespace', Dtype::TYPE_TEXT_BOX, null, true);
74 58
        $this->initVar('category', Dtype::TYPE_TEXT_BOX, null, true);
75
76 58
        $this->xoops_url = \XoopsBaseConfig::get('url');
77 58
        $this->xoops_root_path = \XoopsBaseConfig::get('root-path');
78 58
    }
79
80
    /**
81
     * Load module info
82
     *
83
     * @param string  $dirname module directory
84
     * @param boolean $verbose true for more information
85
     *
86
     * @return void
87
     * @todo module 'version' should be semver based -- 1.0.0 should be OK, not an error
88
     */
89 2
    public function loadInfoAsVar($dirname, $verbose = true)
90
    {
91 2
        $dirname = basename($dirname);
92 2
        if (!isset($this->modinfo)) {
93 2
            if (false === $this->loadInfo($dirname, $verbose)) {
94 1
                return;
95
            }
96
        }
97
98 1
        $modInfoArray = new XoopsArray($this->modinfo);
99 1
        $this->setVar('name', $modInfoArray->get('name'));
100
        // see @todo
101 1
        $versionPieces = explode('.', $this->modinfo['version']);
102 1
        if (count($versionPieces) > 2) {
103
            $this->modinfo['version'] = $versionPieces[0].'.'.$versionPieces[1];
104
        }
105 1
        $this->setVar('version', (int)(100 * ($this->modinfo['version'] + 0.001)));
106 1
        $this->setVar('dirname', $this->modinfo['dirname']);
107
108 1
        $this->setVar('hasmain', (bool) $modInfoArray->get('hasmain', false));
109 1
        $this->setVar('hasadmin', (bool) $modInfoArray->get('hasadmin', false));
110 1
        $this->setVar('hassearch', (bool) $modInfoArray->get('hassearch', false));
111 1
        $this->setVar('hasconfig', ($modInfoArray->has('config') && is_array($modInfoArray->get('config'))) || (bool) $modInfoArray->get('hascomments', false));
112 1
        $this->setVar('hascomments', (bool) $modInfoArray->get('hascomments', false));
113 1
        $this->setVar('hasnotification', (bool) $modInfoArray->get('hasnotification', false));
114 1
        $this->setVar('namespace', $modInfoArray->get('namespace'));
115 1
        $this->setVar('category', $modInfoArray->get('category'));
116 1
    }
117
118
    /**
119
     * add a message
120
     *
121
     * @param string $str message to add
122
     *
123
     * @return void
124
     */
125 1
    public function setMessage($str)
126
    {
127 1
        $this->internalMessages[] = trim($str);
128 1
    }
129
130
    /**
131
     * return the messages for this object as an array
132
     *
133
     * @return array an array of messages
134
     */
135 1
    public function getMessages()
136
    {
137 1
        return $this->internalMessages;
138
    }
139
140
    /**
141
     * Set module info
142
     *
143
     * @param string $name  name
144
     * @param mixed  $value value
145
     *
146
     * @return  bool
147
     **/
148 2
    public function setInfo($name, $value)
149
    {
150 2
        if (empty($name)) {
151
            $this->modinfo = $value;
152
        } else {
153 2
            $this->modinfo[$name] = $value;
154
        }
155 2
        return true;
156
    }
157
158
    /**
159
     * Get module info
160
     *
161
     * @param string $name If $name is set, returns a single module information item as string.
162
     *
163
     * @return string[]|string Array of module information, or just the single name requested
164
     */
165 9
    public function getInfo($name = null)
166
    {
167 9
        if (!isset($this->modinfo)) {
168 5
            $this->loadInfo($this->getVar('dirname'));
0 ignored issues
show
Bug introduced by
It seems like $this->getVar('dirname') can also be of type string[]; however, parameter $dirname of Xoops\Core\Kernel\Handlers\XoopsModule::loadInfo() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

168
            $this->loadInfo(/** @scrutinizer ignore-type */ $this->getVar('dirname'));
Loading history...
169
        }
170 9
        if (isset($name)) {
171 8
            if (isset($this->modinfo[$name])) {
172 5
                return $this->modinfo[$name];
173
            }
174 3
            $return = false;
175 3
            return $return;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $return returns the type false which is incompatible with the documented return type string|string[].
Loading history...
176
        }
177 1
        return $this->modinfo;
178
    }
179
180
    /**
181
     * Get a link to the modules main page
182
     *
183
     * @return string|false FALSE on fail
184
     */
185 2
    public function mainLink()
186
    {
187 2
        if ($this->getVar('hasmain') == 1) {
188 1
            $ret = '<a href="' . $this->xoops_url . '/modules/' . $this->getVar('dirname') . '/">'
189 1
                . $this->getVar('name') . '</a>';
190 1
            return $ret;
191
        }
192 1
        return false;
193
    }
194
195
    /**
196
     * Get links to the subpages
197
     *
198
     * @return string
199
     */
200 1
    public function subLink()
201
    {
202 1
        $ret = array();
203 1
        if ($this->getInfo('sub') && is_array($this->getInfo('sub'))) {
204
            foreach ($this->getInfo('sub') as $submenu) {
205
                $ret[] = array(
206
                    'name' => $submenu['name'] ,
207
                    'url' => $submenu['url']);
208
            }
209
        }
210 1
        return $ret;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $ret returns the type array|array<mixed,array> which is incompatible with the documented return type string.
Loading history...
211
    }
212
213
    /**
214
     * Load the admin menu for the module
215
     *
216
     * @return void
217
     */
218 1
    public function loadAdminMenu()
219
    {
220 1
        $file = $this->xoops_root_path . '/modules/' . $this->getInfo('dirname') . '/' . $this->getInfo('adminmenu');
0 ignored issues
show
Bug introduced by
Are you sure $this->getInfo('adminmenu') of type string|string[] can be used in concatenation? ( Ignorable by Annotation )

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

220
        $file = $this->xoops_root_path . '/modules/' . $this->getInfo('dirname') . '/' . /** @scrutinizer ignore-type */ $this->getInfo('adminmenu');
Loading history...
Bug introduced by
Are you sure $this->getInfo('dirname') of type string|string[] can be used in concatenation? ( Ignorable by Annotation )

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

220
        $file = $this->xoops_root_path . '/modules/' . /** @scrutinizer ignore-type */ $this->getInfo('dirname') . '/' . $this->getInfo('adminmenu');
Loading history...
221 1
        if ($this->getInfo('adminmenu') && $this->getInfo('adminmenu') != '' && \XoopsLoad::fileExists($file)) {
222
            $adminmenu = array();
223
            include $file;
224
            $this->adminmenu = $adminmenu;
225
        }
226 1
    }
227
228
    /**
229
     * Get the admin menu for the module
230
     *
231
     * @return string
232
     */
233 1
    public function getAdminMenu()
234
    {
235 1
        if (!isset($this->adminmenu)) {
236 1
            $this->loadAdminMenu();
237
        }
238 1
        return $this->adminmenu;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->adminmenu returns the type array which is incompatible with the documented return type string.
Loading history...
239
    }
240
241
    /**
242
     * Load the module info for this module
243
     *
244
     * @param string $dirname Module directory
245
     * @param bool   $verbose Give an error on fail?
246
     *
247
     * @return bool
248
     *
249
     * @todo the $modVersions array should be built once when modules are installed/updated and then cached
250
     */
251 8
    public function loadInfo($dirname, $verbose = true)
252
    {
253 8
        static $modVersions;
254 8
        if (empty($dirname)) {
255 5
            return false;
256
        }
257 3
        $dirname = basename($dirname);
258 3
        if (isset($modVersions[$dirname])) {
259 1
            $this->modinfo = $modVersions[$dirname];
260 1
            return true;
261
        }
262 2
        $xoops = \Xoops::getInstance();
263 2
        $dirname = basename($dirname);
264 2
        $xoops->loadLanguage('modinfo', $dirname);
265 2
        $xoops->loadLocale($dirname);
266
267 2
        if (!\XoopsLoad::fileExists($file = $xoops->path('modules/' . $dirname . '/xoops_version.php'))) {
268
            if (false != $verbose) {
0 ignored issues
show
Coding Style Best Practice introduced by
It seems like you are loosely comparing two booleans. Considering using the strict comparison !== instead.

When comparing two booleans, it is generally considered safer to use the strict comparison operator.

Loading history...
269
                echo "Module File for $dirname Not Found!";
270
            }
271
            return false;
272
        }
273 2
        $modversion = array();
274 2
        include $file;
275 2
        $modVersions[$dirname] = $modversion;
276 2
        $this->modinfo = $modVersions[$dirname];
277 2
        return true;
278
    }
279
280
    /**
281
     * getter for mid
282
     *
283
     * @param string $format Dtype::FORMAT_xxxx constant
284
     *
285
     * @return mixed
286
     */
287 1
    public function id($format = 'n')
288
    {
289 1
        return $this->getVar('mid', $format);
290
    }
291
292
    /**
293
     * another getter for mid
294
     *
295
     * @param string $format Dtype::FORMAT_xxxx constant
296
     *
297
     * @return mixed
298
     */
299 1
    public function mid($format = '')
300
    {
301 1
        return $this->getVar('mid', $format);
302
    }
303
304
    /**
305
     * getter for module name
306
     *
307
     * @param string $format Dtype::FORMAT_xxxx constant
308
     *
309
     * @return mixed
310
     */
311 2
    public function name($format = '')
312
    {
313 2
        return $this->getVar('name', $format);
314
    }
315
316
    /**
317
     * getter for module version
318
     *
319
     * @param string $format Dtype::FORMAT_xxxx constant
320
     *
321
     * @return mixed
322
     */
323 1
    public function version($format = '')
324
    {
325 1
        return $this->getVar('version', $format);
326
    }
327
328
    /**
329
     * getter for module last update
330
     *
331
     * @param string $format Dtype::FORMAT_xxxx constant
332
     *
333
     * @return mixed
334
     */
335 1
    public function last_update($format = '')
336
    {
337 1
        return $this->getVar('last_update', $format);
338
    }
339
340
    /**
341
     * getter for weight
342
     *
343
     * @param string $format Dtype::FORMAT_xxxx constant
344
     *
345
     * @return mixed
346
     */
347 1
    public function weight($format = '')
348
    {
349 1
        return $this->getVar('weight', $format);
350
    }
351
352
    /**
353
     * getter for isactive
354
     *
355
     * @param string $format Dtype::FORMAT_xxxx constant
356
     *
357
     * @return mixed
358
     */
359 1
    public function isactive($format = '')
360
    {
361 1
        return $this->getVar('isactive', $format);
362
    }
363
364
    /**
365
     * getter for dirname
366
     *
367
     * @param string $format Dtype::FORMAT_xxxx constant
368
     *
369
     * @return mixed
370
     */
371 1
    public function dirname($format = '')
372
    {
373 1
        return $this->getVar('dirname', $format);
374
    }
375
376
    /**
377
     * getter for hasmain
378
     *
379
     * @param string $format Dtype::FORMAT_xxxx constant
380
     *
381
     * @return mixed
382
     */
383 1
    public function hasmain($format = '')
384
    {
385 1
        return $this->getVar('hasmain', $format);
386
    }
387
388
    /**
389
     * getter for hasadmin
390
     *
391
     * @param string $format Dtype::FORMAT_xxxx constant
392
     *
393
     * @return mixed
394
     */
395
    public function hasadmin($format = '')
396
    {
397
        return $this->getVar('hasadmin', $format);
398
    }
399
400
    /**
401
     * getter for hassearch
402
     *
403
     * @param string $format Dtype::FORMAT_xxxx constant
404
     *
405
     * @return mixed
406
     */
407 1
    public function hassearch($format = '')
408
    {
409 1
        return $this->getVar('hassearch', $format);
410
    }
411
412
    /**
413
     * getter for hasconfig
414
     *
415
     * @param string $format Dtype::FORMAT_xxxx constant
416
     *
417
     * @return mixed
418
     */
419 1
    public function hasconfig($format = '')
420
    {
421 1
        return $this->getVar('hasconfig', $format);
422
    }
423
424
    /**
425
     * getter for hascomments
426
     *
427
     * @param string $format Dtype::FORMAT_xxxx constant
428
     *
429
     * @return mixed
430
     */
431 1
    public function hascomments($format = '')
432
    {
433 1
        return $this->getVar('hascomments', $format);
434
    }
435
436
    /**
437
     * getter for hasnotifications
438
     *
439
     * @param string $format Dtype::FORMAT_xxxx constant
440
     *
441
     * @return mixed
442
     */
443 1
    public function hasnotification($format = '')
444
    {
445 1
        return $this->getVar('hasnotification', $format);
446
    }
447
448
    /**
449
     * get module by dirname
450
     *
451
     * @param string $dirname directory name
452
     *
453
     * @return XoopsModule
454
     */
455 1
    public function getByDirname($dirname)
456
    {
457 1
        return \Xoops::getInstance()->getModuleByDirname($dirname);
0 ignored issues
show
Bug Best Practice introduced by
The expression return Xoops::getInstanc...duleByDirname($dirname) also could return the type boolean which is incompatible with the documented return type Xoops\Core\Kernel\Handlers\XoopsModule.
Loading history...
458
    }
459
}
460