WebAppController.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. <?php
  2. namespace KarmaFW\Routing\Controllers;
  3. use \KarmaFW\WebApp;
  4. use \KarmaFW\Http\Request;
  5. use \KarmaFW\Http\Response;
  6. use \KarmaFW\Lib\Hooks\HooksManager;
  7. class WebAppController extends AppController
  8. {
  9. protected $request;
  10. protected $response;
  11. protected $request_uri = null;
  12. protected $request_method = null;
  13. protected $route = null;
  14. protected $template;
  15. protected $user_id;
  16. protected $flash;
  17. public function __construct(Request $request, Response $response)
  18. {
  19. parent::__construct($request, $response);
  20. $this->request = $request;
  21. $this->response = $response;
  22. $this->request_uri = $request->SERVER['REQUEST_URI'];
  23. $this->request_method = $request->SERVER['REQUEST_METHOD'];
  24. $this->route = $request->getRoute();
  25. if (defined('USE_HOOKS') && USE_HOOKS) {
  26. HooksManager::applyHook('webcontroller.before', [$this]);
  27. }
  28. $this->user_id = session('user_id');
  29. $this->flash = session('flash');
  30. $_SESSION['flash'] = []; // ['success' => 'action done !', 'error' => 'an error occured', 'warning' => 'notice ...']
  31. if (defined('TPL_DIR')) {
  32. $this->template = WebApp::createTemplate();
  33. $this->template->assign('user_id', $this->user_id);
  34. $this->template->assign('flash', $this->flash);
  35. if (defined('APP_NAME')) {
  36. $this->template->assign('meta_title', APP_NAME);
  37. $this->template->assign('meta_description', APP_NAME);
  38. $this->template->assign('h1', APP_NAME);
  39. }
  40. }
  41. if (defined('USE_HOOKS') && USE_HOOKS) {
  42. HooksManager::applyHook('webcontroller.after', [$this]);
  43. }
  44. }
  45. public function getRoute()
  46. {
  47. return $this->route;
  48. }
  49. public function getRequestMethod()
  50. {
  51. return $this->request_method;
  52. }
  53. public function getRequestUri()
  54. {
  55. return $this->request_uri;
  56. }
  57. public function getTemplate()
  58. {
  59. return $this->template;
  60. }
  61. protected function showError($http_status = 500, $meta_title = 'Server Error', $h1 = 'Error 500 - Server Error', $message = 'an error has occured')
  62. {
  63. if ($template = $this->getTemplate()) {
  64. $template->assign('meta_title', $meta_title);
  65. $template->assign('h1', $h1);
  66. $template->assign('p', $message);
  67. $template->assign('http_status', $http_status);
  68. $error_template = 'error.tpl.php';
  69. if (defined('ERROR_TEMPLATE')) {
  70. $error_template = ERROR_TEMPLATE;
  71. }
  72. //$template->display($error_template);
  73. //return $this->response->html( $template->fetch($error_template) , $http_status);
  74. $response_content = $template->fetch($error_template);
  75. } else {
  76. //header("HTTP/1.0 " . $http_status . " " . $meta_title);
  77. $output_html = '';
  78. $output_html .= '<html>' . PHP_EOL;
  79. $output_html .= '<head>' . PHP_EOL;
  80. if (! empty($meta_title)) {
  81. $output_html .= '<title>' . $meta_title . '</title>' . PHP_EOL;
  82. }
  83. $output_html .= '</head>' . PHP_EOL;
  84. $output_html .= '<body>' . PHP_EOL;
  85. if (! empty($h1)) {
  86. $output_html .= '<h1>' . $h1 . '</h1>' . PHP_EOL;
  87. }
  88. if (! empty($message)) {
  89. $output_html .= '<p>' . $message . '</p>' . PHP_EOL;
  90. }
  91. $output_html .= '</body>' . PHP_EOL;
  92. $output_html .= '</html>' . PHP_EOL;
  93. //echo $output_html;
  94. //return $this->response->html($output_html, $http_status);
  95. $response_content = $output_html;
  96. }
  97. throw new \Exception($response_content, $http_status);
  98. }
  99. protected function showError400($title = 'Bad request', $message = '')
  100. {
  101. return $this->showError(400, $title, $title, $message);
  102. }
  103. protected function showError403($title = 'Forbidden', $message = 'you are not allowed')
  104. {
  105. return $this->showError(403, $title, $title, $message);
  106. }
  107. protected function showError404($title = 'Page not found', $message = "The page you're looking for doesn't exist")
  108. {
  109. return $this->showError(404, $title, $title, $message);
  110. }
  111. protected function showError500($title = 'Internal Server Error', $message = 'An error has occured')
  112. {
  113. return $this->showError(500, $title, $title, $message);
  114. }
  115. protected function showError503($title = 'Service Unavailable', $message = 'The service is unavailable')
  116. {
  117. return $this->showError(503, $title, $title, $message);
  118. }
  119. }