OtStatsController.php 3.6 KB

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