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
|
|
|
* @copyright Morris Jobke 2013, 2014 |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace OCA\Music\BusinessLayer; |
14
|
|
|
|
15
|
|
|
use \OCA\Music\AppFramework\BusinessLayer\BusinessLayer; |
16
|
|
|
use \OCA\Music\AppFramework\BusinessLayer\BusinessLayerException; |
17
|
|
|
use \OCA\Music\AppFramework\Core\Logger; |
18
|
|
|
|
19
|
|
|
use \OCA\Music\Db\AlbumMapper; |
20
|
|
|
use \OCA\Music\Db\Album; |
21
|
|
|
|
22
|
|
|
class AlbumBusinessLayer extends BusinessLayer { |
23
|
|
|
|
24
|
|
|
private $logger; |
25
|
|
|
|
26
|
|
|
public function __construct(AlbumMapper $albumMapper, Logger $logger){ |
27
|
|
|
parent::__construct($albumMapper); |
28
|
|
|
$this->logger = $logger; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Return an album |
33
|
|
|
* @param string $albumId the id of the album |
34
|
|
|
* @param string $userId the name of the user |
35
|
|
|
* @return Album album |
36
|
|
|
*/ |
37
|
|
|
public function find($albumId, $userId){ |
38
|
|
|
$album = $this->mapper->find($albumId, $userId); |
39
|
|
|
$albumArtists = $this->mapper->getAlbumArtistsByAlbumId(array($album->getId())); |
40
|
|
|
$album->setArtistIds($albumArtists[$album->getId()]); |
41
|
|
|
return $album; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Returns all albums |
46
|
|
|
* @param string $userId the name of the user |
47
|
|
|
* @return Album[] albums |
48
|
|
|
*/ |
49
|
|
|
public function findAll($userId){ |
50
|
|
|
$albums = $this->mapper->findAll($userId); |
51
|
|
|
return $this->injectArtists($albums); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Returns all albums filtered by artist |
56
|
|
|
* @param string $artistId the id of the artist |
57
|
|
|
* @return Album[] albums |
58
|
|
|
*/ |
59
|
|
|
public function findAllByArtist($artistId, $userId){ |
60
|
|
|
$albums = $this->mapper->findAllByArtist($artistId, $userId); |
61
|
|
|
return $this->injectArtists($albums); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
private function injectArtists($albums){ |
65
|
|
|
if(count($albums) === 0) { |
66
|
|
|
return array(); |
67
|
|
|
} |
68
|
|
|
$albumIds = array(); |
69
|
|
|
foreach ($albums as $album) { |
70
|
|
|
$albumIds[] = $album->getId(); |
71
|
|
|
} |
72
|
|
|
$albumArtists = $this->mapper->getAlbumArtistsByAlbumId($albumIds); |
73
|
|
|
foreach ($albums as $key => $album) { |
74
|
|
|
$albums[$key]->setArtistIds($albumArtists[$album->getId()]); |
75
|
|
|
} |
76
|
|
|
return $albums; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
public function findAlbumOwner($albumId){ |
80
|
|
|
$entities = $this->mapper->findById([$albumId]); |
81
|
|
|
if (count($entities) != 1) { |
82
|
|
|
throw new \OCA\Music\AppFramework\BusinessLayer\BusinessLayerException( |
83
|
|
|
'Expected to find on album but got ' . count($entities)); |
84
|
|
|
} |
85
|
|
|
else { |
86
|
|
|
return $entities[0]->getUserId(); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Adds an album if it does not exist already or updates an existing album |
92
|
|
|
* @param string $name the name of the album |
93
|
|
|
* @param string $year the year of the release |
94
|
|
|
* @param string $discnumber the disk number of this album's disk |
95
|
|
|
* @param integer $albumArtistId |
96
|
|
|
* @param string $userId |
97
|
|
|
* @return Album The added/updated album |
98
|
|
|
*/ |
99
|
|
|
public function addOrUpdateAlbum($name, $year, $discnumber, $albumArtistId, $userId){ |
100
|
|
|
$album = new Album(); |
101
|
|
|
$album->setName($name); |
102
|
|
|
$album->setYear($year); |
103
|
|
|
$album->setDisk($discnumber); |
104
|
|
|
$album->setUserId($userId); |
105
|
|
|
$album->setAlbumArtistId($albumArtistId); |
106
|
|
|
|
107
|
|
|
// Generate hash from the set of fields forming the album identity to prevent duplicates. |
108
|
|
|
// The uniqueness of album name is evaluated in case-insensitive manner. |
109
|
|
|
$lowerName = mb_strtolower($name); |
110
|
|
|
$hash = hash('md5', "$lowerName|$year|$discnumber|$albumArtistId"); |
111
|
|
|
$album->setHash($hash); |
112
|
|
|
|
113
|
|
|
return $this->mapper->insertOrUpdate($album); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Check if given file is used as cover for the given album |
118
|
|
|
* @param int $albumId |
119
|
|
|
* @param int[] $fileIds |
120
|
|
|
* @return boolean |
121
|
|
|
*/ |
122
|
|
|
public function albumCoverIsOneOfFiles($albumId, $fileIds) { |
123
|
|
|
$albums = $this->mapper->findById([$albumId]); |
124
|
|
|
return (count($albums) && in_array($albums[0]->getCoverFileId(), $fileIds)); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* updates the cover for albums in the specified folder without cover |
129
|
|
|
* @param integer $coverFileId the file id of the cover image |
130
|
|
|
* @param integer $folderId the file id of the folder where the albums are looked from |
131
|
|
|
* @return true if one or more albums were influenced |
132
|
|
|
*/ |
133
|
|
|
public function updateFolderCover($coverFileId, $folderId){ |
134
|
|
|
return $this->mapper->updateFolderCover($coverFileId, $folderId); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* set cover file for a specified album |
139
|
|
|
* @param integer $coverFileId the file id of the cover image |
140
|
|
|
* @param integer $albumId the id of the album to be modified |
141
|
|
|
*/ |
142
|
|
|
public function setCover($coverFileId, $albumId){ |
143
|
|
|
$this->mapper->setCover($coverFileId, $albumId); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* removes the cover art from albums, replacement covers will be searched in a background task |
148
|
|
|
* @param integer[] $coverFileIds the file IDs of the cover images |
149
|
|
|
* @param string|null $userId the user whose music library is targeted; all users are targeted if omitted |
150
|
|
|
* @return string[] user IDs of the affected users; empty array if no album was modified |
151
|
|
|
*/ |
152
|
|
|
public function removeCovers($coverFileIds, $userId=null){ |
153
|
|
|
return $this->mapper->removeCovers($coverFileIds, $userId); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* try to find cover arts for albums without covers |
158
|
|
|
* @return array of users whose collections got modified |
159
|
|
|
*/ |
160
|
|
|
public function findCovers(){ |
161
|
|
|
$affectedUsers = []; |
162
|
|
|
$albums = $this->mapper->getAlbumsWithoutCover(); |
163
|
|
|
foreach ($albums as $album){ |
164
|
|
|
if ($this->mapper->findAlbumCover($album['albumId'], $album['parentFolderId'])){ |
165
|
|
|
$affectedUsers[$album['userId']] = 1; |
166
|
|
|
} |
167
|
|
|
} |
168
|
|
|
return array_keys($affectedUsers); |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|