Merge branch 'xorg/1.0.2/master' into xorg/master
authorRaphaël Barrois <raphael.barrois@polytechnique.org>
Wed, 29 Dec 2010 23:10:59 +0000 (00:10 +0100)
committerRaphaël Barrois <raphael.barrois@polytechnique.org>
Wed, 29 Dec 2010 23:10:59 +0000 (00:10 +0100)
Signed-off-by: Raphaël Barrois <raphael.barrois@polytechnique.org>
classes/userfilter/conditions.inc.php
include/ufbuilder.inc.php
include/userset.inc.php
modules/carnet.php
modules/gadgets.php
modules/search.php
templates/search/adv.form.tpl

index 24a2857..4678a55 100644 (file)
@@ -430,7 +430,7 @@ class UFC_Promo extends UserFilterCondition
 // {{{ class UFC_SchoolId
 /** Filters users based on their shoold identifier
  * @param type Parameter type (Xorg, AX, School)
- * @param value School id value
+ * @param value Array of school ids
  */
 class UFC_SchoolId extends UserFilterCondition
 {
@@ -448,23 +448,30 @@ class UFC_SchoolId extends UserFilterCondition
         }
     }
 
-    public function __construct($type, $id)
+    /** Construct a UFC_SchoolId
+     * The first argument is the type, all following arguments can be either ids
+     * or arrays of ids to use:
+     * $ufc = new UFC_SchoolId(UFC_SchoolId::AX, $id1, $id2, array($id3, $id4));
+     */
+    public function __construct($type)
     {
         $this->type = $type;
-        $this->id   = $id;
+        $ids = func_get_args();
+        array_shift($ids);
+        $this->ids   = pl_flatten($ids);
         self::assertType($type);
     }
 
     public function buildCondition(PlFilter $uf)
     {
         $uf->requireProfiles();
-        $id = $this->id;
+        $ids = $this->ids;
         $type = $this->type;
         if ($type == self::School) {
             $type = self::Xorg;
-            $id   = Profile::getXorgId($id);
+            $ids  = array_map(array('Profile', 'getXorgId'), $ids);
         }
-        return XDB::format('p.' . $type . '_id = {?}', $id);
+        return XDB::format('p.' . $type . '_id IN {?}', $ids);
     }
 }
 // }}}
index 0e2b872..79b918a 100644 (file)
@@ -166,6 +166,7 @@ class UFB_QuickSearch extends UserFilterBuilder
     {
         $fields = array(
             new UFBF_Quick('quick', 'Recherche rapide'),
+            new UFBF_NotRegistered('nonins', 'Non inscrits'),
         );
         parent::__construct($fields, $envprefix);
     }
@@ -175,7 +176,12 @@ class UFB_QuickSearch extends UserFilterBuilder
 // {{{ class UFB_AdvancedSearch
 class UFB_AdvancedSearch extends UserFilterBuilder
 {
-    public function __construct($envprefix = '')
+    /** Create a UFB_AdvancedSearch.
+     * @param $include_admin Whether to include 'admin-only' fields
+     * @param $include_ax Whether to include 'ax-only' fields
+     * @param $envprefix Optional prefix for form field names.
+     */
+    public function __construct($include_admin = false, $include_ax = false, $envprefix = '')
     {
         $fields = array(
             new UFBF_Name('name', 'Nom'),
@@ -210,6 +216,11 @@ class UFB_AdvancedSearch extends UserFilterBuilder
 
             new UFBF_Mentor('only_referent', 'Référent'),
         );
+
+        if ($include_admin || $include_ax) {
+            $fields[] = new UFBF_SchoolIds('schoolid_ax', 'Matricule AX', UFC_SchoolId::AX);
+        }
+
         parent::__construct($fields, $envprefix);
     }
 }
@@ -564,6 +575,48 @@ class UFBF_Quick extends UFB_Field
 }
 // }}}
 
