Welcome to mirror list, hosted at ThFree Co, Russian Federation.

BaseController.php « controllers « src - github.com/HuasoFoundries/phpPgAdmin6.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 4b1e9ee100fc3adbc82c717ca8ccf5f922f44db1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
<?php

/**
 * PHPPgAdmin 6.1.0
 */

namespace PHPPgAdmin\Controller;

use PHPPgAdmin\XHtml;

/**
 * Base controller class.
 */
class BaseController
{
    use \PHPPgAdmin\Traits\HelperTrait;

    public $appLangFiles = [];

    public $appThemes = [];

    public $appName = '';

    public $appVersion = '';

    public $form = '';

    public $href = '';

    public $lang = [];

    public $action = '';

    public $controller_name;

    /**
     * Used.
     *
     * @var string
     */
    public $view_name;

    /**
     * Used to print the title passing its value to $lang.
     *
     * @var string
     */
    public $controller_title = 'base';

    public $msg = '';

    /**
     * @var \PHPPgAdmin\ViewManager
     */
    public $view;

    /**
     * @var \PHPPgAdmin\Misc
     */
    public $misc;

    public $conf;

    public $phpMinVer;

    /**
     * @var \PHPPgAdmin\ContainerUtils
     */
    protected $container;

    protected $script;

    protected $data;

    protected $database;

    protected $server_id;

    /**
     * @var XHtml\HTMLTableController
     * @psalm-suppress PropertyNotSetInConstructor
     */
    protected $_table_controller;

    /**
     * @var XHtml\HTMLFooterController
     * @psalm-suppress PropertyNotSetInConstructor
     */
    protected $_footer_controller;

    /**
     * @var XHtml\HTMLHeaderController
     * @psalm-suppress PropertyNotSetInConstructor
     */
    protected $_header_controller;

    /**
     * @var XHtml\HTMLNavbarController
     * @psalm-suppress PropertyNotSetInConstructor
     */
    protected $_trail_controller;

    /**
     * @var TreeController
     * @psalm-suppress PropertyNotSetInConstructor
     */
    protected $_tree_controller;

    protected $scripts = '';

    protected $no_db_connection = false;

    /**
     * Constructs the base controller (common for almost all controllers).
     *
     * @param \PHPPgAdmin\ContainerUtils $container        the $app container
     * @param bool                       $no_db_connection [optional] if true, sets  $this->misc->setNoDBConnection(true);
     */
    public function __construct(\PHPPgAdmin\ContainerUtils $container)
    {
        $this->container = $container;
        $this->lang = $container->get('lang');

        $this->controller_name = \str_replace(__NAMESPACE__ . '\\', '', \get_class($this));
        $this->view_name = \str_replace('controller', '', \mb_strtolower($this->controller_name));
        $this->script = $this->view_name;

        $this->view = $container->get('view');

        $this->msg = $container->get('msg');
        $this->appLangFiles = $container->get('appLangFiles');

        $this->misc = $container->get('misc');
        $this->conf = $this->misc->getConf();

        $this->appThemes = $container->get('appThemes');
        $this->action = $container->get('action');

        $this->appName = $container->get('settings')['appName'];
        $this->appVersion = $container->get('settings')['appVersion'];
        $this->postgresqlMinVer = $container->get('settings')['postgresqlMinVer'];
        $this->phpMinVer = $container->get('settings')['phpMinVer'];

        $msg = $container->get('msg');

        if (true === $this->no_db_connection) {
            $this->misc->setNoDBConnection(true);
        }

        if (false === $this->misc->getNoDBConnection()) {
            if (null === $this->misc->getServerId()) {
                $servers_controller = new \PHPPgAdmin\Controller\ServersController($container);

                $servers_controller->render();
            } else {
                $_server_info = $this->misc->getServerInfo();
                // Redirect to the login form if not logged in
                if (!isset($_server_info['username'])) {
                    $msg = \sprintf($this->lang['strlogoutmsg'], $_server_info['desc']);

                    $servers_controller = new \PHPPgAdmin\Controller\ServersController($container);

                    $servers_controller->render();
                }
            }
        }
    }

    /**
     * Default method to render the controller according to the action parameter. It should return with a PSR
     * responseObject but it prints texts whatsoeever.
     *
     * @return string|void
     */
    public function render()
    {
        $this->misc = $this->misc;

        $this->printHeader();
        $this->printBody();

        switch ($this->action) {
            default:
                $this->doDefault();

                break;
        }

        $this->printFooter();
    }

    public function doDefault()
    {
        $html = '<div><h2>Section title</h2> <p>Main content</p></div>';
        echo $html;

        return $html;
    }

