|
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 Pauli Järvinen <[email protected]> |
|
10
|
|
|
* @copyright Pauli Järvinen 2016 - 2020 |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
namespace OCA\Music\BusinessLayer; |
|
14
|
|
|
|
|
15
|
|
|
use \OCA\Music\AppFramework\BusinessLayer\BusinessLayer; |
|
16
|
|
|
use \OCA\Music\AppFramework\Core\Logger; |
|
17
|
|
|
|
|
18
|
|
|
use \OCA\Music\Db\PlaylistMapper; |
|
19
|
|
|
use \OCA\Music\Db\Playlist; |
|
20
|
|
|
|
|
21
|
|
|
use \OCA\Music\Utility\Util; |
|
22
|
|
|
|
|
23
|
|
|
class PlaylistBusinessLayer extends BusinessLayer { |
|
24
|
|
|
private $logger; |
|
25
|
|
|
private $trackBusinessLayer; |
|
26
|
|
|
|
|
27
|
|
|
public function __construct( |
|
28
|
|
|
PlaylistMapper $playlistMapper, |
|
29
|
|
|
TrackBusinessLayer $trackBusinessLayer, |
|
30
|
|
|
Logger $logger) { |
|
31
|
|
|
parent::__construct($playlistMapper); |
|
32
|
|
|
$this->logger = $logger; |
|
33
|
|
|
$this->trackBusinessLayer = $trackBusinessLayer; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
public function addTracks($trackIds, $playlistId, $userId) { |
|
37
|
|
|
$playlist = $this->find($playlistId, $userId); |
|
38
|
|
|
$prevTrackIds = $playlist->getTrackIdsAsArray(); |
|
39
|
|
|
$playlist->setTrackIdsFromArray(\array_merge($prevTrackIds, $trackIds)); |
|
40
|
|
|
$this->mapper->update($playlist); |
|
41
|
|
|
return $playlist; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function removeTracks($trackIndices, $playlistId, $userId) { |
|
45
|
|
|
$playlist = $this->find($playlistId, $userId); |
|
46
|
|
|
$trackIds = $playlist->getTrackIdsAsArray(); |
|
47
|
|
|
$trackIds = \array_diff_key($trackIds, \array_flip($trackIndices)); |
|
48
|
|
|
$playlist->setTrackIdsFromArray($trackIds); |
|
49
|
|
|
$this->mapper->update($playlist); |
|
50
|
|
|
return $playlist; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public function moveTrack($fromIndex, $toIndex, $playlistId, $userId) { |
|
54
|
|
|
$playlist = $this->find($playlistId, $userId); |
|
55
|
|
|
$trackIds = $playlist->getTrackIdsAsArray(); |
|
56
|
|
|
$movedTrack = \array_splice($trackIds, $fromIndex, 1); |
|
57
|
|
|
\array_splice($trackIds, $toIndex, 0, $movedTrack); |
|
58
|
|
|
$playlist->setTrackIdsFromArray($trackIds); |
|
59
|
|
|
$this->mapper->update($playlist); |
|
60
|
|
|
return $playlist; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
public function create($name, $userId) { |
|
64
|
|
|
$playlist = new Playlist(); |
|
65
|
|
|
$playlist->setName(Util::truncate($name, 256)); // some DB setups can't truncate automatically to column max size |
|
66
|
|
|
$playlist->setUserId($userId); |
|
67
|
|
|
|
|
68
|
|
|
return $this->mapper->insert($playlist); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
public function rename($name, $playlistId, $userId) { |
|
72
|
|
|
$playlist = $this->find($playlistId, $userId); |
|
73
|
|
|
$playlist->setName(Util::truncate($name, 256)); // some DB setups can't truncate automatically to column max size |
|
74
|
|
|
$this->mapper->update($playlist); |
|
75
|
|
|
return $playlist; |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* removes tracks from all available playlists |
|
80
|
|
|
* @param int[] $trackIds array of all track IDs to remove |
|
81
|
|
|
*/ |
|
82
|
|
|
public function removeTracksFromAllLists($trackIds) { |
|
83
|
|
|
foreach ($trackIds as $trackId) { |
|
84
|
|
|
$affectedLists = $this->mapper->findListsContainingTrack($trackId); |
|
85
|
|
|
|
|
86
|
|
|
foreach ($affectedLists as $playlist) { |
|
87
|
|
|
$prevTrackIds = $playlist->getTrackIdsAsArray(); |
|
88
|
|
|
$playlist->setTrackIdsFromArray(\array_diff($prevTrackIds, [$trackId])); |
|
89
|
|
|
$this->mapper->update($playlist); |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* get list of Track objects belonging to a given playlist |
|
96
|
|
|
* @param int $playlistId |
|
97
|
|
|
* @param string $userId |
|
98
|
|
|
* @param int|null $limit |
|
99
|
|
|
* @param int|null $offset |
|
100
|
|
|
* @return Track[] |
|
101
|
|
|
*/ |
|
102
|
|
|
public function getPlaylistTracks($playlistId, $userId, $limit=null, $offset=null) { |
|
103
|
|
|
$playlist = $this->find($playlistId, $userId); |
|
104
|
|
|
$trackIds = $playlist->getTrackIdsAsArray(); |
|
105
|
|
|
|
|
106
|
|
|
$trackIds = \array_slice($trackIds, \intval($offset), $limit); |
|
107
|
|
|
|
|
108
|
|
|
$tracks = empty($trackIds) ? [] : $this->trackBusinessLayer->findById($trackIds, $userId); |
|
109
|
|
|
|
|
110
|
|
|
// The $tracks contains the songs in unspecified order and with no duplicates. |
|
111
|
|
|
// Build a new array where the tracks are in the same order as in $trackIds. |
|
112
|
|
|
// First create an index as a middle-step. |
|
113
|
|
|
$tracksById = Util::createIdLookupTable($tracks); |
|
114
|
|
|
|
|
115
|
|
|
$playlistTracks = []; |
|
116
|
|
|
foreach ($trackIds as $index => $trackId) { |
|
117
|
|
|
$track = $tracksById[$trackId]; |
|
118
|
|
|
// in case the same track comes up again in the list, clone the track object |
|
119
|
|
|
// to have different number of the instances |
|
120
|
|
|
if ($track->getNumberOnPlaylist() !== null) { |
|
121
|
|
|
$track = clone $track; |
|
122
|
|
|
} |
|
123
|
|
|
$track->setNumberOnPlaylist(\intval($offset) + $index + 1); |
|
124
|
|
|
$playlistTracks[] = $track; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
return $playlistTracks; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* get the total duration of all the tracks on a playlist |
|
132
|
|
|
* |
|
133
|
|
|
* @param int $playlistId |
|
134
|
|
|
* @param string $userId |
|
135
|
|
|
* @return int duration in seconds |
|
136
|
|
|
*/ |
|
137
|
|
|
public function getDuration($playlistId, $userId) { |
|
138
|
|
|
$playlist = $this->find ( $playlistId, $userId ); |
|
139
|
|
|
$trackIds = $playlist->getTrackIdsAsArray (); |
|
140
|
|
|
$durations = $this->trackBusinessLayer->mapper->getDurations ( $trackIds ); |
|
141
|
|
|
|
|
142
|
|
|
// We can't simply sum up the values of $durations array, because the playlist may |
|
143
|
|
|
// contain duplicate entries, and those are not reflected in $durations. |
|
144
|
|
|
$sum = 0; |
|
145
|
|
|
foreach ( $trackIds as $trackId ) { |
|
146
|
|
|
$sum += $durations [$trackId]; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
return $sum; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
} |
|
153
|
|
|
|