Completed
Push — xmfsync ( c8fd7a )
by Richard
11:25 queued 02:44
created

Admin::addConfigWarning()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 13

Duplication

Lines 17
Ratio 100 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 17
loc 17
ccs 0
cts 16
cp 0
rs 9.4285
cc 2
eloc 13
nc 2
nop 1
crap 6
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;
13
14
/**
15
 * Xmf\Module\Admin provides helpful methods for module administration
16
 * uses.
17
 *
18
 * Xmf\Module\Admin also provides a method compatible subset of the
19
 * Xoops 2.6 ModuleAdmin class for use in transition from 2.5 to 2.6
20
 *
21
 * @category  Xmf\Module\Admin
22
 * @package   Xmf
23
 * @author    Richard Griffith <[email protected]>
24
 * @copyright 2011-2016 XOOPS Project (http://xoops.org)
25
 * @license   GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
26
 * @version   Release: 1.0
27
 * @link      http://xoops.org
28
 * @since     1.0
29
 */
30
class Admin
31
{
32
33
    /**
34
     * The real ModuleAdmin object
35
     *
36
     * @var object
37
     */
38
    private static $ModuleAdmin = null;
39
    private $version26 = null;
40
    private $lastInfoBoxTitle = null;
41
    private static $paypal = '';
42
43
    /**
44
     * Constructor
45
     */
46
    private function __construct()
47
    {
48
        $this->version26 = self::is26();
49
    }
50
51
    /**
52
     * Retrieve a module admin instance
53
     *
54
     * If we are on 2.6 this will be the a XoopsModuleAdmin instance.
55
     * Older systems with the Frameworks based admin class will get
56
     * an instance of this class which provides compatible methods
57
     * built from the old Frameworks version.
58
     *
59
     * **Always use this to get the ModuleAdmin instance if you use
60
     * anything (even the static methods) of this class.**
61
     *
62
     * @return object a ModuleAdmin instance.
63
     *
64
     * @since  1.0
65
     */
66
    public static function getInstance()
67
    {
68
69
        static $instance;
70
71
        if ($instance === null) {
72
            if (class_exists('\Xoops\Module\Admin', true)) {
73
                $instance  = new \Xoops\Module\Admin;
74
                self::$ModuleAdmin = $instance;
75
            } else {
76
                \Xmf\Loader::loadFile(
77
                    \XoopsBaseConfig::get('root-path') .
78
                    '/Frameworks/moduleclasses/moduleadmin/moduleadmin.php'
79
                );
80
                self::$ModuleAdmin = new \ModuleAdmin;
81
                $instance  = new \Xmf\Module\Admin;
82
            }
83
84
        }
85
86
        return $instance;
87
88
    }
89
90
    /**
91
     * Are we in a 2.6 environment?
92
     *
93
     * just to help with other admin things than ModuleAdmin
94
     *
95
     * not part of 2.6 module admin
96
     *
97
     * @return bool true if we are in a 2.6 environment
98
     */
99
    public static function is26()
100
    {
101
        return class_exists('\Xoops', false);
102
    }
103
104
    /**
105
     * Get an appropriate imagePath for menu.php use.
106
     *
107
     * just to help with other admin things than ModuleAdmin
108
     *
109
     * not part of 2.6 module admin
110
     *
111
     * @param string $image icon name to prepend with path
112
     *
113
     * @return string true if we are in a 2.6 environment
114
     */
115
    public static function menuIconPath($image)
116
    {
117
        if (self::is26()) {
118
            return($image);
119
        } else {
120
            $path='../../Frameworks/moduleclasses/icons/32/';
121
122
            return($path.$image);
123
        }
124
    }
125
126
    /**
127
     * Add config line
128
     *
129
     * @param string $value message to include in config box
130
     * @param string $type  type of line to add
131
     *
132
     * @return bool
133
     */
134
    public function addConfigBoxLine($value = '', $type = 'default')
135
    {
136
        return self::$ModuleAdmin->addConfigBoxLine($value, $type);
137
    }
138
139
    /**
140
     * Add Info box
141
     *
142
     * @param string $title info box title
143
     * @param string $type  for compatibility only
144
     * @param string $extra for compatibility only
145
     *
146
     * @return bool
147
     */
148
    public function addInfoBox($title, $type = 'default', $extra = '')
0 ignored issues
show
Unused Code introduced by
The parameter $type is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $extra is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
149
    {
150
        $this->lastInfoBoxTitle = $title;
151
152
        return self::$ModuleAdmin->addInfoBox($title);
153
    }
154
155
    /**
156
     * Add line to the info box
157
     *
158
     * @param string $text  text to add to info box
159
     * @param string $type  type of infobox line
160
     * @param string $color color for infobox line
161
     *
162
     * @return bool
163
     */
164
    public function addInfoBoxLine($text = '', $type = 'default', $color = 'inherit')
165
    {
166
        return self::$ModuleAdmin->addInfoBoxLine(
167
            $this->lastInfoBoxTitle,
168
            $text,
169
            '',
170
            $color,
171
            $type
172
        );
173
    }
174
175
    /**
176
     * Add Item button
177
     *
178
     * @param string $title title of button
179
     * @param string $link  link for button
180
     * @param string $icon  icon for button
181
     * @param string $extra extra
182
     *
183
     * @return bool
184
     */
185
    public function addItemButton($title, $link, $icon = 'add', $extra = '')
186
    {
187
        return self::$ModuleAdmin->addItemButton($title, $link, $icon, $extra);
188
    }
189
190
    /**
191
     * Render all items buttons
192
     *
193
     * @param string $position  button position (left, right)
194
     * @param string $delimiter delimiter between buttons
195
     *
196
     * @return string
197
     */
198
    public function renderButton($position = null, $delimiter = "&nbsp;")
199
    {
200
        if (null === $position) {
201
            $position = 'right';
202
        }
203
204
        return self::$ModuleAdmin->renderButton($position, $delimiter);
205
    }
206
207
    /**
208
     * Display all item buttons
209
     *
210
     * @param string $position  button position (left, right)
211
     * @param string $delimiter delimiter between buttons
212
     *
213
     * @return void
214
     */
215
    public function displayButton($position = null, $delimiter = "&nbsp;")
216
    {
217
        echo $this->renderButton($position, $delimiter);
218
    }
219
220
    /**
221
     * Render InfoBox
222
     *
223
     * @return string HTML rendered info box
224
     */
225
    public function renderInfoBox()
226
    {
227
        return self::$ModuleAdmin->renderInfoBox();
228
    }
229
230
    /**
231
     * Display InfoBox
232
     *
233
     * @return void
234
     */
235
    public function displayInfoBox()
236
    {
237
        echo $this->renderInfoBox();
238
    }
239
240
    /**
241
     * Render index page for admin
242
     *
243
     * @return string HTML rendered info box
244
     */
245
    public function renderIndex()
246
    {
247
        return self::$ModuleAdmin->renderIndex();
248
    }
249
250
    /**
251
     * Display index page for admin
252
     *
253
     * @return void
254
     */
255
    public function displayIndex()
256
    {
257
        echo $this->renderIndex();
258
    }
259
260
    /**
261
     * Display the navigation menu
262
     *
263
     * @param string $menu menu key (script name, i.e. index.php)
264
     *
265
     * @return void
266
     */
267
    public function displayNavigation($menu = '')
268
    {
269
        echo self::$ModuleAdmin->addNavigation($menu);
270
    }
271
272
    /**
273
     * Render about page
274
     *
275
     * @param bool $logo_xoops display XOOPS logo
276
     *
277
     * @return bool|mixed|string
278
     */
279
    public function renderAbout($logo_xoops = true)
280
    {
281
        return self::$ModuleAdmin->renderAbout(self::$paypal, $logo_xoops);
282
    }
283
284
    /**
285
     * set paypal for 2.5 renderAbout
286
     *
287
     * @param string $paypal PayPal identifier for donate button
288
     *
289
     * @return void
290
     */
291
    public static function setPaypal($paypal = '')
292
    {
293
        self::$paypal = $paypal;
294
    }
295
296
    /**
297
     * Display about page
298
     *
299
     * @param bool $logo_xoops display XOOPS logo
300
     *
301
     * @return void
302
     */
303
    public function displayAbout($logo_xoops = true)
304
    {
305
        echo $this->renderAbout($logo_xoops);
306
    }
307
308
    // not in regular ModuleAdmin
309
310
    /**
311
     * Add error to config box
312
     *
313
     * @param string $value the error message
314
     *
315
     * @return bool
316
     */
317 View Code Duplication
    public static function addConfigError($value = '')
318
    {
319
        if (self::is26()) {
320
            $type='error';
321
        } else {
322
            $path=\XoopsBaseConfig::get('url').'/Frameworks/moduleclasses/icons/16/';
323
            $line = "";
324
            $line .= "<span style='color : red; font-weight : bold;'>";
325
            $line .= "<img src='" . $path . "off.png' >";
326
            $line .= $value;
327
            $line .= "</span>";
328
            $value=$line;
329
            $type = 'default';
330
        }
331
332
        return self::$ModuleAdmin->addConfigBoxLine($value, $type);
333
    }
334
335
    /**
336
     * Add accept (OK) message to config box
337
     *
338
     * @param string $value the OK message
339
     *
340
     * @return bool
341
     */
342 View Code Duplication
    public static function addConfigAccept($value = '')
343
    {
344
        if (self::is26()) {
345
            $type='accept';
346
        } else {
347
            $path=\XoopsBaseConfig::get('url').'/Frameworks/moduleclasses/icons/16/';
348
            $line = "";
349
            $line .= "<span style='color : green;'>";
350
            $line .= "<img src='" . $path . "on.png' >";
351
            $line .= $value;
352
            $line .= "</span>";
353
            $value=$line;
354
            $type = 'default';
355
        }
356
357
        return self::$ModuleAdmin->addConfigBoxLine($value, $type);
358
    }
359
360
    /**
361
     * Add warning to config box
362
     *
363
     * @param string $value the warning message
364
     *
365
     * @return bool
366
     */
367 View Code Duplication
    public static function addConfigWarning($value = '')
368
    {
369
        if (self::is26()) {
370
            $type='warning';
371
        } else {
372
            $path=XOOPS_URL.'/Frameworks/moduleclasses/icons/16/';
373
            $line = "";
374
            $line .= "<span style='color : orange; font-weight : bold;'>";
375
            $line .= "<img src='" . $path . "warning.png' >";
376
            $line .= $value;
377
            $line .= "</span>";
378
            $value=$line;
379
            $type = 'default';
380
        }
381
382
        return self::$ModuleAdmin->addConfigBoxLine($value, $type);
383
    }
384
385
    /**
386
     * Get an appropriate URL for system provided icons.
387
     *
388
     * Things which were in Frameworks in 2.5 are in media in 2.6,
389
     * making it harder to use and rely on the standard icons.
390
     *
391
     * not part of 2.6, just a transition assist
392
     *
393
     * @param string $name the image name to provide URL for, or blank
394
     *                     to just get the URL path.
395
     * @param string $size the icon size (directory). Valid values are
396
     *                     16, 32 or /. A '/' slash will simply set the
397
     *                     path to the icon directory and append $image.
398
     *
399
     * @return string path to icons
400
     */
401
    public static function iconUrl($name = '', $size = '32')
402
    {
403
        switch ($size) {
404
            case '16':
405
                $path='16/';
406
                break;
407
            case '/':
408
                $path='';
409
                break;
410
            default:
411
            case '32':
0 ignored issues
show
Unused Code introduced by
case '32': $path = '32/'; break; does not seem to be reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
412
                $path='32/';
413
                break;
414
        }
415
416
        if (self::is26()) {
417
            $path='/media/xoops/images/icons/'.$path;
418
        } else {
419
            $path='/Frameworks/moduleclasses/icons/'.$path;
420
        }
421
422
        return(\XoopsBaseConfig::get('url') . $path . $name);
423
    }
424
425
    /**
426
     * Check for installed module and version and do addConfigBoxLine()
427
     *
428
     * @param string  $moddir     - module directory name
429
     * @param integer $minversion - minimum acceptable module version (100 = V1.00)
430
     *
431
     * @return bool true if requested version of the module is available
432
     */
433
    public static function checkModuleVersion($moddir, $minversion)
434
    {
435
        \Xmf\Language::load('main', 'xmf');
436
        $return=false;
437
        $helper=\Xmf\Module\Helper::getHelper($moddir);
438
        if (is_object($helper) && is_object($helper->getModule())) {
439
            $mod_modversion=$helper->getModule()->getVar('version');
440
            $mod_version_f = $mod_modversion/100;
441
            $min_version_f = $minversion/100;
442
            $value = sprintf(
443
                _AM_XMF_DEMOMVC_MODULE_VERSION,
444
                strtoupper($moddir),
445
                $min_version_f,
446
                $mod_version_f
447
            );
448
            if ($mod_modversion>=$minversion) {
449
                self::addConfigAccept($value);
450
                $return=true;
451
            } else {
452
                self::addConfigError($value);
453
            }
454
        } else {
455
            $value = sprintf(
456
                _AM_XMF_DEMOMVC_MODULE_NOTFOUND,
457
                strtoupper($moddir),
458
                $minversion/100
459
            );
460
            self::addConfigError($value);
461
        }
462
463
        return $return;
464
    }
465
}
466