    /**
     * Returns the page title for each controller.
     *
     * @param string $title  The title
     * @param string $prefix The prefix
     * @param string $suffix The suffix
     *
     * @return string the page title
     */
    public function headerTitle($title = '', $prefix = '', $suffix = '')
    {
        $title = $title ? $title : $this->controller_title;

        return $prefix . $this->lang[$title] . ($suffix ? ': ' . $suffix : '');
    }

    public function getContainer()
    {
        return $this->container;
    }

    /**
     * Display a table of data.
     *
     * @param \ADORecordSet|\PHPPgAdmin\ArrayRecordSet $tabledata a set of data to be formatted
     * @param array                                    $columns   An associative array of columns to be displayed:
     * @param array                                    $actions   Actions that can be performed on each object:
     * @param string                                   $place     Place where the $actions are displayed. Like 'display-browse',
     * @param string                                   $nodata    (optional) Message to display if data set is empty
     * @param callable                                 $pre_fn    (optional) callback closure for each row
     *
     * @return string the html of the table
     */
    public function printTable(&$tabledata, &$columns, &$actions, $place, $nodata = '', $pre_fn = null)
    {
        $html_table = $this->_getTableController();

        $html_table->initialize($tabledata, $columns, $actions, $place, $nodata, $pre_fn);

        return $html_table->printTable();
    }

    /**
     * Hides or show tree tabs according to their properties.
     *
     * @param array $tabs The tabs
     *
     * @return \PHPPgAdmin\ADORecordSet|\PHPPgAdmin\ArrayRecordSet filtered tabs in the form of an ArrayRecordSet
     */
    public function adjustTabsForTree(&$tabs)
    {
        $tree = $this->_getTreeController();

        return $tree->adjustTabsForTree($tabs);
    }

    /**
     * Produce JSON data for the browser tree.
     *
     * @param \PHPPgAdmin\ADORecordSet|\PHPPgAdmin\ArrayRecordSet $_treedata a set of records to populate the tree
     * @param array                                               $attrs     Attributes for tree items
     * @param string                                              $section   The section where the branch is linked in the tree
     * @param bool                                                $print     either to return or echo the result
     *
     * @return \Slim\Http\Response|string the json rendered tree
     */
    public function printTree(&$_treedata, &$attrs, $section, $print = true)
    {
        $tree = $this->_getTreeController();

        return $tree->printTree($_treedata, $attrs, $section, $print);
    }

    /**
     * Prints a trail.
     *
     * @param array|string $trail
     * @param bool         $do_print The do print
     *
     * @return string ( description_of_the_return_value )
     */
    public function printTrail($trail = [], bool $do_print = true)
    {
        $from = __METHOD__;
        $html_trail = $this->_getNavbarController();

        return $html_trail->printTrail($trail, $do_print, $from);
    }

    /**
     * @param (array|mixed)[][] $navlinks
     * @param string            $place
     * @param array             $env
     * @param mixed             $do_print
     */
    public function printNavLinks(array $navlinks, string $place, array $env = [], $do_print = true)
    {
        $from = __METHOD__;
        $footer_controller = $this->_getFooterController();

        return $footer_controller->printNavLinks($navlinks, $place, $env, $do_print, $from);
    }

    public function printTabs(string $tabs, string $activetab, bool $do_print = true)
    {
        $from = __METHOD__;
        $html_trail = $this->_getNavbarController();

        return $html_trail->printTabs($tabs, $activetab, $do_print, $from);
    }

    /**
     * @param true        $do_print
     * @param null|string $from
     * @param mixed       $link
     */
    public function printLink($link, bool $do_print = true, ?string $from = null)
    {
        if (null === $from) {
            $from = __METHOD__;
        }

        $html_trail = $this->_getNavbarController();

        return $html_trail->printLink($link, $do_print, $from);
    }

    /**
     * @param true $flag
     */
    public function setReloadDropDatabase(bool $flag)
    {
        $footer_controller = $this->_getFooterController();

        return $footer_controller->setReloadDropDatabase($flag);
    }

    /**
     * @param true $flag
     */
    public function setNoBottomLink(bool $flag)
    {
        $footer_controller = $this->_getFooterController();

        return $footer_controller->setNoBottomLink($flag);
    }

    public function printFooter(bool $doBody = true, string $template = 'footer.twig')
    {
        $footer_controller = $this->_getFooterController();

        return $footer_controller->printFooter($doBody, $template);
    }

