<?php

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

class TypeinformationMaladieController 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 type_information_maladies; CREATE  TABLE IF NOT EXISTS type_information_maladies(PRIMARY KEY (code)) SELECT t.code,t.name  FROM type_information_maladie t WHERE t.state= 1 ";
        $requete = $connexion->prepare($sql);
        $requete->execute();
    }
     public function datatableAction()
    {
       $moduleTypeInfoMaladie = new ModuleTypeInformationMaladie();
       $moduleTypeInfoMaladie->listTypeInformationMaladie();
    }

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

    }
    public function editerAction()
    {
       $authDao = new AuthorisationDAO();
        $roleAuthDao = new RoleAuthorisationDAO();
        $roleDao = new RoleDAO();
        $typeInfoMaladieDao = new TypeInformationMaladieDAO();
        
        if ($this->getRequest()->getMethod() == 'POST' && !empty($_POST)) {
             $moduleTypeInfoMaladie = new ModuleTypeInformationMaladie();
             $moduleTypeInfoMaladie->editTypeInformationMaladie();
            
        }else{
            $code = $this->getRequest()->getParam('code');
            $typeInfoMaladie = $typeInfoMaladieDao->getNatureEchantillonByCode($code);
            $this->view->typeInfoMaladie = $typeInfoMaladie;
           
        }
        
        
    }
    
    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();
    }
    
}

