ErrorHandler.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. namespace Opentalent\OtTemplating\Page;
  3. use Psr\Http\Message\ResponseInterface;
  4. use Psr\Http\Message\ServerRequestInterface;
  5. use TYPO3\CMS\Core\Error\PageErrorHandler\PageContentErrorHandler;
  6. use TYPO3\CMS\Core\Error\PageErrorHandler\PageErrorHandlerInterface;
  7. use TYPO3\CMS\Core\Http\HtmlResponse;
  8. use TYPO3\CMS\Core\Utility\GeneralUtility;
  9. use TYPO3\CMS\Fluid\View\TemplateView;
  10. use TYPO3Fluid\Fluid\View\ViewInterface;
  11. class ErrorHandler implements PageErrorHandlerInterface
  12. {
  13. const TEMPLATES_ROOT_PATHS = 'EXT:ot_templating/Resources/Private/Templates';
  14. const LAYOUTS_ROOT_PATHS = 'EXT:ot_templating/Resources/Private/Layouts';
  15. const PARTIALS_ROOT_PATHS = 'EXT:ot_templating/Resources/Private/Partials';
  16. const TEMPLATE_FILES = [
  17. 403 => self::TEMPLATES_ROOT_PATHS . '/Page/Error/403.html',
  18. 404 => self::TEMPLATES_ROOT_PATHS . '/Page/Error/404.html',
  19. 500 => self::TEMPLATES_ROOT_PATHS . '/Page/Error/500.html'
  20. ];
  21. const REDIRECT_FALLBACK = 'https://opentalent.fr';
  22. /**
  23. * Status code of the response
  24. * @var int
  25. */
  26. protected $statusCode;
  27. /**
  28. * Configuration of the error handler as set in site configuration.
  29. * @var array
  30. */
  31. protected $errorHandlerConfiguration;
  32. /**
  33. * @var ViewInterface
  34. */
  35. protected $view;
  36. public function __construct(int $statusCode, array $configuration)
  37. {
  38. $this->statusCode = $statusCode;
  39. $this->errorHandlerConfiguration = $configuration;
  40. $this->view = GeneralUtility::makeInstance(TemplateView::class);
  41. $this->view->setTemplateRootPaths([self::TEMPLATES_ROOT_PATHS]);
  42. $this->view->setLayoutRootPaths([self::LAYOUTS_ROOT_PATHS]);
  43. $this->view->setPartialRootPaths([self::PARTIALS_ROOT_PATHS]);
  44. $this->view->setTemplatePathAndFilename(
  45. GeneralUtility::getFileAbsFileName(self::TEMPLATE_FILES[$statusCode])
  46. );
  47. }
  48. /**
  49. * @param ServerRequestInterface $request
  50. * @param string $message
  51. * @param array $reasons
  52. * @return ResponseInterface
  53. */
  54. public function handlePageError(
  55. ServerRequestInterface $request,
  56. string $message,
  57. array $reasons = []
  58. ): ResponseInterface {
  59. $homeUri = "https://opentalent.fr";
  60. $title = 'Page introuvable';
  61. // This variable role is to prevent redirection loop,
  62. $isCircular = preg_match('/.*\/page-introuvable/', $request->getUri()->getPath());
  63. $pageRepository = GeneralUtility::makeInstance(OtPageRepository::class);
  64. $site = $pageRepository->getCurrentSite();
  65. if ($site && !$isCircular){
  66. $homeUri = $site->getBase();
  67. $rootUid = $site->getRootPageId();
  68. $rootPage = $pageRepository->getPage($rootUid);
  69. if (!empty($rootPage)) {
  70. $title = $rootPage['title'];
  71. $subPages = $pageRepository->getAllSubpagesForPage($rootUid, true);
  72. foreach ($subPages as $page) {
  73. if ($page['slug'] == '/page-introuvable') {
  74. $errorHandler = GeneralUtility::makeInstance(
  75. PageContentErrorHandler::class,
  76. $this->statusCode,
  77. [
  78. 'errorHandler' => 'Page',
  79. 'errorContentSource' => 't3://page?uid=' . $page['uid']
  80. ]
  81. );
  82. return $errorHandler->handlePageError($request, $message, $reasons);
  83. }
  84. }
  85. } else {
  86. $title = "Page inexistante";
  87. $homeUri = self::REDIRECT_FALLBACK;
  88. }
  89. }
  90. // At this point, the site has not been determined or
  91. // this site has no subpage with slug '/page-introuvable'
  92. $this->view->assignMultiple([
  93. 'request' => $request,
  94. 'message' => $message,
  95. 'reasons' => $reasons,
  96. 'homeUri' => $homeUri,
  97. 'siteTitle' => $title
  98. ]);
  99. return new HtmlResponse($this->view->render(), $this->statusCode);
  100. }
  101. }