-absents/-participants bootstrap and renaming
authorFlorent Bruneau <florent.bruneau@polytechnique.org>
Fri, 18 Jan 2008 23:33:02 +0000 (00:33 +0100)
committerFlorent Bruneau <florent.bruneau@polytechnique.org>
Fri, 18 Jan 2008 23:33:02 +0000 (00:33 +0100)
Signed-off-by: Florent Bruneau <florent.bruneau@polytechnique.org>
modules/xnetevents/xnetevents.inc.php

index c9b0a06..3bde74e 100644 (file)
@@ -162,14 +162,18 @@ class XNetEvent
             $dogs[] = 'paymentissubscription';
         }
         $dogs = implode(',', $dogs);
-        global $globals;
+        global $globals, $page;
         if (is_null($this->id)) {
+            if (!$this->bootstrapEventLists()) {
+                return false;
+            }
             if (!XDB::execute("INSERT INTO  groupex.events
                                            (asso_id, respo_uid, shortname, title, description,
                                             sublimit, categories, flags, state)
                                    VALUES  ({?}, {?}. {?}, {?}, {?}, {?}, {?}, {?}, 'prepare')",
                              $globals->asso('id'), $this->respoUID, $this->shortname, $this->title,
                              $this->description, $this->subscriptionLimit, $cats, $dogs)) {
+                $page->trig("Impossible de créer l'événement");
                 return false;
             }
             $this->id = XDB::insertId();
@@ -180,6 +184,7 @@ class XNetEvent
                                 WHERE  id = {?} AND asso_id = {?}",
                               $this->shortname, $this->title, $this->description, $this->sublimit,
                               $cats, $dogs, $this->id, $globals->asso('id'))) {
+                $page->trig("Impossible de mettre à jour l'événement");
                 return false;
             }
         }
@@ -197,24 +202,110 @@ class XNetEvent
 
     // Event edition functions {{{2
 
-    public function setShortname($newname = null) {
-        // TODO: do not forget to update partitipants/absents aliases
+    public function setShortname($newname) {
+        if (!$this->renameEventLists($this->shortname, $newname)) {
+            return false;
+        }
+        $this->shortname = $newname;
         $this->changed = true;
+        return true;
     }
 
     public function setTitle($title) {
         $this->title = $title;
         $this->changed = true;
+        return true;
     }
 
     public function setDescription($description) {
         $this->description = $description;
         $this->changed = true;
+        return true;
     }
 
     public function setSubscriptionLimit($date) {
         $this->subscriptionLimit = $date;
         $this->changed = true;
+        return true;
+    }
+
+
+    // (Non-)Subscriber aliases management {{{2
+
+    /** Build 2 aliases that can be used to contact the member of the group:
+     *    - shortname-participants groups all subscribers to the event
+     *    - shortname-absents groups all users that has not yet subscribed.
+     * Users that tell they won't come to the event are on none of the two lists.
+     *
+     * This function MUST succeed in order to add an event to the databse.
+     */
+    private function bootstrapEventLists() {
+        global $globals, $page;
+        $participants = -1;
+        $absents      = -1;
+        if (!XDB::execute("INSERT INTO  virtual (alias, type)
+                                VALUES  ({?}, 'evt')",
+                           $this->shortname . '-participants@' . $globals->xnet->evts_domain)) {
+            $page->trig("Le nom de l'événement est déjà utilisé, merci d'en choisir un autre.");
+            return false;
+        }
+        $participants = XDB::insertId();
+        // Don't know why this can fail, but be sure...
+        if (!XDB::execute("INSERT INTO  virtual (alias, type)
+                                VALUES  ({?}, 'evt')",
+                          $this->shortname . '-absents@' . $globals->xnet->evts_domain)) {
+            XDB::execute("DELETE FROM  virtual
+                                WHERE  id = {?}",
+                        $participants);
+            $page->trig("Une erreur s'est produite lors de la création de l'événement");
+            return false;
+        }
+        $absents = XDB::insertId();
+
+        // Add all members of the group to the "absents" list
+        XDB::execute("INSERT INTO  virtual_redirect
+                           SELECT  {?} AS vid,
+                                   IF(a.alias IS NOT NULL, CONCAT(a.alias, '@', {?}), m.email) AS redirect
+                             FROM  groupex.membres AS m
+                        LEFT JOIN  aliases AS a ON (m.uid = a.id AND a.type = 'a_vie')
+                            WHERE asso_id = {?}",
+                     $absents, strlen($globals->mail->domain) < strlen($globals->mail->domain2) ? $globals->mail->domain
+                                                                                                : $globals->mail->domain2,
+                     $globals->asso('id'));
+        return true;
+    }
+
+    /** Change the name of the aliases used to send informations to members.
+     * Success is a prerequist to any shortname change.
+     */
+    private function renameEventLists($oldname, $newname) {
+        if (strtolower($oldname) == strtolower($newname)) {
+            return true;
+        }
+        global $globals, $page;
+        if (!XDB::execute("UPDATE  virtual
+                              SET  alias = {?}
+                            WHERE  alias = {?}",
+                          $newname . '-absents@' . $globals->xnet->evts_domain,
+                          $oldname . '-absents@' . $globals->xnet->evts_domain)) {
+            $page->trig("Impossible d'utiliser $newname comme nom, celui-ci est déjà utilisé");
+            return false;
+        }
+        // Don't know why this could fail
+        if (!XDB::execute("UPDATE  virtual
+                              SET  alias = {?}
+                            WHERE  alias = {?}",
+                          $newname . '-participants@' . $globals->xnet->evts_domain,
+                          $oldname . '-participants@' . $globals->xnet->evts_domain)) {
+            XDB::execute("UPDATE  virtual
+                             SET  alias = {?}
+                           WHERE  alias = {?}",
+                          $oldname . '-absents@' . $globals->xnet->evts_domain,
+                          $newname . '-absents@' . $globals->xnet->evts_domain);
+            $page->trig("Une erreur s'est produite lors du renommage de $oldname");
+            return false;
+        }
+        return true;
     }
 
 
@@ -227,6 +318,11 @@ class XNetEvent
         // TODO: do not forget to update participants/absents aliases
     }
 
+    public function canSubscribe() {
+        global $globals;
+        return $this->nonmembre || $globals->perms->hasFlag('groupmember');
+    }
+
     public function payment($login, $value, $method) {
         if ($this->id >= 0) {
             return false;