|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* ownCloud - Music app |
|
5
|
|
|
* |
|
6
|
|
|
* This file is licensed under the Affero General Public License version 3 or |
|
7
|
|
|
* later. See the COPYING file. |
|
8
|
|
|
* |
|
9
|
|
|
* @author Morris Jobke <[email protected]> |
|
10
|
|
|
* @author Volkan Gezer <[email protected]> |
|
11
|
|
|
* @copyright Morris Jobke 2014 |
|
12
|
|
|
* @copyright Volkan Gezer 2014 |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
namespace OCA\Music\Db; |
|
16
|
|
|
|
|
17
|
|
|
use OCP\AppFramework\Db\Entity; |
|
18
|
|
|
use OCP\IDb; |
|
19
|
|
|
|
|
20
|
|
|
class PlaylistMapper extends BaseMapper { |
|
21
|
|
|
|
|
22
|
|
|
public function __construct(IDb $db){ |
|
23
|
|
|
parent::__construct($db, 'music_playlists', '\OCA\Music\Db\Playlist'); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @param string $condition |
|
28
|
|
|
*/ |
|
29
|
|
|
private function makeSelectQuery($condition=null){ |
|
30
|
|
|
return 'SELECT `name`, `id` ' . |
|
31
|
|
|
'FROM `*PREFIX*music_playlists` ' . |
|
32
|
|
|
'WHERE `user_id` = ? ' . $condition; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @param string $userId |
|
37
|
|
|
* @param integer $limit |
|
38
|
|
|
* @param integer $offset |
|
39
|
|
|
* @return Playlist[] |
|
40
|
|
|
*/ |
|
41
|
|
|
public function findAll($userId, $limit=null, $offset=null){ |
|
42
|
|
|
$sql = $this->makeSelectQuery(); |
|
43
|
|
|
$params = array($userId); |
|
44
|
|
|
return $this->findEntities($sql, $params, $limit, $offset); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @param integer $id |
|
49
|
|
|
* @param string $userId |
|
50
|
|
|
* @return Playlist |
|
51
|
|
|
*/ |
|
52
|
|
|
public function find($id, $userId){ |
|
53
|
|
|
$sql = $this->makeSelectQuery('AND `id` = ?'); |
|
54
|
|
|
$params = array($userId, $id); |
|
55
|
|
|
return $this->findEntity($sql, $params); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* adds tracks to a playlist |
|
60
|
|
|
* @param int[] $trackIds array of all track IDs to add |
|
61
|
|
|
* @param int $id playlist ID |
|
62
|
|
|
*/ |
|
63
|
|
|
public function addTracks($trackIds, $id) { |
|
64
|
|
|
$currentTrackIds = $this->getTracks($id); |
|
65
|
|
|
|
|
66
|
|
|
// returns elements of $trackIds that are not in $currentTrackIds |
|
67
|
|
|
$newTrackIds = array_diff($trackIds, $currentTrackIds); |
|
68
|
|
|
|
|
69
|
|
|
$sql = 'INSERT INTO `*PREFIX*music_playlist_tracks` (`playlist_id`, ' . |
|
70
|
|
|
'`track_id`) VALUES ( ?, ? )'; |
|
71
|
|
|
|
|
72
|
|
|
// this is called for each track ID, because the is no identical way |
|
73
|
|
|
// to do a multi insert for all supported databases |
|
74
|
|
|
foreach ($newTrackIds as $trackId) { |
|
75
|
|
|
$this->execute($sql, array($id, $trackId)); |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* gets track of a playlist |
|
81
|
|
|
* @param int $id ID of the playlist |
|
82
|
|
|
* @return int[] list of all track IDs |
|
83
|
|
|
*/ |
|
84
|
|
|
public function getTracks($id) { |
|
85
|
|
|
$sql = 'SELECT `track_id` FROM `*PREFIX*music_playlist_tracks` '. |
|
86
|
|
|
'WHERE `playlist_id` = ?'; |
|
87
|
|
|
$result = $this->execute($sql, array($id)); |
|
88
|
|
|
|
|
89
|
|
|
$trackIds = array(); |
|
90
|
|
|
while($row = $result->fetchRow()){ |
|
91
|
|
|
$trackIds[] = (int) $row['track_id']; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
return $trackIds; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* deletes a playlist |
|
99
|
|
|
* @param integer[] $ids IDs of the entities to be deleted |
|
100
|
|
|
*/ |
|
101
|
|
|
public function deleteById($ids){ |
|
102
|
|
|
$count = count($ids); |
|
103
|
|
|
if($count === 0) { |
|
104
|
|
|
return; |
|
105
|
|
|
} |
|
106
|
|
|
// delete from oc_music_playlists |
|
107
|
|
|
parent::deleteById($ids); |
|
108
|
|
|
|
|
109
|
|
|
// delete from oc_music_playlist_tracks |
|
110
|
|
|
$questionMarks = '?'; |
|
111
|
|
|
for($i = 1; $i < $count; $i++){ |
|
112
|
|
|
$questionMarks .= ',?'; |
|
113
|
|
|
} |
|
114
|
|
|
$sql = 'DELETE FROM `*PREFIX*music_playlist_tracks` WHERE `playlist_id` IN ('. $questionMarks . ')'; |
|
115
|
|
|
$this->execute($sql, $ids); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* removes tracks from a playlist |
|
120
|
|
|
* @param int $id playlist ID |
|
121
|
|
|
* @param int[] $trackIds array of all track IDs to remove - if empty all tracks will be removed |
|
122
|
|
|
*/ |
|
123
|
|
|
public function removeTracks($id, $trackIds = null) { |
|
124
|
|
|
// TODO delete multiple per SQL statement |
|
125
|
|
|
$sql = 'DELETE FROM `*PREFIX*music_playlist_tracks` ' . |
|
126
|
|
|
'WHERE `playlist_id` = ?'; |
|
127
|
|
|
|
|
128
|
|
|
if(is_null($trackIds)) { |
|
129
|
|
|
$this->execute($sql, array($id)); |
|
130
|
|
|
} else { |
|
131
|
|
|
$sql .= ' AND `track_id` = ?'; |
|
132
|
|
|
foreach ($trackIds as $trackId) { |
|
133
|
|
|
$this->execute($sql, array($id, $trackId)); |
|
134
|
|
|
} |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
} |
|
140
|
|
|
|