Nouvelles améliorations
authorx2000bedo <x2000bedo>
Thu, 5 Aug 2004 00:36:24 +0000 (00:36 +0000)
committerx2000bedo <x2000bedo>
Thu, 5 Aug 2004 00:36:24 +0000 (00:36 +0000)
- Navigation sur plusieurs pages de recherches
- Mise en évidence des requêtes trop générales
- Gestion des erreurs de requêtes
- Gestion des requêtes en annuaire public ou privé
- Ajout des liens AX et marketing

Attention !
Ajout de deux nouveaux paramètres dans config.xorg.inc.php :
/* limitations recherche */
$globals->public_max_search_results = 25;
$globals->search_results_per_page = 10;

Reste pas mal d'esthétique et extension au soundex puis formulaire séparé pour la recherche
avancée.

A propos d'esthétique, où on modifie la CSS pour y ajouter de nouvelles classes ?

htdocs/search.php
include/search.classes.inc.php
templates/search.form.tpl [new file with mode: 0644]
templates/search.result.private.tpl [new file with mode: 0644]
templates/search.result.public.tpl [new file with mode: 0644]
templates/search.tpl

index 60b0f0e..9bd51ae 100644 (file)
@@ -1,7 +1,6 @@
 <?php
 require("auto.prepend.inc.php");
 require("search.classes.inc.php");
-$perpage = 10;
 // choix entre partie publique (annuaire_public est vrai) et partie privée de l'annuaire.
 $public_directory = ((isset($_REQUEST['public_directory']) && $_REQUEST['public_directory']==1));
 if ($public_directory)
@@ -15,8 +14,12 @@ if (array_key_exists('rechercher', $_REQUEST)) {
     $firstnameField = new StringSField('firstname',array('u.prenom','i.prenom'),'i.prenom');
     $promoField = new PromoSField('promo','egal',array('u.promo','i.promo'),'i.promo');
     $fields = new SFieldGroup(true,array($nameField,$firstnameField,$promoField));
+    if ($nameField->length()<2 && $firstnameField->length()<2 && 
+        ($public_directory || !$promoField->is_a_single_promo()))
+        new ThrowError('Recherche trop générale.');
     $offset = new NumericSField('offset');
-    $sql = 'SELECT SQL_CALC_FOUND_ROWS 
+    $sql = 'SELECT SQL_CALC_FOUND_ROWS
+                    i.matricule_ax,i.matricule,
                     u.nom!="" AS inscrit,
                     IF(u.nom!="",u.nom,i.nom) AS nom,
                     u.epouse,
@@ -27,15 +30,23 @@ if (array_key_exists('rechercher', $_REQUEST)) {
                     c.uid AS contact
             FROM identification AS i
             LEFT JOIN auth_user_md5 AS u ON (i.matricule=u.matricule)
-            LEFT JOIN contacts AS c ON (c.uid = '.$_SESSION['uid'].' AND c.contact=u.user_id)
+            LEFT JOIN contacts AS c ON (c.uid='.
+            ((array_key_exists('uid',$_SESSION))?$_SESSION['uid']:0).
+            ' AND c.contact=u.user_id)
             WHERE '.$fields->get_where_statement().'
             ORDER BY '.implode(',',array_filter(array($fields->get_order_statement(),
             'promo DESC,nom,prenom'))).'
-            LIMIT '.$offset->value.','.$perpage;
+            LIMIT '.$offset->value.','.$globals->search_results_per_page;
     $page->mysql_assign($sql, 'resultats', 'nb_resultats','nb_resultats_total');
+    if ($public_directory &&
+    $page->get_template_vars('nb_resultats_total')>$globals->public_max_search_results)
+        new ThrowError('Votre recherche a généré trop de résultats pour un affichage public.');
+    $nbpages = ($page->get_template_vars('nb_resultats_total')-1)/$globals->search_results_per_page;
+    $page->assign('offsets',range(0,$nbpages));
     $page->assign('url_args',$fields->get_url());
     $page->assign('offset',$offset->value);
-    $page->assign('perpage',$perpage);
+    $page->assign('perpage',$globals->search_results_per_page);
+    $page->assign('is_admin',has_perms());
 }
 else
     $page->assign('formulaire',1);
index 05165b2..2cb9154 100644 (file)
@@ -1,4 +1,12 @@
 <?php
