OtStatsController.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <?php
  2. namespace Opentalent\OtStats\Controller;
  3. use Opentalent\OtCore\Controller\SelectedSiteController;
  4. use Opentalent\OtCore\Logging\OtLogger;
  5. use Opentalent\OtCore\Website\OtWebsiteRepository;
  6. use Opentalent\OtStats\Domain\Repository\MatomoWebsiteRepository;
  7. use Psr\Http\Message\ResponseInterface;
  8. use TYPO3\CMS\Core\Messaging\AbstractMessage;
  9. use TYPO3\CMS\Core\Utility\GeneralUtility;
  10. use TYPO3\CMS\Extbase\Http\ForwardResponse;
  11. use TYPO3\CMS\Extbase\Mvc\Exception\StopActionException;
  12. use TYPO3\CMS\Extbase\Object\ObjectManager;
  13. /**
  14. * Controller for the OtStats backend submodule
  15. *
  16. * @author olivier.massot
  17. */
  18. class OtStatsController extends SelectedSiteController {
  19. public function __construct(
  20. private readonly MatomoWebsiteRepository $matomoWebsiteRepository,
  21. ) {}
  22. /**
  23. * Access token of the 'typo3' user in matomo
  24. */
  25. const MATOMO_TOKEN = 'd781ebf1e210bc8ab1e9f4a3e21e9b01';
  26. /**
  27. * Index action (default action)
  28. * Displays the customizer page on the backend
  29. */
  30. public function indexAction(): ResponseInterface {
  31. $matomoId = $this->currentWebsite['matomo_site_id'];
  32. if ($matomoId == null) {
  33. return new ForwardResponse('askForActivationConfirmation');
  34. }
  35. $this->view->assign('matomoSiteId', (int)$matomoId);
  36. $this->view->assign('matomoToken', self::MATOMO_TOKEN);
  37. $args = $this->request->getArguments();
  38. // Default interval
  39. $period = $args['period'] ?? 'month';
  40. $this->view->assign('period', $period);
  41. return $this->htmlResponse();
  42. }
  43. /**
  44. * Display a confirmation page before enabling the stats module
  45. */
  46. public function askForActivationConfirmationAction(): ResponseInterface {
  47. return $this->htmlResponse();
  48. }
  49. /**
  50. * Display a confirmation page before disabling the stats module
  51. */
  52. public function askForDeactivationConfirmationAction(): ResponseInterface {
  53. $this->view->assign('rootUid', $this->currentRootUid);
  54. return $this->htmlResponse();
  55. }
  56. /**
  57. * Creates a matomo site record if none exists and
  58. * save its id
  59. */
  60. public function enableStatsAction(): ResponseInterface {
  61. try {
  62. $this->matomoWebsiteRepository->createFor($this->currentRootUid);
  63. } catch (\RuntimeException $e) {
  64. OtLogger::error("OtStats - " . $e);
  65. $this->addFlashMessage(
  66. $this->getLanguageService()->sL(
  67. 'LLL:EXT:ot_core/Resources/Private/Language/locallang.xlf:an_error_occured'
  68. ),
  69. '',
  70. AbstractMessage::ERROR
  71. );
  72. return new ForwardResponse('askForActivationConfirmation');
  73. }
  74. return new ForwardResponse('index');
  75. }
  76. /**
  77. * Disable the stats monitoring
  78. *
  79. * @param int $rootUid Pass the rootUid of the site as an argument to confirm of the decision
  80. * of disabling the website. If no rootUid is passed, the user will be redirected to a
  81. * confirmation page.
  82. * @throws StopActionException
  83. */
  84. public function disableStatsAction(int $rootUid): ResponseInterface {
  85. try {
  86. $this->matomoWebsiteRepository->disableFor($rootUid);
  87. } catch (\RuntimeException $e) {
  88. OtLogger::error("OtStats - " . $e);
  89. $this->addFlashMessage(
  90. $this->getLanguageService()->sL(
  91. 'LLL:EXT:ot_core/Resources/Private/Language/locallang.xlf:an_error_occured'
  92. ),
  93. '',
  94. AbstractMessage::ERROR
  95. );
  96. }
  97. return new ForwardResponse('askForActivationConfirmation');
  98. }
  99. }