OtStatsController.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. namespace Opentalent\OtStats\Controller;
  3. use Opentalent\OtCore\Exception\NoSiteSelected;
  4. use Opentalent\OtStats\Domain\Repository\MatomoWebsiteRepository;
  5. use Opentalent\OtStats\Settings\StatsSettingsRepository;
  6. use Opentalent\OtCore\Page\OtPageRepository;
  7. use TYPO3\CMS\Core\Utility\GeneralUtility;
  8. use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
  9. use TYPO3\CMS\Extbase\Mvc\Exception\StopActionException;
  10. /**
  11. * Controller for the OtStats backend submodule
  12. *
  13. * @author olivier.massot
  14. */
  15. class OtStatsController extends ActionController {
  16. /**
  17. * Index action (default action)
  18. * Displays the customizer page on the backend
  19. */
  20. public function indexAction() {
  21. // Get the current page uid
  22. try {
  23. $rootUid = $this->getCurrentRootUidOrAbort();
  24. } catch (NoSiteSelected $e) {
  25. $this->view->assign('pageSelected', 0);
  26. return;
  27. }
  28. $this->view->assign('pageSelected', 1);
  29. $this->view->assign('rootPage', $rootUid);
  30. $statsSettingsRepository = GeneralUtility::makeInstance(StatsSettingsRepository::class);
  31. $matomoId = $statsSettingsRepository->getMatomoSiteId($rootUid);
  32. $statsActivated = ($matomoId !== null);
  33. $this->view->assign('statsActivated', (int)$statsActivated);
  34. if (!$statsActivated) {
  35. return;
  36. }
  37. $this->view->assign('matomoSiteId', (int)$matomoId);
  38. }
  39. /**
  40. * Creates a matomo site record if none exists and
  41. * save its id
  42. */
  43. public function enableStatsAction() {
  44. // Get the current page uid
  45. try {
  46. $rootUid = $this->getCurrentRootUidOrAbort();
  47. } catch (NoSiteSelected $e) {
  48. $this->forward('index');
  49. }
  50. $matomoRepository = GeneralUtility::makeInstance(MatomoWebsiteRepository::class);
  51. try {
  52. $matomoRepository->createFor($rootUid);
  53. } catch (\RuntimeException $e) {
  54. $this->view->assign(
  55. 'errorMsg',
  56. "Une erreur s'est produite lors de l'opération, veuillez contacter un administrateur"
  57. );
  58. }
  59. $this->forward('index');
  60. }
  61. /**
  62. * Disable the stats monitoring
  63. *
  64. * @param int $rootUid Pass the rootUid of the site as an argument to confirm of the decision
  65. * of disabling the website. If no rootUid is passed, the user will be redirected to a
  66. * confirmation page.
  67. * @throws StopActionException
  68. */
  69. public function disableStatsAction(int $rootUid) {
  70. $matomoRepository = GeneralUtility::makeInstance(MatomoWebsiteRepository::class);
  71. try {
  72. $matomoRepository->disableFor($rootUid);
  73. } catch (\RuntimeException $e) {
  74. $this->view->assign(
  75. 'errorMsg',
  76. "Une erreur s'est produite lors de l'opération, veuillez contacter un administrateur"
  77. );
  78. }
  79. $this->forward('index');
  80. }
  81. /**
  82. * Display a confirmation page
  83. */
  84. public function confirmDeletionAction() {
  85. try {
  86. $rootUid = $this->getCurrentRootUidOrAbort();
  87. } catch (NoSiteSelected $e) {
  88. $this->forward('index');
  89. }
  90. $this->view->assign('rootUid', $rootUid);
  91. }
  92. /**
  93. * Return the root uid of the currently selected website if any,
  94. * or throw a NoSiteSelected exception
  95. *
  96. * @return int|mixed
  97. * @throws NoSiteSelected
  98. */
  99. private function getCurrentRootUidOrAbort() {
  100. $rootUid = (int)GeneralUtility::_GP('id');
  101. $otPageRepository = GeneralUtility::makeInstance(OtPageRepository::class);
  102. $rootPage = $otPageRepository->getRootPageFor($rootUid);
  103. $rootUid = $rootPage['uid'] ?? 0;
  104. if (!$rootUid > 0) {
  105. throw new NoSiteSelected();
  106. }
  107. return $rootUid;
  108. }
  109. }