PlaylistController::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 5
dl 0
loc 12
rs 10
c 0
b 0
f 0
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-2021 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();
0 ignored issues
show
Deprecated Code introduced by
The function OCP\DB\IPreparedStatement::fetch() has been deprecated: 21.0.0 use \OCP\DB\IResult::fetch on the \OCP\DB\IResult returned by \OCP\IDBConnection::prepare ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

80
        $row = /** @scrutinizer ignore-deprecated */ $stmt->fetch();

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
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');
0 ignored issues
show
Deprecated Code introduced by
The function OCP\IDBConnection::lastInsertId() has been deprecated: 21.0.0 use \OCP\DB\QueryBuilder\IQueryBuilder::getLastInsertId ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

86
            $insertid = /** @scrutinizer ignore-deprecated */ $this->db->lastInsertId('*PREFIX*audioplayer_playlists');

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
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();
0 ignored issues
show
Deprecated Code introduced by
The function OCP\DB\IPreparedStatement::fetch() has been deprecated: 21.0.0 use \OCP\DB\IResult::fetch on the \OCP\DB\IResult returned by \OCP\IDBConnection::prepare ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

133
        $row = /** @scrutinizer ignore-deprecated */ $stmt->fetch();

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
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