Test Failed
Push — main ( 8846ad...255e1c )
by Rafael
05:35
created

View::getMenu()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 235
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 7
nc 1
nop 0
dl 0
loc 235
rs 10
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Alxarafe. Development of PHP applications in a flash!
4
 * Copyright (C) 2018 Alxarafe <[email protected]>
5
 */
6
7
namespace Alxarafe\Core\Base;
8
9
use Alxarafe\Core\Helpers\Globals;
10
use Alxarafe\Core\Singletons\Config;
11
use Alxarafe\Core\Singletons\Debug;
12
use Alxarafe\Core\Singletons\FlashMessages;
13
use Alxarafe\Core\Singletons\Render;
14
use Alxarafe\Core\Singletons\Translator;
15
use Alxarafe\Core\Utils\ClassUtils;
16
use Alxarafe\Models\Menu;
17
18
/**
19
 * Class View
20
 *
21
 * @package Alxarafe\Base
22
 */
23
abstract class View
24
{
25
    /**
26
     * Error messages to show
27
     *
28
     * @var string[]
29
     */
30
    public array $errors;
31
32
    /**
33
     * Title of the HTML page
34
     *
35
     * @var string
36
     */
37
    public string $title;
38
39
    /**
40
     * Contiene el nombre de la plantilla a utilizar.
41
     *
42
     * @var ?string
43
     */
44
    public ?string $template = null;
45
46
    /**
47
     * Contains an array with the main menu options. (top menu)
48
     *
49
     * @var array
50
     */
51
    public array $menu;
52
53
    /**
54
     * Contains an array with the submenu options. (left menu)
55
     *
56
     * @var array
57
     */
58
    public array $submenu;
59
60
    public bool $hasMenu = false;
61
62
    /**
63
     * Array that contains the variables that will be passed to the template.
64
     * Among others it will contain the user name, the view and the controller.
65
     *
66
     * @var array
67
     */
68
    private array $vars;
69
70
    /**
71
     * Load the JS and CSS files and define the ctrl, view and user variables
72
     * for the templates.
73
     *
74
     * @param Controller $controller
75
     *
76
     * @throws DebugBarException
77
     */
78
    public function __construct(BasicController $controller)
79
    {
80
        $title = ClassUtils::getShortName($controller, $controller);
81
        $this->title = Translator::trans(strtolower($title)) . ' - ' . Globals::APP_NAME . ' ' . Globals::APP_VERSION;
82
83
        $this->setTemplate();
84
        Render::setTemplate($this->template);
85
        $this->vars = [];
86
        $this->vars['ctrl'] = $controller;
87
        $this->vars['view'] = $this;
88
        $this->vars['user'] = Config::getUsername();
89
        $this->vars['templateuri'] = Render::getTemplatesUri();
90
        $this->addCSS();
91
        $this->addJS();
92
        $this->hasMenu = $controller->hasMenu;
93
        if ($this->hasMenu) {
94
            $this->getMenu();
95
            $this->getSubmenu();
96
        }
97
    }
98
99
    /**
100
     * Method to assign the template to the view.
101
     */
102
    abstract public function setTemplate(): void;
103
104
    /**
105
     * addCSS includes the common CSS files to all views templates. Also defines CSS folders templates.
106
     *
107
     * @return void
108
     * @throws DebugBarException
109
     */
110
    public function addCSS()
111
    {
112
        //        $this->addToVar('cssCode', $this->addResource('/bower_modules/bootstrap/dist/css/bootstrap.min', 'css'));
113
        //        $this->addToVar('cssCode', $this->addResource('/css/alxarafe', 'css'));
114
    }
115
116
    /**
117
     * addJS includes the common JS files to all views templates. Also defines JS folders templates.
118
     *
119
     * @return void
120
     * @throws DebugBarException
121
     */
122
    public function addJS()
123
    {
124
        //        $this->addToVar('jsCode', $this->addResource('/bower_modules/jquery/dist/jquery.min', 'js'));
125
        //        $this->addToVar('jsCode', $this->addResource('/bower_modules/bootstrap/dist/js/bootstrap.min', 'js'));
126
        //        $this->addToVar('jsCode', $this->addResource('/js/alxarafe', 'js'));
127
    }
128
129
    /**
130
     * The menu options in Dolibarr are defined in eldy.lib.php in the print_eldy_menu function.
131
     * In the case of using the Auguria template, change eldy to auguria.
132
     *
133
     * TODO: The options not allowed for the user should be disabled
134
     * TODO: Soon, this information will be in a template yaml file
135
     *
136
     * @author  Rafael San José Tovar <[email protected]>
137
     * @version sept. 2021
138
     *
139
     */
140
    private function getMenu()
141
    {
142
        /**
143
         * Array
144
         * (
145
         * [0] => Array (
146
         *   [name] => Home
147
         *   [link] => /index.php?mainmenu=home&leftmenu=home
148
         *   [title] => Home
149
         *   [level] => 0
150
         *   [enabled] => 1
151
         *   [target] =>
152
         *   [mainmenu] => home
153
         *   [leftmenu] =>
154
         *   [position] => 10
155
         *   [id] => mainmenu
156
         *   [idsel] => home
157
         *   [classname] => class="tmenusel"
158
         *   [prefix] => fa fa-home
159
         * )
160
         *
161
         * [1] => Array (
162
         *   [name] => Members
163
         *   [link] => /adherents/index.php?mainmenu=members&leftmenu=
164
         *   [title] => MenuMembers
165
         *   [level] => 0
166
         *   [enabled] => 0
167
         *   [target] =>
168
         *   [mainmenu] => members
169
         *   [leftmenu] =>
170
         *   [position] => 18
171
         *   [id] => mainmenu
172
         *   [idsel] => members
173
         *   [classname] => class="tmenu"
174
         *   [prefix] => fas fa-user-alt
175
         * )
176
         *
177
         * [2] => Array (
178
         *   [name] => Companies
179
         *   [link] => /societe/index.php?mainmenu=companies&leftmenu=
180
         *   [title] => ThirdParties
181
         *   [level] => 0
182
         *   [enabled] => 1
183
         *   [target] =>
184
         *   [mainmenu] => companies
185
         *   [leftmenu] =>
186
         *   [position] => 20
187
         *   [id] => mainmenu
188
         *   [idsel] => companies
189
         *   [classname] => class="tmenu"
190
         *   [prefix] => fas fa-building
191
         *   [session] => 1
192
         * )
193
         *
194
         * [3] => Array (
195
         *   [name] => Products
196
         *   [link] => /product/index.php?mainmenu=products&leftmenu=
197
         *   [title] => Array (
198
         *     [0] => TMenuProducts
199
         *     [1] =>  |
200
         *     [2] => TMenuServices
201
         *   )
202
         *   [level] => 0
203
         *   [enabled] => 1
204
         *   [target] =>
205
         *   [mainmenu] => products
206
         *   [leftmenu] =>
207
         *   [position] => 30
208
         *   [id] => mainmenu
209
         *   [idsel] => products
210
         *   [classname] => class="tmenu"
211
         *   [prefix] => fas fa-cube
212
         *   [session] => 1
213
         * )
214
         *
215
         * [4] => Array (
216
         *   [name] => TMenuMRP
217
         *   [link] => /mrp/index.php?mainmenu=mrp&leftmenu=
218
         *   [title] => TMenuMRP
219
         *   [level] => 0
220
         *   [enabled] => 1
221
         *   [target] =>
222
         *   [mainmenu] => mrp
223
         *   [leftmenu] =>
224
         *   [position] => 31
225
         *   [id] => mainmenu
226
         *   [idsel] => mrp
227
         *   [classname] => class="tmenu"
228
         *   [prefix] => fas fa-cubes
229
         *   [session] => 1
230
         * )
231
         *
232
         * [5] => Array (
233
         *   [name] => Projet
234
         *   [link] => /projet/index.php?mainmenu=project&leftmenu=
235
         *   [title] => Projects
236
         *   [level] => 0
237
         *   [enabled] => 1
238
         *   [target] =>
239
         *   [mainmenu] => project
240
         *   [leftmenu] =>
241
         *   [position] => 35
242
         *   [id] => mainmenu
243
         *   [idsel] => project
244
         *   [classname] => class="tmenu"
245
         *   [prefix] => fas fa-project-diagram
246
         *   [session] => 1
247
         * )
248
         *
249
         * [6] => Array (
250
         *   [name] => Commercial
251
         *   [link] => /comm/index.php?mainmenu=commercial&leftmenu=
252
         *   [title] => Commercial
253
         *   [level] => 0
254
         *   [enabled] => 1
255
         *   [target] =>
256
         *   [mainmenu] => commercial
257
         *   [leftmenu] =>
258
         *   [position] => 40
259
         *   [id] => mainmenu
260
         *   [idsel] => commercial
261
         *   [classname] => class="tmenu"
262
         *   [prefix] => fas fa-suitcase
263
         *   [session] => 1
264
         * )
265
         *
266
         * [7] => Array (
267
         *   [name] => Compta
268
         *   [link] => /compta/index.php?mainmenu=billing&leftmenu=
269
         *   [title] => MenuFinancial
270
         *   [level] => 0
271
         *   [enabled] => 1
272
         *   [target] =>
273
         *   [mainmenu] => billing
274
         *   [leftmenu] =>
275
         *   [position] => 50
276
         *   [id] => mainmenu
277
         *   [idsel] => billing
278
         *   [classname] => class="tmenu"
279
         *   [prefix] => fas fa-file-invoice-dollar
280
         *   [session] => 1
281
         * )
282
         *
283
         * [8] => Array (
284
         *   [name] => Bank
285
         *   [link] => /compta/bank/list.php?mainmenu=bank&leftmenu=
286
         *   [title] => MenuBankCash
287
         *   [level] => 0
288
         *   [enabled] => 0
289
         *   [target] =>
290
         *   [mainmenu] => bank
291
         *   [leftmenu] =>
292
         *   [position] => 52
293
         *   [id] => mainmenu
294
         *   [idsel] => bank
295
         *   [classname] => class="tmenu"
296
         *   [prefix] => fas fa-university
297
         *   [session] => 1
298
         * )
299
         *
300
         * [9] => Array (
301
         *   [name] => Accounting
302
         *   [link] => /accountancy/index.php?mainmenu=accountancy&leftmenu=
303
         *   [title] => MenuAccountancy
304
         *   [level] => 0
305
         *   [enabled] => 0
306
         *   [target] =>
307
         *   [mainmenu] => accountancy
308
         *   [leftmenu] =>
309
         *   [position] => 54
310
         *   [id] => mainmenu
311
         *   [idsel] => accountancy
312
         *   [classname] => class="tmenu"
313
         *   [prefix] => fas fa-search-dollar
314
         *   [session] => 1
315
         * )
316
         *
317
         * [10] => Array (
318
         *   [name] => HRM
319
         *   [link] => /hrm/index.php?mainmenu=hrm&leftmenu=
320
         *   [title] => HRM
321
         *   [level] => 0
322
         *   [enabled] => 0
323
         *   [target] =>
324
         *   [mainmenu] => hrm
325
         *   [leftmenu] =>
326
         *   [position] => 80
327
         *   [id] => mainmenu
328
         *   [idsel] => hrm
329
         *   [classname] => class="tmenu"
330
         *   [prefix] => fas fa-user-tie
331
         *   [session] => 1
332
         * )
333
         *
334
         * [11] => Array (
335
         *   [name] => Ticket
336
         *   [link] => /ticket/index.php?mainmenu=ticket&leftmenu=
337
         *   [title] => Tickets
338
         *   [level] => 0
339
         *   [enabled] => 1
340
         *   [target] =>
341
         *   [mainmenu] => ticket
342
         *   [leftmenu] =>
343
         *   [position] => 88
344
         *   [id] => mainmenu
345
         *   [idsel] => ticket
346
         *   [classname] => class="tmenu"
347
         *   [prefix] => fas fa-ticket-alt
348
         *   [session] => 1
349
         * )
350
         *
351
         * [12] => Array (
352
         *   [name] => Tools
353
         *   [link] => /core/tools.php?mainmenu=tools&leftmenu=
354
         *   [title] => Tools
355
         *   [level] => 0
356
         *   [enabled] => 1
357
         *   [target] =>
358
         *   [mainmenu] => tools
359
         *   [leftmenu] =>
360
         *   [position] => 90
361
         *   [id] => mainmenu
362
         *   [idsel] => tools
363
         *   [classname] => class="tmenu"
364
         *   [prefix] => fas fa-tools
365
         *   [session] => 1
366
         * )
367
         */
368
        $module = strtolower(filter_input(INPUT_GET, 'module'));
369
        $this->menu = [];
370
        $this->menu[] = $this->addItem(
371
            'portfolio',
372
            Translator::trans('portfolio'),
373
            '?module=Portfolio&controller=Index',
374
            $module === 'portfolio'
375
        );
376
    }
377
378
    private function addItem($id, $title, $href, $active = false)
379
    {
380
        return [
381
            'id' => $id,
382
            'title' => $title,
383
            'href' => $href,
384
            'active' => $active,
385
        ];
386
    }
387
388
    /**
389
     * The left menu options in Dolibarr are defined in eldy.lib.php in the print_left_eldy_menu function.
390
     * In the case of using the Auguria template, change eldy to auguria.
391
     *
392
     * TODO: The options not allowed for the user should be disabled
393
     * TODO: The initial implementation is very basic. Needs improvements.
394
     *
395
     * @author  Rafael San José Tovar <[email protected]>
396
     * @version sept. 2021
397
     *
398
     */
399
    private function getSubmenu()
400
    {
401
        $module = strtolower(filter_input(INPUT_GET, 'module'));
402
        $menu = new Menu();
403
        $this->submenu = $menu->getSubmenu($module);
404
    }
405
406
    /**
407
     * Add a new element to a value saved in the array that is passed to the
408
     * template.
409
     * It is used when what we are saving is an array and we want to add a
410
     * new element to that array.
411
     *
412
     * @param $name
413
     * @param $value
414
     *
415
     * @return void
416
     */
417
    public function addToVar(string $name, string $value)
418
    {
419
        $this->vars[$name][] = $value;
420
    }
421
422
    /**
423
     * Check if the resource is in the application's resource folder (for example, in the css or js folders
424
     * of the skin folder). It's a specific file.
425
     *
426
     * If it can not be found, check if it is in the templates folder (for example in the css or
427
     * js folders of the templates folder). It's a common file.
428
     *
429
     * If it is not in either of the two, no route is specified (it will surely give loading error).
430
     *
431
     * @param string  $resourceName      , is the name of the file (without extension)
432
     * @param string  $resourceExtension , is the extension (type) of the resource (js or css)
433
     * @param boolean $relative          , set to false for use an absolute path.
434
     *
435
     * @return string the complete path of resource.
436
     * @throws DebugBarException
437
     */
438
    public function addResource(string $resourceName, string $resourceExtension = 'css', $relative = true): string
439
    {
440
        $path = $resourceName . '.' . $resourceExtension;
441
        if ($relative) {
442
            if (file_exists(BASE_FOLDER . '/vendor/almasaeed2010/adminlte/' . $path)) {
443
                return BASE_URI . '/vendor/almasaeed2010/adminlte/' . $path;
444
            }
445
            if (file_exists(Render::getTemplatesFolder() . $path)) {
446
                return Render::getTemplatesUri() . $path;
447
            }
448
            if (file_exists(Render::getCommonTemplatesFolder() . $path)) {
449
                return Render::getCommonTemplatesUri() . $path;
0 ignored issues
show
Bug Best Practice introduced by
The method Alxarafe\Core\Singletons...getCommonTemplatesUri() is not static, but was called statically. ( Ignorable by Annotation )

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

449
                return Render::/** @scrutinizer ignore-call */ getCommonTemplatesUri() . $path;
Loading history...
450
            }
451
            if (file_exists(BASE_FOLDER . $path)) {
452
                return BASE_URI . $path;
453
            }
454
            Debug::addMessage('messages', "Relative resource '$path' not found!");
455
        }
456
        if (!file_exists($path)) {
457
            Debug::addMessage('messages', "Absolute resource '$path' not found!");
458
        }
459
        return $path;
460
    }
461
462
    /**
463
     * Finally render the result.
464
     *
465
     * @throws DebugBarException
466
     */
467
    public function __destruct()
468
    {
469
        if (!Render::hasTemplate()) {
470
            Render::setTemplate('default');
471
        }
472
        echo Render::render($this->vars);
473
    }
474
475
    /**
476
     * Saves a value in the array that is passed to the template.
477
     *
478
     * @param string $name
479
     * @param string $value
480
     *
481
     * @return void
482
     */
483
    public function setVar(string $name, string $value)
484
    {
485
        $this->vars[$name] = $value;
486
    }
487
488
    /**
489
     * Returns a previously saved value in the array that is passed to the
490
     * template.
491
     *
492
     * @param $name
493
     *
494
     * @return array|string|boolean|null
495
     */
496
    public function getVar(string $name)
497
    {
498
        return isset($this->vars[$name]) ?? [];
499
    }
500
501
    /**
502
     * Returns the necessary html code in the header of the template, to
503
     * display the debug bar.
504
     */
505
    public function getHeader(): string
506
    {
507
        return Debug::getRenderHeader();
508
    }
509
510
    /**
511
     * Returns the necessary html code at the footer of the template, to
512
     * display the debug bar.
513
     */
514
    public function getFooter(): string
515
    {
516
        return Debug::getRenderFooter();
517
    }
518
519
    /**
520
     * Obtains an array with all errors messages in the stack
521
     *
522
     * @return array
523
     */
524
    public function getErrors(): array
525
    {
526
        return FlashMessages::getContainer();
527
    }
528
}
529