Completed
Push — modnamespaces ( 25e254 )
by Richard
08:02
created

XoopsModule::id()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
ccs 2
cts 2
cp 1
crap 1
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 57
    public function __construct()
59
    {
60 57
        $this->initVar('mid', Dtype::TYPE_INTEGER, null, false);
61 57
        $this->initVar('name', Dtype::TYPE_TEXT_BOX, null, true, 150);
62 57
        $this->initVar('version', Dtype::TYPE_INTEGER, 100, false);
63 57
        $this->initVar('last_update', Dtype::TYPE_INTEGER, null, false);
64 57
        $this->initVar('weight', Dtype::TYPE_INTEGER, 0, false);
65 57
        $this->initVar('isactive', Dtype::TYPE_INTEGER, 1, false);
66 57
        $this->initVar('dirname', Dtype::TYPE_TEXT_BOX, null, true);
67 57
        $this->initVar('hasmain', Dtype::TYPE_INTEGER, 0, false);
68 57
        $this->initVar('hasadmin', Dtype::TYPE_INTEGER, 0, false);
69 57
        $this->initVar('hassearch', Dtype::TYPE_INTEGER, 0, false);
70 57
        $this->initVar('hasconfig', Dtype::TYPE_INTEGER, 0, false);
71 57
        $this->initVar('hascomments', Dtype::TYPE_INTEGER, 0, false);
72 57
        $this->initVar('hasnotification', Dtype::TYPE_INTEGER, 0, false);
73 57
        $this->initVar('namespace', Dtype::TYPE_TEXT_BOX, null, true);
74 57
        $this->initVar('category', Dtype::TYPE_TEXT_BOX, null, true);
75
76 57
        $this->xoops_url = \XoopsBaseConfig::get('url');
77 57
        $this->xoops_root_path = \XoopsBaseConfig::get('root-path');
78 57
    }
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 1
    public function loadInfoAsVar($dirname, $verbose = true)
90
    {
91 1
        $dirname = basename($dirname);
92 1
        if (!isset($this->modinfo)) {
93 1
            $this->loadInfo($dirname, $verbose);
94
        }
95
96 1
        $modInfoArray = new XoopsArray($this->modinfo);
97 1
        $this->setVar('name', $modInfoArray->get('name'));
98
        // see @todo
99 1
        $versionPieces = explode('.', $this->modinfo['version']);
100 1
        if (count($versionPieces) > 2) {
101
            $this->modinfo['version'] = $versionPieces[0].'.'.$versionPieces[1];
102
        }
103 1
        $this->setVar('version', (int)(100 * ($this->modinfo['version'] + 0.001)));
104 1
        $this->setVar('dirname', $this->modinfo['dirname']);
105
106 1
        $this->setVar('hasmain', (bool) $modInfoArray->get('hasmain', false));
107 1
        $this->setVar('hasadmin', (bool) $modInfoArray->get('hasadmin', false));
108 1
        $this->setVar('hassearch', (bool) $modInfoArray->get('hassearch', false));
109 1
        $this->setVar('hasconfig', ($modInfoArray->has('config') && is_array($modInfoArray->get('config'))) || (bool) $modInfoArray->get('hascomments', false));
110 1
        $this->setVar('hascomments', (bool) $modInfoArray->get('hascomments', false));
111 1
        $this->setVar('hasnotification', (bool) $modInfoArray->get('hasnotification', false));
112 1
        $this->setVar('namespace', $modInfoArray->get('namespace'));
113 1
        $this->setVar('category', $modInfoArray->get('category'));
114 1
    }
115
116
    /**
117
     * add a message
118
     *
119
     * @param string $str message to add
120
     *
121
     * @return void
122
     */
123 1
    public function setMessage($str)
124
    {
125 1
        $this->internalMessages[] = trim($str);
126 1
    }
127
128
    /**
129
     * return the messages for this object as an array
130
     *
131
     * @return array an array of messages
132
     */
133 1
    public function getMessages()
134
    {
135 1
        return $this->internalMessages;
136
    }
137
138
    /**
139
     * Set module info
140
     *
141
     * @param string $name  name
142
     * @param mixed  $value value
143
     *
144
     * @return  bool
145
     **/
146 2
    public function setInfo($name, $value)
147
    {
148 2
        if (empty($name)) {
149
            $this->modinfo = $value;
150
        } else {
151 2
            $this->modinfo[$name] = $value;
152
        }
153 2
        return true;
154
    }
