| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- <?php
- namespace Opentalent\OtStats\Controller;
- use Opentalent\OtCore\Exception\NoSiteSelected;
- use Opentalent\OtStats\Domain\Repository\MatomoWebsiteRepository;
- use Opentalent\OtStats\Settings\StatsSettingsRepository;
- use Opentalent\OtCore\Page\OtPageRepository;
- use TYPO3\CMS\Core\Utility\GeneralUtility;
- use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
- use TYPO3\CMS\Extbase\Mvc\Exception\StopActionException;
- /**
- * Controller for the OtStats backend submodule
- *
- * @author olivier.massot
- */
- class OtStatsController extends ActionController {
- /**
- * Index action (default action)
- * Displays the customizer page on the backend
- */
- public function indexAction() {
- // Get the current page uid
- try {
- $rootUid = $this->getCurrentRootUidOrAbort();
- } catch (NoSiteSelected $e) {
- $this->view->assign('pageSelected', 0);
- return;
- }
- $this->view->assign('pageSelected', 1);
- $this->view->assign('rootPage', $rootUid);
- $statsSettingsRepository = GeneralUtility::makeInstance(StatsSettingsRepository::class);
- $matomoId = $statsSettingsRepository->getMatomoSiteId($rootUid);
- $statsActivated = ($matomoId !== null);
- $this->view->assign('statsActivated', (int)$statsActivated);
- if (!$statsActivated) {
- return;
- }
- $this->view->assign('matomoSiteId', (int)$matomoId);
- }
- /**
- * Creates a matomo site record if none exists and
- * save its id
- */
- public function enableStatsAction() {
- // Get the current page uid
- try {
- $rootUid = $this->getCurrentRootUidOrAbort();
- } catch (NoSiteSelected $e) {
- $this->forward('index');
- }
- $matomoRepository = GeneralUtility::makeInstance(MatomoWebsiteRepository::class);
- try {
- $matomoRepository->createFor($rootUid);
- } catch (\RuntimeException $e) {
- $this->view->assign(
- 'errorMsg',
- "Une erreur s'est produite lors de l'opération, veuillez contacter un administrateur"
- );
- }
- $this->forward('index');
- }
- /**
- * Disable the stats monitoring
- *
- * @param int $rootUid Pass the rootUid of the site as an argument to confirm of the decision
- * of disabling the website. If no rootUid is passed, the user will be redirected to a
- * confirmation page.
- * @throws StopActionException
- */
- public function disableStatsAction(int $rootUid) {
- $matomoRepository = GeneralUtility::makeInstance(MatomoWebsiteRepository::class);
- try {
- $matomoRepository->disableFor($rootUid);
- } catch (\RuntimeException $e) {
- $this->view->assign(
- 'errorMsg',
- "Une erreur s'est produite lors de l'opération, veuillez contacter un administrateur"
- );
- }
- $this->forward('index');
- }
- /**
- * Display a confirmation page
- */
- public function confirmDeletionAction() {
- try {
- $rootUid = $this->getCurrentRootUidOrAbort();
- } catch (NoSiteSelected $e) {
- $this->forward('index');
- }
- $this->view->assign('rootUid', $rootUid);
- }
- /**
- * Return the root uid of the currently selected website if any,
- * or throw a NoSiteSelected exception
- *
- * @return int|mixed
- * @throws NoSiteSelected
- */
- private function getCurrentRootUidOrAbort() {
- $rootUid = (int)GeneralUtility::_GP('id');
- $otPageRepository = GeneralUtility::makeInstance(OtPageRepository::class);
- $rootPage = $otPageRepository->getRootPageFor($rootUid);
- $rootUid = $rootPage['uid'] ?? 0;
- if (!$rootUid > 0) {
- throw new NoSiteSelected();
- }
- return $rootUid;
- }
- }
|