<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Log
 *
 * @author Messanga Landry <messangalandry@gmail.com>
 */
include APPLICATION_PATH . '/Models/Log.php';

class LogDAO {

    //put your code here
    public $log;

    function LogDAO() {
        $this->log = new \Application\Models\Log();
        $registry = Zend_Registry::getInstance();
        $this->_em = $registry->entitymanager;
    }

    //Enregistrer un log
    public function enregistrer($log) {

        $this->_em->persist($log);
        $this->_em->flush();
    }

    //Supprimer un log
    public function supprimer($log) {

        $this->_em->merge($log);
        $this->_em->flush();
    }

    //Lister tous les logs
    public function listerLog() {
        $liste = $this->_em->getRepository('\Application\Models\Log')
                ->findAll();
        return $liste;
    }

    //Obtenir log par utilisateur
    public function obtenirLogParTousCriteres($user = null, \DateTime $dateDeb = null, \DateTime $dateFin = null) {
        $log = $this->_em->getRepository('\Application\Models\Log')->createQueryBuilder('x')
                ->where("1=1");  //déclarer une condition tjrs vrai avec le where pour pouvoir déclarer les andWhere
        if ($user != null) {
            $log->andWhere(" x.idutilisateur = :Puser ")
                    ->setParameter("Puser", $user);
        }
        if ($dateDeb != null) {
            $log->andWhere(" x.datelog > :Ddeb ")
                    ->setParameter("Ddeb", $dateDeb);
        }
        if ($dateFin != null) {
            $log->andWhere(" x.datelog < :Dfin ")
                    ->setParameter("Dfin", $dateFin);
        }
       // var_dump($log->getQuery()); exit;
        try {          
            $array = array();
            $result = $log->getQuery()->getResult();
          
            $array=$result;
            
            return $array;
           
        } catch (Exception $exc) {        
            echo $exc->getMessage();
        }
    }
}
