Passed
Push — develop ( 2969a7...df24bb )
by Felipe
05:41 queued 11s
created

BaseController::_getTreeInstance()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
c 0
b 0
f 0
nc 2
nop 2
dl 0
loc 10
rs 10
1
<?php
2
3
/**
4
 * PHPPgAdmin v6.0.0-RC6
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
115
    /**
116
     * Default method to render the controller according to the action parameter.
117
     */
118
    public function render()
119
    {
120
        $this->misc = $this->misc;
121
122
        $this->printHeader();
123
        $this->printBody();
124
125
        switch ($this->action) {
126
            default:
127
                $this->doDefault();
128
129
                break;
130
        }
131
132
        $this->printFooter();
133
    }
134
135
    public function doDefault()
136
    {
137
        $html = '<div><h2>Section title</h2> <p>Main content</p></div>';
138
        echo $html;
139
140
        return $html;
141
    }
142
143
    /**
144
     * Returns the page title for each controller.
145
     *
146
     * @param string $title  The title
147
     * @param string $prefix The prefix
148
     * @param string $suffix The suffix
149
     *
150
     * @return string the page title
151
     */
152
    public function headerTitle($title = '', $prefix = '', $suffix = '')
153
    {
154
        $title = $title ? $title : $this->controller_title;
155
156
        return $prefix.$this->lang[$title].($suffix ? ': '.$suffix : '');
157
    }
158
159
    public function getContainer()
160
    {
161
        return $this->container;
162
    }
163
164
    private function _getTableController()
165
    {
166
        if (null === $this->table_controller) {
167
            $this->table_controller = new \PHPPgAdmin\XHtml\HTMLTableController($this->getContainer(), $this->controller_name);
168
        }
169
170
        return $this->table_controller;
171
    }
172
173
    private function _getFooterController()
174
    {
175
        if (null === $this->footer_controller) {
176
            $this->footer_controller = new \PHPPgAdmin\XHtml\HTMLFooterController($this->getContainer(), $this->controller_name);
177
        }
178
179
        return $this->footer_controller;
180
    }
181
182
    private function _getHeaderController()
183
    {
184
        if (null === $this->header_controller) {
185
            $this->header_controller = new \PHPPgAdmin\XHtml\HTMLHeaderController($this->getContainer(), $this->controller_name);
186
        }
187
188
        return $this->header_controller;
189
    }
190
191
    private function _getNavbarController()
192
    {
193
        if (null === $this->trail_controller) {
194
            $this->trail_controller = new \PHPPgAdmin\XHtml\HTMLNavbarController($this->getContainer(), $this->controller_name);
195
        }
196
197
        return $this->trail_controller;
198
    }
199
200
    private function _getTreeController()
201
    {
202
        if (null === $this->tree_controller) {
203
            $this->tree_controller = new \PHPPgAdmin\XHtml\TreeController($this->getContainer(), $this->controller_name);
204
        }
205
206
        return $this->tree_controller;
207
    }
208
209
    /**
210
     * Display a table of data.
211
     *
212
     * @param \PHPPgAdmin\ADORecordSet|\PHPPgAdmin\ArrayRecordSet $tabledata a set of data to be formatted
213
     * @param array                                               $columns   An associative array of columns to be displayed:
214
     * @param array                                               $actions   Actions that can be performed on each object:
215
     * @param string                                              $place     Place where the $actions are displayed. Like 'display-browse',
216
     * @param string                                              $nodata    (optional) Message to display if data set is empty
217
     * @param callable                                            $pre_fn    (optional) callback closure for each row
218
     *
219
     * @return string the html of the table
220
     */
221
    public function printTable(&$tabledata, &$columns, &$actions, $place, $nodata = '', $pre_fn = null)
222
    {
223
        $html_table = $this->_getTableController();
224
225
        $html_table->initialize($tabledata, $columns, $actions, $place, $nodata, $pre_fn);
226
227
        return $html_table->printTable();
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 \PHPPgAdmin\ArrayRecordSet $_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>'.PHP_EOL;
401
        }
402
        if ($do_print) {
403
            echo $html;
404
405
            return $html;
406
        }
407
408
        return $html;
409
    }
410
}
411