Passed
Push — develop ( d8a719...f4d730 )
by Felipe
04:10
created

BaseController::_getTableController()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 0
1
<?php
2
3
/**
4
 * PHPPgAdmin v6.0.0-beta.44
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($this->lang[$this->controller_title]);
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
    public function getContainer()
146
    {
147
        return $this->container;
148
    }
149
150
    private function _getTableController()
151
    {
152
        if (null === $this->table_controller) {
153
            $this->table_controller = new \PHPPgAdmin\XHtml\HTMLTableController($this->getContainer(), $this->controller_name);
154
        }
155
156
        return $this->table_controller;
157
    }
158
159
    private function _getFooterController()
160
    {
161
        if (null === $this->footer_controller) {
162
            $this->footer_controller = new \PHPPgAdmin\XHtml\HTMLFooterController($this->getContainer(), $this->controller_name);
163
        }
164
165
        return $this->footer_controller;
166
    }
167
168
    private function _getHeaderController()
169
    {
170
        if (null === $this->header_controller) {
171
            $this->header_controller = new \PHPPgAdmin\XHtml\HTMLHeaderController($this->getContainer(), $this->controller_name);
172
        }
173
174
        return $this->header_controller;
175
    }
176
177
    private function _getNavbarController()
178
    {
179
        if (null === $this->trail_controller) {
180
            $this->trail_controller = new \PHPPgAdmin\XHtml\HTMLNavbarController($this->getContainer(), $this->controller_name);
181
        }
182
183
        return $this->trail_controller;
184
    }
185
186
    private function _getTreeController()
187
    {
188
        if (null === $this->tree_controller) {
189
            $this->tree_controller = new \PHPPgAdmin\XHtml\TreeController($this->getContainer(), $this->controller_name);
190
        }
191
192
        return $this->tree_controller;
193
    }
194
195
    /**
196
     * Display a table of data.
197
     *
198
     * @param \PHPPgAdmin\ADORecordSet|\PHPPgAdmin\ArrayecordSet $tabledata a set of data to be formatted, as returned by $data->getDatabases() etc
0 ignored issues
show
Bug introduced by
The type PHPPgAdmin\ArrayecordSet was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
199
     * @param array                                              $columns   An associative array of columns to be displayed:
200
     *                                                                      $columns = array(
201
     *                                                                      column_id => array(
202
     *                                                                      'title' => Column heading,
203
     *                                                                      'class' => The class to apply on the column cells,
204
     *                                                                      'field' => Field name for $tabledata->fields[...],
205
     *                                                                      'help'  => Help page for this column,
206
     *                                                                      ), ...
207
     *                                                                      );
208
     * @param array                                              $actions   Actions that can be performed on each object:
209
     *                                                                      $actions = array(
210
     *                                                                      * multi action support
211
     *                                                                      * parameters are serialized for each entries and given in $_REQUEST['ma']
212
     *                                                                      'multiactions' => array(
213
     *                                                                      'keycols' => Associative array of (URL variable => field name), // fields included in the form
214
     *                                                                      'url' => URL submission,
215
     *                                                                      'default' => Default selected action in the form. If null, an empty action is added & selected
216
     *                                                                      ),
217
     *                                                                      * actions *
218
     *                                                                      action_id => array(
219
     *                                                                      'title' => Action heading,
220
     *                                                                      'url'   => Static part of URL.  Often we rely
221
     *                                                                      relative urls, usually the page itself (not '' !), or just a query string,
222
     *                                                                      'vars'  => Associative array of (URL variable => field name),
223
     *                                                                      'multiaction' => Name of the action to execute.
224
     *                                                                      Add this action to the multi action form
225
     *                                                                      ), ...
226
     *                                                                      );
227
     * @param string                                             $place     Place where the $actions are displayed. Like 'display-browse',  where 'display'
228
     *                                                                      is the entrypoint (/src/views/display) and 'browse' is the action used inside its controller (in this case, doBrowse).
229
     * @param string                                             $nodata    (optional) Message to display if data set is empty
230
     * @param callable                                           $pre_fn    (optional) callback closure for each row. It will be passed two params: $rowdata and $actions,
231
     *                                                                      it may be used to derive new fields or modify actions.
232
     *                                                                      It can return an array of actions specific to the row,  or if nothing is returned then the standard actions are used.
233
     *                                                                      (see TblpropertiesController and ConstraintsController for examples)
234
     *                                                                      The function must not must not store urls because     they are relative and won't work out of context.
235
     *
236
     *  @return string the html of the table
0 ignored issues
show
Coding Style introduced by
Expected 1 space after asterisk; 2 found
Loading history...
237
     */
238
    public function printTable(&$tabledata, &$columns, &$actions, $place, $nodata = null, $pre_fn = null)
239
    {
240
        $html_table = $this->_getTableController();
241
242
        return $html_table->printTable($tabledata, $columns, $actions, $place, $nodata, $pre_fn);
243
    }
244
245
    public function adjustTabsForTree($tabs)
246
    {
247
        $tree = $this->_getTreeController();
248
249
        return $tree->adjustTabsForTree($tabs);
250
    }
251
252
    public function printTree(&$_treedata, &$attrs, $section, $print = true)
