<?php

if (core::ifPost("from") AND core::getPost("from") == "user") {
    
    if(core::getPost("id") == "add"){
        user::add_user(core::getPost());

        historique::recRef("/user-".user::lastUser().".html");
        historique::add(array(
            "idType" => historique::getIdRef("ACTION"),
            "idUser" => session::getId(),
            "idPage" => historique::getIdRef("/user-".user::lastUser().".html"),
            "log" => "Création du compte de " . user::lastUser()
        ));

        alert::recSuccess("Création du compte de ". user::getNameById(user::lastUser()));
        header("Location: /user-" . user::lastUser() . ".html");
        exit();
        
    } else {
        user::maj_user(core::getPost());

        historique::recRef("/user-".core::getPost("id").".html");
        historique::add(array(
            "idType" => historique::getIdRef("ACTION"),
            "idUser" => session::getId(),
            "idPage" => historique::getIdRef("/user-".core::getPost("id").".html"),
            "log" => "Modification du compte de " . user::getNameById(core::getPost("id"))
        ));

        if(core::getPost("id") == session::getId()){
            alert::recSuccess("Modification de votre compte");
            header("Location: /user.html");
        } else {
            alert::recSuccess("Modification du compte de ". user::getNameById(core::getPost("id")));
            header("Location: /user-" . core::getPost("id") . ".html");
        }
        exit();
    }
    
} else {
    header('HTTP/1.0 401 Unauthorized');
    exit();
}

