ajout SAE PHP 19/20 Meilleure note de la promo

This commit is contained in:
EmmanuelTiamzon
2025-12-05 10:30:43 +01:00
parent b05817dbcd
commit 0ae7d097f9
290 changed files with 69511 additions and 0 deletions

View File

@@ -0,0 +1,67 @@
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Auth extends CI_Controller {
public function __construct() {
parent::__construct();
$this->load->model('User_model');
$this->load->library('session');
$this->load->helper(['url', 'form']);
}
/* Connexion*/
public function login() {
if ($this->input->method() == 'post') {
$email = $this->input->post('email', true);
$password = $this->input->post('password', true);
$user = $this->User_model->login($email, $password);
if ($user) {
$this->session->set_userdata([
'user_id' => $user->id,
'user_email' => $user->email,
'logged_in' => true
]);
redirect('tvshow');
} else {
$this->session->set_flashdata('error', 'Identifiants invalides');
redirect('login');
}
} else {
$this->load->view('login');
}
}
/* Déconnexion*/
public function logout() {
$this->session->sess_destroy();
redirect('tvshow');
}
/* Page inscription*/
public function register() {
if ($this->input->method() == 'post') {
$firstname = trim($this->input->post('firstname', true));
$lastname = trim($this->input->post('lastname', true));
$email = trim($this->input->post('email', true));
$password = $this->input->post('password', true);
$password_confirm = $this->input->post('password_confirm', true);
if (!$firstname || !$lastname || !$email || !$password) {
$data['error'] = "Tous les champs sont obligatoires.";
} elseif ($password !== $password_confirm) {
$data['error'] = "Les mots de passe ne correspondent pas.";
} elseif (!$this->User_model->is_email_unique($email)) {
$data['error'] = "Un compte avec cet email existe déjà.";
} elseif ($this->User_model->register($firstname, $lastname, $email, $password)) {
redirect('login');
} else {
$data['error'] = "Erreur lors de l'inscription.";
}
}
$this->load->view('register', isset($data) ? $data : []);
}
}

View File

@@ -0,0 +1,81 @@
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Critiques extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->model('Critique_model');
$this->load->library('session');
$this->load->helper(['url', 'form', 'html']);
}
public function add()
{
if (!$this->session->userdata('logged_in')) {
redirect('login');
return;
}
$user_id = $this->session->userdata('user_id');
$tvshow_id = $this->input->post('tvshow_id');
$season_id = $this->input->post('season_id');
$note = $this->input->post('note');
$commentaire = $this->input->post('commentaire');
if (empty($tvshow_id) || empty($note)) {
$this->session->set_flashdata('error', 'Données manquantes pour ajouter la critique.');
redirect('tvshow/detail/' . $tvshow_id);
return;
}
$data = [
'user_id' => $user_id,
'tvshow_id' => $tvshow_id,
'season_id' => $season_id ?: null,
'note' => (int)$note,
'commentaire' => $commentaire,
'date_creation' => date('Y-m-d H:i:s')
];
if ($this->Critique_model->add_critique($data)) {
$this->session->set_flashdata('success', 'Critique ajoutée avec succès.');
} else {
$this->session->set_flashdata('error', 'Erreur lors de l\'ajout de la critique.');
}
redirect('tvshow/detail/' . $tvshow_id);
}
public function edit($id) {
$this->load->model('Critique_model');
$critique = $this->Critique_model->get_by_id($id);
if (!$critique) {
show_404();
}
if ($this->input->method() === 'post') {
$note = $this->input->post('note');
$commentaire = $this->input->post('commentaire');
$data = [
'note' => (int) $note,
'commentaire' => $commentaire,
];
$this->Critique_model->update_critique($id, $data);
redirect('user/profile');
}
$data['critique'] = $critique;
$this->load->view('layout/header');
$this->load->view('edit_review', $data);
$this->load->view('layout/footer');
}
}

View File

@@ -0,0 +1,25 @@
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Favoris extends CI_Controller {
public function ajouter($tvshow_id)
{
if ($this->session->userdata('logged_in')) {
$user_id = $this->session->userdata('user_id');
$this->load->model('Favoris_model');
$this->Favoris_model->ajouter_favori($user_id, $tvshow_id);
}
redirect('tvshow/detail/' . $tvshow_id);
}
public function supprimer($tvshow_id)
{
if ($this->session->userdata('logged_in')) {
$user_id = $this->session->userdata('user_id');
$this->load->model('Favoris_model');
$this->Favoris_model->supprimer_favori($user_id, $tvshow_id);
}
redirect('tvshow/detail/' . $tvshow_id);
}
}

View File