    public function printReload($database, $do_print = true)
    {
        $footer_controller = $this->_getFooterController();

        return $footer_controller->printReload($database, $do_print);
    }

    /**
     * Outputs JavaScript to set default focus.
     *
     * @param mixed $object eg. forms[0].username
     */
    public function setFocus($object)
    {
        $footer_controller = $this->_getFooterController();

        return $footer_controller->setFocus($object);
    }

    /**
     * Outputs JavaScript to set the name of the browser window.
     *
     * @param string $name      the window name
     * @param bool   $addServer if true (default) then the server id is
     *                          attached to the name
     */
    public function setWindowName($name, $addServer = true)
    {
        $footer_controller = $this->_getFooterController();

        return $footer_controller->setWindowName($name, $addServer);
    }

    /**
     * @param true $flag
     */
    public function setNoOutput(bool $flag)
    {
        $header_controller = $this->_getHeaderController();

        return $header_controller->setNoOutput((bool) $flag);
    }

    /**
     * @param null|string $script
     * @param string      $title
     * @param bool        $do_print
     * @param string      $template
     */
    public function printHeader(string $title = '', ?string $script = null, bool $do_print = true, string $template = 'header.twig')
    {
        $title = $title ? $title : $this->headerTitle();
        $header_controller = $this->_getHeaderController();

        return $header_controller->printHeader($title, $script, $do_print, $template);
    }

    public function printBody(bool $doBody = true, string $bodyClass = 'detailbody', bool $onloadInit = false)
    {
        $header_controller = $this->_getHeaderController();

        return $header_controller->printBody($doBody, $bodyClass, $onloadInit);
    }

    /**
     * @param null|string $help
     * @param string      $title
     * @param bool        $do_print
     */
    public function printTitle(string $title, ?string $help = null, bool $do_print = true)
    {
        $header_controller = $this->_getHeaderController();

        return $header_controller->printTitle($title, $help, $do_print);
    }

    /**
     * @param string      $key
     * @param null|string $default
     */
    public function getRequestParam(string $key, ?string $default = null)
    {
        return \requestInstance()->getParam($key, $default);
    }

    /**
     * @param string                           $key
     * @param null|array|bool|float|int|string $default
     *
     * @return bool| null|array|string|int|float
     */
    public function getPostParam(string $key, $default = null)
    {
        return \requestInstance()->getParsedBodyParam($key, $default);
    }

    /**
     * @param string                      $key
     * @param null|array|float|int|string $default
     *
     * @return null|array|float|int|string
     */
    public function getQueryStrinParam($key, $default = null)
    {
        return \requestInstance()->getQueryParam($key, $default);
    }

    /**
     * @return array
     */
    public function getAllParams(): array
    {
        return \array_merge(
            \requestInstance()->getQueryParams() ?? [],
            \requestInstance()->getParsedBody() ?? []
        );
    }

    /**
     * Print out a message.
     *
     * @param string $msg      The message
     * @param bool   $do_print if true, print the message. Return the string otherwise
     *
     * @return string a paragraph containing the message, whose linebreaks are replaced by <br> elements
     */
    public function printMsg($msg, $do_print = true)
    {
        $html = '';
        $msg = \htmlspecialchars(\PHPPgAdmin\ContainerUtils::br2ln($msg));

        if ('' !== $msg) {
            $html .= '<p class="message">' . \nl2br($msg) . '</p>' . \PHP_EOL;
        }

        if ($do_print) {
            echo $html;

            return $html;
        }

        return $html;
    }

    private function _getTableController()
    {
        if (null === $this->_table_controller) {
            $this->_table_controller = new XHtml\HTMLTableController($this->getContainer(), $this->controller_name);
        }

        return $this->_table_controller;
    }

    private function _getFooterController()
    {
        if (null === $this->_footer_controller) {
            $this->_footer_controller = new XHtml\HTMLFooterController($this->getContainer(), $this->controller_name);
        }

        return $this->_footer_controller;
    }

    private function _getHeaderController()
    {
        if (null === $this->_header_controller) {
            $this->_header_controller = new XHtml\HTMLHeaderController($this->getContainer(), $this->controller_name);
        }

        return $this->_header_controller;
    }

    private function _getNavbarController()
    {
        if (null === $this->_trail_controller) {
            $this->_trail_controller = new XHtml\HTMLNavbarController($this->getContainer(), $this->controller_name);
        }

        return $this->_trail_controller;
    }

    private function _getTreeController()
    {
        if (null === $this->_tree_controller) {
            $this->_tree_controller = new TreeController($this->getContainer(), $this->controller_name);
        }

        return $this->_tree_controller;
    }
}