+// {{{ class UFBF_SchoolIds
+class UFBF_SchoolIds extends UFB_Field
+{
+    // One of UFC_SchoolId types
+    protected $type;
+
+    public function __construct($envfield, $formtext, $type = UFC_SchoolId::AX)
+    {
+        parent::__construct($envfield, $formtext);
+        $this->type = $type;
+    }
+
+    protected function check(UserFilterBuilder &$ufb)
+    {
+        if ($ufb->blank($this->envfield)) {
+            $this->empty = true;
+            return true;
+        }
+
+        $value = $ufb->t($this->envfield);
+        $values = explode("\n", $value);
+        $ids = array();
+        foreach ($values as $val) {
+            if (preg_match('/^[0-9A-Z]{0,8}$/', $val)) {
+                $ids[] = $val;
+            }
+        }
+        if (count($ids) == 0) {
+            return $this->raise("Le champ %s ne contient aucune valeur valide.");
+        }
+
+        $this->val = $ids;
+        return true;
+    }
+
+    protected function buildUFC(UserFilterBuilder &$ufb)
+    {
+        return new UFC_SchoolId($this->type, $this->val);
+    }
+}
+// }}}
+
 // {{{ class UFBF_Name
 class UFBF_Name extends UFBF_Text
 {
@@ -656,6 +709,22 @@ class UFBF_Sex extends UFBF_Enum
 }
 // }}}
 
+// {{{ class UFBF_NotRegistered
+// Simple field for selecting only alive, not registered users (for quick search)
+class UFBF_NotRegistered extends UFBF_Bool
+{
+    protected function buildUFC(UserFilterBuilder &$ufb)
+    {
+        if ($this->val) {
+            return new PFC_And(
+                new PFC_Not(new UFC_Dead()),
+                new PFC_Not(new UFC_Registered())
+            );
+        }
+    }
+}
+// }}}
+
 // {{{ class UFBF_Registered
 class UFBF_Registered extends UFBF_Enum
 {
index c648005..21ad42d 100644 (file)
@@ -45,21 +45,15 @@ class ProfileSet extends PlSet
     }
 }
 
