OtStatsController.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. $this->getLanguageService()->sL(
  61. 'LLL:EXT:ot_core/Resources/Private/Language/locallang.xlf:an_error_occured'
  62. ),
  63. '',
  64. AbstractMessage::ERROR
  65. );
  66. $this->forward('askForActivationConfirmation');
  67. }
  68. $this->forward('index');
  69. }
  70. /**
  71. * Disable the stats monitoring
  72. *
  73. * @param int $rootUid Pass the rootUid of the site as an argument to confirm of the decision
  74. * of disabling the website. If no rootUid is passed, the user will be redirected to a
  75. * confirmation page.
  76. * @throws StopActionException
  77. */
  78. public function disableStatsAction(int $rootUid) {
  79. $matomoRepository = GeneralUtility::makeInstance(ObjectManager::class)->get(MatomoWebsiteRepository::class);
  80. try {
  81. $matomoRepository->disableFor($rootUid);
  82. } catch (\RuntimeException $e) {
  83. OtLogger::error("OtStats - " . $e);
  84. $this->addFlashMessage(
  85. $this->getLanguageService()->sL(
  86. 'LLL:EXT:ot_core/Resources/Private/Language/locallang.xlf:an_error_occured'
  87. ),
  88. '',
  89. AbstractMessage::ERROR
  90. );
  91. }
  92. $this->forward('askForActivationConfirmation');
  93. }
  94. }