Merge branch 'xorg/maint' into xorg/master
[platal.git] / include / validations / paiements.inc.php
index bb3afe4..0214520 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /***************************************************************************
- *  Copyright (C) 2003-2009 Polytechnique.org                              *
+ *  Copyright (C) 2003-2011 Polytechnique.org                              *
  *  http://opensource.polytechnique.org/                                   *
  *                                                                         *
  *  This program is free software; you can redistribute it and/or modify   *
@@ -37,16 +37,17 @@ class PayReq extends Validate
     public $asso;
     public $evt;
     public $evt_intitule;
+    public $public;
 
-    public $rules = "Vérifier que les balises &lt;salutation&gt;, &lt;prenom&gt;, &lt;nom&gt; et &lt;montant&gt; n'ont pas été modifiées.
+    public $rules = "Vérifier que les balises &lt;salutation&gt;, &lt;prenom&gt;, &lt;nom&gt;,  &lt;montant&gt; et &lt;comment&gt; n'ont pas été modifiées.
 Vérifier que le demandeur n'a pas laissé les crochets [].
 Si le télépaiement n'est pas lié à un groupe ou supérieur à 51 euros, laisser la validation à un trésorier";
     // }}}
     // {{{ constructor
 
-    public function __construct(User &$_user, $_intitule, $_site, $_montant, $_msg,
+    public function __construct(User $_user, $_intitule, $_site, $_montant, $_msg,
                                 $_montantmin=0, $_montantmax=999, $_asso_id = 0,
-                                $_evt = 0, $_stamp=0)
+                                $_evt = 0, $_public = false, $_stamp = 0)
     {
         parent::__construct($_user, false, 'paiements', $_stamp);
 
@@ -58,13 +59,14 @@ Si le télépaiement n'est pas lié à un groupe ou supérieur à 51 euros, lais
         $this->montant      = $_montant;
         $this->montant_min  = $_montantmin;
         $this->montant_max  = $_montantmax;
+        $this->public       = $_public;
 
         if ($_asso_id) {
-            $res = XDB::query("SELECT nom FROM groupex.asso WHERE id = {?}", $_asso_id);
+            $res = XDB::query("SELECT nom FROM groups WHERE id = {?}", $_asso_id);
             $this->asso = $res->fetchOneCell();
         }
         if ($_asso_id && $_evt) {
-            $res = XDB::query("SELECT intitule FROM groupex.evenements WHERE asso_id = {?} AND eid = {?}", $_asso_id, $_evt);
+            $res = XDB::query("SELECT intitule FROM group_events WHERE asso_id = {?} AND eid = {?}", $_asso_id, $_evt);
             $this->evt_intitule = $res->fetchOneCell();
         }
     }
@@ -87,7 +89,7 @@ Si le télépaiement n'est pas lié à un groupe ou supérieur à 51 euros, lais
     public function accept()
     {
         // no text [AI JMIAJM IJA MIJ]
-        if (preg_match('/\[[-\'"A-Z ]+\]/', $this->msg_reponse)) {
+        if (preg_match('/\[[-\'"a-zA-Z ]+\]/', replace_accent($this->msg_reponse))) {
             $this->trigError("La demande de paiement n'est pas valide. Merci de compléter le texte avant de la soumettre");
             return false;
         }
@@ -137,6 +139,7 @@ Si le télépaiement n'est pas lié à un groupe ou supérieur à 51 euros, lais
         $this->montant_min = Env::i('pay_montant_min');
         $this->montant_max = Env::i('pay_montant_max');
         $this->msg_reponse = Env::v('pay_msg_reponse');
+        $this->public      = (Env::v('pay_public') == 'yes');
         return true;
     }
 
@@ -154,9 +157,9 @@ Si le télépaiement n'est pas lié à un groupe ou supérieur à 51 euros, lais
     protected function _mail_body($isok)
     {
         if ($isok) {
-            return "  Le paiement que tu avais demandé pour {$this->titre} vient d'être créé.".($this->evt?" Il a bien été associé à la gestion de l'événement du groupe":"");
+            return "  Le paiement demandé pour {$this->titre} vient d'être créé.".($this->evt?" Il a bien été associé à la gestion de l'événement du groupe":"");
         } else {
-            return "  La demande que tu avais faite pour le paiement de {$this->intitule} a été refusée.";
+            return "  La demande faite pour le paiement de {$this->intitule} a été refusée.";
         }
     }
 
@@ -165,43 +168,40 @@ Si le télépaiement n'est pas lié à un groupe ou supérieur à 51 euros, lais
 
     public function commit()
     {
-        $res = XDB::query("SELECT MAX(id) FROM paiement.paiements");
+        $res = XDB::query("SELECT MAX(id) FROM payments");
         $id = $res->fetchOneCell()+1;
-        $ret = XDB::execute("INSERT INTO paiement.paiements VALUES
-            ( {?}, {?}, {?}, '',
-            {?}, {?}, {?},
-            {?}, {?}, {?} )
-            ",
-            $id, $this->titre, $this->site,
-            $this->montant, $this->montant_min, $this->montant_max,
-            $this->user->bestEmail(), $this->msg_reponse, $this->asso_id);
+        $ret = XDB::execute('INSERT INTO  payments (id, text, url, amount_def, amount_min, amount_max, mail, confirmation, asso_id, flags)
+                                  VALUES  ({?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?})',
+                            $id, $this->titre, $this->site, $this->montant, $this->montant_min,
+                            $this->montant_max, $this->user->bestEmail(), $this->msg_reponse, $this->asso_id,
+                            ($this->public ? 'public' : ''));
         if ($this->asso_id && $this->evt) {
-            XDB::execute("UPDATE  groupex.evenements
+            XDB::execute("UPDATE  group_events
                              SET  paiement_id = {?}
                            WHERE  asso_id = {?} AND eid = {?}",
                          $id, $this->asso_id, $this->evt);
             $res = XDB::query("SELECT  a.nom, a.diminutif, e.intitule
-                                 FROM  groupex.asso AS a
-                           INNER JOIN  groupex.evenements AS e ON (a.id = e.asso_id)
+                                 FROM  groups AS a
+                           INNER JOIN  group_events AS e ON (a.id = e.asso_id)
                                 WHERE  e.eid = {?}",
                               $this->evt);
             list($nom, $diminutif, $evt) = $res->fetchOneRow();
             require_once dirname(__FILE__) . '/../../modules/xnetevents/xnetevents.inc.php';
-            $participants = get_event_participants(get_event_detail($this->evt, false, $this->asso_id), null, 'nom');
-            foreach ($participants as &$u) {
+            $participants = get_event_participants(get_event_detail($this->evt, false, $this->asso_id), null);
+            foreach ($participants as $u) {
                 if (!$u['notify_payment']) {
                     continue;
                 }
                 $topay = $u['montant'] - $u['paid'];
                 if ($topay > 0) {
                     $mailer = new PlMailer('xnetevents/newpayment.mail.tpl');
+                    $mailer->addTo($u['user']);
                     $mailer->assign('asso', $nom);
                     $mailer->assign('diminutif', $diminutif);
                     $mailer->assign('evt', $evt);
                     $mailer->assign('payment', $id);
-                    $mailer->assign('prenom', $u['prenom']);
+                    $mailer->assign('prenom', $u['user']->firstName());
                     $mailer->assign('topay', $topay);
-                    $mailer->assign('to', $u['email']);
                     $mailer->send();
                 }
             }