PlaylistController   A
last analyzed

Complexity

Total Complexity 17

Size/Duplication

Total Lines 186
Duplicated Lines 0 %

Importance

Changes 5
Bugs 2 Features 0
Metric Value
eloc 84
dl 0
loc 186
rs 10
c 5
b 2
f 0
wmc 17

9 Methods

Rating   Name   Duplication   Size   Complexity  
A updatePlaylist() 0 14 2
A addPlaylist() 0 18 3
A removePlaylist() 0 22 2
A writePlaylistToDB() 0 14 2
A __construct() 0 12 1
A updatePlaylistToDB() 0 5 1
A sortPlaylist() 0 14 2
A addTrackToPlaylist() 0 11 2
A removeTrackFromPlaylist() 0 11 2
1
<?php
2
/**
3
 * Audio Player
4
 *
5
 * This file is licensed under the Affero General Public License version 3 or
6
 * later. See the LICENSE.md file.
7
 *
8
 * @author Marcel Scherello <[email protected]>
9
 * @author Sebastian Doell <[email protected]>
10
 * @copyright 2016-2020 Marcel Scherello
11
 * @copyright 2015 Sebastian Doell
12
 */
13
14
namespace OCA\audioplayer\Controller;
15
16
use OCP\AppFramework\Controller;
17
use OCP\AppFramework\Http\JSONResponse;
18
use OCP\IRequest;
19
use OCP\IL10N;
20
use OCP\IDbConnection;
21
22
/**
23
 * Controller class for main page.
24
 */
