OtStatsController.php 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. namespace Opentalent\OtStats\Controller;
  3. use Opentalent\OtCore\Cache\OtCacheManager;
  4. use Opentalent\OtCore\Controller\SelectedSiteController;
  5. use Opentalent\OtCore\Logging\OtLogger;
  6. use Opentalent\OtCore\Messaging\FlashMessageService;
  7. use Opentalent\OtStats\Domain\Repository\MatomoWebsiteRepository;
  8. use Opentalent\OtStats\Settings\StatsSettingsRepository;
  9. use TYPO3\CMS\Core\Messaging\AbstractMessage;
  10. use TYPO3\CMS\Core\Utility\GeneralUtility;
  11. use TYPO3\CMS\Extbase\Mvc\Exception\StopActionException;
  12. /**
  13. * Controller for the OtStats backend submodule
  14. *
  15. * @author olivier.massot
  16. */
  17. class OtStatsController extends SelectedSiteController {
  18. /**
  19. * Index action (default action)
  20. * Displays the customizer page on the backend
  21. */
  22. public function indexAction() {
  23. $this->view->assign('rootPage', $this->currentRootUid);
  24. $statsSettingsRepository = GeneralUtility::makeInstance(StatsSettingsRepository::class);
  25. $matomoId = $statsSettingsRepository->getMatomoSiteId($this->currentRootUid);
  26. if ($matomoId == null) {
  27. $this->forward('askForActivationConfirmation');
  28. }
  29. $this->view->assign('matomoSiteId', (int)$matomoId);
  30. }
  31. /**
  32. * Display a confirmation page before enabling the stats module
  33. */
  34. public function askForActivationConfirmationAction() {}
  35. /**
  36. * Display a confirmation page before disabling the stats module
  37. */
  38. public function askForDeactivationConfirmationAction() {
  39. $this->view->assign('rootUid', $this->currentRootUid);
  40. }
  41. /**
  42. * Creates a matomo site record if none exists and
  43. * save its id
  44. */
  45. public function enableStatsAction() {
  46. $matomoRepository = GeneralUtility::makeInstance(MatomoWebsiteRepository::class);
  47. try {
  48. $matomoRepository->createFor($this->currentRootUid);
  49. } catch (\RuntimeException $e) {
  50. OtLogger::error("OtStats - " . $e);
  51. $this->addFlashMessage(
  52. "Une erreur s'est produite lors de l'opération, veuillez contacter un administrateur.",
  53. '',
  54. AbstractMessage::ERROR
  55. );
  56. $this->forward('askForActivationConfirmation');
  57. }
  58. $this->forward('index');
  59. }
  60. /**
  61. * Disable the stats monitoring
  62. *
  63. * @param int $rootUid Pass the rootUid of the site as an argument to confirm of the decision
  64. * of disabling the website. If no rootUid is passed, the user will be redirected to a
  65. * confirmation page.
  66. * @throws StopActionException
  67. */
  68. public function disableStatsAction(int $rootUid) {
  69. $matomoRepository = GeneralUtility::makeInstance(MatomoWebsiteRepository::class);
  70. try {
  71. $matomoRepository->disableFor($rootUid);
  72. } catch (\RuntimeException $e) {
  73. OtLogger::error("OtStats - " . $e);
  74. $this->addFlashMessage(
  75. "Une erreur s'est produite lors de l'opération, veuillez contacter un administrateur.",
  76. '',
  77. AbstractMessage::ERROR
  78. );
  79. }
  80. $this->forward('askForActivationConfirmation');
  81. }
  82. }