| 858 | 
           gduche | 
           1 | 
           <?php
  | 
        
        
            | 
            | 
           2 | 
           // declare(encoding='UTF-8');
  | 
        
        
            | 
            | 
           3 | 
           /**
  | 
        
        
           | 1795 | 
           jpm | 
           4 | 
            * Retourne la liste commentaires correspondant aux filtres passés dans l'url :
  | 
        
        
            | 
            | 
           5 | 
            * http://localhost/del/services/0.1/commentaires => liste tous les commentaires
  | 
        
        
            | 
            | 
           6 | 
            * Filtres : voir le fichier de config : commentaires.masques_possibles
  | 
        
        
           | 858 | 
           gduche | 
           7 | 
            *
  | 
        
        
           | 1795 | 
           jpm | 
           8 | 
            * @category   DEL
  | 
        
        
            | 
            | 
           9 | 
            * @package    Services
  | 
        
        
            | 
            | 
           10 | 
            * @subpackage Commentaires
  | 
        
        
            | 
            | 
           11 | 
            * @version    0.1
  | 
        
        
            | 
            | 
           12 | 
            * @author     Mathias CHOUET <mathias@tela-botanica.org>
  | 
        
        
            | 
            | 
           13 | 
            * @author     Jean-Pascal MILCENT <jpm@tela-botanica.org>
  | 
        
        
            | 
            | 
           14 | 
            * @author     Aurelien PERONNET <aurelien@tela-botanica.org>
  | 
        
        
            | 
            | 
           15 | 
            * @license    GPL v3 <http://www.gnu.org/licenses/gpl.txt>
  | 
        
        
            | 
            | 
           16 | 
            * @license    CECILL v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt>
  | 
        
        
            | 
            | 
           17 | 
            * @copyright  1999-2014 Tela Botanica <accueil@tela-botanica.org>
  | 
        
        
           | 858 | 
           gduche | 
           18 | 
            */
  | 
        
        
            | 
            | 
           19 | 
              | 
        
        
            | 
            | 
           20 | 
           class ListeCommentaires {
  | 
        
        
           | 1612 | 
           jpm | 
           21 | 
              | 
        
        
           | 858 | 
           gduche | 
           22 | 
           	private $conteneur;
  | 
        
        
            | 
            | 
           23 | 
           	private $navigation;
  | 
        
        
            | 
            | 
           24 | 
           	private $bdd;
  | 
        
        
           | 1612 | 
           jpm | 
           25 | 
              | 
        
        
           | 1795 | 
           jpm | 
           26 | 
           	private $mapping = array();
  | 
        
        
            | 
            | 
           27 | 
           	private $mappingInverse = array();
  | 
        
        
            | 
            | 
           28 | 
              | 
        
        
           | 858 | 
           gduche | 
           29 | 
           	public function __construct(Conteneur $conteneur = null) {
  | 
        
        
           | 1795 | 
           jpm | 
           30 | 
           		$this->conteneur = ($conteneur == null) ? new Conteneur() : $conteneur;
  | 
        
        
            | 
            | 
           31 | 
           		$this->navigation = $this->conteneur->getNavigation();
  | 
        
        
           | 1793 | 
           jpm | 
           32 | 
           		$this->bdd = $this->conteneur->getBdd();
  | 
        
        
           | 1795 | 
           jpm | 
           33 | 
              | 
        
        
            | 
            | 
           34 | 
           		$this->mapping = $this->conteneur->getParametreTableau('commentaires.mapping');
  | 
        
        
            | 
            | 
           35 | 
           		$this->mappingInverse = array_flip($this->mapping);
  | 
        
        
           | 858 | 
           gduche | 
           36 | 
           	}
  | 
        
        
           | 1612 | 
           jpm | 
           37 | 
              | 
        
        
           | 858 | 
           gduche | 
           38 | 
           	/**
  | 
        
        
            | 
            | 
           39 | 
           	 * Méthode principale de la classe.
  | 
        
        
           | 1612 | 
           jpm | 
           40 | 
           	 * Lance la récupération des images dans la base et les place dans un objet ResultatService
  | 
        
        
           | 858 | 
           gduche | 
           41 | 
           	 * pour l'afficher.
  | 
        
        
            | 
            | 
           42 | 
           	 * @param array $ressources les ressources situées après l'url de base (ex : http://url/ressource1/ressource2)
  | 
        
        
            | 
            | 
           43 | 
           	 * @param array $parametres les paramètres situés après le ? dans l'url
  | 
        
        
            | 
            | 
           44 | 
           	 * */
  | 
        
        
           | 1795 | 
           jpm | 
           45 | 
           	public function consulter($ressources) {
  | 
        
        
           | 858 | 
           gduche | 
           46 | 
           		// Lancement du service
  | 
        
        
            | 
            | 
           47 | 
           		$commentaires = $this->chargerCommentaires();
  | 
        
        
           | 1795 | 
           jpm | 
           48 | 
           		$total = $this->compterCommentairesTotal();
  | 
        
        
            | 
            | 
           49 | 
              | 
        
        
           | 858 | 
           gduche | 
           50 | 
           		$commentaires = $this->formaterCommentaires($commentaires);
  | 
        
        
            | 
            | 
           51 | 
           		$this->navigation->setTotal($total);
  | 
        
        
           | 863 | 
           gduche | 
           52 | 
              | 
        
        
           | 858 | 
           gduche | 
           53 | 
           		$resultat = new ResultatService();
  | 
        
        
           | 1794 | 
           jpm | 
           54 | 
           		$resultat->corps = array('entete' => $this->navigation->getEntete(), 'resultats' => $commentaires);
  | 
        
        
           | 858 | 
           gduche | 
           55 | 
           		return $resultat;
  | 
        
        
            | 
            | 
           56 | 
           	}
  | 
        
        
           | 1612 | 
           jpm | 
           57 | 
              | 
        
        
           | 858 | 
           gduche | 
           58 | 
           	/**
  | 
        
        
           | 1795 | 
           jpm | 
           59 | 
           	* Chargement depuis la bdd de tous les commentaires
  | 
        
        
            | 
            | 
           60 | 
           	* */
  | 
        
        
            | 
            | 
           61 | 
           	private function chargerCommentaires() {
  | 
        
        
            | 
            | 
           62 | 
           		$requete = 'SELECT DISTINCT SQL_CALC_FOUND_ROWS * '.
  | 
        
        
            | 
            | 
           63 | 
           			'FROM del_commentaire '.
  | 
        
        
            | 
            | 
           64 | 
           			'WHERE '.$this->creerClauseWhere().' '.
  | 
        
        
            | 
            | 
           65 | 
           			'LIMIT '.$this->navigation->getDepart().','.$this->navigation->getLimite().' '.
  | 
        
        
            | 
            | 
           66 | 
           			' -- '.__FILE__.' : '.__LINE__;
  | 
        
        
            | 
            | 
           67 | 
           		$resultat = $this->bdd->recupererTous($requete);
  | 
        
        
            | 
            | 
           68 | 
           		return is_array($resultat) ? $resultat : array();
  | 
        
        
           | 858 | 
           gduche | 
           69 | 
           	}
  | 
        
        
           | 1612 | 
           jpm | 
           70 | 
              | 
        
        
           | 1795 | 
           jpm | 
           71 | 
           	private function creerClauseWhere() {
  | 
        
        
           | 858 | 
           gduche | 
           72 | 
           		$where = array();
  | 
        
        
           | 1806 | 
           jpm | 
           73 | 
           		$filtres = $this->navigation->getFiltre();
  | 
        
        
           | 1795 | 
           jpm | 
           74 | 
           		if (!empty($filtres)) {
  | 
        
        
            | 
            | 
           75 | 
           			foreach ($filtres as $cle => $valeur) {
  | 
        
        
            | 
            | 
           76 | 
           				$where[] = $this->mappingInverse[$cle].' = '.$this->bdd->proteger($valeur);
  | 
        
        
           | 1612 | 
           jpm | 
           77 | 
           			}
  | 
        
        
           | 858 | 
           gduche | 
           78 | 
           		}
  | 
        
        
           | 1795 | 
           jpm | 
           79 | 
           		$clauseWhere =  (!empty($where)) ? ' '.implode(' AND ', $where).' ' : ' 1 ';
  | 
        
        
            | 
            | 
           80 | 
           		return $clauseWhere;
  | 
        
        
           | 858 | 
           gduche | 
           81 | 
           	}
  | 
        
        
           | 1612 | 
           jpm | 
           82 | 
              | 
        
        
           | 858 | 
           gduche | 
           83 | 
           	/**
  | 
        
        
           | 1795 | 
           jpm | 
           84 | 
           	 * Compter le nombre total de commentaires dans la base vis à vis des filtres de l'url.
  | 
        
        
            | 
            | 
           85 | 
           	 * Utilisation du mécanisme SQL_CALC_FOUND_ROW de Mysql pour éviter une deuxième requete avec un COUNT.
  | 
        
        
            | 
            | 
           86 | 
           	 */
  | 
        
        
            | 
            | 
           87 | 
           	private function compterCommentairesTotal() {
  | 
        
        
           | 858 | 
           gduche | 
           88 | 
           		$requete = 'SELECT FOUND_ROWS() AS nbre ';
  | 
        
        
            | 
            | 
           89 | 
           		$resultats = $this->bdd->recuperer($requete);
  | 
        
        
            | 
            | 
           90 | 
           		return (int) $resultats['nbre'];
  | 
        
        
            | 
            | 
           91 | 
           	}
  | 
        
        
           | 1612 | 
           jpm | 
           92 | 
              | 
        
        
           | 858 | 
           gduche | 
           93 | 
           	/**
  | 
        
        
            | 
            | 
           94 | 
           	*  Formater les commentaires
  | 
        
        
            | 
            | 
           95 | 
           	*  @param $commentaires les commentaires à mettre à jour
  | 
        
        
            | 
            | 
           96 | 
           	*  @return $commentaires les commentaires mis à jour
  | 
        
        
            | 
            | 
           97 | 
           	* */
  | 
        
        
            | 
            | 
           98 | 
           	private function formaterCommentaires($commentaires) {
  | 
        
        
           | 1795 | 
           jpm | 
           99 | 
           		$retour = array();
  | 
        
        
            | 
            | 
           100 | 
           		foreach ($commentaires as $idCommentaire => $infos) {
  | 
        
        
            | 
            | 
           101 | 
           			$idCommentaire = $infos['id_commentaire'];
  | 
        
        
            | 
            | 
           102 | 
           			foreach ($this->mapping as $nomChampBdd => $nomAttributSortie) {
  | 
        
        
            | 
            | 
           103 | 
           				$retour[$idCommentaire][$nomAttributSortie] = $infos[$nomChampBdd];
  | 
        
        
           | 858 | 
           gduche | 
           104 | 
           			}
  | 
        
        
            | 
            | 
           105 | 
           		}
  | 
        
        
           | 1795 | 
           jpm | 
           106 | 
           		return $retour;
  | 
        
        
           | 858 | 
           gduche | 
           107 | 
           	}
  | 
        
        
           | 1795 | 
           jpm | 
           108 | 
           }
  |