OtStatsController.php 3.5 KB

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