Subversion Repositories eFlore/Applications.del

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
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 $masque;
25
	private $bdd;
26
	private $parametres = array();
27
	private $ressources = array();
1612 jpm 28
 
1795 jpm 29
	private $mapping = array();
30
	private $mappingInverse = array();
31
 
858 gduche 32
	public function __construct(Conteneur $conteneur = null) {
1795 jpm 33
		$this->conteneur = ($conteneur == null) ? new Conteneur() : $conteneur;
34
		$this->navigation = $this->conteneur->getNavigation();
35
		$this->masque = $this->conteneur->getMasque();
1793 jpm 36
		$this->bdd = $this->conteneur->getBdd();
1795 jpm 37
 
38
		$this->mapping = $this->conteneur->getParametreTableau('commentaires.mapping');
39
		$this->mappingInverse = array_flip($this->mapping);
858 gduche 40
	}
1612 jpm 41
 
858 gduche 42
	/**
43
	 * Méthode principale de la classe.
1612 jpm 44
	 * Lance la récupération des images dans la base et les place dans un objet ResultatService
858 gduche 45
	 * pour l'afficher.
46
	 * @param array $ressources les ressources situées après l'url de base (ex : http://url/ressource1/ressource2)
47
	 * @param array $parametres les paramètres situés après le ? dans l'url
48
	 * */
1795 jpm 49
	public function consulter($ressources) {
858 gduche 50
		// Lancement du service
51
		$commentaires = $this->chargerCommentaires();
1795 jpm 52
		$total = $this->compterCommentairesTotal();
53
 
858 gduche 54
		$commentaires = $this->formaterCommentaires($commentaires);
55
		$this->navigation->setTotal($total);
863 gduche 56
 
858 gduche 57
		$resultat = new ResultatService();
1794 jpm 58
		$resultat->corps = array('entete' => $this->navigation->getEntete(), 'resultats' => $commentaires);
858 gduche 59
		return $resultat;
60
	}
1612 jpm 61
 
858 gduche 62
	/**
1795 jpm 63
	* Chargement depuis la bdd de tous les commentaires
64
	* */
65
	private function chargerCommentaires() {
66
		$requete = 'SELECT DISTINCT SQL_CALC_FOUND_ROWS * '.
67
			'FROM del_commentaire '.
68
			'WHERE '.$this->creerClauseWhere().' '.
69
			'LIMIT '.$this->navigation->getDepart().','.$this->navigation->getLimite().' '.
70
			' -- '.__FILE__.' : '.__LINE__;
71
		$resultat = $this->bdd->recupererTous($requete);
72
		return is_array($resultat) ? $resultat : array();
858 gduche 73
	}
1612 jpm 74
 
1795 jpm 75
	private function creerClauseWhere() {
858 gduche 76
		$where = array();
1795 jpm 77
		$filtres = $this->masque->getMasque();
78
		if (!empty($filtres)) {
79
			foreach ($filtres as $cle => $valeur) {
80
				$where[] = $this->mappingInverse[$cle].' = '.$this->bdd->proteger($valeur);
1612 jpm 81
			}
858 gduche 82
		}
1795 jpm 83
		$clauseWhere =  (!empty($where)) ? ' '.implode(' AND ', $where).' ' : ' 1 ';
84
		return $clauseWhere;
858 gduche 85
	}
1612 jpm 86
 
858 gduche 87
	/**
1795 jpm 88
	 * Compter le nombre total de commentaires dans la base vis à vis des filtres de l'url.
89
	 * Utilisation du mécanisme SQL_CALC_FOUND_ROW de Mysql pour éviter une deuxième requete avec un COUNT.
90
	 */
91
	private function compterCommentairesTotal() {
858 gduche 92
		$requete = 'SELECT FOUND_ROWS() AS nbre ';
93
		$resultats = $this->bdd->recuperer($requete);
94
		return (int) $resultats['nbre'];
95
	}
1612 jpm 96
 
858 gduche 97
	/**
98
	*  Formater les commentaires
99
	*  @param $commentaires les commentaires à mettre à jour
100
	*  @return $commentaires les commentaires mis à jour
101
	* */
102
	private function formaterCommentaires($commentaires) {
1795 jpm 103
		$retour = array();
104
		foreach ($commentaires as $idCommentaire => $infos) {
105
			$idCommentaire = $infos['id_commentaire'];
106
			foreach ($this->mapping as $nomChampBdd => $nomAttributSortie) {
107
				$retour[$idCommentaire][$nomAttributSortie] = $infos[$nomChampBdd];
858 gduche 108
			}
109
		}
1795 jpm 110
		return $retour;
858 gduche 111
	}
1795 jpm 112
}