VotesController.php 7.08 KB
Newer Older
Dainis Abols's avatar
Dainis Abols committed
1
2
3
4
5
6
7
8
<?php

namespace Lu\LuNabamusic\Controller;

use Lu\LuNabamusic\Domain\Model\Song;
use Lu\LuNabamusic\Domain\Model\Vote;
use Lu\LuNabamusic\Domain\Repository\SongRepository;
use Lu\LuNabamusic\Helpers\DataHelper;
Dainis Abols's avatar
Dainis Abols committed
9
use TYPO3\CMS\Backend\Routing\UriBuilder as BackendUriBuilder;
Dainis Abols's avatar
Dainis Abols committed
10
11
12
13
14
15
16
17
18
19
20
21
22
use TYPO3\CMS\Backend\Template\Components\ButtonBar;
use TYPO3\CMS\Backend\Utility\BackendUtility as BackendUtilityCore;
use TYPO3\CMS\Backend\View\BackendTemplateView;
use TYPO3\CMS\Core\Configuration\ExtensionConfiguration;
use TYPO3\CMS\Core\FormProtection\FormProtectionFactory;
use TYPO3\CMS\Core\Imaging\Icon;
use TYPO3\CMS\Core\Imaging\IconFactory;
use TYPO3\CMS\Core\Page\PageRenderer;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Utility\HttpUtility;
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
use TYPO3\CMS\Extbase\Mvc\View\ViewInterface;
use TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder;
Dainis Abols's avatar
Dainis Abols committed
23
use TYPO3\CMS\Core\Localization\LanguageService;
Dainis Abols's avatar
Dainis Abols committed
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138

/**
 * Class MusicController
 *
 * @author  Dainis Abols <dainis.abols@lu.lv>
 * @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();
Dainis Abols's avatar
Dainis Abols committed
139
            $song_pid   = $this->songRepository->findByUid($song_id)->getPid();
Dainis Abols's avatar
Dainis Abols committed
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165

            // 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) {
Dainis Abols's avatar
Dainis Abols committed
166
            $this->redirectToModule();
Dainis Abols's avatar
Dainis Abols committed
167
168
        } else {
            // Redirect back to FE page
Dainis Abols's avatar
Dainis Abols committed
169
            $RedirectPageId = empty($this->settings['afterVoteContent']) ? $this->pageUid : $this->settings['afterVoteContent'];
Dainis Abols's avatar
Dainis Abols committed
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
            $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();
        }

Dainis Abols's avatar
Dainis Abols committed
195
196
197
198
199
200
201
202
203
204
205
206
207
208
        // 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)
        ]);
Dainis Abols's avatar
Dainis Abols committed
209
210
211
212
213
214
215
216
217
218
        HttpUtility::redirect($returnUrl);
    }

    /**
     * Returns the LanguageService
     *
     * @return LanguageService
     */
    protected function getLanguageService()
    {
Dainis Abols's avatar
Dainis Abols committed
219
        return $GLOBALS['LANG'] ?? LanguageService::createFromUserPreferences($GLOBALS['BE_USER']);
Dainis Abols's avatar
Dainis Abols committed
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
    }

    /**
     * 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;
        }
    }
}