Automation of the management of associations' mailing lists (Closes #817), Updates...
[platal.git] / include / validations / listes.inc.php
index 47471e4..a2fc0e6 100644 (file)
@@ -27,6 +27,8 @@ class ListeReq extends Validate
 
     public $liste;
     public $desc;
+    public $asso;
+    public $domain;
 
     public $advertise;
     public $modlevel;
@@ -40,12 +42,14 @@ class ListeReq extends Validate
     // }}}
     // {{{ constructor
 
-    public function __construct($_uid, $_liste, $_desc, $_advertise, $_modlevel,
-                                $_inslevel, $_owners, $_members, $_stamp=0)
+    public function __construct($_uid, $_asso, $_liste, $_domain, $_desc, $_advertise,
+                                $_modlevel, $_inslevel, $_owners, $_members, $_stamp=0)
     {
         parent::__construct($_uid, false, 'liste', $_stamp);
 
+        $this->asso      = $_asso;
         $this->liste     = $_liste;
+        $this->domain    = $_domain;
         $this->desc      = $_desc;
         $this->advertise = $_advertise;
         $this->modlevel  = $_modlevel;
@@ -78,6 +82,15 @@ class ListeReq extends Validate
         if (Env::has('listname')) {
             $this->liste = trim(Env::v('listname'));
         }
+        if (Env::has('domainname')) {
+            $this->domain = trim(Env::v('domainname'));
+        }
+        if (Env::has('assotype')) {
+            $this->asso = trim(Env::v('assotype'));
+        }
+        if (!$this->asso) {
+            $this->domain = "polytechnique.org";
+        }
         return true;
     }
 
@@ -106,15 +119,53 @@ class ListeReq extends Validate
 
     public function commit()
     {
-        $list = new MMList(S::v('uid'), S::v('password'));
+        global $globals;
+
+        if ($this->asso == "alias") {
+            $new = $this->liste . '@' . $this->domain;
+            XDB::query('INSERT INTO x4dat.virtual (alias,type) VALUES({?}, "user")', $new);
+            foreach ($this->members as $member) {
+                $res = XDB::query(
+                        "SELECT  a.alias, b.alias
+                           FROM  x4dat.aliases AS a
+                      LEFT JOIN  x4dat.aliases AS b ON (a.id=b.id AND b.type = 'a_vie')
+                          WHERE  a.alias={?} AND a.type!='homonyme'", $member);
+                list($alias, $blias) = $res->fetchOneRow();
+                $alias = empty($blias) ? $alias : $blias;
+                XDB::query(
+                    "INSERT INTO  x4dat.virtual_redirect (vid,redirect)
+                          SELECT  vid, {?}
+                            FROM  x4dat.virtual
+                           WHERE  alias={?}", $alias . "@" . $globals->mail->domain, $new);
+            }
+            return 1;
+        }
+
+        $list = new MMList(S::v('uid'), S::v('password'), $this->domain);
         $ret = $list->create_list($this->liste, utf8_decode($this->desc), $this->advertise,
                                   $this->modlevel, $this->inslevel,
                                   $this->owners, $this->members);
         $liste = strtolower($this->liste);
-        if ($ret) {
-            foreach(Array($liste, $liste."-owner", $liste."-admin", $liste."-bounces", $liste."-unsubscribe") as $l) {
+        if ($ret && !$this->asso) {
+            foreach(Array($liste, $liste . "-owner", $liste . "-admin", $liste . "-bounces", $liste . "-unsubscribe") as $l) {
                 XDB::execute("INSERT INTO aliases (alias,type) VALUES({?}, 'liste')", $l);
             }
+        } else {
+            if ($ret) {
+                foreach (Array('', 'owner', 'admin', 'bounces', 'unsubscribe') as $app) {
+                    $mdir = $app == '' ? '+post' : '+' . $app;
+                    if (!empty($app)) {
+                        $app  = '-' . $app;
+                    }
+                    $red = $this->domain . '_' . $liste;
+                    XDB::execute('INSERT INTO x4dat.virtual (alias,type)
+                                            VALUES({?},{?})', $liste . $app . '@' . $this->domain, 'list');
+                    XDB::execute('INSERT INTO x4dat.virtual_redirect (vid,redirect)
+                                            VALUES ({?}, {?})', XDB::insertId(),
+                                           $red . $mdir . '@listes.polytechnique.org');
+                    $list->mass_subscribe($liste, join(' ', $this->members));
+                }
+            }
         }
         return $ret;
     }