+class ThrowError {
+    function ThrowError($explain) {
+        global $page;
+        $page->assign('error','Erreur : '.$explain);
+        $page->run();
+    }
+}
+
 class SField {
     var $fieldFormName;
     var $fieldDbName;
@@ -17,12 +25,6 @@ class SField {
         (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!='')?
         '('.implode(' OR ',array_map(array($this,'get_single_where_statement'),$this->fieldDbName)).')'
@@ -52,7 +54,7 @@ class NumericSField extends SField {
         if ($this->value=='')
             $this->value = 0;
         if (!preg_match("/^[0-9]+$/", $this->value))
-            $this->error('Un champ numérique contient des caractères alphanumériques.<br>');
+            new ThrowError('Un champ numérique contient des caractères alphanumériques.<br>');
     }
 }
 
@@ -60,13 +62,14 @@ 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'
+            new ThrowError('Un champ contient un caractère interdit rendant la recherche'
             .' impossible.<br>');
     }
 
     function length() {
+        global $lc_accent,$uc_accent;
         return
-        length($this->value)-length(ereg_replace('[a-z]'.$CARACTERES_ACCENTUES,'',strtolower($this->value)));
+        strlen($this->value)-strlen(ereg_replace('[a-z'.$lc_accent.$uc_accent.']','',strtolower($this->value)));
     }
 
     function get_single_where_statement($field) {
@@ -96,7 +99,7 @@ class PromoSField extends SField {
     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.<br>');
+            new ThrowError('La promotion est une année à quatre chiffres.<br>');
     }
 
     function is_a_single_promo() {
diff --git a/templates/search.form.tpl b/templates/search.form.tpl
new file mode 100644 (file)
index 0000000..fd59724
--- /dev/null
@@ -0,0 +1,37 @@
+<div class="rubrique">
+  Recherche
+</div>
+{if $error}
+  <p class="error">
+    {$error}
+  </p>
+{/if}
+<div class="center">
+  <form action="{$smarty.server.PHP_SELF}" method="post">
+    <input type="hidden" name="public_directory" value="{$public_directory}">
+    <table class="tinybicol" cellpadding="3" summary="Recherche">
+      <tr>
+        <td>Nom</td>
+        <td><input type="text" name="name" size="50" maxlength="50" /></td>
+      </tr>
+      <tr>
+        <td>Prénom</td>
+        <td><input type="text" name="firstname" size="50" maxlength="50" /></td>
+      </tr>
+      <tr>
+        <td>Promotion</td>
+        <td>
+          <select name="egal">
+            <option value="=" selected>&nbsp;=&nbsp;</option>
+            <option value=">" >&nbsp;&gt;&nbsp;</option>
+            <option value="<" >&nbsp;&lt;&nbsp;</option>
+          </select>
+          <input type="text" name="promo" size="4" maxlength="4" />
+        </td>
+      </tr>
+      <tr>
+        <td colspan="2" class="center"><input type="submit" name="rechercher" value="Ok" /></td>
+      </tr>
+    </table>
+  </form>
+</div>
diff --git a/templates/search.result.private.tpl b/templates/search.result.private.tpl
new file mode 100644 (file)
index 0000000..558ee01
--- /dev/null
@@ -0,0 +1,24 @@
+<td>
+  {if $result.inscrit==1}
+    <a href="javascript:x()"  onclick="popWin('x.php?x={$result.username}')">
+      <img src="images/loupe.gif" border=0 ALT="Afficher les détails">
+    </a>
+    <a href="vcard.php/{$result.username}.vcf?x={$result.username}">
+      <img src="images/vcard.png" border=0 ALT="Afficher la carte de visite">
+    </a>
+    <a href="mescontacts.php?action={if $result.contact!=""}retirer{else}ajouter{/if}&amp;user={$result.username}&amp;mode=normal">
+      <img src="images/{if $result.contact!=""}retirer{else}ajouter{/if}.gif" border=0 ALT="{if $result.contact!=""}Retirer de{else}Ajouter parmi{/if} mes contacts">
+    </a>
+  {else}
+    {if $result.decede != 1}
+      <a href="marketing.php?num={$result.matricule*2-100}" target="_blank">
+        clique ici si tu connais son adresse email !
+      </a>
+    {/if}
+  {/if}
+  {if $is_admin==1}
+    <a href="http://www.polytechniciens.com/index.php?page=AX_FICHE_ANCIEN&amp;anc_id={$result.matricule_ax}" target="_blank">
+      AX
+    </a>
+  {/if}
+</td>
diff --git a/templates/search.result.public.tpl b/templates/search.result.public.tpl
new file mode 100644 (file)
index 0000000..4c5bf87
--- /dev/null
@@ -0,0 +1,12 @@
+<td>
+  <strong>{$result.nom} {$result.prenom}</strong>
+  {if $result.epouse neq ""}
+    <div>({$result.epouse} {$result.prenom})</div>
+  {/if}
+  {if $result.decede == 1}
+    <div>(décédé)</div>
+  {/if}
+</td>
+<td>
+  (X {$result.promo})
+</td>
index 7e82678..f92a06c 100644 (file)
@@ -1,78 +1,51 @@
 {dynamic}
-{if $formulaire==0}
+{if $formulaire==0 and !$error}
   <div class="rubrique">
     Résultats
   </div>
+  <div class="right">
+    <a href="{$smarty.server.PHP_SELF}?public_directory={$public_directory}">Nouvelle recherche</a>
+  </div>
   <p class="smaller">
     {if $nb_resultats_total==0}Aucune{else}{$nb_resultats_total}{/if} réponse{if $nb_resultats_total>1}s{/if}.
   </p>
   <table class="bicol">
     {section name=resultat loop=$resultats}
     <tr class="{cycle values="pair,impair"}">
-      <td>
-        <strong>{$resultats[resultat].nom} {$resultats[resultat].prenom}</strong>
-        {if $resultats[resultat].epouse neq ""}
-          <div>({$resultats[resultat].epouse} {$resultats[resultat].prenom})</div>
-        {/if}
-        {if $resultats[resultat].decede == 1}
-          <div>(décédé)</div>
-        {/if}
-      </td>
-      <td>
-        (X {$resultats[resultat].promo})
-        {if $resultats[resultat].inscrit==1}
-          <a href="javascript:x()"  onclick="popWin('x.php?x={$resultats[resultat].username}')">
-          <img src="images/loupe.gif" border=0 ALT="Afficher les détails"></a>
-          <a href="vcard.php/{$resultats[resultat].username}.vcf?x={$resultats[resultat].username}">
-          <img src="images/vcard.png" border=0 ALT="Afficher la carte de visite"></a>
-          <a href="mescontacts.php?action={if $resultats[resultat].contact!=""}retirer{else}ajouter{/if}&amp;user={$resultats[resultat].username}&amp;mode=normal">
-          <img src="images/{if $resultats[resultat].contact!=""}retirer{else}ajouter{/if}.gif" border=0 ALT="{if $resultats[resultat].contact!=""}Retirer de{else}Ajouter parmi{/if} mes contacts"></a>
-        {/if}
-      </td>
+      {include file="search.result.public.tpl" result=$resultats[resultat]}
+      {if $public_directory!=1}
+        {include file="search.result.private.tpl" result=$resultats[resultat]}
+      {/if}
     </tr>
     {/section}
   </table>
   {if $perpage<$nb_resultats_total}
+  <p>
     {if $offset!=0}
-        <a href="{$smarty.server.PHP_SELF}?public_directory={$public_directory}&rechercher=1&{$url_args}&offset=0">Précédent</a>
+      <a href="{$smarty.server.PHP_SELF}?public_directory={$public_directory}&rechercher=1&{$url_args}&offset={$offset-$perpage}">
+        Précédent
+      </a>
+      &nbsp;
     {/if}
+    {section name=offset loop=$offsets}
+      {if $offset!=$smarty.section.offset.index*$perpage}
+        <a href="{$smarty.server.PHP_SELF}?public_directory={$public_directory}&rechercher=1&{$url_args}&offset={$smarty.section.offset.index*$perpage}">
+          {$smarty.section.offset.index+1}
+        </a>
+      {else}
+        <strong>{$smarty.section.offset.index+1}</strong>
+      {/if}
+      &nbsp;
+    {/section}
     {if $offset<$nb_resultats_total-$perpage}
-        <a
-        href="{$smarty.server.PHP_SELF}?public_directory={$public_directory}&rechercher=1&{$url_args}&offset={$offset+$perpage}">Suivant</a>
+      <a href="{$smarty.server.PHP_SELF}?public_directory={$public_directory}&rechercher=1&{$url_args}&offset={$offset+$perpage}">
+        Suivant
+      </a>
+      &nbsp;
     {/if}
+  </p>
   {/if}
 {else}
-  <div class="rubrique">
-    Recherche
-  </div>
-  <div class="center">
-    <form action="{$smarty.server.PHP_SELF}" method="post">
-    <input type="hidden" name="public_directory" value="{$public_directory}">
-    <table class="tinybicol" cellpadding="3" summary="Recherche">
-      <tr>
-        <td>Nom</td>
-        <td><input type="text" name="name" size="50" maxlength="50" /></td>
-      </tr>
-      <tr>
-        <td>Prénom</td>
-        <td><input type="text" name="firstname" size="50" maxlength="50" /></td>
-      </tr>
-      <tr>
-        <td>Promotion</td>
-        <td>
-        <select name="egal">
-        <option value="=" selected>&nbsp;=&nbsp;</option>
-        <option value=">" >&nbsp;&gt;&nbsp;</option>
-        <option value="<" >&nbsp;&lt;&nbsp;</option>
-        </select>
-        <input type="text" name="promo" size="4" maxlength="4" />
-        </td>
-      </tr>
-      <tr>
-        <td colspan="2" class="center"><input type="submit" name="rechercher" value="Ok" /></td>
-      </tr>
-    </table>
-    </form>
-  </div>
+  {include file="search.form.tpl"}
 {/if}
 {/dynamic}