189 if ($_SERVER[
'REQUEST_METHOD'] !==
'POST') {
195 $managerChanson =
new ChansonDAO($this->getPdo());
196 $managerGenre =
new GenreDAO($this->getPdo());
197 $managerAlbum =
new AlbumDAO($this->getPdo());
199 $idAlbumExistant = $_POST[
'id_album_existant'] ??
null;
201 if ($idAlbumExistant) {
203 $albumCree = $managerAlbum->find((
int)$idAlbumExistant);
204 if (!$albumCree || $albumCree->getArtisteAlbum() !== $_SESSION[
'user_email']) {
208 $idAlbum = $albumCree->getIdAlbum();
211 $album =
new Album();
212 $album->setTitreAlbum($_POST[
'titre_album'] ??
'');
213 $album->setDateSortieAlbum($_POST[
'date_sortie'] ??
'');
214 $album->setArtisteAlbum($_SESSION[
'user_email']);
217 if (isset($_FILES[
'pochette_album']) && $_FILES[
'pochette_album'][
'error'] === UPLOAD_ERR_OK) {
218 $uploadDir =
'assets' . DIRECTORY_SEPARATOR .
'images' . DIRECTORY_SEPARATOR .
'albums' . DIRECTORY_SEPARATOR;
219 if (!is_dir($uploadDir)) {
220 mkdir($uploadDir, 0777,
true);
222 $nomFichier = basename($_FILES[
'pochette_album'][
'name']);
223 $cheminPochette = $uploadDir . uniqid() .
'-' . $nomFichier;
224 if (move_uploaded_file($_FILES[
'pochette_album'][
'tmp_name'], $cheminPochette)) {
225 $album->seturlPochetteAlbum($cheminPochette);
229 $album->seturlPochetteAlbum(
'assets' . DIRECTORY_SEPARATOR .
'images' . DIRECTORY_SEPARATOR .
'albums' . DIRECTORY_SEPARATOR .
'default.png');
231 $idAlbum = $managerAlbum->create($album);
232 $albumCree = $managerAlbum->find($idAlbum);
236 if (isset($_POST[
'tracks']) && isset($_FILES[
'tracks'])) {
237 $uploadDirMusique =
'assets' . DIRECTORY_SEPARATOR .
'audio' . DIRECTORY_SEPARATOR;
239 require_once
'vendor/james-heinrich/getid3/getid3/getid3.php';
240 $getID3 =
new getID3;
242 if (!is_dir($uploadDirMusique)) {
243 mkdir($uploadDirMusique, 0777,
true);
246 foreach ($_POST[
'tracks'] as $index => $chansonData) {
248 if (!isset($_FILES[
'tracks'][
'tmp_name'][$index][
'file']) || $_FILES[
'tracks'][
'error'][$index][
'file'] !== UPLOAD_ERR_OK) {
252 $nomFichierOriginal = basename($_FILES[
'tracks'][
'name'][$index][
'file']);
253 $cheminCible = $uploadDirMusique . uniqid() .
'-' . $nomFichierOriginal;
255 if (!move_uploaded_file($_FILES[
'tracks'][
'tmp_name'][$index][
'file'], $cheminCible)) {
260 $infoFichier = $getID3->analyze($cheminCible);
261 $duree = (int)($infoFichier[
'playtime_seconds'] ?? 0);
264 $chanson->setTitreChanson($chansonData[
'title']);
265 $chanson->setDureeChanson($duree);
266 $chanson->setDateTeleversementChanson(
new DateTime());
267 $chanson->setAlbumChanson($albumCree);
268 $chanson->setEmailPublicateur($_SESSION[
'user_email']);
269 $chanson->setUrlAudioChanson($cheminCible);
271 $nomGenre = $chansonData[
'genre'] ??
null;
273 $genreExistant = $managerGenre->findByName($nomGenre);
274 if ($genreExistant) {
275 $chanson->setGenreChanson($genreExistant);
277 $idNouveauGenre = $managerGenre->create($nomGenre);
278 $chanson->setGenreChanson($managerGenre->find($idNouveauGenre));
282 $managerChanson->createChanson($chanson);
286 if ($idAlbumExistant) {
288 $this->
redirectTo(
'album',
'afficherDetails', [
'idAlbum' => $idAlbumExistant,
'success' => 1]);
291 $this->
redirectTo(
'home',
'afficher', [
'success' => 1]);
356 if ($_SERVER[
'REQUEST_METHOD'] !==
'POST') {
362 $idChanson = $_GET[
'idChanson'] ??
null;
363 $idAlbum = $_POST[
'id_album'] ??
null;
365 if (!$idChanson || !$idAlbum) {
367 $this->
redirectTo(
'home',
'afficher', [
'error' => 1]);
371 $chanson = $chansonDAO->findId((
int)$idChanson);
374 if (!$chanson || $chanson->getAlbumChanson()->getArtisteAlbum() !== $_SESSION[
'user_email']) {
375 $this->
redirectTo(
'home',
'afficher', [
'error' =>
'unauthorized']);
379 $chanson->setTitreChanson($_POST[
'titre_chanson']);
382 $nomGenre = $_POST[
'genre_chanson'] ??
'';
383 $genre =
$genreDAO->findOrCreateByName($nomGenre);
384 $chanson->setGenreChanson($genre);
386 $chansonDAO->updateChanson($chanson);
389 $this->
redirectTo(
'album',
'afficherDetails', [
'idAlbum' => $idAlbum,
'success_update' => 1]);
404 if ($_SERVER[
'REQUEST_METHOD'] !==
'POST') {
410 $idChanson = $_GET[
'idChanson'] ??
null;
411 $idAlbum = $_POST[
'id_album'] ??
null;
413 if (!$idChanson || !$idAlbum) {
414 $this->
redirectTo(
'home',
'afficher', [
'error' => 1]);
418 $chanson = $chansonDAO->findId((
int)$idChanson);
420 if (!$chanson || !$chanson->getAlbumChanson() || $chanson->getAlbumChanson()->getArtisteAlbum() !== $_SESSION[
'user_email']) {
421 $this->
redirectTo(
'home',
'afficher', [
'error' =>
'unauthorized']);
425 $urlAudio = $chanson->getUrlAudioChanson();
426 if ($urlAudio && strpos($urlAudio,
'http') !== 0) {
428 if (file_exists($urlAudio) && is_writable($urlAudio)) {
434 $chansonDAO->deleteChanson((
int)$idChanson);
437 $this->
redirectTo(
'album',
'afficherDetails', [
'idAlbum' => $idAlbum,
'deleted' => 1]);