ErrorHandler.php 5.0 KB

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