Adds 'by AX id' advanced search for admins (Closes #1366)
authorRaphaël Barrois <raphael.barrois@polytechnique.org>
Tue, 28 Dec 2010 16:05:09 +0000 (17:05 +0100)
committerRaphaël Barrois <raphael.barrois@polytechnique.org>
Wed, 29 Dec 2010 17:55:16 +0000 (18:55 +0100)
This field is only available for X.org admins and AX secretaries.

Signed-off-by: Raphaël Barrois <raphael.barrois@polytechnique.org>
include/ufbuilder.inc.php
include/userset.inc.php
modules/search.php
templates/search/adv.form.tpl

index 2eacd43..f189ca3 100644 (file)
@@ -176,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 +215,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 +574,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
 {
index d2e1823..21ad42d 100644 (file)
@@ -115,9 +115,11 @@ class QuickSearchSet extends SearchSet
 // Specialized SearchSet for advanced search.
 class AdvancedSearchSet extends SearchSet
 {
-    public function __construct(PlFilterCondition $cond = null)
+    public function __construct($xorg_admin_fields, $ax_admin_fields,
+                                PlFilterCondition $cond = null)
     {
-        parent::__construct(new UFB_AdvancedSearch(), $cond);
+        parent::__construct(new UFB_AdvancedSearch($xorg_admin_fields, $ax_admin_fields),
+                            $cond);
     }
 }
 
index 21d9870..86ccc55 100644 (file)
@@ -169,7 +169,10 @@ class SearchModule extends PLModule
             }
 
             require_once 'userset.inc.php';
-            $view = new AdvancedSearchSet();
+            // 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 167f8d4..b239e4e 100644 (file)
@@ -487,6 +487,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">