OtStatsController.php 3.7 KB

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