OtStatsController.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 Psr\Http\Message\ResponseInterface;
  7. use TYPO3\CMS\Core\Messaging\AbstractMessage;
  8. use TYPO3\CMS\Core\Utility\GeneralUtility;
  9. use TYPO3\CMS\Extbase\Http\ForwardResponse;
  10. use TYPO3\CMS\Extbase\Mvc\Exception\StopActionException;
  11. use TYPO3\CMS\Extbase\Object\ObjectManager;
  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(): ResponseInterface {
  27. $matomoId = $this->currentWebsite['matomo_site_id'];
  28. if ($matomoId == null) {
  29. return new ForwardResponse('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 = $args['period'] ?? 'month';
  36. $this->view->assign('period', $period);
  37. return $this->htmlResponse();
  38. }
  39. /**
  40. * Display a confirmation page before enabling the stats module
  41. */
  42. public function askForActivationConfirmationAction(): ResponseInterface {
  43. return $this->htmlResponse();
  44. }
  45. /**
  46. * Display a confirmation page before disabling the stats module
  47. */
  48. public function askForDeactivationConfirmationAction(): ResponseInterface {
  49. $this->view->assign('rootUid', $this->currentRootUid);
  50. return $this->htmlResponse();
  51. }
  52. /**
  53. * Creates a matomo site record if none exists and
  54. * save its id
  55. */
  56. public function enableStatsAction(): ResponseInterface {
  57. $matomoRepository = GeneralUtility::makeInstance(ObjectManager::class)->get(MatomoWebsiteRepository::class);
  58. try {
  59. $matomoRepository->createFor($this->currentRootUid);
  60. } catch (\RuntimeException $e) {
  61. OtLogger::error("OtStats - " . $e);
  62. $this->addFlashMessage(
  63. $this->getLanguageService()->sL(
  64. 'LLL:EXT:ot_core/Resources/Private/Language/locallang.xlf:an_error_occured'
  65. ),
  66. '',
  67. AbstractMessage::ERROR
  68. );
  69. return new ForwardResponse('askForActivationConfirmation');
  70. }
  71. return new ForwardResponse('index');
  72. }
  73. /**
  74. * Disable the stats monitoring
  75. *
  76. * @param int $rootUid Pass the rootUid of the site as an argument to confirm of the decision
  77. * of disabling the website. If no rootUid is passed, the user will be redirected to a
  78. * confirmation page.
  79. * @throws StopActionException
  80. */
  81. public function disableStatsAction(int $rootUid): ResponseInterface {
  82. $matomoRepository = GeneralUtility::makeInstance(ObjectManager::class)->get(MatomoWebsiteRepository::class);
  83. try {
  84. $matomoRepository->disableFor($rootUid);
  85. } catch (\RuntimeException $e) {
  86. OtLogger::error("OtStats - " . $e);
  87. $this->addFlashMessage(
  88. $this->getLanguageService()->sL(
  89. 'LLL:EXT:ot_core/Resources/Private/Language/locallang.xlf:an_error_occured'
  90. ),
  91. '',
  92. AbstractMessage::ERROR
  93. );
  94. }
  95. return new ForwardResponse('askForActivationConfirmation');
  96. }
  97. }