From e1f30cd6f1f2b25d9b9fe162e1c232607743ac08 Mon Sep 17 00:00:00 2001 From: x2000bedo Date: Mon, 2 Aug 2004 22:43:31 +0000 Subject: [PATCH] =?utf8?q?D=E9but=20d'impl=E9mentation=20de=20recherche.ph?= =?utf8?q?p=20(=3D>search.php)?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Dans un premier temps, je développe une page de recherche simplifiée pour les champs nom,prénom,promo sans soundex puis on proposera l'option avec soundex puis enfin une recherche avancée qui ne concernera que les inscrits (avec les champs binets etc...) J'ai choisi d'implémenter une classe pour chaque type de champ. Je pense que ça rend le machin plus clair. Il reste à améliorer l'affichage des résultats (limitations à 10 par page et navigation + liens vers la fiche pour les inscrits) pour cette recherche simplifiée. --- htdocs/search.php | 33 ++++++++++++++ include/search.classes.inc.php | 100 +++++++++++++++++++++++++++++++++++++++++ templates/search.tpl | 54 ++++++++++++++++++++++ 3 files changed, 187 insertions(+) create mode 100644 htdocs/search.php create mode 100644 include/search.classes.inc.php create mode 100644 templates/search.tpl diff --git a/htdocs/search.php b/htdocs/search.php new file mode 100644 index 0000000..76e37c1 --- /dev/null +++ b/htdocs/search.php @@ -0,0 +1,33 @@ +assign('formulaire',0); + $nameField = new StringSField('name',array('u.nom','u.epouse')); + $firstnameField = new StringSField('firstname',array('u.prenom')); + $promoField = new PromoSField('promo','egal','u.promo'); + $fields = new SFieldGroup(true,array($nameField,$firstnameField,$promoField)); + $nameField = new StringSField('name',array('i.nom')); + $firstnameField = new StringSField('firstname',array('i.prenom')); + $promoField = new PromoSField('promo','egal','i.promo'); + $fields2 = new SFieldGroup(true,array($nameField,$firstnameField,$promoField)); + $sql = '(SELECT u.nom,u.prenom,u.promo + FROM auth_user_md5 AS u + WHERE '.$fields->get_where_statement().') + UNION + (SELECT i.nom,i.prenom,i.promo + FROM identification AS i + WHERE '.$fields2->get_where_statement().') + ORDER BY promo DESC,nom,prenom'; + $page->mysql_assign($sql, 'resultats', 'nb_resultats'); +} +else + $page->assign('formulaire',1); +$page->run(); +?> diff --git a/include/search.classes.inc.php b/include/search.classes.inc.php new file mode 100644 index 0000000..0a9f549 --- /dev/null +++ b/include/search.classes.inc.php @@ -0,0 +1,100 @@ +fieldFormName = $_fieldFormName; + $this->fieldDbName = $_fieldDbName; + $this->get_request(); + } + + function get_request() { + $this->value = + (isset($_REQUEST[$this->fieldFormName]))?trim(stripslashes($_REQUEST[$this->fieldFormName])):''; + } + + function error($explain) { + global $page; + $page->assign('error',$explain); + $page->run(); + } + + function get_where_statement() { + return ($this->value!=''); + } +} + +class StringSField extends SField { + function get_request() { + parent::get_request(); + if (preg_match(":[][<>{}~/§_`|%$^=+]|\*\*:", $this->value)) + $this->error('Un champ contient un caractère interdit rendant la recherche' + .' impossible.
'); + } + + function length() { + return + length($this->value)-length(ereg_replace('[a-z]'.$CARACTERES_ACCENTUES,'',strtolower($this->value))); + } + + function get_like($field) { + //on rend les traits d'union et les espaces équivalents + $regexp = preg_replace('/[ -]/','[ \-]',$this->value); + //on remplace le pseudo language des * par une regexp + $regexp = str_replace('*','.+',$regexp); + return $field." RLIKE '^(.*[ -])?".replace_accent_regexp($regexp).".*'"; + } + + function get_where_statement() { + if (!parent::get_where_statement()) + return false; + return '('.implode(' OR ',array_map(array($this,'get_like'),$this->fieldDbName)).')'; + } +} + +class PromoSField extends SField { + var $compareField; + + function PromoSField($_fieldFormName,$_compareFieldFormName,$_fieldDbName) { + parent::SField($_fieldFormName,$_fieldDbName); + $this->compareField = new SField($_compareFieldFormName); + } + + function get_request() { + parent::get_request(); + if (!(empty($this->value) or preg_match("/^[0-9]{4}$/", $this->value))) + $this->error('La promotion est une année à quatre chiffres.
'); + } + + function is_a_single_promo() { + return ($this->compareField->value=='=' && $this->value!=''); + } + + function get_where_statement() { + if (!parent::get_where_statement()) + return false; + return $this->fieldDbName.$this->compareField->value.$this->value; + } +} + +class SFieldGroup { + var $fields; + var $and; + + function SFieldGroup($_and,$_fields) { + $this->fields = $_fields; + $this->and = $_and; + } + + function field_get_where($f) { + return $f->get_where_statement(); + } + + function get_where_statement() { + $joinText=($this->and)?' AND ':' OR '; + return '('.implode($joinText,array_filter(array_map(array($this,'field_get_where'),$this->fields))).')'; + } +} +?> diff --git a/templates/search.tpl b/templates/search.tpl new file mode 100644 index 0000000..2dae2a0 --- /dev/null +++ b/templates/search.tpl @@ -0,0 +1,54 @@ +{dynamic} +{if $formulaire==0} +
+ Résultats +
+

+ {if $nb_resultats==0}Aucune{else}{$nb_resultats}{/if} réponse{if $nb_resultats>1}s{/if}. +

+ + {section name=resultat loop=$resultats} + + + + + {/section} +
+ {$resultats[resultat].nom} {$resultats[resultat].prenom} + + (X {$resultats[resultat].promo}) +
+{else} +
+ Recherche +
+
+
+ + + + + + + + + + + + + + + + +
Nom
Prénom
Promotion + + +
+
+
+{/if} +{/dynamic} -- 2.1.4