Passed
Pull Request — develop (#184)
by Felipe
06:07 queued 31s
created

BaseController::getLastTabURL()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * PHPPgAdmin v6.0.0-beta.45
5
 */
6
7
namespace PHPPgAdmin\Controller;
8
9
ini_set('display_errors', 1);
10
/**
11
 * Base controller class.
12
 *
13
 * @package PHPPgAdmin
14
 */
15
class BaseController
16
{
17
    use \PHPPgAdmin\Traits\HelperTrait;
18
19
    protected $container;
20
    protected $app;
21
    protected $data;
22
    protected $database;
23
    protected $server_id;
24
    public $appLangFiles = [];
25
    public $appThemes    = [];
26
    public $appName      = '';
27
    public $appVersion   = '';
28
    public $form         = '';
29
    public $href         = '';
30
    public $lang         = [];
31
    public $action       = '';
32
    public $controller_name;
33
34
    /**
35
     * Used.
36
     *
37
     * @var string
38
     */
39
    public $view_name;
40
    /**
41
     * Used to print the title passing its value to $lang.
42
     *
43
     * @var string
44
     */
45
    public $controller_title = 'base';
46
    protected $table_controller;
47
    protected $trail_controller;
48
    protected $tree_controller;
49
    protected $footer_controller;
50
    protected $header_controller;
51
    protected $scripts = '';
52
    public $msg        = '';
53
    public $view;
54
    public $plugin_manager;
55
    public $misc;
56
    public $conf;
57
    public $phpMinVer;
58
    protected $no_db_connection = false;
59
60
    /**
61
     * Constructs the base controller (common for almost all controllers).
62
     *
63
     * @param \Slim\Container $container        the $app container
64
     * @param bool            $no_db_connection [optional] if true, sets  $this->misc->setNoDBConnection(true);
65
     */
66
    public function __construct(\Slim\Container $container)
67
    {
68
        $this->container = $container;
69
        $this->lang      = $container->get('lang');
70
71
        $this->controller_name = str_replace(__NAMESPACE__.'\\', '', get_class($this));
72
        $this->view_name       = str_replace('controller', '', strtolower($this->controller_name));
73
        $this->script          = $this->view_name;
74
75
        $this->view           = $container->get('view');
76
        $this->plugin_manager = $container->get('plugin_manager');
77
        $this->msg            = $container->get('msg');
78
        $this->appLangFiles   = $container->get('appLangFiles');
79
80
        $this->misc = $container->get('misc');
81
        $this->conf = $this->misc->getConf();
82
83
        $this->appThemes = $container->get('appThemes');
84
        $this->action    = $container->get('action');
85
86
        $this->appName          = $container->get('settings')['appName'];
87
        $this->appVersion       = $container->get('settings')['appVersion'];
88
        $this->postgresqlMinVer = $container->get('settings')['postgresqlMinVer'];
89
        $this->phpMinVer        = $container->get('settings')['phpMinVer'];
90
91
        $msg = $container->get('msg');
92
93
        if (true === $this->no_db_connection) {
94
            $this->misc->setNoDBConnection(true);
95
        }
96
97
        if (false === $this->misc->getNoDBConnection()) {
98
            if (null === $this->misc->getServerId()) {
99
                $servers_controller = new \PHPPgAdmin\Controller\ServersController($container);
100
101
                return $servers_controller->render();
102
            }
103
            $_server_info = $this->misc->getServerInfo();
104
            // Redirect to the login form if not logged in
105
            if (!isset($_server_info['username'])) {
106
                $msg = sprintf($this->lang['strlogoutmsg'], $_server_info['desc']);
107
108
                $servers_controller = new \PHPPgAdmin\Controller\ServersController($container);
109
110
                return $servers_controller->render();
111
            }
112
        }
113
114
        //\PC::debug(['name' => $this->controller_name, 'no_db_connection' => $this->misc->getNoDBConnection()], 'instanced controller');
115
    }
116
117
    /**
118
     * Default method to render the controller according to the action parameter.
119
     */
120
    public function render()
121
    {
122
        $this->misc = $this->misc;
123
124
        $this->printHeader();
125
        $this->printBody();
126
127
        switch ($this->action) {
128
            default:
129
                $this->doDefault();
130
131
                break;
132
        }
133
134
        $this->printFooter();
135
    }
136
137
    public function doDefault()
138
    {
139
        $html = '<div><h2>Section title</h2> <p>Main content</p></div>';
140
        echo $html;
141
142
        return $html;
143
    }
144
145
    /**
146
     * Returns the page title for each controller.
147
     *
148
     * @param string $title  The title
149
     * @param string $prefix The prefix
150
     * @param string $suffix The suffix
151
     *
152
     * @return string the page title
153
     */
154
    public function headerTitle($title = '', $prefix = '', $suffix = '')
155
    {
156
        $title = $title ? $title : $this->controller_title;
157
158
        return $prefix.$this->lang[$title].($suffix ? ': '.$suffix : '');
159
    }
160
161
    public function getContainer()
162
    {
163
        return $this->container;
164
    }
165
166
    private function _getTableController()
167
    {
168
        if (null === $this->table_controller) {
169
            $this->table_controller = new \PHPPgAdmin\XHtml\HTMLTableController($this->getContainer(), $this->controller_name);
170
        }
171
172
        return $this->table_controller;
173
    }
174
175
    private function _getFooterController()
176
    {
177
        if (null === $this->footer_controller) {
178
            $this->footer_controller = new \PHPPgAdmin\XHtml\HTMLFooterController($this->getContainer(), $this->controller_name);
179
        }
180
181
        return $this->footer_controller;
182
    }
183
184
    private function _getHeaderController()
185
    {
186
        if (null === $this->header_controller) {
187
            $this->header_controller = new \PHPPgAdmin\XHtml\HTMLHeaderController($this->getContainer(), $this->controller_name);
188
        }
189
190
        return $this->header_controller;
191
    }
192
193
    private function _getNavbarController()
194
    {
195
        if (null === $this->trail_controller) {
196
            $this->trail_controller = new \PHPPgAdmin\XHtml\HTMLNavbarController($this->getContainer(), $this->controller_name);
197
        }
198
199
        return $this->trail_controller;
200
    }
201
202
    private function _getTreeController()
203
    {
204
        if (null === $this->tree_controller) {
205
            $this->tree_controller = new \PHPPgAdmin\XHtml\TreeController($this->getContainer(), $this->controller_name);
206
        }
207
208
        return $this->tree_controller;
209
    }
210
211
    /**
212
     * Display a table of data.
213
     *
214
     * @param \PHPPgAdmin\ADORecordSet|\PHPPgAdmin\ArrayRecordSet $tabledata a set of data to be formatted
215
     * @param array                                               $columns   An associative array of columns to be displayed:
216
     * @param array                                               $actions   Actions that can be performed on each object:
217
     * @param string                                              $place     Place where the $actions are displayed. Like 'display-browse',
218
     * @param string                                              $nodata    (optional) Message to display if data set is empty
219
     * @param callable                                            $pre_fn    (optional) callback closure for each row
220
     *
221
     * @return string the html of the table
222
     */
223
    public function printTable(&$tabledata, &$columns, &$actions, $place, $nodata = null, $pre_fn = null)
224
    {
225
        $html_table = $this->_getTableController();
226
227
        return $html_table->printTable($tabledata, $columns, $actions, $place, $nodata, $pre_fn);
228
    }
229
230
    public function adjustTabsForTree($tabs)
231
    {
232
        $tree = $this->_getTreeController();
233
234
        return $tree->adjustTabsForTree($tabs);
235
    }
236
237
    /**
238
     * Produce JSON data for the browser tree.
239
     *
240
     * @param array  $_treedata a set of records to populate the tree
241
     * @param array  $attrs     Attributes for tree items
242
     * @param string $section   The section where the branch is linked in the tree
243
     * @param bool   $print     either to return or echo the result
244
     *
245
     * @return \Slim\Http\Response|string the json rendered tree
246
     */
247
    public function printTree(&$_treedata, &$attrs, $section, $print = true)
248
    {
249
        $tree = $this->_getTreeController();
250
251
        return $tree->printTree($_treedata, $attrs, $section, $print);
252
    }
253
254
    public function printTrail($trail = [], $do_print = true)
255
    {
256
        $from       = __METHOD__;
257
        $html_trail = $this->_getNavbarController();
258
259
        return $html_trail->printTrail($trail, $do_print, $from);
260
    }
261
262
    public function printNavLinks($navlinks, $place, $env = [], $do_print = true)
263
    {
264
        $from              = __METHOD__;
265
        $footer_controller = $this->_getFooterController();
266
267
        return $footer_controller->printNavLinks($navlinks, $place, $env, $do_print, $from);
268
    }
269
270
    public function printTabs($tabs, $activetab, $do_print = true)
271
    {
272
        $from       = __METHOD__;
273
        $html_trail = $this->_getNavbarController();
274
275
        return $html_trail->printTabs($tabs, $activetab, $do_print, $from);
276
    }
277
278
    public function printLink($link, $do_print = true, $from = null)
279
    {
280
        if (null === $from) {
281
            $from = __METHOD__;
282
        }
283
284
        $html_trail = $this->_getNavbarController();
285
286
        return $html_trail->printLink($link, $do_print, $from);
287
    }
288
289
    public function setReloadDropDatabase($flag)
290
    {
291
        $footer_controller = $this->_getFooterController();
292
293
        return $footer_controller->setReloadDropDatabase($flag);
294
    }
295
296
    public function setNoBottomLink($flag)
297
    {
298
        $footer_controller = $this->_getFooterController();
299
300
        return $footer_controller->setNoBottomLink($flag);
301
    }
302
303
    public function printFooter($doBody = true, $template = 'footer.twig')
304
    {
305
        $footer_controller = $this->_getFooterController();
306
307
        return $footer_controller->printFooter($doBody, $template);
308
    }
309
310
    public function printReload($database, $do_print = true)
311
    {
312
        $footer_controller = $this->_getFooterController();
313
314
        return $footer_controller->printReload($database, $do_print);
315
    }
316
317
    /**
318
     * Outputs JavaScript to set default focus.
319
     *
320
     * @param mixed $object eg. forms[0].username
321
     */
322
    public function setFocus($object)
323
    {
324
        $footer_controller = $this->_getFooterController();
325
326
        return $footer_controller->setFocus($object);
327
    }
328
329
    /**
330
     * Outputs JavaScript to set the name of the browser window.
331
     *
332
     * @param string $name      the window name
333
     * @param bool   $addServer if true (default) then the server id is
334
     *                          attached to the name
335
     */
336
    public function setWindowName($name, $addServer = true)
337
    {
338
        $footer_controller = $this->_getFooterController();
339
340
        return $footer_controller->setWindowName($name, $addServer);
341
    }
342
343
    public function setNoOutput($flag)
344
    {
345
        $header_controller = $this->_getHeaderController();
346
347
        return $header_controller->setNoOutput((bool) $flag);
348
    }
349
350
    public function printHeader($title = '', $script = null, $do_print = true, $template = 'header.twig')
351
    {
352
        $title             = $title ? $title : $this->headerTitle();
353
        $header_controller = $this->_getHeaderController();
354
355
        return $header_controller->printHeader($title, $script, $do_print, $template);
356
    }
357
358
    public function printBody($doBody = true, $bodyClass = 'detailbody', $onloadInit = false)
359
    {
360
        $header_controller = $this->_getHeaderController();
361
362
        return $header_controller->printBody($doBody, $bodyClass, $onloadInit);
363
    }
364
365
    public function printTitle($title, $help = null, $do_print = true)
366
    {
367
        $header_controller = $this->_getHeaderController();
368
369
        return $header_controller->printTitle($title, $help, $do_print);
370
    }
371
372
    public function getRequestParam($key, $default = null)
373
    {
374
        return $this->container->requestobj->getParam($key, $default);
375
    }
376
377
    public function getPostParam($key, $default = null)
378
    {
379
        return $this->container->requestobj->getParsedBodyParam($key, $default);
380
    }
381
382
    public function getQueryParam($key, $default = null)
383
    {
384
        return $this->container->requestobj->getQueryParam($key, $default);
385
    }
386
387
    /**
388
     * Print out a message.
389
     *
390
     * @param string $msg      The message
391
     * @param bool   $do_print if true, print the message. Return the string otherwise
392
     *
393
     * @return string a paragraph containing the message, whose linebreaks are replaced by <br> elements
394
     */
395
    public function printMsg($msg, $do_print = true)
396
    {
397
        $html = '';
398
        $msg  = htmlspecialchars(\PHPPgAdmin\Traits\HelperTrait::br2ln($msg));
399
        if ('' != $msg) {
400
            $html .= '<p class="message">'.nl2br($msg).'</p>'."\n";
401
        }
402
        if ($do_print) {
403
            echo $html;
404
405
            return $html;
406
        }
407
408
        return $html;
409
    }
410
}
411