Remove ValidateIterator, replaced by Validate::iterate().
authorFlorent Bruneau <florent.bruneau@polytechnique.org>
Mon, 27 Sep 2010 14:07:03 +0000 (16:07 +0200)
committerFlorent Bruneau <florent.bruneau@polytechnique.org>
Mon, 27 Sep 2010 15:26:12 +0000 (17:26 +0200)
Signed-off-by: Florent Bruneau <florent.bruneau@polytechnique.org>
include/validations.inc.php
modules/admin.php
modules/email.php

index b268fb6..d5678bd 100644 (file)
 define('SIZE_MAX', 32768);
 
 global $globals;
-require_once $globals->spoolroot . '/core/classes/xdb.php';
 
-/**
- * Iterator class, that lists objects through the database
- */
-class ValidateIterator extends XOrgDBIterator
-{
-    // {{{ constuctor
-
-    public function __construct()
-    {
-        parent::__construct('SELECT  data, DATE_FORMAT(stamp, "%Y%m%d%H%i%s")
-                               FROM  requests
-                           ORDER BY  stamp', MYSQL_NUM);
-    }
-
-    // }}}
-    // {{{ function next()
-
-    public function next()
-    {
-        if (list($result, $stamp) = parent::next()) {
-            $result = Validate::unserialize($result);
-            $result->stamp = $stamp;
-            return $result;
-        }
-
-        return null;
-    }
-
-    // }}}
-}
 
 /** Virtual class to adapt for every possible implementation.
  */
@@ -426,6 +395,43 @@ abstract class Validate
     }
 
     // }}}
+
+    /** Return an iterator over the validation concerning the given type
+     * and the given user.
+     *
+     * @param type The type of the validations to fetch, null mean "any type"
+     * @param applyTo A User or a Profile object the validation applies to.
+     */
+    public static function iterate($type = null, $applyTo = null)
+    {
+        function toValidation($elt)
+        {
+            list($result, $stamp) = $elt;
+            $result = Validate::unserialize($result);
+            $result->stamp = $stamp;
+            return $result;
+        }
+
+        $where = array();
+        if ($type) {
+            $where[] = XDB::format('type = {?}', $type);
+        }
+        if ($applyTo) {
+            if ($applyTo instanceof User) {
+                $where[] = XDB::format('uid = {?}', $applyTo->id());
+            } else if ($applyTo instanceof Profile) {
+                $where[] = XDB::format('pid = {?}', $applyTo->id());
+            }
+        }
+        if (!empty($where)) {
+            $where = 'WHERE ' . implode('AND', $where);
+        }
+        $it = XDB::iterRow('SELECT  data, DATE_FORMAT(stamp, "%Y%m%d%H%i%s")
+                              FROM  requests
+                                 ' . $where . '
+                          ORDER BY  stamp');
+        return PlIteratorUtils::map($it, 'toValidation');
+    }
 }
 
 /** Virtual class for profile related validation.
index c34e2fb..e1e2ceb 100644 (file)
@@ -1060,7 +1060,7 @@ class AdminModule extends PLModule
         // where several copies of the site use the same DB, but not the same "dynamic configuration"
         global $globals;
         $globals->updateNbValid();
-        $page->assign('vit', new ValidateIterator());
+        $page->assign('vit', Validate::iterate());
     }
 
     function handler_validate_answers(&$page, $action = 'list', $id = null)
index e352e6a..6f85504 100644 (file)
@@ -169,9 +169,9 @@ class EmailModule extends PLModule
                 }
 
                 //vérifier que l'alias n'est pas déja en demande
-                $it = new ValidateIterator();
+                $it = Validate::iterate('alias');
                 while($req = $it->next()) {
-                    if ($req->type == 'alias' and $req->alias == $alias_mail) {
+                    if ($req->alias == $alias_mail) {
                         $page->trigError("L'alias $alias_mail a déja été demandé.
                                     Tu ne peux donc pas l'obtenir pour l'instant.");
                         return ;