Move and factorize code:
[platal.git] / include / validations.inc.php
index 536975f..12ec281 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /***************************************************************************
- *  Copyright (C) 2003-2007 Polytechnique.org                              *
+ *  Copyright (C) 2003-2008 Polytechnique.org                              *
  *  http://opensource.polytechnique.org/                                   *
  *                                                                         *
  *  This program is free software; you can redistribute it and/or modify   *
@@ -21,7 +21,8 @@
 
 define('SIZE_MAX', 32768);
 
-require_once dirname(__FILE__) . '/../classes/xdb.php';
+global $globals;
+require_once $globals->spoolroot . '/core/classes/xdb.php';
 
 /**
  * Iterator class, that lists objects through the database
@@ -115,6 +116,8 @@ abstract class Validate
         XDB::execute('INSERT INTO requests (user_id, type, data, stamp) VALUES ({?}, {?}, {?}, {?})',
                 $this->uid, $this->type, $this, $this->stamp);
 
+        global $globals;
+        $globals->updateNbValid();
         return true;
     }
 
@@ -138,24 +141,26 @@ abstract class Validate
     public function clean()
     {
         if ($this->unique) {
-            return XDB::execute('DELETE FROM requests WHERE user_id={?} AND type={?}',
-                    $this->uid, $this->type);
+            $success = XDB::execute('DELETE FROM requests WHERE user_id={?} AND type={?}',
+                                    $this->uid, $this->type);
         } else {
-            return XDB::execute('DELETE FROM requests WHERE user_id={?} AND type={?} AND stamp={?}',
-                    $this->uid, $this->type, $this->stamp);
+            $success =  XDB::execute('DELETE FROM requests WHERE user_id={?} AND type={?} AND stamp={?}',
+                                      $this->uid, $this->type, $this->stamp);
         }
+        $globals->updateNbValid();
+        return $success;
     }
 
     // }}}
     // {{{ function handle_formu()
 
-    /** fonction à réaliser en cas de valistion du formulaire
+    /** fonction à réaliser en cas de validation du formulaire
      */
     public function handle_formu()
     {
         if (Env::has('delete')) {
             $this->clean();
-            $this->trig('requete supprimée');
+            $this->trigSuccess('Requête supprimée');
             return true;
         }
 
@@ -163,7 +168,7 @@ abstract class Validate
         if (Env::has('edit')) {
             if ($this->handle_editor()) {
                 $this->update();
-                $this->trig('requête mise à jour');
+                $this->trigSuccess('Requête mise à jour');
                 return true;
             }
             return false;
@@ -198,7 +203,7 @@ abstract class Validate
             $mailer->send();
 
             $this->update();
-            $this->trig('commentaire ajouté');
+            $this->trigSuccess('Commentaire ajouté');
             return true;
         }
 
@@ -206,10 +211,10 @@ abstract class Validate
             if ($this->commit()) {
                 $this->sendmail(true);
                 $this->clean();
-                $this->trig('mail envoyé');
+                $this->trigSuccess('Mail de validation envoyé');
                 return true;
             } else {
-                $this->trig('erreur lors de la validation');
+                $this->trigError('Erreur lors de la validation');
                 return false;
             }
         }
@@ -218,10 +223,10 @@ abstract class Validate
             if (Env::v('comm')) {
                 $this->sendmail(false);
                 $this->clean();
-                $this->trig('mail envoyé');
+                $this->trigSuccess('Mail de refus envoyé');
                 return true;
             } else {
-                $this->trig('pas de motivation pour le refus !!!');
+                $this->trigError('pas de motivation pour le refus !!!');
             }
         }
 
@@ -252,10 +257,19 @@ abstract class Validate
     // }}}
     // {{{ function trig()
 
-    protected function trig($msg)
+    protected function trigError($msg)
+    {
+        Platal::page()->trigError($msg);
+    }
+
+    protected function trigWarning($msg)
+    {
+        Platal::page()->trigWarning($msg);
+    }
+
+    protected function trigSuccess($msg)
     {
-        global $page;
-        $page->trig($msg);
+        Platal::page()->trigSuccess($msg);
     }
 
     // }}}
@@ -309,6 +323,17 @@ abstract class Validate
     }
 
     // }}}
+    // {{{ function get_typed_requests_count()
+
+    /** same as get_typed_requests() but return the count of available requests.
+     */
+    static public function get_typed_requests_count($uid, $type)
+    {
+        $res = XDB::query('SELECT COUNT(data) FROM requests WHERE user_id={?} and type={?}', $uid, $type);
+        return $res->fetchOneCell();
+    }
+
+    // }}}
     // {{{ function _mail_body
 
     abstract protected function _mail_body($isok);