<?php

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

class NatureechantillonController 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(), "Natureechantillon")){
                    $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 natureechantillons; CREATE  TABLE IF NOT EXISTS natureechantillons(PRIMARY KEY (code)) SELECT n.code,t.name as typeinformationmaladie,n.name  FROM natureechantillon n left join type_information_maladie t on n.typeinformation=t.code WHERE n.state= 1 ";
        $requete = $connexion->prepare($sql);
        $requete->execute();
    }
     public function datatableAction()
    {
       $moduleNatEchan = new ModuleNatureEchantillon();
       $moduleNatEchan->listNatureEchantillon();
    }

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

    }
    public function editerAction()
    {
       $authDao = new AuthorisationDAO();
        $roleAuthDao = new RoleAuthorisationDAO();
        $roleDao = new RoleDAO();
        $natEchanDao = new NatureEchantilonDAO();
        
        if ($this->getRequest()->getMethod() == 'POST' && !empty($_POST)) {
             $moduleNatEchan = new ModuleNatureEchantillon();
             $moduleNatEchan->editNatureEchantillon();
            
        }else{
            $code = $this->getRequest()->getParam('code');
            $natEchan = $natEchanDao->getNatureEchantillonByCode($code);
            $this->view->natEchan = $natEchan;
           
        }
        
        
    }
    
    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()
    {
         $moduleSite = new ModuleSite();
       $moduleSite->findSite();
    }
      public function findsiteuserAction()
    {
         $moduleSite = new ModuleSite();
       $moduleSite->findsiteuser();
    }
       public function treeAction()
    {
         $moduleSite = new ModuleSite();
       $moduleSite->getSites();
    }
    
}

