OtStatsController.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. $args = $this->request->getArguments();
  36. // Default interval
  37. $period = isset($args['period']) ? $args['period'] : 'month';
  38. $this->view->assign('period', $period);
  39. // <-- Shall be removed when a fix is released for https://github.com/matomo-org/matomo/issues/16867
  40. $matomoRepository = GeneralUtility::makeInstance(MatomoWebsiteRepository::class);
  41. $matomoRepository->clearBruteforceLog();
  42. // -->
  43. }
  44. /**
  45. * Display a confirmation page before enabling the stats module
  46. */
  47. public function askForActivationConfirmationAction() {}
  48. /**
  49. * Display a confirmation page before disabling the stats module
  50. */
  51. public function askForDeactivationConfirmationAction() {
  52. $this->view->assign('rootUid', $this->currentRootUid);
  53. }
  54. /**
  55. * Creates a matomo site record if none exists and
  56. * save its id
  57. */
  58. public function enableStatsAction() {
  59. $matomoRepository = GeneralUtility::makeInstance(MatomoWebsiteRepository::class);
  60. try {
  61. $matomoRepository->createFor($this->currentRootUid);
  62. } catch (\RuntimeException $e) {
  63. OtLogger::error("OtStats - " . $e);
  64. $this->addFlashMessage(
  65. "Une erreur s'est produite lors de l'opération, veuillez contacter un administrateur.",
  66. '',
  67. AbstractMessage::ERROR
  68. );
  69. $this->forward('askForActivationConfirmation');
  70. }
  71. $this->forward('index');
  72. }
  73. /**
  74. * Disable the stats monitoring
  75. *
  76. * @param int $rootUid Pass the rootUid of the site as an argument to confirm of the decision
  77. * of disabling the website. If no rootUid is passed, the user will be redirected to a
  78. * confirmation page.
  79. * @throws StopActionException
  80. */
  81. public function disableStatsAction(int $rootUid) {
  82. $matomoRepository = GeneralUtility::makeInstance(MatomoWebsiteRepository::class);
  83. try {
  84. $matomoRepository->disableFor($rootUid);
  85. } catch (\RuntimeException $e) {
  86. OtLogger::error("OtStats - " . $e);
  87. $this->addFlashMessage(
  88. "Une erreur s'est produite lors de l'opération, veuillez contacter un administrateur.",
  89. '',
  90. AbstractMessage::ERROR
  91. );
  92. }
  93. $this->forward('askForActivationConfirmation');
  94. }
  95. }