25
class PlaylistController extends Controller
26
{
27
28
    private $userId;
29
    private $l10n;
30
    private $db;
31
32
    public function __construct(
33
        $appName,
34
        IRequest $request,
35
        $userId,
36
        IL10N $l10n,
37
        IDbConnection $db
38
    )
39
    {
40
        parent::__construct($appName, $request);
41
        $this->userId = $userId;
42
        $this->l10n = $l10n;
43
        $this->db = $db;
44
    }
45
46
    /**
47
     * @NoAdminRequired
48
     * @param $playlist
49
     * @return null|JSONResponse
50
     */
51
    public function addPlaylist($playlist)
52
    {
53
        if ($playlist !== '') {
54
            $aResult = $this->writePlaylistToDB($playlist);
55
            if ($aResult['msg'] === 'new') {
56
                $result = [
57
                    'status' => 'success',
58
                    'data' => ['playlist' => $playlist]
59
                ];
60
            } else {
61
                $result = [
62
                    'status' => 'success',
63
                    'data' => 'exist',
64
                ];
65
            }
66
            return new JSONResponse($result);
67
        } else {
68
            return null;
69
        }
70
    }
71
72
    /**
73
     * @param $sName
74
     * @return array
75
     */
76
    private function writePlaylistToDB($sName)
77
    {
78
        $stmt = $this->db->prepare('SELECT `id` FROM `*PREFIX*audioplayer_playlists` WHERE `user_id` = ? AND `name` = ?');
79
        $stmt->execute(array($this->userId, $sName));
80
        $row = $stmt->fetch();
81
        if ($row) {
82
            $result = ['msg' => 'exist', 'id' => $row['id']];
83
        } else {
84
            $stmt = $this->db->prepare('INSERT INTO `*PREFIX*audioplayer_playlists` (`user_id`,`name`) VALUES(?,?)');
85
            $stmt->execute(array($this->userId, $sName));
86
            $insertid = $this->db->lastInsertId('*PREFIX*audioplayer_playlists');
87
            $result = ['msg' => 'new', 'id' => $insertid];
88
        }
89
        return $result;
90
    }
91
92
    /**
93
     * @NoAdminRequired
94
     *
95
     * @param integer $plId
96
     * @param string $newname
97
     * @return JSONResponse
98
     */
99
    public function updatePlaylist($plId, $newname)
100
    {
101
102
        if ($this->updatePlaylistToDB($plId, $newname)) {
103
            $params = [
104
                'status' => 'success',
105
            ];
106
        } else {
107
            $params = [
108
                'status' => 'error',
109
            ];
110
        }
111
112
        return new JSONResponse($params);
113
    }
114
115
    private function updatePlaylistToDB($id, $sName)
116
    {
117
        $stmt = $this->db->prepare('UPDATE `*PREFIX*audioplayer_playlists` SET `name` = ? WHERE `user_id`= ? AND `id`= ?');
118
        $stmt->execute(array($sName, $this->userId, $id));
119
        return true;
120
    }
121
122
    /**
123
     * @NoAdminRequired
124
     * @param $playlistid
125
     * @param $songid
126
     * @param $sorting
127
     * @return bool
128
     */
129
    public function addTrackToPlaylist($playlistid, $songid, $sorting)
130
    {
131
        $stmt = $this->db->prepare('SELECT COUNT(*) AS tracks FROM `*PREFIX*audioplayer_playlist_tracks` WHERE `playlist_id` = ? AND `track_id` = ?');
132
        $stmt->execute(array($playlistid, $songid));
133
        $row = $stmt->fetch();
134
        if ($row['tracks']) {
135
            return false;
136
        } else {
137
            $stmt = $this->db->prepare('INSERT INTO `*PREFIX*audioplayer_playlist_tracks` (`playlist_id`,`track_id`,`sortorder`) VALUES(?,?,?)');
138
            $stmt->execute(array($playlistid, $songid, (int)$sorting));
139
            return true;
140
        }
141
    }
142
143
    /**
144
     * @NoAdminRequired
145
     * @param $playlistid
146
     * @param $songids
147
     * @return JSONResponse
148
     */
149
    public function sortPlaylist($playlistid, $songids)
150
    {
151
        $iTrackIds = explode(';', $songids);
152
        $counter = 1;
153
        foreach ($iTrackIds as $trackId) {
154
            $stmt = $this->db->prepare('UPDATE `*PREFIX*audioplayer_playlist_tracks` SET `sortorder` = ? WHERE `playlist_id` = ? AND `track_id` = ?');
155
            $stmt->execute(array($counter, $playlistid, $trackId));
156
            $counter++;
157
        }
158
        $result = [
159
            'status' => 'success',
160
            'msg' => (string)$this->l10n->t('Sorting Playlist success! Playlist reloaded!')
161
        ];
162
        return new JSONResponse($result);
163
    }
164
165
    /**
166
     * @NoAdminRequired
167
     * @param $playlistid
168
     * @param $trackid
169
     * @return bool
170
     */
171
    public function removeTrackFromPlaylist($playlistid, $trackid)
172
    {
173
        try {
174
            $sql = 'DELETE FROM `*PREFIX*audioplayer_playlist_tracks` '
175
                . 'WHERE `playlist_id` = ? AND `track_id` = ?';
176
            $stmt = $this->db->prepare($sql);
177
            $stmt->execute(array($playlistid, $trackid));
178
        } catch (\Exception $e) {
179
            return false;
180
        }
181
        return true;
182
    }
183
184
    /**
185
     * @NoAdminRequired
186
     * @param $playlistid
187
     * @return bool|JSONResponse
188
     */
189
    public function removePlaylist($playlistid)
190
    {
191
        try {
192
            $sql = 'DELETE FROM `*PREFIX*audioplayer_playlists` '
193
                . 'WHERE `id` = ? AND `user_id` = ?';
194
            $stmt = $this->db->prepare($sql);
195
            $stmt->execute(array($playlistid, $this->userId));
196
197
            $sql = 'DELETE FROM `*PREFIX*audioplayer_playlist_tracks` '
198
                . 'WHERE `playlist_id` = ?';
199
            $stmt = $this->db->prepare($sql);
200
            $stmt->execute(array($playlistid));
201
        } catch (\Exception $e) {
202
            return false;
203
        }
204
205
        $result = [
206
            'status' => 'success',
207
            'data' => ['playlist' => $playlistid]
208
        ];
209
210
        return new JSONResponse($result);
211
    }
212
}
213