<?php 

    require("./models/project_model.php");
    require("./entities/project_entity.php");
    require("./models/category_model.php");
    require("./entities/category_entity.php");
    require("./models/image_model.php");
    require("./entities/image_entity.php");
    require("./models/user_model.php");
    require("./entities/user_entity.php");
    require("./models/authorisation_model.php");
    require("./entities/authorisation_entity.php");
    require("mother_controller.php");

    /**
     * Le controller de la partie accessible uniquement par l'admin
     * @author Laura
     */

    class AdminCtrl extends MotherCtrl{

        public function admin(){

            if (!isset($_SESSION['user']) && ($_SESSION['user']['user_status'] != 1 )){ 
				header("Location:index.php?ctrl=error&action=error_403");
				exit;
			}

            //gestion de l'user
            $objCategoryModel = new CategoryModel;

            if (!empty($_POST['new_category'])) {
                $newCat = new Category();
                $newCat->setName($_POST['new_category']);
                $objCategoryModel->insertCategory($newCat);
                header('Location: index.php?ctrl=admin&action=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);
                header('Location: index.php?ctrl=admin&action=admin');
                exit;
            }

            //affichage select des catégories
            $arrCategory = $objCategoryModel->findAllCategory();
            $arrCategoryToDisplay = array(); 

            foreach($arrCategory as $arrDetCategory){
                $objCategory = new Category;
                $objCategory->hydrate($arrDetCategory);
                $arrCategoryToDisplay[] = $objCategory;
            }

            //gestion de l'user
            $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['message_success'] = "Le statut a bien été modifié !";
                        }
                    } 
                    elseif ($_POST['action'] === 'delete_user') {
                        $objUserModel->delete_soft($intUserId);
                        $_SESSION['message_success'] = "L'utilisateur a été supprimé.";
                    }
                    header("Location: index.php?ctrl=admin&action=admin");
                    exit;
                }
            }

            //affichage select des users
            $arrUser = $objUserModel->findAllUsers();
            $arrUserToDisplay = array(); 

            foreach($arrUser as $arrDetUser){
                $objUser = new User;
                $objUser->hydrate($arrDetUser);
                $arrUserToDisplay[] = $objUser;
            }

            //affichage select des authorisations
            $objAuthorisationModel = new AuthorisationModel;
            $arrAuthorisation = $objAuthorisationModel->findAllAuthorisation();
            $arrAuthorisationToDisplay = array(); 

            foreach($arrAuthorisation as $arrDetAuthorisation){
                $objAuthorisation = new Authorisation;
                $objAuthorisation->hydrate($arrDetAuthorisation);
                $arrAuthorisationToDisplay[] = $objAuthorisation;
            }
            
            //gérer l'affichage
            $this->_arrData['arrCategoryToDisplay']	        = $arrCategoryToDisplay;
            $this->_arrData['arrUserToDisplay']	            = $arrUserToDisplay;
            $this->_arrData['arrAuthorisationToDisplay']	= $arrAuthorisationToDisplay;
			$this->_display("admin"); 
        }
    }
