Top.php 5.41 KB
Newer Older
Dainis Abols's avatar
Dainis Abols committed
1
2
3
4
5
<?php

namespace Lu\LuNabamusic\Domain\Model;

use Lu\LuNabamusic\Domain\Repository\SongRepository;
6
use Lu\LuNabamusic\Helpers\CacheHelper;
Dainis Abols's avatar
Dainis Abols committed
7
8
9
10
11
12
13
14
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
97
98
99
100
101
102
103
104
105
106
107
108
use Lu\LuNabamusic\Interfaces\TableView;
use TYPO3\CMS\Core\Cache\CacheManager;
use TYPO3\CMS\Core\Configuration\ExtensionConfiguration;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\DomainObject\AbstractEntity;
use TYPO3\CMS\Extbase\Object\ObjectManager;
use TYPO3\CMS\Extbase\Persistence\Generic\Typo3QuerySettings;

/**
 * Class Top
 *
 * @author  Dainis Abols <dainis.abols@lu.lv>
 * @owner   University of Latvia
 * @version 2.0.0
 * @since   11.07.2018
 *
 * @package Lu\LuNabamusic\Domain\Model
 */
class Top extends AbstractEntity implements TableView
{
    /**
     * Table name
     *
     * @var string
     */
    protected $tableName = 'tx_lunabamusic_domain_model_top';

    /**
     * Front-end group ID
     *
     * @var int
     */
    protected $fe_group;

    /**
     * TOP title name
     *
     * @var string
     */
    protected $title;

    /**
     * Set front-end group ID
     *
     * @param $fe_group
     */
    public function setFEGroup($fe_group)
    {
        $this->fe_group = $fe_group;
    }

    /**
     * Get front-end group ID
     *
     * @return int
     */
    public function getFEGroup()
    {
        return $this->fe_group;
    }

    /**
     * Set TOP title
     *
     * @param $title
     */
    public function setTitle($title)
    {
        $this->title = $title;
    }

    /**
     * Get TOP title
     *
     * @return string
     */
    public function getTitle()
    {
        return $this->title;
    }

    /**
     * Get the table name
     *
     * @return string
     */
    public function getTableName()
    {
        return $this->tableName;
    }

    /**
     * Cache top songs
     *
     * @return bool
     */
    public function executeCron()
    {
        $objectManager = GeneralUtility::makeInstance(ObjectManager::class);
        $songRepository = $objectManager->get(SongRepository::class);
        $querySettings = $objectManager->get(Typo3QuerySettings::class);
        $config = GeneralUtility::makeInstance(ExtensionConfiguration::class)->get('lu_nabamusic');
109
        $cacheHelper = new CacheHelper();
Dainis Abols's avatar
Dainis Abols committed
110
111
112

        $querySettings->setStoragePageIds([$config['top10StorageId']]);
        $songRepository->setDefaultQuerySettings($querySettings);
113
        $cacheHelper->set('naba_top_10', $this->buildSongList($songRepository, 10));
Dainis Abols's avatar
Dainis Abols committed
114
115
116

        $querySettings->setStoragePageIds([$config['top25StorageId']]);
        $songRepository->setDefaultQuerySettings($querySettings);
117
        $cacheHelper->set('naba_top_25', $this->buildSongList($songRepository, 25));
Dainis Abols's avatar
Dainis Abols committed
118
119

        $playModel = new Play();
120
121
        $cacheHelper->set('todays_songs', $playModel->getTodaysSongs());
        $cacheHelper->set('most_played_songs', $playModel->getMostPlayedSongs(50));
Dainis Abols's avatar
Dainis Abols committed
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
151
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
182
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
210

        return true;
    }

    /**
     * @param $songRepository
     * @param $top
     *
     * @return array
     */
    private function buildSongList($songRepository, $top)
    {
        // Preset used variables
        $SongModel  = new Song();
        $sort = 'votesweekago';
        $data = [];

        // Load songs
        $songs = $songRepository->findAll();
        if (count($songs)) {
            foreach ($songs as $item) {
                // Load extra data for songs
                $item->loadAdditionalInfo();

                // Load votes
                $item->loadVotes();
                $votes = $item->getVotes();

                // Load weeks
                $item->loadWeeks();

                // Add detailed info for graphs
                $item->loadDetailInfo();

                // Add top list start date
                $data['currentDate'] = $votes['current']['start']->format('d.m.Y');

                // Assign value to data array
                $data['songs'][] = $item->toArray();
            }

            // Get previous place
            $SongModel->sortArrayByKey($data['songs'], 'votes2weeksago', true, false);
            $k = 0;
            foreach ($data['songs'] as &$song) {
                $k++;
                $song['oldPlace'] = $k > $top ? '0' : $k;
            }

            // Set currently active sort
            if ($sort == 'votescurrent') {
                $data['sort']['current'] = true;
            } else if ($sort == 'votes2weeksago') {
                $data['sort']['2WeeksAgo'] = true;
            } else {
                $data['sort']['weekAgo'] = true;
            }

            // Resort data
            $SongModel->sortArrayByKey($data['songs'], 'artist');
            $SongModel->sortArrayByKey($data['songs'], $sort, true, false);

            // Add additional data
            $k = 0;
            foreach ($data['songs'] as &$song) {
                $k++;
                $song['place'] = $k > $top ? 'j' : $k;
                $song['diff']  = !empty($song['oldPlace']) ? ($song['oldPlace'] - (int)$song['place']) : $song['place'];

                if ($song['place'] === 'j') {
                    $song['oldPlace'] = 'j';
                    $song['diff']     = 0;
                }

                if ($song['oldPlace'] == 0) {
                    $song['oldPlace'] = 'j';
                    $song['diff']     = 0;
                }

                if ($song['votes']['2WeeksAgo']['count'] == 0) {
                    $song['oldPlace'] = 'j';
                    $song['diff']     = 0;
                }
            }
        }

        return $data;
    }
}