OtStatsController.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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 TYPO3\CMS\Core\Messaging\AbstractMessage;
  7. use TYPO3\CMS\Core\Utility\GeneralUtility;
  8. use TYPO3\CMS\Extbase\Mvc\Exception\StopActionException;
  9. use TYPO3\CMS\Extbase\Object\ObjectManager;
  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. $matomoId = $this->currentWebsite['matomo_site_id'];
  26. if ($matomoId == null) {
  27. $this->forward('askForActivationConfirmation');
  28. }
  29. $this->view->assign('matomoSiteId', (int)$matomoId);
  30. $this->view->assign('matomoToken', self::MATOMO_TOKEN);
  31. $args = $this->request->getArguments();
  32. // Default interval
  33. $period = $args['period'] ?? 'month';
  34. $this->view->assign('period', $period);
  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(ObjectManager::class)->get(MatomoWebsiteRepository::class);
  52. try {
  53. $matomoRepository->createFor($this->currentRootUid);
  54. } catch (\RuntimeException $e) {
  55. OtLogger::error("OtStats - " . $e);
  56. $this->addFlashMessage(
  57. $this->getLanguageService()->sL(
  58. 'LLL:EXT:ot_core/Resources/Private/Language/locallang.xlf:an_error_occured'
  59. ),
  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(ObjectManager::class)->get(MatomoWebsiteRepository::class);
  77. try {
  78. $matomoRepository->disableFor($rootUid);
  79. } catch (\RuntimeException $e) {
  80. OtLogger::error("OtStats - " . $e);
  81. $this->addFlashMessage(
  82. $this->getLanguageService()->sL(
  83. 'LLL:EXT:ot_core/Resources/Private/Language/locallang.xlf:an_error_occured'
  84. ),
  85. '',
  86. AbstractMessage::ERROR
  87. );
  88. }
  89. $this->forward('askForActivationConfirmation');
  90. }
  91. }