Rev 880 | Blame | Compare with Previous | Last modification | View Log | RSS feed
<?phpclass NomsListeGenerique {private $parametres = null;private $ressources = null;private $nomDao = null;private $nomFormateur = null;private $listeUrl = null;private $nbreNomsTotal = 0;private $noms = array();public function __construct(Ressources $ressources, Parametres $parametres, NomDAO $nomDao, NomFormateur $nomFormateur) {$this->ressources = $ressources;$this->parametres = $parametres;$this->nomDao = $nomDao;$this->nomFormateur = $nomFormateur;}public function setListeUrl($url) {$this->listeUrl = $url;}public function consulter() {$this->rechercher();if ($this->avoirResultats()) {$this->trierNoms();$retour = $this->construireTableauRetour();} else {$message = "Aucun résultat ne correspond a votre requête !";$code = RestServeur::HTTP_CODE_RESSOURCE_INTROUVABLE;throw new Exception($message, $code);}return $retour;}private function avoirResultats() {$resultat = ($this->nbreNomsTotal == 0) ? false : true;return $resultat;}private function rechercher() {$resultats = array();$recherche = $this->parametres->get('recherche');if ($recherche == 'floue') {$resultats = $this->nomDao->rechercherFloue();} else {$resultats = $this->nomDao->rechercher();}$this->noms = $resultats;$this->nbreNomsTotal = $this->nomDao->recupererNombreNomsTotal();}private function trierNoms() {$recherche = $this->parametres->get('recherche');if ($recherche == 'floue') {$this->trierRechercheFloue();}}public function trierRechercheFloue() {$nomDemande = $this->parametres->get('masque');$nomDemandeSimple = strtolower(Chaine::supprimerAccents($nomDemande));foreach ($this->noms as $id => $nom) {$nomFlouSimple = strtolower(Chaine::supprimerAccents($nom['nom_sci']));// Prime pour la ressemblance globale :$score = 500 - levenshtein($nomFlouSimple, $nomDemandeSimple);// On affine$score += similar_text($nomDemandeSimple, $nomFlouSimple) * 3;$this->noms[$id]['score'] = $score;}$noms = $this->noms;$this->noms = Tableau::trierMD($noms, array('score' => false));//print_r($this->noms);}private function construireTableauRetour() {$retour = array('entete' => array(), 'resultats' => array());$retour['resultats'] = $this->construireResultats();$retour['entete'] = $this->construireEntete();return $retour;}private function construireResultats() {$nomsFormates = array();foreach ($this->noms as $nom) {$id = $nom['num_nom'];$nomsFormates[$id] = $this->formaterNom($nom);}return $nomsFormates;}private function formaterNom($infos) {$nomAFormater = new NomDO($infos);$this->nomFormateur->setNomAFormater($nomAFormater);$this->nomFormateur->setChampsRetour($this->parametres->getListe('retour.champs'));$nom = $this->nomFormateur->formaterListe();return $nom;}private function construireEntete() {$entete = array('masque' => '', 'depart' => 0, 'limite' => 100, 'total' => 0);$entete['masque'] = $this->formaterEnteteMasque();$entete['depart'] = (int) $this->parametres->get('navigation.depart');$entete['limite'] = (int) $this->parametres->get('navigation.limite');$entete['total'] = $this->nbreNomsTotal;if ($hrefPrecedent = $this->formaterEnteteHrefPrecedent()) {$entete['href.precedent'] = $hrefPrecedent;}if ($hrefSuivant = $this->formaterEnteteHrefSuivant()) {$entete['href.suivant'] = $hrefSuivant;}return $entete;}private function formaterEnteteMasque() {$masquesStrictes = array('nn', 'rg');$paramsMasque = array('' => 'nom_sci','nn' => 'num_nom','rg' => 'rang','sg' => 'nom_supra_generique','gen' => 'genre','sp' => 'epithete_sp','ssp' => 'epithete_infra_sp','au' => 'auteur','an' => 'annee');$etendre = ($this->parametres->get('recherche') == 'etendue') ? true : false;$masqueComplet = array();foreach ($paramsMasque as $masqueType => $champ) {$masqueParam = 'masque'.($masqueType != '' ? '.'.$masqueType : $masqueType);if ($this->parametres->exister($masqueParam)) {$masqueValeur = $this->parametres->get($masqueParam);$masque = "$champ=$masqueValeur";$masque .= ($etendre && in_array($masqueType, $masquesStrictes) === false) ? '%' : '';$masqueComplet[] = $masque;}}return implode('&', $masqueComplet);}private function formaterEnteteHrefPrecedent() {$limite = $this->parametres->get('navigation.limite');$departActuel = $this->parametres->get('navigation.depart');$departPrecedent = $departActuel - $limite;$href = null;if ($departPrecedent >= 0) {$squelette = $this->construireTplHrefNavigation();$href = sprintf($squelette, $departPrecedent, $limite);}return $href;}private function formaterEnteteHrefSuivant() {$limite = $this->parametres->get('navigation.limite');$departActuel = $this->parametres->get('navigation.depart');$departSuivant = $departActuel + $limite;$href = null;if ($departSuivant < $this->nbreNomsTotal) {$squelette = $this->construireTplHrefNavigation();$href = sprintf($squelette, $departSuivant, $limite);}return $href;}private function construireTplHrefNavigation() {$requetes = array();$this->parametres->rewind();while (is_null($parametre = $this->parametres->key()) === false) {if (strpos($parametre, 'navigation') === false) {$valeur = $this->parametres->current();$requetes[] = "$parametre=$valeur";}$this->parametres->next();}$requetes[] = "navigation.depart=%s";$requetes[] = "navigation.limite=%s";$tpl = $this->listeUrl.'?'.implode('&', $requetes);return $tpl;}}?>