<?php

namespace Modules\Ajax\Repository;

use Helpers\Outil\Sql;

class Favoris
{
    
    private $pdo;
    
    public function __construct(\PDO $pdo)
    {
        $this->pdo = $pdo;
    }
    
    public function listeFavoris()
    {
        $req = 'SELECT * FROM FAVORIS with (nolock) WHERE ID_USER = :idUser';
        
        $arrayReq = [
            ':idUser' => $_SESSION['Session_Login']
        ];
        
        $fav = $this->pdo->prepare($req);
        $fav->execute($arrayReq);
        
        $return = $fav->fetchAll($this->pdo::FETCH_ASSOC);
        if ($return) {
            return $return;
        } else {
            return [];
        }
    }
    
    public function listeFavorisAccesRapide()
    {
        $req = 'SELECT * FROM FAVORIS with (nolock) WHERE ID_USER=:idUser AND ACCES_RAPIDE = '.SQL::setBoolSql(1).' ORDER BY ID_FAV';
        
        $arrayReq = [
            ':idUser' => $_SESSION['Session_Login']
        ];
        
        $fav = $this->pdo->prepare($req);
        $fav->execute($arrayReq);

        $return = $fav->fetchAll($this->pdo::FETCH_ASSOC);
        if ($return) {
            return $return;
        } else {
            return [];
        }
    }
    
    public function updateFavorisAccesRapide($idFav, $accesRapide)
    {
        $this->pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
        $req = 'UPDATE FAVORIS SET ACCES_RAPIDE='.SQL::setBoolSql($accesRapide).' WHERE ID_FAV=:idFav';
        
        $arrayReq = [
            ':idFav' => $idFav
        ];
        
        $fav = $this->pdo->prepare($req);
        $fav->execute($arrayReq);
    }
}