155
156
    /**
157
     * Get module info
158
     *
159
     * @param string $name If $name is set, returns a single module information item as string.
160
     *
161
     * @return string[]|string Array of module information, or just the single name requested
162
     */
163 9
    public function getInfo($name = null)
164
    {
165 9
        if (!isset($this->modinfo)) {
166 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

166
            $this->loadInfo(/** @scrutinizer ignore-type */ $this->getVar('dirname'));
Loading history...
167
        }
168 9
        if (isset($name)) {
169 8
            if (isset($this->modinfo[$name])) {
170 5
                return $this->modinfo[$name];
171
            }
172 3
            $return = false;
173 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...
174
        }
175 1
        return $this->modinfo;
176
    }
177
178
    /**
179
     * Get a link to the modules main page
180
     *
181
     * @return string|false FALSE on fail
182
     */
183 2
    public function mainLink()
184
    {
185 2
        if ($this->getVar('hasmain') == 1) {
186 1
            $ret = '<a href="' . $this->xoops_url . '/modules/' . $this->getVar('dirname') . '/">'
187 1
                . $this->getVar('name') . '</a>';
188 1
            return $ret;
189
        }
190 1
        return false;
191
    }
192
193
    /**
194
     * Get links to the subpages
195
     *
196
     * @return string
197
     */
198 1
    public function subLink()
199
    {
200 1
        $ret = array();
201 1
        if ($this->getInfo('sub') && is_array($this->getInfo('sub'))) {
202
            foreach ($this->getInfo('sub') as $submenu) {
203
                $ret[] = array(
204
                    'name' => $submenu['name'] ,
205
                    'url' => $submenu['url']);
206
            }
207
        }
208 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...
209
    }
210
211
    /**
212
     * Load the admin menu for the module
213
     *
214
     * @return void
215
     */
216 1
    public function loadAdminMenu()
217
    {
218 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('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

218
        $file = $this->xoops_root_path . '/modules/' . /** @scrutinizer ignore-type */ $this->getInfo('dirname') . '/' . $this->getInfo('adminmenu');
Loading history...
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

218
        $file = $this->xoops_root_path . '/modules/' . $this->getInfo('dirname') . '/' . /** @scrutinizer ignore-type */ $this->getInfo('adminmenu');
Loading history...
219 1
        if ($this->getInfo('adminmenu') && $this->getInfo('adminmenu') != '' && \XoopsLoad::fileExists($file)) {
220
            $adminmenu = array();
221
            include $file;
222
            $this->adminmenu = $adminmenu;
223
        }
224 1
    }
225
226
    /**
227
     * Get the admin menu for the module
228
     *
229
     * @return string
230
     */
231 1
    public function getAdminMenu()
232
    {
233 1
        if (!isset($this->adminmenu)) {
234 1
            $this->loadAdminMenu();
235
        }
236 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...
237
    }
238
239
    /**
240
     * Load the module info for this module
241
     *
242
     * @param string $dirname Module directory
243
     * @param bool   $verbose Give an error on fail?
244
     *
245
     * @return bool
246
     *
247
     * @todo the $modVersions array should be built once when modules are installed/updated and then cached
248
     */
249 7
    public function loadInfo($dirname, $verbose = true)
250
    {
251 7
        static $modVersions;
252 7
        if (empty($dirname)) {
253 4
            return false;
254
        }
255 3
        $dirname = basename($dirname);
256 3
        if (isset($modVersions[$dirname])) {
257 1
            $this->modinfo = $modVersions[$dirname];
258 1
            return true;
259
        }
260 2
        $xoops = \Xoops::getInstance();
261 2
        $dirname = basename($dirname);
262 2
        $xoops->loadLanguage('modinfo', $dirname);
263 2
        $xoops->loadLocale($dirname);
264
265 2
        if (!\XoopsLoad::fileExists($file = $xoops->path('modules/' . $dirname . '/xoops_version.php'))) {
266
            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...
267
                echo "Module File for $dirname Not Found!";
268
            }
269
            return false;
270
        }
271 2
        $modversion = array();
272 2
        include $file;
273 2
        $modVersions[$dirname] = $modversion;
274 2
        $this->modinfo = $modVersions[$dirname];
275 2
        return true;
276
    }
