projet_php/controllers/AdminCtrl.php
2026-03-03 22:23:25 +01:00

105 lines
3.9 KiB
PHP

<?php
namespace Controllers;
use Models\CategoryModel;
use Models\UserModel;
use Models\AuthorisationModel;
use Entities\Category;
use Entities\Authorisation;
use Entities\User;
/**
* Le controller de la partie accessible uniquement par l'admin
* @author Laura
*/
class AdminCtrl extends MotherCtrl{
/**
* Page Admin
*/
public function admin(){
if (!isset($_SESSION['user']) && ($_SESSION['user']['user_status'] != 1 )){
$error = new \Controllers\ErrorCtrl();
return $error->error_403();
}
$objCategoryModel = new CategoryModel;
if (!empty($_POST['new_category'])) {
$newCat = new Category();
$newCat->setName($_POST['new_category']);
$objCategoryModel->insertCategory($newCat);
$_SESSION['success'] = "La catégorie a bien été ajoutée";
header('Location: '.$_ENV['BASE_URL'].'/admin/admin');
exit;
}
if (!empty($_POST['id_to_edit']) && !empty($_POST['new_name'])) {
$editCat = new Category();
$editCat->setId($_POST['id_to_edit']);
$editCat->setName($_POST['new_name']);
$objCategoryModel->editCategory($editCat);
$_SESSION['success'] = "La catégorie a bien été modifiée";
header('Location: '.$_ENV['BASE_URL'].'/admin/admin');
exit;
}
$arrCategory = $objCategoryModel->findAllCategory();
$arrCategoryToDisplay = array();
foreach($arrCategory as $arrDetCategory){
$objCategory = new Category;
$objCategory->hydrate($arrDetCategory);
$arrCategoryToDisplay[] = $objCategory;
}
$objUserModel = new UserModel;
if (!empty($_POST['action'])) {
$intUserId = (int)$_POST['user_id'];
if ($intUserId > 0) {
if ($_POST['action'] === 'update_status' && !empty($_POST['new_status'])) {
$objUser = new User();
$objUser->setId($intUserId);
$objUser->setStatus((int)$_POST['new_status']);
if ($objUserModel->editStatus($objUser)) {
$_SESSION['success'] = "Le statut a bien été modifié !";
}
}
elseif ($_POST['action'] === 'delete_user') {
$objUserModel->delete_soft($intUserId);
$_SESSION['success'] = "L'utilisateur a été supprimé.";
}
header('Location: '.$_ENV['BASE_URL'].'/admin/admin');
exit;
}
}
$arrUser = $objUserModel->findAllUsers();
$arrUserToDisplay = array();
foreach($arrUser as $arrDetUser){
$objUser = new User;
$objUser->hydrate($arrDetUser);
$arrUserToDisplay[] = $objUser;
}
$objAuthorisationModel = new AuthorisationModel;
$arrAuthorisation = $objAuthorisationModel->findAllAuthorisation();
$arrAuthorisationToDisplay = array();
foreach($arrAuthorisation as $arrDetAuthorisation){
$objAuthorisation = new Authorisation;
$objAuthorisation->hydrate($arrDetAuthorisation);
$arrAuthorisationToDisplay[] = $objAuthorisation;
}
$this->_arrData['arrCategoryToDisplay'] = $arrCategoryToDisplay;
$this->_arrData['arrUserToDisplay'] = $arrUserToDisplay;
$this->_arrData['arrAuthorisationToDisplay'] = $arrAuthorisationToDisplay;
$this->_display("admin");
}
}