Passed
Push — master ( 442876...4ec1bc )
by Felipe
15:55 queued 10:33
created

BaseController::adjustTabsForTree()   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
0 ignored issues
show
Coding Style introduced by
You must use "/**" style comments for a file comment
Loading history...
3
/*
4
 * PHPPgAdmin v6.0.0-beta.30
5
 */
6
7
namespace PHPPgAdmin\Controller;
8
9
/**
10
 * Base controller class.
11
 */
5 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @package tag in class comment
Loading history...
Coding Style introduced by
Missing @author tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
12
class BaseController
13
{
14
    use \PHPPgAdmin\HelperTrait;
15
16
    protected $container;
17
    protected $_connection;
1 ignored issue
show
Coding Style introduced by
Protected member variable _connection must not be prefixed with an underscore as per coding-style.
Loading history...
18
    protected $app;
19
    protected $data;
20
    protected $database;
21
    protected $server_id;
22
    public $appLangFiles     = [];
23
    public $appThemes        = [];
24
    public $appName          = '';
25
    public $appVersion       = '';
26
    public $form             = '';
27
    public $href             = '';
28
    public $lang             = [];
29
    public $action           = '';
30
    public $controller_name  = 'BaseController';
31
    public $controller_title = 'base';
32
    protected $table_controller;
33
    protected $trail_controller;
34
    protected $tree_controller;
35
    protected $footer_controller;
36
    protected $header_controller;
37
    protected $scripts = '';
38
    public $msg        = '';
39
    public $view;
40
    public $plugin_manager;
41
    public $misc;
42
    public $conf;
43
    public $phpMinVer;
44
45
    /**
46
     * Constructs the base controller (common for almost all controllers).
47
     *
48
     * @param \Slim\Container $container        the $app container
49
     * @param bool            $no_db_connection [optional] if true, sets  $this->misc->setNoDBConnection(true);
50
     */
51
    public function __construct(\Slim\Container $container, $no_db_connection = false)
52
    {
53
        $this->container = $container;
54
        $this->lang      = $container->get('lang');
55
56
        $this->view           = $container->get('view');
57
        $this->plugin_manager = $container->get('plugin_manager');
58
        $this->msg            = $container->get('msg');
59
        $this->appLangFiles   = $container->get('appLangFiles');
60
61
        $this->misc = $container->get('misc');
62
        $this->conf = $this->misc->getConf();
63
64
        $this->appThemes = $container->get('appThemes');
65
        $this->action    = $container->get('action');
66
67
        $this->appName          = $container->get('settings')['appName'];
68
        $this->appVersion       = $container->get('settings')['appVersion'];
69
        $this->postgresqlMinVer = $container->get('settings')['postgresqlMinVer'];
70
        $this->phpMinVer        = $container->get('settings')['phpMinVer'];
71
72
        $msg = $container->get('msg');
1 ignored issue
show
Unused Code introduced by
The assignment to $msg is dead and can be removed.
Loading history...
73
74
        if (true === $no_db_connection) {
75
            $this->misc->setNoDBConnection(true);
76
        }
77
78
        if (false === $this->misc->getNoDBConnection()) {
79
            if (null === $this->misc->getServerId()) {
80
                $servers_controller = new \PHPPgAdmin\Controller\ServersController($container, true);
81
82
                return $servers_controller->render();
83
            }
84
            $_server_info = $this->misc->getServerInfo();
85
            // Redirect to the login form if not logged in
86
            if (!isset($_server_info['username'])) {
87
                $msg = sprintf($this->lang['strlogoutmsg'], $_server_info['desc']);
88
89
                $servers_controller = new \PHPPgAdmin\Controller\ServersController($container, true);
90
91
                return $servers_controller->render();
92
            }
93
        }
94
95
        //\PC::debug(['name' => $this->controller_name, 'no_db_connection' => $this->misc->getNoDBConnection()], 'instanced controller');
96
    }
97
98
    /**
99
     * Default method to render the controller according to the action parameter.
100
     */
101
    public function render()
102
    {
103
        $this->misc = $this->misc;
104
        $lang       = $this->lang;
105
        $action     = $this->action;
106
107
        $this->printHeader($lang[$this->controller_title]);
108
        $this->printBody();
109
110
        switch ($action) {
111
            default:
112
                $this->doDefault();
113
114
                break;
115
        }
116
117
        $this->printFooter();
118
    }
119
120
    public function doDefault()
1 ignored issue
show
Coding Style introduced by
Missing function doc comment
Loading history...
121
    {
122
        $html = '<div><h2>Section title</h2> <p>Main content</p></div>';
123
        echo $html;
124
125
        return $html;
126
    }
127
128
    public function getContainer()
1 ignored issue
show
Coding Style introduced by
Missing function doc comment
Loading history...
129
    {
130
        return $this->container;
131
    }
132
133
    private function getTableController()
2 ignored issues
show
Coding Style introduced by
Missing function doc comment
Loading history...
Coding Style introduced by
Private method name "BaseController::getTableController" must be prefixed with an underscore
Loading history...
134
    {
135
        if (null === $this->table_controller) {
136
            $this->table_controller = new \PHPPgAdmin\XHtml\HTMLTableController($this->getContainer(), $this->controller_name);
137
        }
138
139
        return $this->table_controller;
140
    }
141
142
    private function getFooterController()
2 ignored issues
show
Coding Style introduced by
Missing function doc comment
Loading history...
Coding Style introduced by
Private method name "BaseController::getFooterController" must be prefixed with an underscore
Loading history...
143
    {
144
        if (null === $this->footer_controller) {
145
            $this->footer_controller = new \PHPPgAdmin\XHtml\HTMLFooterController($this->getContainer(), $this->controller_name);
146
        }
147
148
        return $this->footer_controller;
149
    }
150
151
    private function getHeaderController()
2 ignored issues
show
Coding Style introduced by
Missing function doc comment
Loading history...
Coding Style introduced by
Private method name "BaseController::getHeaderController" must be prefixed with an underscore
Loading history...
152
    {
153
        if (null === $this->header_controller) {
154
            $this->header_controller = new \PHPPgAdmin\XHtml\HTMLHeaderController($this->getContainer(), $this->controller_name);
155
        }
156
157
        return $this->header_controller;
158
    }
159
160
    private function getNavbarController()
2 ignored issues
show
Coding Style introduced by
Missing function doc comment
Loading history...
Coding Style introduced by
Private method name "BaseController::getNavbarController" must be prefixed with an underscore
Loading history...
161
    {
162
        if (null === $this->trail_controller) {
163
            $this->trail_controller = new \PHPPgAdmin\XHtml\HTMLNavbarController($this->getContainer(), $this->controller_name);
164
        }
165
166
        return $this->trail_controller;
167
    }
168
169
    private function getTreeController()
2 ignored issues
show
Coding Style introduced by
Missing function doc comment
Loading history...
Coding Style introduced by
Private method name "BaseController::getTreeController" must be prefixed with an underscore
Loading history...
170
    {
171
        if (null === $this->tree_controller) {
172
            $this->tree_controller = new \PHPPgAdmin\XHtml\TreeController($this->getContainer(), $this->controller_name);
173
        }
174
175
        return $this->tree_controller;
176
    }
177
178
    /**
179
     * Instances an HTMLTable and returns its html content.
180
     *
181
     * @param [type] &$tabledata [description]
0 ignored issues
show
Coding Style introduced by
Doc comment for parameter &$tabledata does not match actual variable name $tabledata
Loading history...
182
     * @param [type] &$columns   [description]
0 ignored issues
show
Coding Style introduced by
Doc comment for parameter &$columns does not match actual variable name $columns
Loading history...
183
     * @param [type] &$actions   [description]
0 ignored issues
show
Coding Style introduced by
Doc comment for parameter &$actions does not match actual variable name $actions
Loading history...
184
     * @param [type] $place      [description]
185
     * @param [type] $nodata     [description]
186
     * @param [type] $pre_fn     [description]
187
     *
188
     * @return [type] [description]
189
     */
0 ignored issues
show
Documentation Bug introduced by
The doc comment [type] at position 0 could not be parsed: Unknown type name '[' at position 0 in [type].
Loading history...
190
    public function printTable(&$tabledata, &$columns, &$actions, $place, $nodata = null, $pre_fn = null)
191
    {
192
        $html_table = $this->getTableController();
193
194
        return $html_table->printTable($tabledata, $columns, $actions, $place, $nodata, $pre_fn);
195
    }
196
197
    public function adjustTabsForTree($tabs)
1 ignored issue
show
Coding Style introduced by
Missing function doc comment
Loading history...
198
    {
199
        $tree = $this->getTreeController();
200
201
        return $tree->adjustTabsForTree($tabs);
202
    }
203
204
    public function printTree(&$_treedata, &$attrs, $section, $print = true)
1 ignored issue
show
Coding Style introduced by
Missing function doc comment
Loading history...
205
    {
206
        $tree = $this->getTreeController();
207
208
        return $tree->printTree($_treedata, $attrs, $section, $print);
209
    }
210
211
    public function printTrail($trail = [], $do_print = true)
1 ignored issue
show
Coding Style introduced by
Missing function doc comment
Loading history...
212
    {
213
        $from       = __METHOD__;
214
        $html_trail = $this->getNavbarController();
215
216
        return $html_trail->printTrail($trail, $do_print, $from);
217
    }
218
219
    public function printNavLinks($navlinks, $place, $env = [], $do_print = true)
1 ignored issue
show
Coding Style introduced by
Missing function doc comment
Loading history...
220
    {
221
        $from       = __METHOD__;
222
        $html_trail = $this->getNavbarController();
223
224
        return $html_trail->printNavLinks($navlinks, $place, $env, $do_print, $from);
225
    }
226
227
    public function printTabs($tabs, $activetab, $do_print = true)
1 ignored issue
show
Coding Style introduced by
Missing function doc comment
Loading history...
228
    {
229
        $from       = __METHOD__;
230
        $html_trail = $this->getNavbarController();
231
232
        return $html_trail->printTabs($tabs, $activetab, $do_print, $from);
233
    }
234
235
    public function getLastTabURL($section)
1 ignored issue
show
Coding Style introduced by
Missing function doc comment
Loading history...
236
    {
237
        $html_trail = $this->getNavbarController();
238
239
        return $html_trail->getLastTabURL($section);
240
    }
241
242
    public function printLink($link, $do_print = true, $from = null)
1 ignored issue
show
Coding Style introduced by
Missing function doc comment
Loading history...
243
    {
244
        if (null === $from) {
245
            $from = __METHOD__;
246
        }
247
248
        $html_trail = $this->getNavbarController();
249
250
        return $html_trail->printLink($link, $do_print, $from);
251
    }
252
253
    public function setReloadDropDatabase($flag)
1 ignored issue
show
Coding Style introduced by
Missing function doc comment
Loading history...
254
    {
255
        $footer_controller = $this->getFooterController();
256
257
        return $footer_controller->setReloadDropDatabase($flag);
258
    }
259
260
    public function setNoBottomLink($flag)
1 ignored issue
show
Coding Style introduced by
Missing function doc comment
Loading history...
261
    {
262
        $footer_controller = $this->getFooterController();
263
264
        return $footer_controller->setNoBottomLink($flag);
265
    }
266
267
    public function printFooter($doBody = true, $template = 'footer.twig')
1 ignored issue
show
Coding Style introduced by
Missing function doc comment
Loading history...
268
    {
269
        $footer_controller = $this->getFooterController();
270
271
        return $footer_controller->printFooter($doBody, $template);
272
    }
273
274
    public function printReload($database, $do_print = true)
1 ignored issue
show
Coding Style introduced by
Missing function doc comment
Loading history...
275
    {
276
        $footer_controller = $this->getFooterController();
277
278
        return $footer_controller->printReload($database, $do_print);
279
    }
280
281
    /**
282
     * Outputs JavaScript to set default focus.
283
     *
284
     * @param mixed $object eg. forms[0].username
285
     */
286
    public function setFocus($object)
287
    {
288
        $footer_controller = $this->getFooterController();
289
290
        return $footer_controller->setFocus($object);
291
    }
292
293
    /**
294
     * Outputs JavaScript to set the name of the browser window.
295
     *
296
     * @param string $name      the window name
297
     * @param bool   $addServer if true (default) then the server id is
298
     *                          attached to the name
299
     */
300
    public function setWindowName($name, $addServer = true)
301
    {
302
        $footer_controller = $this->getFooterController();
303
304
        return $footer_controller->setWindowName($name, $addServer);
305
    }
306
307
    public function setNoOutput($flag)
1 ignored issue
show
Coding Style introduced by
Missing function doc comment
Loading history...
308
    {
309
        $header_controller = $this->getHeaderController();
310
311
        return $header_controller->setNoOutput((bool) $flag);
312
    }
313
314
    public function printHeader($title = '', $script = null, $do_print = true, $template = 'header.twig')
1 ignored issue
show
Coding Style introduced by
Missing function doc comment
Loading history...
315
    {
316
        $header_controller = $this->getHeaderController();
317
318
        return $header_controller->printHeader($title, $script, $do_print, $template);
319
    }
320
321
    public function printBody($doBody = true, $bodyClass = 'detailbody')
1 ignored issue
show
Coding Style introduced by
Missing function doc comment
Loading history...
322
    {
323
        $header_controller = $this->getHeaderController();
324
325
        return $header_controller->printBody($doBody, $bodyClass);
326
    }
327
328
    public function printTitle($title, $help = null, $do_print = true)
1 ignored issue
show
Coding Style introduced by
Missing function doc comment
Loading history...
329
    {
330
        $header_controller = $this->getHeaderController();
331
332
        return $header_controller->printTitle($title, $help, $do_print);
333
    }
334
335
    /**
336
     * Print out a message.
337
     *
338
     * @param string $msg      The message
339
     * @param bool   $do_print if true, print the message. Return the string otherwise
340
     *
341
     * @return string a paragraph containing the message, whose linebreaks are replaced by <br> elements
342
     */
343
    public function printMsg($msg, $do_print = true)
344
    {
345
        $html = '';
346
        $msg  = htmlspecialchars(\PHPPgAdmin\HelperTrait::br2ln($msg));
347
        if ('' != $msg) {
348
            $html .= '<p class="message">'.nl2br($msg).'</p>'."\n";
349
        }
350
        if ($do_print) {
351
            echo $html;
352
353
            return $html;
354
        }
355
356
        return $html;
357
    }
358
}
359