277
278
    /**
279
     * getter for mid
280
     *
281
     * @param string $format Dtype::FORMAT_xxxx constant
282
     *
283
     * @return mixed
284
     */
285 1
    public function id($format = 'n')
286
    {
287 1
        return $this->getVar('mid', $format);
288
    }
289
290
    /**
291
     * another getter for mid
292
     *
293
     * @param string $format Dtype::FORMAT_xxxx constant
294
     *
295
     * @return mixed
296
     */
297 1
    public function mid($format = '')
298
    {
299 1
        return $this->getVar('mid', $format);
300
    }
301
302
    /**
303
     * getter for module name
304
     *
305
     * @param string $format Dtype::FORMAT_xxxx constant
306
     *
307
     * @return mixed
308
     */
309 2
    public function name($format = '')
310
    {
311 2
        return $this->getVar('name', $format);
312
    }
313
314
    /**
315
     * getter for module version
316
     *
317
     * @param string $format Dtype::FORMAT_xxxx constant
318
     *
319
     * @return mixed
320
     */
321 1
    public function version($format = '')
322
    {
323 1
        return $this->getVar('version', $format);
324
    }
325
326
    /**
327
     * getter for module last update
328
     *
329
     * @param string $format Dtype::FORMAT_xxxx constant
330
     *
331
     * @return mixed
332
     */
333 1
    public function last_update($format = '')
334
    {
335 1
        return $this->getVar('last_update', $format);
336
    }
337
338
    /**
339
     * getter for weight
340
     *
341
     * @param string $format Dtype::FORMAT_xxxx constant
342
     *
343
     * @return mixed
344
     */
345 1
    public function weight($format = '')
346
    {
347 1
        return $this->getVar('weight', $format);
348
    }
349
350
    /**
351
     * getter for isactive
352
     *
353
     * @param string $format Dtype::FORMAT_xxxx constant
354
     *
355
     * @return mixed
356
     */
357 1
    public function isactive($format = '')
358
    {
359 1
        return $this->getVar('isactive', $format);
360
    }
361
362
    /**
363
     * getter for dirname
364
     *
365
     * @param string $format Dtype::FORMAT_xxxx constant
366
     *
367
     * @return mixed
368
     */
369 1
    public function dirname($format = '')
370
    {
371 1
        return $this->getVar('dirname', $format);
372
    }
373
374
    /**
375
     * getter for hasmain
376
     *
377
     * @param string $format Dtype::FORMAT_xxxx constant
378
     *
379
     * @return mixed
380
     */
381 1
    public function hasmain($format = '')
382
    {
383 1
        return $this->getVar('hasmain', $format);
384
    }
385
386
    /**
387
     * getter for hasadmin
388
     *
389
     * @param string $format Dtype::FORMAT_xxxx constant
390
     *
391
     * @return mixed
392
     */
393
    public function hasadmin($format = '')
394
    {
395
        return $this->getVar('hasadmin', $format);
396
    }
397
398
    /**
399
     * getter for hassearch
400
     *
401
     * @param string $format Dtype::FORMAT_xxxx constant
402
     *
403
     * @return mixed
404
     */
405 1
    public function hassearch($format = '')
406
    {
407 1
        return $this->getVar('hassearch', $format);
408
    }
409
410
    /**
411
     * getter for hasconfig
412
     *
413
     * @param string $format Dtype::FORMAT_xxxx constant
414
     *
415
     * @return mixed
416
     */
417 1
    public function hasconfig($format = '')
418
    {
419 1
        return $this->getVar('hasconfig', $format);
420
    }
421
422
    /**
423
     * getter for hascomments
424
     *
425
     * @param string $format Dtype::FORMAT_xxxx constant
426
     *
427
     * @return mixed
428
     */
429 1
    public function hascomments($format = '')
430
    {
431 1
        return $this->getVar('hascomments', $format);
432
    }
433
434
    /**
435
     * getter for hasnotifications
436
     *
437
     * @param string $format Dtype::FORMAT_xxxx constant
438
     *
439
     * @return mixed
440
     */
441 1
    public function hasnotification($format = '')
442
    {
443 1
        return $this->getVar('hasnotification', $format);
444
    }
445
446
    /**
447
     * get module by dirname
448
     *
449
     * @param string $dirname directory name
450
     *
451
     * @return XoopsModule
452
     */
453 1
    public function getByDirname($dirname)
454
    {
455 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...
456
    }
457
}
458