X-Git-Url: http://git.polytechnique.org/?a=blobdiff_plain;f=include%2Fvalidations.inc.php;h=d5678bd4567e8b2f2ff8bfd4a3d815baa7196b5b;hb=d9487b8f058912a1d405eb72941f28c837c0c4fa;hp=e04596ad33aeb830d067d4c9d1df5b68500cd087;hpb=7c4ae1c3d38d458ac3b6dea25ca76ebdd2c0ecd8;p=platal.git diff --git a/include/validations.inc.php b/include/validations.inc.php index e04596a..d5678bd 100644 --- a/include/validations.inc.php +++ b/include/validations.inc.php @@ -22,38 +22,7 @@ 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.