Subversion Repositories eFlore/Applications.del

Rev

Rev 1840 | Rev 1855 | 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();
1845 jpm 60
		$this->navigation->setTotal($this->sql->getTotalLignesTrouvees());
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 getInfosImages($idImgs) {
104
		$obsChamps = $this->sql->getAliasDesChamps($this->mappings['observations']);
105
		$imgChamps = $this->sql->getAliasDesChamps($this->mappings['images']);
106
		$idImgsConcat = implode(',', $idImgs);
1491 raphael 107
 
1840 jpm 108
		$requete = "SELECT CONCAT(id_image, '-', id_observation) AS jsonindex, $obsChamps, $imgChamps ".
109
			'FROM v_del_image '.
110
			"WHERE id_image IN ($idImgsConcat) ".
111
			"ORDER BY FIELD(id_image, $idImgsConcat) ". // important car MySQL ne conserve par l'ordre du IN()
112
			'-- '.__FILE__.':'.__LINE__;
1486 raphael 113
 
1840 jpm 114
		return $this->bdd->recupererTous($requete);
1495 raphael 115
	}
1486 raphael 116
 
1604 jpm 117
	// cf Observation::reformateObservationSimpleIndex() et ListeObservations::reformateObservation()
118
	// (trop de variétés de formatage, à unifier côté client pour unifier côté backend ...)
1840 jpm 119
	private function reformaterImagesDoubleIndex($imagesInfos) {
120
		$urlImgTpl = $this->conteneur->getParametre('cel_img_url_tpl');
121
		$imageFormat = isset($this->paramsFiltres['format']) ? $this->paramsFiltres['format'] : 'XL';
1564 mathias 122
		$obs_merged = $obs_keyed_by_id_image = array();
1840 jpm 123
		foreach ($imagesInfos as $infos) {
1604 jpm 124
			// ceci nous complique la tâche pour le reste du processing...
1840 jpm 125
			$id = $infos['jsonindex'];
1604 jpm 126
			// ainsi nous utilisons deux tableaux: le final, indexé par couple d'id(image-obs)
127
			// et celui indexé par simple id_image qui est fort utile pour mapVotesToImages()
128
			// mais tout deux partage leur référence à "protocole"
129
			$image = array(
1840 jpm 130
				'id_image' => $infos['id_image'],
131
				'binaire.href' => sprintf($urlImgTpl, $infos['id_image'], $imageFormat),
132
				'mots_cles_texte' => @$infos['mots_cles_texte_img'], // @, peut avoir été filtré par array_map() ci-dessus
1604 jpm 133
			);
1845 jpm 134
 
1840 jpm 135
			unset($infos['id_image'], $infos['mots_cles_texte_img'], $infos['jsonindex']);
1604 jpm 136
			if (!isset($obs_merged[$id])) {
137
				$obs_merged[$id] = $image;
138
			}
1840 jpm 139
			$obs_merged[$id]['observation'] = $infos;
1604 jpm 140
			$obs_merged[$id]['protocoles_votes'] = array();
141
 
142
			$obs_keyed_by_id_image[$image['id_image']]['protocoles_votes'] =& $obs_merged[$id]['protocoles_votes'];
1564 mathias 143
		}
1604 jpm 144
 
1840 jpm 145
		return array($obs_merged, $obs_keyed_by_id_image);
1604 jpm 146
	}
1490 raphael 147
 
1840 jpm 148
	/**
149
	 * Supprime une image directement dans le CEL en faisant un appel à un web service du CEL.
150
	 * Utilisé uniquement par les admins.
151
	 *
152
	 * @param array		$ressources tableau des informations contenues dans l'url après le nom du service
153
	 * @param array		$parametres contenu du post
154
	 * @return mixed	Chaine "OK" (en majuscule) en cas de succès, booléen "false" en cas d'échec
155
	 */
156
	public function supprimer($ressources) {
157
		$controlAcces = $this->conteneur->getControleAcces();
158
		$controlAcces->etreUtilisateurAvecDroitAdmin();
1490 raphael 159
 
1840 jpm 160
		$urlServiceBase = $this->conteneur->getParametre('urlServiceCelImage');
161
		$idImage = $ressources[0];
162
		$url = $urlServiceBase.$idImage;
1604 jpm 163
 
1840 jpm 164
		$clientHttp = $this->conteneur->getRestClient();
165
		$retourCel = $clientHttp->supprimer($url);
166
		$retour = preg_match('/^OK$/i', $retourCel) ? 'OK' : false;
167
		return $retour;
1604 jpm 168
	}
1827 jpm 169
}