+require_once "ufbuilder.inc.php";
+
 class SearchSet extends ProfileSet
 {
-    public  $advanced = false;
-    private $score    = null;
-    private $quick    = false;
-    private $valid    = true;
+    protected $score    = null;
+    protected $valid    = true;
 
-    public function __construct($quick = false, PlFilterCondition $cond = null)
+    public function __construct(UserFilterBuilder &$ufb, PlFilterCondition $cond = null)
     {
-        if (isset($no_search)) {
-            return;
-        }
-
-        $this->quick = $quick;
-
         if (is_null($cond)) {
             $conds = new PFC_And();
         } else if ($cond instanceof PFC_And) {
@@ -68,30 +62,6 @@ class SearchSet extends ProfileSet
             $conds = new PFC_And($cond);
         }
 
-        if ($quick) {
-            $this->getQuick($conds);
-        } else {
-            $this->getAdvanced($conds);
-        }
-    }
-
-    public function isValid()
-    {
-        return $this->valid;
-    }
-
-    /** Sets up the conditions for a Quick Search
-     * @param $conds Additional conds (as a PFC_And)
-     */
-    private function getQuick($conds)
-    {
-        if (!S::logged()) {
-            Env::kill('with_soundex');
-        }
-
-        require_once 'ufbuilder.inc.php';
-        $ufb = new UFB_QuickSearch();
-
         if (!$ufb->isValid()) {
             $this->valid = false;
             return;
@@ -102,36 +72,12 @@ class SearchSet extends ProfileSet
 
         $orders = $ufb->getOrders();
 
-        if (S::logged() && Env::has('nonins')) {
-            $conds = new PFC_And($conds,
-                new PFC_Not(new UFC_Dead()),
-                new PFC_Not(new UFC_Registered())
-            );
-        }
-
         parent::__construct($conds, $orders);
     }
 
-    /** Sets up the conditions for an Advanced Search
-     * @param $conds Additional conds (as a PFC_And)
-     */
-    private function getAdvanced($conds)
+    public function isValid()
     {
-        $this->advanced = true;
-        require_once 'ufbuilder.inc.php';
-        $ufb = new UFB_AdvancedSearch();
-
-        if (!$ufb->isValid()) {
-            $this->valid = false;
-            return;
-        }
-
-        $ufc = $ufb->getUFC();
-        $conds->addChild($ufc);
-
-        $orders = $ufb->getOrders();
-
-        parent::__construct($conds, $orders);
+        return $this->valid;
     }
 
     /** Add a "rechercher=Chercher" field to the query to simulate the POST
@@ -153,6 +99,30 @@ class SearchSet extends ProfileSet
     }
 }
 
+// Specialized SearchSet for quick search.
+class QuickSearchSet extends SearchSet
+{
+    public function __construct(PlFilterCondition $cond = null)
+    {
+        if (!S::logged()) {
+            Env::kill('with_soundex');
+        }
+
+        parent::__construct(new UFB_QuickSearch(), $cond);
+    }
+}
+
+// Specialized SearchSet for advanced search.
+class AdvancedSearchSet extends SearchSet
+{
+    public function __construct($xorg_admin_fields, $ax_admin_fields,
+                                PlFilterCondition $cond = null)
+    {
+        parent::__construct(new UFB_AdvancedSearch($xorg_admin_fields, $ax_admin_fields),
+                            $cond);
+    }
+}
+
 /** Simple set based on an array of User objects
  */
 class ArraySet extends ProfileSet
index 9454062..ba5e1df 100644 (file)
@@ -316,7 +316,7 @@ class CarnetModule extends PLModule
         }
         if ($search && trim(Env::v('quick'))) {
             $base = 'carnet/contacts/search';
-            $view = new SearchSet(true, new UFC_Contact($user));
+            $view = new QuickSearchSet(new UFC_Contact($user));
         } else {
             $base = 'carnet/contacts';
             $view = new ProfileSet(new UFC_Contact($user));
index b8700de..d3bbe8b 100644 (file)
@@ -74,7 +74,7 @@ class GadgetsModule extends PLModule
             global $globals;
             require_once 'userset.inc.php';
 
-            $view = new SearchSet(true);
+            $view = new QuickSearchSet();
             $view->addMod('gadget', 'Gadget', true);
             $view->apply(null, $page);
 
index 6065438..feea72a 100644 (file)
@@ -119,7 +119,7 @@ class SearchModule extends PLModule
             $page->assign('formulaire', 0);
 
             require_once 'userset.inc.php';
-            $view = new SearchSet(true);
+            $view = new QuickSearchSet();
             $view->addMod('minifiche', 'Mini-fiches', true, array('with_score' => true, 'starts_with' => $byletter));
             if (S::logged() && !Env::i('nonins')) {
                 $view->addMod('trombi', 'Trombinoscope', false, array('with_promo' => true, 'with_score' => true));
@@ -167,7 +167,10 @@ class SearchModule extends PLModule
             }
 
             require_once 'userset.inc.php';
-            $view = new SearchSet(false);
+            // Enable X.org fields for X.org admins, and AX fields for AX secretaries.
+            $view = new AdvancedSearchSet(S::admin(),
+                                          S::user()->checkPerms(User::PERM_EDIT_DIRECTORY));
+
             if (!$view->isValid()) {
                 $this->form_prepare();
                 $page->trigError('Recherche invalide.');
index 3ae3da2..9940d40 100644 (file)
@@ -514,6 +514,15 @@ function cleanForm(f) {
         </table>
       </td>
     </tr>
+        {if hasPerm('admin,edit_directory')}
+    <tr>
+      <td>Matricule AX</td>
+      <td>
+        <textarea name="schoolid_ax" rows="10" cols="12">{$smarty.request.schoolid_ax}</textarea>
+        <br />
+        <i>Entrer une liste de matricules AX (un par ligne)</i>
+      </td>
+        {/if}
         {if $smarty.session.auth ge AUTH_COOKIE}
     <tr>
       <td colspan="2">