<?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 findauthAction()
            {
               $moduleRole = new ModuleRole();
               $moduleRole->findauth();


            }
     public function allroleAction()
            {
               $moduleRole = new ModuleRole();
               $moduleRole->allrole();


            }
 public function findauthchoiceAction()
    {
       $moduleRole = new ModuleRole();
       $moduleRole->findchoice();
       
    }

}

