Completed
Push — playlist-again ( f64bdd...ee69ee )
by Pauli
11:41
created

PlaylistMapper::removeTracksFromAllLists()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 7
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
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) {
104
			// delete from oc_music_playlists
105
			parent::deleteById($ids);
106
107
			// delete from oc_music_playlist_tracks
108
			$sql = 'DELETE FROM `*PREFIX*music_playlist_tracks` WHERE `playlist_id` IN ' . $this->questionMarks($count);
109
			$this->execute($sql, $ids);
110
		}
111
	}
112
113
	/**
114
	 * removes tracks from a playlist
115
	 * @param int $id       playlist ID
116
	 * @param int[] $trackIds array of all track IDs to remove - if empty all tracks will be removed
117
	 */
118
	public function removeTracks($id, $trackIds = null) {
119
		$sql = 'DELETE FROM `*PREFIX*music_playlist_tracks` WHERE `playlist_id` = ?';
120
121
		if(is_null($trackIds)) {
122
			$this->execute($sql, array($id));
123
		} else {
124
			$count = count($trackIds);
125
			if ($count) {
126
				$sql .= ' AND `track_id` IN ' . $this->questionMarks($count);
127
				$this->execute($sql, array_merge([$id], $trackIds));
128
			}
129
		}
130
	}
131
132
	/**
133
	 * removes tracks from all available playlists
134
	 * @param int[] $trackIds array of all track IDs to remove
135
	 */
136
	public function removeTracksFromAllLists($trackIds) {
137
		$count = count($trackIds);
138
		if($count) {
139
			$sql = 'DELETE FROM `*PREFIX*music_playlist_tracks` WHERE `track_id` IN ' . $this->questionMarks($count);
140
			$this->execute($sql, $trackIds);
141
		}
142
	}
143
}
144