253
    {
254
        $tree = $this->_getTreeController();
255
256
        return $tree->printTree($_treedata, $attrs, $section, $print);
257
    }
258
259
    public function printTrail($trail = [], $do_print = true)
260
    {
261
        $from       = __METHOD__;
262
        $html_trail = $this->_getNavbarController();
263
264
        return $html_trail->printTrail($trail, $do_print, $from);
265
    }
266
267
    public function printNavLinks($navlinks, $place, $env = [], $do_print = true)
268
    {
269
        $from       = __METHOD__;
270
        $html_trail = $this->_getNavbarController();
271
272
        return $html_trail->printNavLinks($navlinks, $place, $env, $do_print, $from);
273
    }
274
275
    public function printTabs($tabs, $activetab, $do_print = true)
276
    {
277
        $from       = __METHOD__;
278
        $html_trail = $this->_getNavbarController();
279
280
        return $html_trail->printTabs($tabs, $activetab, $do_print, $from);
281
    }
282
283
    public function getLastTabURL($section)
284
    {
285
        $html_trail = $this->_getNavbarController();
286
287
        return $html_trail->getLastTabURL($section);
288
    }
289
290
    public function printLink($link, $do_print = true, $from = null)
291
    {
292
        if (null === $from) {
293
            $from = __METHOD__;
294
        }
295
296
        $html_trail = $this->_getNavbarController();
297
298
        return $html_trail->printLink($link, $do_print, $from);
299
    }
300
301
    public function setReloadDropDatabase($flag)
302
    {
303
        $footer_controller = $this->_getFooterController();
304
305
        return $footer_controller->setReloadDropDatabase($flag);
306
    }
307
308
    public function setNoBottomLink($flag)
309
    {
310
        $footer_controller = $this->_getFooterController();
311
312
        return $footer_controller->setNoBottomLink($flag);
313
    }
314
315
    public function printFooter($doBody = true, $template = 'footer.twig')
316
    {
317
        $footer_controller = $this->_getFooterController();
318
319
        return $footer_controller->printFooter($doBody, $template);
320
    }
321
322
    public function printReload($database, $do_print = true)
323
    {
324
        $footer_controller = $this->_getFooterController();
325
326
        return $footer_controller->printReload($database, $do_print);
327
    }
328
329
    /**
330
     * Outputs JavaScript to set default focus.
331
     *
332
     * @param mixed $object eg. forms[0].username
333
     */
334
    public function setFocus($object)
335
    {
336
        $footer_controller = $this->_getFooterController();
337
338
        return $footer_controller->setFocus($object);
339
    }
340
341
    /**
342
     * Outputs JavaScript to set the name of the browser window.
343
     *
344
     * @param string $name      the window name
345
     * @param bool   $addServer if true (default) then the server id is
346
     *                          attached to the name
347
     */
348
    public function setWindowName($name, $addServer = true)
349
    {
350
        $footer_controller = $this->_getFooterController();
351
352
        return $footer_controller->setWindowName($name, $addServer);
353
    }
354
355
    public function setNoOutput($flag)
356
    {
357
        $header_controller = $this->_getHeaderController();
358
359
        return $header_controller->setNoOutput((bool) $flag);
360
    }
361
362
    public function printHeader($title = '', $script = null, $do_print = true, $template = 'header.twig')
363
    {
364
        $header_controller = $this->_getHeaderController();
365
366
        return $header_controller->printHeader($title, $script, $do_print, $template);
367
    }
368
369
    public function printBody($doBody = true, $bodyClass = 'detailbody', $onloadInit = false)
370
    {
371
        $header_controller = $this->_getHeaderController();
372
373
        return $header_controller->printBody($doBody, $bodyClass, $onloadInit);
374
    }
375
376
    public function printTitle($title, $help = null, $do_print = true)
377
    {
378
        $header_controller = $this->_getHeaderController();
379
380
        return $header_controller->printTitle($title, $help, $do_print);
381
    }
382
383
    public function getRequestParam($key, $default = null)
384
    {
385
        return $this->container->requestobj->getParam($key, $default);
386
    }
387
388
    public function getPostParam($key, $default = null)
389
    {
390
        return $this->container->requestobj->getParsedBodyParam($key, $default);
391
    }
392
393
    public function getQueryParam($key, $default = null)
394
    {
395
        return $this->container->requestobj->getQueryParam($key, $default);
396
    }
397
398
    /**
399
     * Print out a message.
400
     *
401
     * @param string $msg      The message
402
     * @param bool   $do_print if true, print the message. Return the string otherwise
403
     *
404
     * @return string a paragraph containing the message, whose linebreaks are replaced by <br> elements
405
     */
406
    public function printMsg($msg, $do_print = true)
407
    {
408
        $html = '';
409
        $msg  = htmlspecialchars(\PHPPgAdmin\Traits\HelperTrait::br2ln($msg));
410
        if ('' != $msg) {
411
            $html .= '<p class="message">'.nl2br($msg).'</p>'."\n";
412
        }
413
        if ($do_print) {
414
            echo $html;
415
416
            return $html;
417
        }
418
419
        return $html;
420
    }
421
}
422