controller.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. <?php
  2. class Controller {
  3. private $request = null;
  4. private $template = '';
  5. private $view = null;
  6. /**
  7. *
  8. * @param Array $request $_GET and $_POST
  9. */
  10. public function __construct($request) {
  11. ErrorHandler::$eh = new ErrorHandler();
  12. Database::$db = new Database();
  13. $this->view = new View();
  14. $this->request = $request;
  15. if(!empty($request['action']) && $request['action']) { // any action needs to be done?
  16. $this->actionController();
  17. }
  18. $this->template = !empty(strtok($_SERVER['REQUEST_URI'], '?')) ? strtok($_SERVER['REQUEST_URI'], '?') : 'default';
  19. }
  20. /**
  21. * Return requested page
  22. *
  23. * @return String Content of requested page
  24. */
  25. public function display() {
  26. $singlePage = false; // set to true if header/footer shouldn't be used
  27. switch($this->template) {
  28. case 'default':
  29. default:
  30. $singlePage = true;
  31. $this->view->setTemplate('default');
  32. $this->view->assign("test", "joo");
  33. break;
  34. }
  35. if(!$singlePage) {
  36. $headerView = new View();
  37. $footerView = new View();
  38. $headerView->setTemplate("header");
  39. $footerView->setTemplate("footer");
  40. $header = $headerView->loadTemplate(); // save them in vars to prevent errors being ignored
  41. $footer = $footerView->loadTemplate();
  42. $this->view->assign("errors", ErrorHandler::$eh->getErrors()); // get errors as last thing
  43. return $header . $this->view->loadTemplate() . $footer;
  44. }
  45. $this->view->assign("errors", ErrorHandler::$eh->getErrors()); // get errors as last thing
  46. return $this->view->loadTemplate();
  47. }
  48. private function actionController() {
  49. switch($this->request['action']) {
  50. default:
  51. error_log("Unknown action: " . $this->request['action']);
  52. break;
  53. }
  54. }
  55. }
  56. ?>