HuasoFoundries /
phpPgAdmin6
| 1 | <?php |
||
| 2 | |||
|
0 ignored issues
–
show
Coding Style
introduced
by
Loading history...
|
|||
| 3 | /* |
||
| 4 | * PHPPgAdmin v6.0.0-beta.30 |
||
| 5 | */ |
||
| 6 | |||
| 7 | namespace PHPPgAdmin\Controller; |
||
| 8 | |||
| 9 | /** |
||
| 10 | * Base controller class. |
||
| 11 | */ |
||
| 12 | class BaseController |
||
| 13 | { |
||
| 14 | use \PHPPgAdmin\HelperTrait; |
||
| 15 | |||
| 16 | protected $container; |
||
| 17 | protected $_connection; |
||
|
1 ignored issue
–
show
|
|||
| 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'); |
||
| 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() |
||
| 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() |
||
| 129 | { |
||
| 130 | return $this->container; |
||
| 131 | } |
||
| 132 | |||
| 133 | private function getTableController() |
||
| 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() |
||
| 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() |
||
| 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() |
||
| 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() |
||
| 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] |
||
| 182 | * @param [type] &$columns [description] |
||
| 183 | * @param [type] &$actions [description] |
||
| 184 | * @param [type] $place [description] |
||
| 185 | * @param [type] $nodata [description] |
||
| 186 | * @param [type] $pre_fn [description] |
||
| 187 | * |
||
| 188 | * @return [type] [description] |
||
| 189 | */ |
||
| 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) |
||
| 198 | { |
||
| 199 | $tree = $this->getTreeController(); |
||
| 200 | |||
| 201 | return $tree->adjustTabsForTree($tabs); |
||
| 202 | } |
||
| 203 | |||
| 204 | public function printTree(&$_treedata, &$attrs, $section, $print = true) |
||
| 205 | { |
||
| 206 | $tree = $this->getTreeController(); |
||
| 207 | |||
| 208 | return $tree->printTree($_treedata, $attrs, $section, $print); |
||
| 209 | } |
||
| 210 | |||
| 211 | public function printTrail($trail = [], $do_print = true) |
||
| 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) |
||
| 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) |
||
| 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) |
||
| 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) |
||
| 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) |
||
| 254 | { |
||
| 255 | $footer_controller = $this->getFooterController(); |
||
| 256 | |||
| 257 | return $footer_controller->setReloadDropDatabase($flag); |
||
| 258 | } |
||
| 259 | |||
| 260 | public function setNoBottomLink($flag) |
||
| 261 | { |
||
| 262 | $footer_controller = $this->getFooterController(); |
||
| 263 | |||
| 264 | return $footer_controller->setNoBottomLink($flag); |
||
| 265 | } |
||
| 266 | |||
| 267 | public function printFooter($doBody = true, $template = 'footer.twig') |
||
| 268 | { |
||
| 269 | $footer_controller = $this->getFooterController(); |
||
| 270 | |||
| 271 | return $footer_controller->printFooter($doBody, $template); |
||
| 272 | } |
||
| 273 | |||
| 274 | public function printReload($database, $do_print = true) |
||
| 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) |
||
| 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') |
||
| 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') |
||
| 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) |
||
| 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 |