OtStatsController.php 3.4 KB

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