Subversion Repositories eFlore/Applications.del

Rev

Rev 1845 | Rev 1863 | 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 $conteneur;
29
	private $bdd;
30
	private $filtrage;
31
	private $sql;
32
	private $navigation;
33
	private $paramsFiltres = array();
34
	private $mappings = array();
1390 raphael 35
 
1827 jpm 36
	public function __construct(Conteneur $conteneur) {
37
		$this->conteneur = $conteneur;
38
		$this->bdd = $this->conteneur->getBdd();
1840 jpm 39
		$this->filtrage = $this->conteneur->getParametresFiltrage();
40
		$this->sql = $this->conteneur->getSql();
41
		$this->navigation = $this->conteneur->getNavigation();
42
 
43
		$this->mappings['observations'] = $this->conteneur->getParametreTableau('observations.mapping');
44
		$this->mappings['images'] = $this->conteneur->getParametreTableau('images.mapping');
1604 jpm 45
	}
1390 raphael 46
 
1604 jpm 47
	public function consulter($ressources, $parametres) {
1840 jpm 48
		$this->paramsFiltres = $this->filtrage->filtrerUrlParamsAppliImg();
49
		$this->sql->setParametres($this->paramsFiltres);
50
		$this->sql->ajouterContraintes();
51
		$this->sql->ajouterConstrainteAppliImg();
52
		$this->sql->definirOrdreSqlAppliImg();
1422 raphael 53
 
1840 jpm 54
		$idImgs = $this->getIdImages();
1845 jpm 55
		$this->navigation->setTotal($this->sql->getTotalLignesTrouvees());
1422 raphael 56
 
1840 jpm 57
		// Ce n'est pas la peine de continuer s'il n'y a pas eu de résultats
58
		$resultat = new ResultatService();
59
		$resultat->corps = array('entete' => $this->navigation->getEntete(), 'resultats' => array());
60
		if (count($idImgs) > 0) {
61
			$liaisons = $this->getInfosImages($idImgs);
62
			list($images, $images_indexe_par_id_image) = $this->reformaterImagesDoubleIndex($liaisons);
1390 raphael 63
 
1840 jpm 64
			// Chargement des votes pour ces images et pour *tous* les protocoles
65
			$votes = $this->sql->getVotesDesImages($idImgs);
66
			if ($votes) {
67
				// ATTENTION : $images_indexe_par_id_image est lié par référence à $images !
68
				$this->sql->ajouterInfosVotesProtocoles($votes, $images_indexe_par_id_image);
69
			}
1422 raphael 70
 
1840 jpm 71
			$resultat->corps = array(
72
				'entete' => $this->navigation->getEntete(),
73
				'resultats' => $images);
1604 jpm 74
		}
75
		return $resultat;
76
	}
1495 raphael 77
 
1840 jpm 78
	private function getIdImages() {
79
		$requete = 'SELECT SQL_CALC_FOUND_ROWS id_image '.
80
			'FROM v_del_image AS vdi '.
81
			$this->sql->getJoin().
82
			'WHERE '.$this->sql->getWhere().
83
			$this->sql->getGroupBy().
84
			$this->sql->getOrderBy().
85
			$this->sql->getLimit().
86
			' -- '.__FILE__.':'.__LINE__;
1690 jpm 87
 
1840 jpm 88
		$resultats = $this->bdd->recupererTous($requete);
89
		$idImgs = array();
90
		if ($resultats !== false ) {
91
			foreach ($resultats as $resultat) {
92
				$idImgs[] = $resultat['id_image'];
1604 jpm 93
			}
1584 mathias 94
		}
1840 jpm 95
		return $idImgs;
1604 jpm 96
	}
1486 raphael 97
 
