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

namespace Lu\LuNabamusic\Controller;

use Lu\LuNabamusic\Domain\Model\Play;
use Lu\LuNabamusic\Domain\Model\Top;
use Lu\LuNabamusic\Domain\Repository\SongRepository;
8
use Lu\LuNabamusic\Helpers\CacheHelper;
Dainis Abols's avatar
Dainis Abols committed
9
10
11
12
13
use TYPO3\CMS\Core\Configuration\ExtensionConfiguration;
use TYPO3\CMS\Core\FormProtection\FormProtectionFactory;
use TYPO3\CMS\Core\Page\PageRenderer;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Mvc\Controller\ActionController;
Dainis Abols's avatar
Dainis Abols committed
14
use TYPO3\CMS\Core\Localization\LanguageService;
Dainis Abols's avatar
Dainis Abols committed
15
16
17
18
19
20
21
22
23
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

/**
 * Class FrontController
 *
 * @author  Dainis Abols <dainis.abols@lu.lv>
 * @owner   University of Latvia
 * @version 2.0.0
 * @since   20.07.2018
 *
 * @package Lu\LuNabamusic\Controller
 */
class FrontController extends ActionController
{
    /**
     * @var SongRepository
     */
    private $songRepository;

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

        // Fetch config
        $this->conf = GeneralUtility::makeInstance(ExtensionConfiguration::class)->get('lu_nabamusic');
    }

    /**
     * Inject the product repository
     *
     * @param \Lu\LuNabamusic\Domain\Repository\SongRepository $songRepository
     */
    public function injectProductRepository(SongRepository $songRepository)
    {
        $this->songRepository = $songRepository;
    }

    /**
     * List Action
     *
     * @return void
     */
    public function listAction()
    {
        // Preset used variables
        $playModel  = new Play();
        $swap = $this->settings['pluginType'] ?? '';
97
        $cacheHelper = new CacheHelper();
Dainis Abols's avatar
Dainis Abols committed
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
139
140
141
142
143
144
145
146
147
148
149
150
        $data = [];

        // Swap view types @TODO Make data gather to methods!
        switch ($swap) {
            default:
            case "frontpageTop":
                $data = $this->getTopSongs();

                $this->setTemplateUrl('Front/MiniList.html');
                break;

            case "extendedTop":
                $data = $this->getTopSongs();

                // Add CSS and JavaScript files
                $pageRenderer = GeneralUtility::makeInstance(PageRenderer::class);
                $pageRenderer->addCssFile("/typo3conf/ext/lu_nabamusic/Resources/Public/Css/tinyPlayer.css");
                $pageRenderer->addCssFile("https://netdna.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css");

                $pageRenderer->addJsFooterFile("/typo3conf/ext/lu_nabamusic/Resources/Public/JavaScript/scripts.js");
                $pageRenderer->addJsFooterFile("/typo3conf/ext/lu_nabamusic/Resources/Public/JavaScript/howler.min.js");
                $pageRenderer->addJsFooterFile("/typo3conf/ext/lu_nabamusic/Resources/Public/JavaScript/tinyPlayer.js");
                break;

            case "frontpageLive":
                // Check to small song list, if voting not allowed
                $data = [
                    'programme' => $playModel->getCurrentProgramme(),
                    'songString' => $playModel->getCurrentSong(true),
                ];

                // Add scripts for correct frontend remndering
                $pageRenderer = GeneralUtility::makeInstance(PageRenderer::class);
                $pageRenderer->addJsFooterFile("/typo3conf/ext/lu_nabamusic/Resources/Public/JavaScript/jquery-3.4.1.min.js");
                $pageRenderer->addJsFooterFile("/typo3conf/ext/lu_nabamusic/Resources/Public/JavaScript/jquery.hammer.js");
                $pageRenderer->addJsFooterFile("/typo3conf/ext/lu_nabamusic/Resources/Public/JavaScript/lunabamusic.js");

                // Set custom view
                $this->setTemplateUrl('Front/FrontLive.html');
                break;

            case "monthSearch":
                $args = $this->request->getArguments();
                $pickedMonth = $args['month'];
                $limit = $this->settings['songsInTop'] ? $this->settings['songsInTop'] : 50;

                if ($pickedMonth) {
                    $data['mostPlayedSongs'] = [
                        'songs' => $playModel->getMostPlayedSongs($limit, $pickedMonth),
                        'month' => $pickedMonth
                    ];
                } else {
                    $data['mostPlayedSongs'] = [
151
                        'songs' => $cacheHelper->get('most_played_songs') ?: $playModel->getMostPlayedSongs($limit),
Dainis Abols's avatar
Dainis Abols committed
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
                    ];
                }

                $this->setTemplateUrl('Front/SongHistoryMonth.html');

                $this->request->setArguments([]);

                $pageRenderer = GeneralUtility::makeInstance(PageRenderer::class);
                $pageRenderer->addCssFile("https://netdna.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css");
                $pageRenderer->addJsFooterFile("/typo3conf/ext/lu_nabamusic/Resources/Public/JavaScript/bootstrap-datepicker.min.js");
                $pageRenderer->addJsFooterFile("/typo3conf/ext/lu_nabamusic/Resources/Public/JavaScript/bootstrap-datepicker.lv.min.js");
                $pageRenderer->addCssFile("/typo3conf/ext/lu_nabamusic/Resources/Public/Css/bootstrap-datepicker3.min.css");
                $pageRenderer->addJsFooterFile("/typo3conf/ext/lu_nabamusic/Resources/Public/JavaScript/datepicker-setup.js");
                break;

            case "search":
                $args = $this->request->getArguments();
                $pickedDay = $args['date'];
                $query = $args['name'];

                // If date or query was passed in search for them
                // Otherwise get songs played on current day
                if ($pickedDay || $query) {
                    $data['mostPlayedSongs'] = [
                        'songs' => $playModel->searchSongs($pickedDay, $query),
                        'date'  => $pickedDay,
                        'name'  => $query
                    ];
                } else {
                    $data['mostPlayedSongs'] = [
182
                        'songs' => $cacheHelper->get('todays_songs') ?: $playModel->getTodaysSongs(),
Dainis Abols's avatar
Dainis Abols committed
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
                    ];
                }

                $this->setTemplateUrl('Front/SongSearch.html');

                $this->request->setArguments([]);

                $pageRenderer = GeneralUtility::makeInstance(PageRenderer::class);
                $pageRenderer->addCssFile("https://netdna.bootstrapcdn.com/font-awesome/4.7.0/css/font-awesome.min.css");
                $pageRenderer->addJsFooterFile("/typo3conf/ext/lu_nabamusic/Resources/Public/JavaScript/bootstrap-datepicker.min.js");
                $pageRenderer->addJsFooterFile("/typo3conf/ext/lu_nabamusic/Resources/Public/JavaScript/bootstrap-datepicker.lv.min.js");
                $pageRenderer->addCssFile("/typo3conf/ext/lu_nabamusic/Resources/Public/Css/bootstrap-datepicker3.min.css");
                $pageRenderer->addJsFooterFile("/typo3conf/ext/lu_nabamusic/Resources/Public/JavaScript/datepicker-setup.js");
                break;
        }

        // Assign data to view
        $this->view->assign('data', $data);
    }

    /**
     * Get songs from cache
     *
     * @return mixed
     */
    private function getTopSongs()
    {
210
211
        $cacheHelper = new CacheHelper();
        $data = $cacheHelper->get('naba_top_'.$this->settings['songsInTop']);
Dainis Abols's avatar
Dainis Abols committed
212
213
214
215
216
        $UriBuilder = $this->controllerContext->getUriBuilder()->reset()->setCreateAbsoluteUri(true);

        // If songs list is not cached do it now
        if(!$data) {
            GeneralUtility::makeInstance(Top::class)->executeCron();
217
            $data = $cacheHelper->get('naba_top_'.$this->settings['songsInTop']);
Dainis Abols's avatar
Dainis Abols committed
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
        }

        foreach ($data['songs'] as $key => $item) {
            // Create voting url
            $data['songs'][$key]['votes']['current']['plus'] = $UriBuilder->reset()->setCreateAbsoluteUri(true)->uriFor('increase', ['time_period' => $data['currentDate'], 'song_id' => $item[0], 'admin' => true], 'Votes');
        }

        // Add full list view url
        $data['fullListView'] = !empty($this->settings['fullListView']) ? $this->settings['fullListView'] : 0;

        return $data;
    }

    /**
     * Returns the LanguageService
     *
     * @return LanguageService
     */
    protected function getLanguageService()
    {
Dainis Abols's avatar
Dainis Abols committed
238
        return $GLOBALS['LANG'] ?? LanguageService::createFromUserPreferences($GLOBALS['BE_USER']);
Dainis Abols's avatar
Dainis Abols committed
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
    }

    /**
     * Get a CSRF token
     *
     * @param bool $tokenOnly Set it to TRUE to get only the token, otherwise including the &moduleToken= as prefix
     *
     * @return string
     */
    protected function getToken($tokenOnly = false)
    {
        $token = FormProtectionFactory::get()->generateToken('moduleCall', 'web_LuNabamusicMusic');
        if ($tokenOnly) {
            return $token;
        } else {
            return '&moduleToken='.$token;
        }
    }

    /**
     * Get tempate path with config params and sets the it
     *
     * @param $name
     *
     * @return string
     */
    private function setTemplateUrl($name)
    {
        $extbaseFrameworkConfiguration = $this->configurationManager->getConfiguration(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface::CONFIGURATION_TYPE_FRAMEWORK);
        if (is_array($extbaseFrameworkConfiguration['view']['templateRootPaths'])) {
            for ($k = sizeof($extbaseFrameworkConfiguration['view']['templateRootPaths']); $k--; $k < 0) {
                $templateRootPath =  $extbaseFrameworkConfiguration['view']['templateRootPaths'][$k];
                if (!empty($templateRootPath)) {
                    break;
                }
            }
        }

        $tempaltePath = trim($templateRootPath, ' /') . '/' . $name;
        $this->view->setTemplatePathAndFilename($tempaltePath);
    }
}