Subversion Repositories eFlore/Applications.del

Rev

Rev 1827 | Rev 1845 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1390 raphael 1
<?php
1840 jpm 2
// declare(encoding='UTF-8');
1390 raphael 3
/**
1840 jpm 4
 * Listes des images avec leurs infos liées.
1422 raphael 5
 *
1840 jpm 6
 * del/services/0.1/images?navigation.depart=0&navigation.limite=12&tri=votes&ordre=desc
7
 * del/services/0.1/images?navigation.depart=0&navigation.limite=12&tri=votes&ordre=desc&masque=plop
8
 * del/services/0.1/images?navigation.depart=0&navigation.limite=12&tri=votes&ordre=desc&protocole=3
9
 * del/services/0.1/images?navigation.depart=0&navigation.limite=12&tri=votes&ordre=desc&protocole=3&masque=plop
1422 raphael 10
 *
1840 jpm 11
 * @category   DEL
12
 * @package    Services
13
 * @subpackage Images
14
 * @version    0.1
15
 * @author     Mathias CHOUET <mathias@tela-botanica.org>
16
 * @author     Jean-Pascal MILCENT <jpm@tela-botanica.org>
17
 * @author     Aurelien PERONNET <aurelien@tela-botanica.org>
18
 * @license    GPL v3 <http://www.gnu.org/licenses/gpl.txt>
19
 * @license    CECILL v2 <http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt>
20
 * @copyright  1999-2014 Tela Botanica <accueil@tela-botanica.org>
1390 raphael 21
 */
22
 