1840 jpm 98
	private function getInfosImages($idImgs) {
99
		$obsChamps = $this->sql->getAliasDesChamps($this->mappings['observations']);
100
		$imgChamps = $this->sql->getAliasDesChamps($this->mappings['images']);
101
		$idImgsConcat = implode(',', $idImgs);
1491 raphael 102
 
1840 jpm 103
		$requete = "SELECT CONCAT(id_image, '-', id_observation) AS jsonindex, $obsChamps, $imgChamps ".
104
			'FROM v_del_image '.
105
			"WHERE id_image IN ($idImgsConcat) ".
106
			"ORDER BY FIELD(id_image, $idImgsConcat) ". // important car MySQL ne conserve par l'ordre du IN()
107
			'-- '.__FILE__.':'.__LINE__;
1486 raphael 108
 
1840 jpm 109
		return $this->bdd->recupererTous($requete);
1495 raphael 110
	}
1486 raphael 111
 
1604 jpm 112
	// cf Observation::reformateObservationSimpleIndex() et ListeObservations::reformateObservation()
113
	// (trop de variétés de formatage, à unifier côté client pour unifier côté backend ...)
1840 jpm 114
	private function reformaterImagesDoubleIndex($imagesInfos) {
115
		$urlImgTpl = $this->conteneur->getParametre('cel_img_url_tpl');
116
		$imageFormat = isset($this->paramsFiltres['format']) ? $this->paramsFiltres['format'] : 'XL';
1564 mathias 117
		$obs_merged = $obs_keyed_by_id_image = array();
1840 jpm 118
		foreach ($imagesInfos as $infos) {
1604 jpm 119
			// ceci nous complique la tâche pour le reste du processing...
1840 jpm 120
			$id = $infos['jsonindex'];
1604 jpm 121
			// ainsi nous utilisons deux tableaux: le final, indexé par couple d'id(image-obs)
122
			// et celui indexé par simple id_image qui est fort utile pour mapVotesToImages()
123
			// mais tout deux partage leur référence à "protocole"
124
			$image = array(
1840 jpm 125
				'id_image' => $infos['id_image'],
126
				'binaire.href' => sprintf($urlImgTpl, $infos['id_image'], $imageFormat),
127
				'mots_cles_texte' => @$infos['mots_cles_texte_img'], // @, peut avoir été filtré par array_map() ci-dessus
1604 jpm 128
			);
1845 jpm 129
 
1840 jpm 130
			unset($infos['id_image'], $infos['mots_cles_texte_img'], $infos['jsonindex']);
1604 jpm 131
			if (!isset($obs_merged[$id])) {
132
				$obs_merged[$id] = $image;
133
			}
1840 jpm 134
			$obs_merged[$id]['observation'] = $infos;
1604 jpm 135
			$obs_merged[$id]['protocoles_votes'] = array();
136
 
137
			$obs_keyed_by_id_image[$image['id_image']]['protocoles_votes'] =& $obs_merged[$id]['protocoles_votes'];
1564 mathias 138
		}
1604 jpm 139
 
1840 jpm 140
		return array($obs_merged, $obs_keyed_by_id_image);
1604 jpm 141
	}
1490 raphael 142
 
1840 jpm 143
	/**
144
	 * Supprime une image directement dans le CEL en faisant un appel à un web service du CEL.
145
	 * Utilisé uniquement par les admins.
146
	 *
147
	 * @param array		$ressources tableau des informations contenues dans l'url après le nom du service
148
	 * @param array		$parametres contenu du post
149
	 * @return mixed	Chaine "OK" (en majuscule) en cas de succès, booléen "false" en cas d'échec
150
	 */
151
	public function supprimer($ressources) {
152
		$controlAcces = $this->conteneur->getControleAcces();
153
		$controlAcces->etreUtilisateurAvecDroitAdmin();
1490 raphael 154
 
1840 jpm 155
		$urlServiceBase = $this->conteneur->getParametre('urlServiceCelImage');
156
		$idImage = $ressources[0];
157
		$url = $urlServiceBase.$idImage;
1604 jpm 158
 
1840 jpm 159
		$clientHttp = $this->conteneur->getRestClient();
160
		$retourCel = $clientHttp->supprimer($url);
161
		$retour = preg_match('/^OK$/i', $retourCel) ? 'OK' : false;
162
		return $retour;
1604 jpm 163
	}
1827 jpm 164
}