@@ -0,0 +1,164 @@
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Tvshow extends CI_Controller
{
public function __construct()
{
parent::__construct();
$this->load->model('Model_tvshow');
$this->load->model('Critique_model');
$this->load->model('Favoris_model');
$this->load->helper(['url', 'html']);
$this->load->library('session');
if (!$this->session->has_userdata('visitor_id')) {
$this->session->set_userdata('visitor_id', uniqid('visitor_', true));
}
}
/* Liste des séries avec possibilité de recherche */
public function index()
{
$search = $this->input->get('search');
$genre = $this->input->get('type');
$sort = $this->input->get('sort');
$min_rating = $this->input->get('min_rating');
$order_by = null;
if ($sort === 'note_desc') {
$order_by = ['average_rating' => 'DESC'];
} elseif ($sort === 'note_asc') {
$order_by = ['average_rating' => 'ASC'];
}
$tvshows = $this->Model_tvshow->getAllFiltered($genre, $search, $order_by, $min_rating);
$data = [
'tvshows' => $tvshows,
'all_genres' => $this->Model_tvshow->getAllGenres(),
'session' => $this->session,
'search' => $search,
];
$this->load->view('layout/header', $data);
if (!empty($search) && empty($tvshows)) {
$this->load->view('search_results', $data);
} else {
$this->load->view('tvshow_list', $data);
}
$this->load->view('layout/footer');
}
/* Détail dune série (avec épisodes et critiques globales) */
public function detail($id = 0)
{
$tvshow = $this->Model_tvshow->getTvshowById($id);
if (!$tvshow) show_404();
$episodes = $this->Model_tvshow->getEpisodesByTvshowId($id);
$critiques = $this->Critique_model->get_critiques_by_tvshow($id);
$genres = $this->Model_tvshow->getGenresByTvshowId($id);
$homepage = $this->Model_tvshow->getHomepageById($id);
$suggestions = $this->Model_tvshow->getSuggestionsByTvshowId($id);
$all_genres = $this->Model_tvshow->getAllGenres();
$est_favori = false;
if ($this->session->userdata('logged_in')) {
$user_id = $this->session->userdata('user_id');
$est_favori = $this->Favoris_model->is_favori($user_id, $id);
}
$ratingData = $this->Critique_model->getAverageRating($id);
$moyenne = $ratingData['moyenne'];
$nb_votes = $ratingData['nb_votes'];
$data = [
'tvshow' => $tvshow,
'episodes' => $episodes,
'critiques' => $critiques,
'genres' => $genres,
'all_genres' => $all_genres,
'session' => $this->session,
'homepage' => $homepage,
'moyenne' => $moyenne,
'est_favori' => $est_favori,
'nb_votes' => $nb_votes,
'suggestions' => $suggestions,
];
$this->load->view('layout/header', $data);
$this->load->view('tvshow_detail', $data);
$this->load->view('layout/footer');
}
/* Détail dune saison spécifique */
public function saison($tvshow_id, $season_number)
{
$tvshow = $this->Model_tvshow->getTvshowById($tvshow_id);
if (!$tvshow) show_404();
$season = $this->Model_tvshow->getSeasonByNumber($tvshow_id, $season_number);
if (!$season) show_404();
$episodes = $this->Model_tvshow->getEpisodesBySeason($tvshow_id, $season_number);
$all_genres = $this->Model_tvshow->getAllGenres();
$critiques = $this->Critique_model->get_critiques_by_tvshow($tvshow_id);
$critiques_saison = array_filter($critiques, fn($c) => $c->season_id == $season_number);
$data = [
'tvshow' => $tvshow,
'season' => $season,
'episodes' => $episodes,
'all_genres' => $all_genres,
'session' => $this->session,
'critiques_saison' => $critiques_saison
];
$this->load->view('layout/header', $data);
$this->load->view('season_detail', $data);
$this->load->view('layout/footer');
}
/* Ajout dune critique */
public function add_critique()
{
if (!$this->session->userdata('logged_in')) {
redirect('login');
return;
}
$user_id = $this->session->userdata('user_id');
$tvshow_id = $this->input->post('tvshow_id');
$season_id = $this->input->post('season_id');
$note = $this->input->post('note');
$commentaire = $this->input->post('commentaire');
if (!$tvshow_id || !$note) {
$this->session->set_flashdata('error', 'Critique incomplète.');
redirect('tvshow/detail/' . $tvshow_id);
return;
}
$data = [
'user_id' => $user_id,
'tvshow_id' => $tvshow_id,
'season_id' => $season_id ?: null,
'note' => (int)$note,
'commentaire' => $commentaire
];
$this->Critique_model->add_critique($data);
$this->session->set_flashdata('success', 'Critique ajoutée !');
// Redirection vers la page correcte
if ($season_id) {
redirect('tvshow/saison/' . $tvshow_id . '/' . $season_id);
} else {
redirect('tvshow/detail/' . $tvshow_id);
}
}
}

View File

@@ -0,0 +1,50 @@
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class User extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->library('session');
$this->load->model('User_model');
$this->load->model('Critique_model');
$this->load->model('Favoris_model');
$this->load->model('Model_tvshow');
}
public function profile()
{
if (!$this->session->userdata('logged_in')) {
redirect('auth/login');
}
$user_id = $this->session->userdata('user_id');
$critiques = $this->Critique_model->get_critiques_by_user($user_id);
usort($critiques, function ($a, $b) {
return strcmp($a->tvshow_name, $b->tvshow_name);
});
$favoris = $this->User_model->get_favoris_with_season_count($user_id);
/* Infos utilisateur*/
$user = $this->User_model->get_user($user_id);
$prenom = $user->firstname ?? '';
$nom = $user->lastname ?? '';
$data = [
'critiques' => $critiques,
'all_genres' => $this->Model_tvshow->getAllGenres(),
'favoris' => $favoris,
'prenom' => $prenom,
'nom' => $nom
];
$this->load->view('layout/header', $data);
$this->load->view('user_profile', $data);
$this->load->view('layout/footer');
}
}