Test Failed
Push — main ( 71ef7e...ad999b )
by Rafael
05:44
created

View::trans()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

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

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