1840 jpm 23
//restore_error_handler();
24
//restore_exception_handler();
25
//error_reporting(E_ALL);
1514 aurelien 26
class ListeImages {
1390 raphael 27
 
1840 jpm 28
	private $ressources = array();
29
	private $parametres = array();
30
	private $conteneur;
31
	private $bdd;
32
	private $filtrage;
33
	private $sql;
34
	private $navigation;
35
	private $paramsFiltres = array();
36
	private $mappings = array();
1390 raphael 37
 
1827 jpm 38
	public function __construct(Conteneur $conteneur) {
39
		$this->conteneur = $conteneur;
40
		$this->bdd = $this->conteneur->getBdd();
1840 jpm 41
		$this->filtrage = $this->conteneur->getParametresFiltrage();
42
		$this->sql = $this->conteneur->getSql();
43
		$this->navigation = $this->conteneur->getNavigation();
44
 
45
		$this->mappings['observations'] = $this->conteneur->getParametreTableau('observations.mapping');
46
		$this->mappings['images'] = $this->conteneur->getParametreTableau('images.mapping');
1604 jpm 47
	}
1390 raphael 48
 
1604 jpm 49
	public function consulter($ressources, $parametres) {
1840 jpm 50
		$this->ressources = $ressources;
51
		$this->parametres = $parametres;
1390 raphael 52
 
1840 jpm 53
		$this->paramsFiltres = $this->filtrage->filtrerUrlParamsAppliImg();
54
		$this->sql->setParametres($this->paramsFiltres);
55
		$this->sql->ajouterContraintes();
56
		$this->sql->ajouterConstrainteAppliImg();
57
		$this->sql->definirOrdreSqlAppliImg();
1422 raphael 58
 
1840 jpm 59
		$idImgs = $this->getIdImages();
60
		$this->navigation->setTotal($this->getTotal());
1422 raphael 61
 
1840 jpm 62
		// Ce n'est pas la peine de continuer s'il n'y a pas eu de résultats
63
		$resultat = new ResultatService();
64
		$resultat->corps = array('entete' => $this->navigation->getEntete(), 'resultats' => array());
65
		if (count($idImgs) > 0) {
66
			$liaisons = $this->getInfosImages($idImgs);
67
			list($images, $images_indexe_par_id_image) = $this->reformaterImagesDoubleIndex($liaisons);
1390 raphael 68
 
1840 jpm 69
			// Chargement des votes pour ces images et pour *tous* les protocoles
70
			$votes = $this->sql->getVotesDesImages($idImgs);
71
			if ($votes) {
72
				// ATTENTION : $images_indexe_par_id_image est lié par référence à $images !
73
				$this->sql->ajouterInfosVotesProtocoles($votes, $images_indexe_par_id_image);
74
			}
1422 raphael 75
 
1840 jpm 76
			$resultat->corps = array(
77
				'entete' => $this->navigation->getEntete(),
78
				'resultats' => $images);
1604 jpm 79
		}
80
		return $resultat;
81
	}
1495 raphael 82
 
1840 jpm 83
	private function getIdImages() {
84
		$requete = 'SELECT SQL_CALC_FOUND_ROWS id_image '.
85
			'FROM v_del_image AS vdi '.
86
			$this->sql->getJoin().
87
			'WHERE '.$this->sql->getWhere().
88
			$this->sql->getGroupBy().
89
			$this->sql->getOrderBy().
90
			$this->sql->getLimit().
91
			' -- '.__FILE__.':'.__LINE__;
1690 jpm 92
 
1840 jpm 93
		$resultats = $this->bdd->recupererTous($requete);
94
		$idImgs = array();
95
		if ($resultats !== false ) {
96
			foreach ($resultats as $resultat) {
97
				$idImgs[] = $resultat['id_image'];
1604 jpm 98
			}
1584 mathias 99
		}
1840 jpm 100
		return $idImgs;
1604 jpm 101
	}
1486 raphael 102
 
1840 jpm 103
	private function getTotal() {
104
		$resultat = $this->bdd->recuperer('SELECT FOUND_ROWS() AS nbre');
105
		return intval($resultat['nbre']);
1495 raphael 106
	}
1486 raphael 107
 
1840 jpm 108
	private function getInfosImages($idImgs) {
109
		$obsChamps = $this->sql->getAliasDesChamps($this->mappings['observations']);
110
		$imgChamps = $this->sql->getAliasDesChamps($this->mappings['images']);
111
		$idImgsConcat = implode(',', $idImgs);
1491 raphael 112
 
1840 jpm 113
		$requete = "SELECT CONCAT(id_image, '-', id_observation) AS jsonindex, $obsChamps, $imgChamps ".
114
			'FROM v_del_image '.
115
			"WHERE id_image IN ($idImgsConcat) ".
116
			"ORDER BY FIELD(id_image, $idImgsConcat) ". // important car MySQL ne conserve par l'ordre du IN()
117
			'-- '.__FILE__.':'.__LINE__;
1486 raphael 118
 
1840 jpm 119
		return $this->bdd->recupererTous($requete);
1495 raphael 120
	}
1486 raphael 121
 
1604 jpm 122
	// cf Observation::reformateObservationSimpleIndex() et ListeObservations::reformateObservation()
123
	// (trop de variétés de formatage, à unifier côté client pour unifier côté backend ...)
1840 jpm 124
	private function reformaterImagesDoubleIndex($imagesInfos) {
125
		$urlImgTpl = $this->conteneur->getParametre('cel_img_url_tpl');
126
		$imageFormat = isset($this->paramsFiltres['format']) ? $this->paramsFiltres['format'] : 'XL';
1564 mathias 127
		$obs_merged = $obs_keyed_by_id_image = array();
1840 jpm 128
		foreach ($imagesInfos as $infos) {
1604 jpm 129
			// ceci nous complique la tâche pour le reste du processing...
1840 jpm 130
			$id = $infos['jsonindex'];
1604 jpm 131
			// ainsi nous utilisons deux tableaux: le final, indexé par couple d'id(image-obs)
132
			// et celui indexé par simple id_image qui est fort utile pour mapVotesToImages()
133
			// mais tout deux partage leur référence à "protocole"
134
			$image = array(
1840 jpm 135
				'id_image' => $infos['id_image'],
136
				'binaire.href' => sprintf($urlImgTpl, $infos['id_image'], $imageFormat),
137
				'mots_cles_texte' => @$infos['mots_cles_texte_img'], // @, peut avoir été filtré par array_map() ci-dessus
1604 jpm 138
			);
1840 jpm 139
 
140
			unset($infos['id_image'], $infos['mots_cles_texte_img'], $infos['jsonindex']);
1604 jpm 141
			if (!isset($obs_merged[$id])) {
142
				$obs_merged[$id] = $image;
143
			}
1840 jpm 144
			$obs_merged[$id]['observation'] = $infos;
1604 jpm 145
			$obs_merged[$id]['protocoles_votes'] = array();
146
 
147
			$obs_keyed_by_id_image[$image['id_image']]['protocoles_votes'] =& $obs_merged[$id]['protocoles_votes'];
1564 mathias 148
		}
1604 jpm 149
 
1840 jpm 150
		return array($obs_merged, $obs_keyed_by_id_image);
1604 jpm 151
	}
1490 raphael 152
 
1840 jpm 153
	/**
154
	 * Supprime une image directement dans le CEL en faisant un appel à un web service du CEL.
155
	 * Utilisé uniquement par les admins.
156
	 *
157
	 * @param array		$ressources tableau des informations contenues dans l'url après le nom du service
158
	 * @param array		$parametres contenu du post
159
	 * @return mixed	Chaine "OK" (en majuscule) en cas de succès, booléen "false" en cas d'échec
160
	 */
161
	public function supprimer($ressources) {
162
		$controlAcces = $this->conteneur->getControleAcces();
163
		$controlAcces->etreUtilisateurAvecDroitAdmin();
1490 raphael 164
 
1840 jpm 165
		$urlServiceBase = $this->conteneur->getParametre('urlServiceCelImage');
166
		$idImage = $ressources[0];
167
		$url = $urlServiceBase.$idImage;
1604 jpm 168
 
1840 jpm 169
		$clientHttp = $this->conteneur->getRestClient();
170
		$retourCel = $clientHttp->supprimer($url);
171
		$retour = preg_match('/^OK$/i', $retourCel) ? 'OK' : false;
172
		return $retour;
1604 jpm 173
	}
1827 jpm 174
}