Subversion Repositories eFlore/Applications.coel-consultation

Rev

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

Rev Author Line No. Line
7 jpm 1
<?php
2
// declare(encoding='UTF-8');
3
/**
4
 * classe Controleur du module Recherche.
5
 *
6
 * @package		Collection
7
 * @category	Php5
8
 * @author		Jean-Pascal MILCENT <jpm@tela-botanica.org>
9
 * @copyright	2010 Tela-Botanica
10
 * @license		http://www.cecill.info/licences/Licence_CeCILL_V2-fr.txt Licence CECILL
11
 * @license		http://www.gnu.org/licenses/gpl.html Licence GNU-GPL
20 jpm 12
 * @version		SVN: $Id: Recherche.php 43 2010-04-26 14:46:24Z jpm $
7 jpm 13
 */
9 jpm 14
class Recherche extends ColControleur {
7 jpm 15
 
8 jpm 16
	//+----------------------------------------------------------------------------------------------------------------+
9 jpm 17
	// Méthodes
7 jpm 18
	/**
19
	 * Fonction d'affichage par défaut, elle appelle la liste des administrateurs
20
	 */
21
	public function executerActionParDefaut() {
8 jpm 22
		return $this->rechercher();
7 jpm 23
	}
24
 
25
	/**
26
	 * Charge le moteur de recherche et l'envoie à la vue.
27
	 */
28
	public function chargerMoteurRecherche() {
29
		$donnees = array();
30
 
8 jpm 31
		// Gestion des données de la requête
15 jpm 32
		$chaine = $this->obtenirChaineRecherche();
33
		$this->memoriserChaineRecherche($chaine);
20 jpm 34
		$donnees['recherche'] = htmlspecialchars(stripslashes($chaine));
8 jpm 35
 
21 jpm 36
		// Gestion des urls
7 jpm 37
		$this->url->setVariableRequete('module', 'Recherche');
38
		$this->url->setVariableRequete('action', 'rechercher');
39
		$donnees['url_form'] = $this->url->getUrl();
40
		$donnees['url_module'] = 'Recherche';
41
		$donnees['url_action'] = 'rechercher';
21 jpm 42
		$this->url->setVariableRequete('recherche', '%s');
43
		$donnees['url_exemple'] = $this->url->getUrl();
25 jpm 44
		$this->url->unsetVariablesRequete(array('module', 'action', 'recherche'));
7 jpm 45
 
8 jpm 46
		// Gestion du squelette et de la sortie
47
		$this->setSortie(self::RENDU_TETE, $this->getVue('moteur', $donnees));
7 jpm 48
	}
49
 
15 jpm 50
	private function obtenirChaineRecherche() {
51
		$chaine = '';
52
		if (isset($_GET['recherche'])) {
53
			$chaine = $_GET['recherche'];
54
		} else if (isset($_SESSION['col']['recherche'])) {
55
			$chaine = $_SESSION['col']['recherche'];
56
		}
57
		return $chaine;
58
	}
59
 
60
	private function memoriserChaineRecherche($chaine) {
61
		$_SESSION['col']['recherche'] = $chaine;
62
	}
63
 
7 jpm 64
	/**
65
	 * Recherche des collections.
66
	 * @return string la vue correspondante
67
	 */
68
	public function rechercher() {
69
		$donnees = array();
70
		$rechercheDao = $this->getModele('RechercheDao');
20 jpm 71
		$parametres = array('mots' => '*');
7 jpm 72
 
73
		// Récupération des paramêtres de l'url
20 jpm 74
		$chaine_de_recherche = '';
7 jpm 75
		if (isset($_GET['recherche'])) {
20 jpm 76
			$chaine_de_recherche = $_GET['recherche'];
23 jpm 77
			$this->url->setVariableRequete('recherche', $chaine_de_recherche);
7 jpm 78
		}
20 jpm 79
		$parametres = $this->parserChaineDeRecherche($chaine_de_recherche);
80
 
7 jpm 81
		// Gestion du nombre de résultats
82
		$donnees_total = $rechercheDao->chercherStructureNbre($parametres);
83
 
84
		// Gestion du fragmenteur
85
		$options = array(
86
			'url' => $this->url,
43 jpm 87
			'donnees_total' => $donnees_total,
88
			'donnees_par_page' => Config::get('resultat_par_page_defaut'),
89
			'donnees_par_page_choix' => Config::get('resultat_par_page_choix'),
90
		);
7 jpm 91
		$fragmenteur = Composant::fabrique('fragmenteur', $options);
92
		$donnees['fragmenteur'] = $fragmenteur->executer();
93
		list($de, $a) = $fragmenteur->getDeplacementParPageId();
94
 
95
		// Gestion de l'accès aux données
96
		$rechercheDao->setLimitation(($de - 1), $fragmenteur->getDonneesParPage());
20 jpm 97
		$rechercheDao->setDistinction(1);
7 jpm 98
		$resultats = $rechercheDao->chercher($parametres);
21 jpm 99
 
7 jpm 100
		// Post-traitement des résultats pour l'affichage
9 jpm 101
		$this->url->setVariableRequete('module', 'Fiche');
7 jpm 102
		foreach ($resultats as $resultat) {
103
			$structure_id = $resultat['cs_id_structure'];
104
			if (!isset($donnees['infos'][$structure_id])) {
9 jpm 105
				$this->url->setVariableRequete('action', 'afficherStructure');
106
				$this->url->setVariableRequete('id', $resultat['cs_id_structure']);
7 jpm 107
				$structure = array(
108
					'nom' => $resultat['cs_nom'],
9 jpm 109
					'ville' => $resultat['cs_ville'],
110
					'url' => $this->url->getURL());
111
				$this->url->unsetVariableRequete('action');
112
				$this->url->unsetVariableRequete('id');
7 jpm 113
				$donnees['infos'][$structure_id]['structure'] = $structure;
114
			}
9 jpm 115
			$this->url->setVariableRequete('action', 'afficherCollection');
116
			$this->url->setVariableRequete('id', $resultat['cc_id_collection']);
117
			$collection = array('nom' => $resultat['cc_nom'],
118
				'url' => $this->url->getURL());
119
			$this->url->unsetVariableRequete('action');
120
			$this->url->unsetVariableRequete('id');
7 jpm 121
			$donnees['infos'][$structure_id]['collections'][] = $collection;
122
		}
9 jpm 123
		$this->url->unsetVariableRequete('module');
7 jpm 124
 
125
		// Gestion des squelettes
8 jpm 126
		$this->chargerMoteurRecherche();
127
		$resultat = $this->getVue('resultat', $donnees);
128
		$this->setSortie(self::RENDU_CORPS, $resultat);
129
		$this->chargerPiedDePage();
7 jpm 130
	}
8 jpm 131
 
20 jpm 132
	private function parserChaineDeRecherche($chaine) {
133
		// Pré-traitement de la chaine de recherche
134
		// Suppression des slash ajouté automatiquement par PHP
135
		$chaine = stripslashes($chaine);
136
 
137
		$mots = preg_split('/ /i', $chaine, -1, PREG_SPLIT_NO_EMPTY);
138
		$parametres = array('mots' => '');
139
		$cle_precedente = null;
140
		foreach ($mots as $mot) {
33 jpm 141
			if (preg_match('/^(sci|bot|zg|p|pr|str-d):(.*)$/', $mot, $match)) {
20 jpm 142
				$cle = $match[1];
143
				$cle_precedente = $cle;
144
				$valeur = $match[2];
145
				$parametres[$cle] = $valeur;
146
			} else if (!is_null($cle_precedente)) {
147
				$parametres[$cle_precedente] .= ' '.$mot;
148
			} else if (is_null($cle_precedente)) {
149
				if (empty($parametres['mots'])) {
150
					$parametres['mots'] = $mot;
151
				} else {
152
					$parametres['mots'] .= ' '.$mot;
153
				}
154
			}
155
		}
156
		$this->remplacerAbreviationParId($parametres);
21 jpm 157
 
20 jpm 158
		return $parametres;
159
	}
160
 
161
	private function remplacerAbreviationParId(&$parametres) {
162
		// liste 27 : Liste des relations entre une collection et une personne (id:1030)
163
		// liste 80 : Liste des types de collection botanique (id:1083)
164
		$params_a_remplacer = array('bot' => 1083, 'pr' => 1030);
165
		foreach ($params_a_remplacer as $param => $id_liste) {
166
			if (isset($parametres[$param])) {
167
				$liste = Ontologie::getListeTrieeParAbreviation($id_liste);
168
				$cle = strtoupper($parametres[$param]);
169
				if (isset($liste[$cle])) {
170
					$parametres[$param] = $liste[$cle]['id'];
171
				}
172
			}
173
		}
174
	}
175
 
8 jpm 176
	/**
177
	 * Recherche des collections.
178
	 * @return string la vue correspondante
179
	 */
180
	public function chargerPiedDePage() {
181
		$this->setSortie(self::RENDU_PIED, $this->getVue('pied'));
182
	}
7 jpm 183
}