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: 3a5f792c7ce1755ff9688dcf6f3ebaa43c3ee040 (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
<?php

/**
 * PHPPgAdmin v6.0.0-RC8
 */

namespace PHPPgAdmin\Controller;

ini_set('display_errors', 1);
/**
 * Base controller class.
 *
 * @package PHPPgAdmin
 */
class BaseController
{
    use \PHPPgAdmin\Traits\HelperTrait;

    protected $container;
    protected $app;
    protected $data;
    protected $database;
    protected $server_id;
    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';
    protected $table_controller;
    protected $trail_controller;
    protected $tree_controller;
    protected $footer_controller;
    protected $header_controller;
    protected $scripts = '';
    public $msg        = '';
    public $view;
    public $plugin_manager;
    public $misc;
    public $conf;
    public $phpMinVer;
    protected $no_db_connection = false;

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

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

        $this->view           = $container->get('view');
        $this->plugin_manager = $container->get('plugin_manager');
        $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);

                return $servers_controller->render();
            }
            $_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);

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

    /**
     * Default method to render the controller according to the action parameter.
     */
    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;
    }

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

        return $this->table_controller;
    }

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

        return $this->footer_controller;
    }

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

        return $this->header_controller;
    }

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

        return $this->trail_controller;
    }

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

        return $this->tree_controller;
    }

    /**
     * Display a table of data.
     *
     * @param \PHPPgAdmin\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();
    }

    public function adjustTabsForTree($tabs)
    {
        $tree = $this->_getTreeController();

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

    /**
     * Produce JSON data for the browser tree.
     *
     * @param \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);
    }

    public function printTrail($trail = [], $do_print = true)
    {
        $from       = __METHOD__;
        $html_trail = $this->_getNavbarController();

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

    public function printNavLinks($navlinks, $place, $env = [], $do_print = true)
    {
        $from              = __METHOD__;
        $footer_controller = $this->_getFooterController();

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

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

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

    public function printLink($link, $do_print = true, $from = null)
    {
        if (null === $from) {
            $from = __METHOD__;
        }

        $html_trail = $this->_getNavbarController();

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

    public function setReloadDropDatabase($flag)
    {
        $footer_controller = $this->_getFooterController();

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

    public function setNoBottomLink($flag)
    {
        $footer_controller = $this->_getFooterController();

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

    public function printFooter($doBody = true, $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);
    }

    public function setNoOutput($flag)
    {
        $header_controller = $this->_getHeaderController();

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

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

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

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

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

    public function printTitle($title, $help = null, $do_print = true)
    {
        $header_controller = $this->_getHeaderController();

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

    public function getRequestParam($key, $default = null)
    {
        return $this->container->requestobj->getParam($key, $default);
    }

    public function getPostParam($key, $default = null)
    {
        return $this->container->requestobj->getParsedBodyParam($key, $default);
    }

    public function getQueryParam($key, $default = null)
    {
        return $this->container->requestobj->getQueryParam($key, $default);
    }

    /**
     * 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\Traits\HelperTrait::br2ln($msg));
        if ('' != $msg) {
            $html .= '<p class="message">'.nl2br($msg).'</p>'.PHP_EOL;
        }
        if ($do_print) {
            echo $html;

            return $html;
        }

        return $html;
    }
}