<?php

include APPLICATION_PATH . '/api/RoleApi.php';

class RoleController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
        session_start();
        if ( isset($_SESSION["codeUser"])){
            $_SESSION["derniereAction"] = new \DateTime();
             $utilisateurDao = new UtilisateurDAO();
             $roleAuthDao = new RoleAuthorisationDAO();
            $user = $utilisateurDao->getUtilisateurByCode($_SESSION["codeUser"]);
            if(is_object($user)){
                if(!$roleAuthDao->existController($user->getRole(), "Role")){
                    $this->_helper->redirector('index' , 'Index');
                }
            }else{
                $this->_helper->redirector('index' , 'Index');
            }
        }else{
            $this->_helper->redirector('index' , 'Index');
        }
    }

    public function indexAction()
    {
         $userDao = new UtilisateurDAO();
        $connexion = $userDao->_em->getConnection();
        $sql = "drop table if exists roles; CREATE  TABLE IF NOT EXISTS roles(PRIMARY KEY (code)) SELECT code,label,description  FROM role r WHERE r.state= 1 ";
        $requete = $connexion->prepare($sql);
        $requete->execute();
    }
     public function datatableAction()
    {
         
          
       
       $moduleRole = new ModuleRole();
       $moduleRole->listRole();
             
    }

    public function enregistrerAction()
    {
       
        if (!empty($_POST)) {
            
           $moduleRole = new ModuleRole();
           $moduleRole->addRole();
        } else {
            $authDao = new AuthorisationDAO();
            $this->view->auths = $authDao->listAuthParent();
        }

    }
    public function editerAction()
    {
       $authDao = new AuthorisationDAO();
        $roleAuthDao = new RoleAuthorisationDAO();
        $roleDao = new RoleDAO();
        
        if ($this->getRequest()->getMethod() == 'POST' && !empty($_POST)) {
             $moduleRole = new ModuleRole();
             $moduleRole->editRole();
            
        }else{
            $code = $this->getRequest()->getParam('code');
            $leRole = $roleDao->getRoleByCode($code);
            $this->view->auths = $authDao->listAuthParent();
            $roleAuthDao = new RoleAuthorisationDAO();
            $listAuth= $roleAuthDao->listRoleAuthorisationByRole($leRole);
            
            $this->view->role = $leRole;
        }
        
        
    }
    
    public function deleteAction()
    {
        if (!empty($_POST)) {

            $moduleRole = new ModuleRole();
             $moduleRole->deleteRole();
        }else{
                    $rep = array('type' => "error", 'msg' => "Requete mal formée");
                          echo json_encode($rep);
                         exit();
        }
    }
       public function findAction()
    {
        try{
           $nom = $_POST["nom"];
           $profilDao = new ProfilDAO();
           $liste= $profilDao->chercherProfilParNom($nom);
           $resultat = array();
           foreach ( $liste as $profil ){
               $resultat[]=["id"=>$profil->getIdprofil(),"value"=>$profil->getNom()];
              
           }
           echo json_encode(array('data'=>$resultat,'nb'=>count($resultat)));
        } catch (Exception $ex) {
           echo json_encode(array("type" => "error"));
        }
        
        exit();
        
        
    }
    
        public function findauthAction()
    {
       $moduleRole = new ModuleRole();
       $moduleRole->findauth();
        
        
    }
 public function findauthchoiceAction()
    {
       $moduleRole = new ModuleRole();
       $moduleRole->findchoice();
       
    }

}

