<?php

namespace controller;

use models\Artiste;
use models\db\PlaylistDB;
use utils\Utils;


class ControllerPubliePlaylist extends Controller
{
  public function view(): void
  {
  }


  public function publierPlaylist(): void
  {
    $playlistDB = new PlaylistDB();
    if ($_SERVER['REQUEST_METHOD'] === 'POST') {
      $titre = $_POST['titre'];
      $description = $_POST['description'];
      $auteur = Utils::getIdUtilisateurConnecte();
      $dateMaj = date('d/m/Y');
      $anneeP = date('d/m/Y');
      if (isset($_FILES['image'])) {
        $image = Utils::traiterImage();
        $imagePath = str_replace('fixtures/images/', '', $image);
      } else {
        $imagePath = 'default.jpg';
      }
      $playlistId = $playlistDB->insererPlaylist($titre, $description, $auteur, $dateMaj, $image, $anneeP);
      $response = [
        'success' => $playlistId !== null,
        'message' => $playlistId !== null ? 'Playlist publié valide.' : 'Erreur lors de la publication de la playlist.',
        'playlistId' => $playlistId
      ];
      header('Content-Type: application/json');
      echo json_encode($response);
      exit;
    }
  }

  public function getPlaylistItem($playlistId)
  {
    $playlistDB = new PlaylistDB();
    $playlist = $playlistDB->getPlaylist($playlistId);

    return $playlist->render();
  }
}
<?php

use controller\ControllerAlbum;
use controller\ControllerHome;
use controller\ControllerLogin;
use controller\ControllerTest;
use controller\ControllerArtiste;
use controller\ControllerPublier;
use controller\ControllerPubliePlaylist;
use route\Route;

require "classes/autoload.php";

session_start();

$routes = [
    new Route("/", "GET", ControllerHome::class, "view", []),
    new Route("/test", "GET", ControllerTest::class, "view"),
    new Route("/artiste", "GET", ControllerArtiste::class, "view", [], ["id"]),
    new Route("/album", "GET", ControllerAlbum::class, "view", [], ["id"]),
    new Route("/publier", "GET", ControllerPublier::class, "view", []),
    new Route("/publier", "POST", ControllerPublier::class, "publierContenue", []),
    new Route("/publierPlaylist", "POST", ControllerPubliePlaylist::class, "publierPlaylist", []),
    new Route("/getPlaylistItem/{id}", "GET", ControllerPubliePlaylist::class, "getPlaylistItem", [], ["id"]),
    new Route("/login", "POST", ControllerLogin::class),
    new Route("/login", "GET", ControllerLogin::class),
];


$uri = parse_url($_SERVER["REQUEST_URI"]);
$method = $_SERVER["REQUEST_METHOD"];
$url = $uri["path"] ?? "/";
$role = $_SESSION["utilisateur"]["role"] ?? "user";
$params = $_REQUEST;
$action = $_REQUEST["action"] ?? null;

$notFound = true;

foreach($routes as $route){
    if(!($url === $route->getUrl()))continue;
    if(!($method === $route->getMethod()))continue;
    if(count($route->getRoles()) > 0 and !in_array($role, $route->getRoles()))continue;
    if(is_null($route->getAction()) and is_null($action))continue;

    $p = false;
    foreach($route->getParams() as $param){
        if(!isset($params[$param])){
            $p = true;
        }
    }
    if($p)continue;

    $controller = $route->getController();
    $actionRoute = $route->getAction();

    if(is_null($actionRoute)){
        (new $controller($params))->$action();
    }else{
        (new $controller($params))->$actionRoute();
    }

    $notFound = false;

    break;
}

if($notFound){
    header("HTTP/1.0 404 Not Found");   
}$(document).ready(function() {
  $('.form-publie-playlist').on('submit', function(e) {
    e.preventDefault();

    var formData = new FormData(this);

    $.ajax({
      type: 'POST',
      url: '/publierPlaylist',
      data: formData,
      contentType: false,
      processData: false,
      success: function(response) {
        if (response.success) {
          console.log(response);
          alert('Playlist publiée avec succès');
          // récupère le composant playlist via une autre requête AJAX
          $.ajax({
            type: 'GET',
            url: '/getPlaylistItem/' + response.playlistId,
            success: function(html) {
              $('.scroll-1 .elements').append(html);
            },
            error: function(xhr, status, error) {
              console.error(error);
            }
          });
        } else {
          alert('Erreur lors de la publication de la playlist');}
      },
      error: function(xhr, status, error) {
        console.error(error);
        // Gérer les erreurs si nécessaire
      }
    });
  });
});
[Tue Feb 13 01:14:22 2024] [::1]:58528 [404]: GET /getPlaylistItem/20