* @owner University of Latvia * @version 2.0.0 * @since 18.07.2018 * * @package Lu\LuNabamusic\Controller */ class VotesController extends ActionController { /** * @var SongRepository */ private $songRepository; /** * Backend Template Container * * @var string */ protected $defaultViewObjectName = \TYPO3\CMS\Backend\View\BackendTemplateView::class; /** * Backend-Session * * @var \Lu\LuNabamusic\Domain\Session\BackendSessionHandler */ protected $backendSession; /** * Frontend-Session * * @var \Lu\LuNabamusic\Domain\Session\FrontendSessionHandler */ protected $frontendSession; /** * Frontend- or Backend-Session * The type of session is set automatically in initializeAction(). * * @var \Lu\LuNabamusic\Domain\Session\SessionHandler */ protected $session; /** * Current page ID * * @var int */ protected $pageUid; /** * Plugin configuration settings * * @var array */ private $conf; /** * MusicController constructor. * * Assign page ID */ public function __construct() { if (!$this->pageUid) { $this->pageUid = $_REQUEST['id'] ?? $GLOBALS['TSFE']->id; } $this->conf = GeneralUtility::makeInstance(ExtensionConfiguration::class)->get('lu_nabamusic'); } /** * Set up the doc header properly here * * @param ViewInterface $view * * @return void */ protected function initializeView(ViewInterface $view) { parent::initializeView($view); if ($view instanceof BackendTemplateView) { $view->getModuleTemplate()->getPageRenderer()->loadRequireJsModule('TYPO3/CMS/Backend/Modal'); } } /** * Inject song repository * * @param \Lu\LuNabamusic\Domain\Repository\SongRepository $songRepository */ public function injectSongRepository(SongRepository $songRepository) { $this->songRepository = $songRepository; } /** * Increase votes */ public function increaseAction() { // Gather request data $song_id = $this->request->hasArgument('song_id') ? $this->request->getArgument('song_id') : null; $isAdmin = $this->request->hasArgument('admin') && $this->request->getArgument('admin') == 1 ? true : false; $timePeriod = $this->request->hasArgument('time_period') ? $this->request->getArgument('time_period') : time(); // Check if song Id provided if ($song_id) { $VoteModel = new Vote(); $Datahelper = new DataHelper(); $song_pid = $this->songRepository->findByUid($song_id)->getPid(); // Check if admin increase request if ($isAdmin) { // @TODO: For Config value! $ip = []; for ($k = 0; $k < $this->conf['ipIncreaseCount']; $k++) { $ip[] = $Datahelper->getRandomIP(); } } else { $ip = [$Datahelper->getClientIp()]; } // Add votes foreach ($ip as $ip_val) { $VoteModel->setPageId($song_pid); $VoteModel->setVoterIP($ip_val); $VoteModel->setSongId($song_id); $VoteModel->setAdmin($isAdmin); $VoteModel->setTimePeriod($timePeriod); $VoteModel->setMaxVotes($this->settings['maxVotesPerIP']); $VoteModel->save(); } } // Redirect back to listing if ($isAdmin) { $this->redirectToModule(); } else { // Redirect back to FE page $RedirectPageId = empty($this->settings['afterVoteContent']) ? $this->pageUid : $this->settings['afterVoteContent']; $returnUrl = $this->controllerContext->getUriBuilder()->setCreateAbsoluteUri(true)->reset() ->setTargetPageUid($RedirectPageId)->buildFrontendUri(); HttpUtility::redirect($returnUrl); } } /** * Decrease votes */ public function decreaseAction() { // Gather request data $song_id = $this->request->hasArgument('song_id') ? $this->request->getArgument('song_id') : null; $isAdmin = $this->request->hasArgument('admin') && $this->request->getArgument('admin') == 1 ? true : false; $timePeriod = $this->request->hasArgument('time_period') ? $this->request->getArgument('time_period') : 0; // Check if song Id provided and is admin. Only admin can perform this action if ($song_id && $isAdmin) { $VoteModel = new Vote(); $VoteModel->setSongId($song_id); $VoteModel->setTimePeriod($timePeriod); $VoteModel->setLimit($this->conf['ipDecreaseCount']); $VoteModel->delete(); } // Redirect back $this->redirectToModule(); } /** * Redirects back to module */ private function redirectToModule() { $routeBuilder = GeneralUtility::makeInstance(BackendUriBuilder::class); $returnUrl = $routeBuilder->buildUriFromRoutePath('/web/LuNabamusicMusic/', [ 'id'=> $this->pageUid, 'token' => $this->getToken(true) ]); HttpUtility::redirect($returnUrl); } /** * Returns the LanguageService * * @return LanguageService */ protected function getLanguageService() { return $GLOBALS['LANG'] ?? LanguageService::createFromUserPreferences($GLOBALS['BE_USER']); } /** * Get a CSRF token * * @param bool $tokenOnly Set it to TRUE to get only the token, otherwise including the &token= as prefix * * @return string */ protected function getToken($tokenOnly = false) { $token = FormProtectionFactory::get('backend')->generateToken('route', 'web_LuNabamusicMusic'); if ($tokenOnly) { return $token; } else { return '&token='.$token; } } }