OtStatsController.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. namespace Opentalent\OtStats\Controller;
  3. use Opentalent\OtCore\Cache\OtCacheManager;
  4. use Opentalent\OtCore\Controller\SelectedSiteController;
  5. use Opentalent\OtCore\Logging\OtLogger;
  6. use Opentalent\OtCore\Messaging\FlashMessageService;
  7. use Opentalent\OtStats\Domain\Repository\MatomoWebsiteRepository;
  8. use Opentalent\OtStats\Settings\StatsSettingsRepository;
  9. use TYPO3\CMS\Core\Messaging\AbstractMessage;
  10. use TYPO3\CMS\Core\Utility\GeneralUtility;
  11. use TYPO3\CMS\Extbase\Mvc\Exception\StopActionException;
  12. /**
  13. * Controller for the OtStats backend submodule
  14. *
  15. * @author olivier.massot
  16. */
  17. class OtStatsController extends SelectedSiteController {
  18. /**
  19. * Access token of the 'typo3' user in matomo
  20. */
  21. const MATOMO_TOKEN = 'd781ebf1e210bc8ab1e9f4a3e21e9b01';
  22. /**
  23. * Index action (default action)
  24. * Displays the customizer page on the backend
  25. */
  26. public function indexAction() {
  27. $this->view->assign('rootPage', $this->currentRootUid);
  28. $statsSettingsRepository = GeneralUtility::makeInstance(StatsSettingsRepository::class);
  29. $matomoId = $statsSettingsRepository->getMatomoSiteId($this->currentRootUid);
  30. if ($matomoId == null) {
  31. $this->forward('askForActivationConfirmation');
  32. }
  33. $this->view->assign('matomoSiteId', (int)$matomoId);
  34. $this->view->assign('matomoToken', self::MATOMO_TOKEN);
  35. // Default interval
  36. $date_end = new \DateTime('today');
  37. $date_start = clone $date_end;
  38. $date_start->sub(new \DateInterval('P1M'));
  39. $this->view->assign('date_start', $date_start);
  40. $this->view->assign('date_end', $date_end);
  41. // <-- Shall be removed when a fix is released for https://github.com/matomo-org/matomo/issues/16867
  42. $matomoRepository = GeneralUtility::makeInstance(MatomoWebsiteRepository::class);
  43. $matomoRepository->clearBruteforceLog();
  44. // -->
  45. }
  46. /**
  47. * Display a confirmation page before enabling the stats module
  48. */
  49. public function askForActivationConfirmationAction() {}
  50. /**
  51. * Display a confirmation page before disabling the stats module
  52. */
  53. public function askForDeactivationConfirmationAction() {
  54. $this->view->assign('rootUid', $this->currentRootUid);
  55. }
  56. /**
  57. * Creates a matomo site record if none exists and
  58. * save its id
  59. */
  60. public function enableStatsAction() {
  61. $matomoRepository = GeneralUtility::makeInstance(MatomoWebsiteRepository::class);
  62. try {
  63. $matomoRepository->createFor($this->currentRootUid);
  64. } catch (\RuntimeException $e) {
  65. OtLogger::error("OtStats - " . $e);
  66. $this->addFlashMessage(
  67. "Une erreur s'est produite lors de l'opération, veuillez contacter un administrateur.",
  68. '',
  69. AbstractMessage::ERROR
  70. );
  71. $this->forward('askForActivationConfirmation');
  72. }
  73. $this->forward('index');
  74. }
  75. /**
  76. * Disable the stats monitoring
  77. *
  78. * @param int $rootUid Pass the rootUid of the site as an argument to confirm of the decision
  79. * of disabling the website. If no rootUid is passed, the user will be redirected to a
  80. * confirmation page.
  81. * @throws StopActionException
  82. */
  83. public function disableStatsAction(int $rootUid) {
  84. $matomoRepository = GeneralUtility::makeInstance(MatomoWebsiteRepository::class);
  85. try {
  86. $matomoRepository->disableFor($rootUid);
  87. } catch (\RuntimeException $e) {
  88. OtLogger::error("OtStats - " . $e);
  89. $this->addFlashMessage(
  90. "Une erreur s'est produite lors de l'opération, veuillez contacter un administrateur.",
  91. '',
  92. AbstractMessage::ERROR
  93. );
  94. }
  95. $this->forward('askForActivationConfirmation');
  96. }
  97. }