Subversion Repositories eFlore/Applications.coel-consultation

Rev

Rev 15 | Rev 21 | 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 20 2010-03-29 17:28:43Z 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
 
7 jpm 36
		// Gestion de l'url
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';
42
 
8 jpm 43
		// Gestion du squelette et de la sortie
44
		$this->setSortie(self::RENDU_TETE, $this->getVue('moteur', $donnees));
7 jpm 45
	}
46
 
15 jpm 47
	private function obtenirChaineRecherche() {
48
		$chaine = '';
49
		if (isset($_GET['recherche'])) {
50
			$chaine = $_GET['recherche'];
51
		} else if (isset($_SESSION['col']['recherche'])) {
52
			$chaine = $_SESSION['col']['recherche'];
53
		}
54
		return $chaine;
55
	}
56
 
57
	private function memoriserChaineRecherche($chaine) {
58
		$_SESSION['col']['recherche'] = $chaine;
59
	}
60
 
7 jpm 61
	/**
62
	 * Recherche des collections.
63
	 * @return string la vue correspondante
64
	 */
65
	public function rechercher() {
66
		$donnees = array();
67
		$rechercheDao = $this->getModele('RechercheDao');
20 jpm 68
		$parametres = array('mots' => '*');
7 jpm 69
 
70
		// Récupération des paramêtres de l'url
20 jpm 71
		$chaine_de_recherche = '';
7 jpm 72
		if (isset($_GET['recherche'])) {
20 jpm 73
			$chaine_de_recherche = $_GET['recherche'];
7 jpm 74
		}
20 jpm 75
		$parametres = $this->parserChaineDeRecherche($chaine_de_recherche);
76
 
7 jpm 77
		// Gestion du nombre de résultats
78
		$donnees_total = $rechercheDao->chercherStructureNbre($parametres);
79
 
80
		// Gestion du fragmenteur
81
		$options = array(
82
			'url' => $this->url,
83
			'donnees_total' => $donnees_total);
84
		$fragmenteur = Composant::fabrique('fragmenteur', $options);
85
		$donnees['fragmenteur'] = $fragmenteur->executer();
86
		list($de, $a) = $fragmenteur->getDeplacementParPageId();
87
 
88
		// Gestion de l'accès aux données
89
		$rechercheDao->setLimitation(($de - 1), $fragmenteur->getDonneesParPage());
20 jpm 90
		$rechercheDao->setDistinction(1);
7 jpm 91
		$resultats = $rechercheDao->chercher($parametres);
20 jpm 92
		Debug::printr($resultats);
7 jpm 93
		// Post-traitement des résultats pour l'affichage
9 jpm 94
		$this->url->setVariableRequete('module', 'Fiche');
7 jpm 95
		foreach ($resultats as $resultat) {
96
			$structure_id = $resultat['cs_id_structure'];
97
			if (!isset($donnees['infos'][$structure_id])) {
9 jpm 98
				$this->url->setVariableRequete('action', 'afficherStructure');
99
				$this->url->setVariableRequete('id', $resultat['cs_id_structure']);
7 jpm 100
				$structure = array(
101
					'nom' => $resultat['cs_nom'],
9 jpm 102
					'ville' => $resultat['cs_ville'],
103
					'url' => $this->url->getURL());
104
				$this->url->unsetVariableRequete('action');
105
				$this->url->unsetVariableRequete('id');
7 jpm 106
				$donnees['infos'][$structure_id]['structure'] = $structure;
107
			}
9 jpm 108
			$this->url->setVariableRequete('action', 'afficherCollection');
109
			$this->url->setVariableRequete('id', $resultat['cc_id_collection']);
110
			$collection = array('nom' => $resultat['cc_nom'],
111
				'url' => $this->url->getURL());
112
			$this->url->unsetVariableRequete('action');
113
			$this->url->unsetVariableRequete('id');
7 jpm 114
			$donnees['infos'][$structure_id]['collections'][] = $collection;
115
		}
9 jpm 116
		$this->url->unsetVariableRequete('module');
7 jpm 117
 
118
		// Gestion des squelettes
8 jpm 119
		$this->chargerMoteurRecherche();
120
		$resultat = $this->getVue('resultat', $donnees);
121
		$this->setSortie(self::RENDU_CORPS, $resultat);
122
		$this->chargerPiedDePage();
7 jpm 123
	}
8 jpm 124
 
20 jpm 125
	private function parserChaineDeRecherche($chaine) {
126
		// Pré-traitement de la chaine de recherche
127
		// Suppression des slash ajouté automatiquement par PHP
128
		$chaine = stripslashes($chaine);
129
 
130
		$mots = preg_split('/ /i', $chaine, -1, PREG_SPLIT_NO_EMPTY);
131
		Debug::printr($mots);
132
		$parametres = array('mots' => '');
133
		$cle_precedente = null;
134
		foreach ($mots as $mot) {
135
			if (preg_match('/^(sci|bot|zg|p|pr):(.*)$/', $mot, $match)) {
136
				$cle = $match[1];
137
				$cle_precedente = $cle;
138
				$valeur = $match[2];
139
				$parametres[$cle] = $valeur;
140
			} else if (!is_null($cle_precedente)) {
141
				$parametres[$cle_precedente] .= ' '.$mot;
142
			} else if (is_null($cle_precedente)) {
143
				if (empty($parametres['mots'])) {
144
					$parametres['mots'] = $mot;
145
				} else {
146
					$parametres['mots'] .= ' '.$mot;
147
				}
148
			}
149
		}
150
		Debug::printr($parametres);
151
		$this->remplacerAbreviationParId($parametres);
152
		Debug::printr($parametres);
153
		return $parametres;
154
	}
155
 
156
	private function remplacerAbreviationParId(&$parametres) {
157
		// liste 27 : Liste des relations entre une collection et une personne (id:1030)
158
		// liste 80 : Liste des types de collection botanique (id:1083)
159
		$params_a_remplacer = array('bot' => 1083, 'pr' => 1030);
160
		foreach ($params_a_remplacer as $param => $id_liste) {
161
			if (isset($parametres[$param])) {
162
				$liste = Ontologie::getListeTrieeParAbreviation($id_liste);
163
				Debug::printr($liste);
164
				$cle = strtoupper($parametres[$param]);
165
				if (isset($liste[$cle])) {
166
					$parametres[$param] = $liste[$cle]['id'];
167
				}
168
			}
169
		}
170
	}
171
 
8 jpm 172
	/**
173
	 * Recherche des collections.
174
	 * @return string la vue correspondante
175
	 */
176
	public function chargerPiedDePage() {
177
		$this->setSortie(self::RENDU_PIED, $this->getVue('pied'));
178
	}
7 jpm 179
}