<?php
include APPLICATION_PATH . '/api/UtilisateurApi.php';

class IndexController extends Zend_Controller_Action
{

    public function init()
    {
         header('Access-Control-Allow-Origin: *');
    }

    public function indexAction()
    {
        
        
    }

    public function connexionAction()
    {
         $login = isset($_POST["login"])?  $_POST["login"] : null;
       $password = isset($_POST['password'])? sha1(md5($_POST["password"])) : null;
       $userDao = new UtilisateurDAO();
       $roleAuthDao = new RoleAuthorisationDAO();
       $user =  $userDao->getUtilisateurLoginPassword($login, $password);
       
       if ( is_object($user)){
           session_cache_limiter('private_no_expire');
           session_cache_expire(60);
           session_start();
           $_SESSION["codeUser"] = $user->getCode();
           $_SESSION["nameUser"] = $user->getName();
            //Module d'enregistrement des logs 
            try {
                $f = new Fonction();
               
                $descr = "Connexion de l'utilisateur " . $user->getName() . ", dont le login est " . $user->getLogin() . "";
                
                $f->EnregistrerLog($descr, $user);
                
            } catch (Exception $exc) {
                  $response = array('type' => "error", 'msg' => $exc->getMessage(),"result"=>array());
                 echo json_encode($response);
                 exit();
            }
            $authDao = new AuthorisationDAO();
            $listeParent = $authDao->listAuthParent();
            $list=$roleAuthDao->listRoleAuthorisationByRole($user->getRole());
            
                 $object = new stdClass();
                $object->name = $user->getName();
                $object->login = $user->getLogin();
                $object->password = $user->getLogin();
               // $object->birthday = $user->getBirthday();
                 $object->phone= $user->getPhone();
                 //$object->cni= $user->getCni();
                 $object->sexe= $user->getSexe();
                 $roleAuthDao = new RoleAuthorisationDAO();
                 $listRoleAuth = $roleAuthDao->getRoleAuthorisationByRole($user->getRole());
                 $listAuth = array();
                 foreach ($list as $roleAuth){
                     
                   $auth = new stdClass();
                   $auth->label = $roleAuth->getAuthorisation()->getLabel();
                   $auth->code = $roleAuth->getAuthorisation()->getCode();
                   $listAuth[] = $auth;
                 }
                 $object->role= array('code'=>$user->getRole()->getCode(),'label'=>$user->getRole()->getLabel(),'authorisation'=>$listAuth);
            
           echo json_encode(array("type" => "success",'msg' => "User registration success","result"=>$object));
            exit();
       }else{
           
           echo json_encode(array("type" => "error","result"=>array()));
       }
       exit();
         
       
    }

    public function deconnexionAction()
    {
            
        
        if ( isset($_SESSION["codeUser"])){
            //Module d'enregistrement des logs  
            $codeUser = $_SESSION["codeUser"];
            $UserDAO = new UtilisateurDAO();
            $user = $UserDAO->getUtilisateurByCode($codeUser);
            
            try {
                $f = new Fonction();
               
                $descr = "Deconnexion de l'utilisateur " . $user->getName() . ", dont le login est " . $user->getLogin() . "";
                
                $f->EnregistrerLog($descr, $user);
                
            } catch (Exception $exc) {

            }
            
            $_SESSION = array();
        }
        
        session_destroy();
        $this->_helper->redirector('index' , 'Index');
    }



}







