ErrorHandler.php 5.3 KB

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