<?php

namespace Modules\Informations\Repository;

class Informations {
    
    private $pdo;
    
    public function __construct(\PDO $pdo)
    {
        $this->pdo = $pdo;
    }
    
    public function listePatch()
    {
        $req = "SELECT distinct(VERSPROD), SUBSTRING(VERSPROD, 1,7) VERSION  FROM VERSPROD where DTEFINMR!='00000000' and VERSPROD LIKE '10.%' and VERSPROD >= '10.4' order by SUBSTRING(VERSPROD, 1,7) DESC, VERSPROD DESC";
        
        $requete = $this->pdo->prepare($req);
        
        $requete->execute();
        return $requete->fetchAll($this->pdo::FETCH_ASSOC);
    }
    
    public function listeVersion()
    {
        $req = "SELECT distinct(SUBSTRING(VERSPROD, 1,7)) VERSION  FROM VERSPROD where VERSPROD LIKE '10.%' and VERSPROD >= '10.4' order by SUBSTRING(VERSPROD, 1,7) DESC";
        
        $requete = $this->pdo->prepare($req);
        
        $requete->execute();
        return $requete->fetchAll($this->pdo::FETCH_ASSOC);
    }
    
    public function listeFixe($version, $post)
    {
        $codeClient = $_SESSION['user_logged']['CODECLT'];
        
        $filtre = '';
        if($codeClient!='OXAR&D')
        {
            $filtre = "AND (CODECLT = '' or CODECLT = :CODECLT)";
            $arrayReq[':CODECLT'] = $codeClient;
        }
        
        $languedifferentFR = '0';
        if($_SESSION['Session_Langue']!='fr')
        {
            $languedifferentFR = '1';
        }
        
        $req = "select * from 
                (
                	SELECT a.CODPROD, a.Type_de_cause, a.CAUSE_EVENEMENT, 
                	CASE 
                		WHEN (1=$languedifferentFR and a.TITRE_ENG != '') THEN a.TITRE_ENG
                		ELSE a.TITEVE
                	END TITEVE, 
                	CASE 
                		WHEN (1=$languedifferentFR and c.MEMO != '') THEN c.MEMO
                		ELSE b.MEMO
                	END MEMO
                	from VERSLIGN a (nolock) 
                	LEFT OUTER JOIN MEMOS b ON a.NMCLT=b.NM 
                	LEFT OUTER JOIN MEMOS c ON a.NMCLI_ENG=c.NM
                	where VERSPROD=:version $filtre
                ) fixe";
        
        $arrayReq[':version'] = $version;

        $whereArray = \Helpers\Filter\FilterTable::splitFilter($post, 0, 0, 0);
        
        if(count($whereArray )>  0)
        {
            $req .= $whereArray[0].$whereArray['tri'];
            
            if(count($whereArray[1]) != 0)
                $arrayReq = array_merge($arrayReq, $whereArray[1]);
        }
        
        $requete = $this->pdo->prepare($req);
        
        $requete->execute($arrayReq);
        
        return $requete->fetchAll($this->pdo::FETCH_ASSOC);
    }
    
    public function listeModule($version)
    {
        $req = "select distinct(CODPROD) from VERSLIGN where VERSPROD=:version";
        
        $arrayReq= [
            ':version' => $version,
        ];
        
        $requete = $this->pdo->prepare($req);

        $requete->execute($arrayReq);
        return $requete->fetchAll($this->pdo::FETCH_ASSOC);
    }
    
    
    public function contentApropos()
    {
        $req = "select * from VERSION_NEWS where VERSPROD='APROPOS' order by BLOC";

        
        $requete = $this->pdo->prepare($req);
        
        $requete->execute();
        return $requete->fetchAll($this->pdo::FETCH_ASSOC);
    }
    
    public function contentVersion($version)
    {
        $req = "select * from VERSION_NEWS where VERSPROD=:version order by BLOC";
        
        $arrayReq= [
            ':version' => $version,
        ];
        
        $requete = $this->pdo->prepare($req);
        
        $requete->execute($arrayReq);
        return $requete->fetchAll($this->pdo::FETCH_ASSOC);
    }
    
    public function listePatchByVersion($version)
    {
        $req = "SELECT distinct(VERSPROD), SUBSTRING(VERSPROD, 1,7) VERSION  FROM VERSPROD 
                where VERSPROD LIKE '10.%' and VERSPROD >= '10.4' and SUBSTRING(VERSPROD, 1,7)=:version
                order by SUBSTRING(VERSPROD, 1,7) DESC, VERSPROD DESC";
        
        $arrayReq= [
            ':version' => $version,
        ];
        
        $requete = $this->pdo->prepare($req);
        
        $requete->execute($arrayReq);
        return $requete->fetchAll($this->pdo::FETCH_ASSOC);
    }
    
    public function getRsClient($codeClient)
    {
        $req = "select RS from ADRESSES where CODECLT = :codeClient";
        
        $arrayReq = [':codeClient' => $codeClient];
        
        $requete = $this->pdo->prepare($req);
        
        $requete->execute($arrayReq);
        return $requete->fetch($this->pdo::FETCH_ASSOC);
    }
    
}