Unsubscribes user from groupNL when unsubscribing from group.
[platal.git] / modules / xnetgrp.php
index 051b0da..0a40bae 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /***************************************************************************
- *  Copyright (C) 2003-2013 Polytechnique.org                              *
+ *  Copyright (C) 2003-2014 Polytechnique.org                              *
  *  http://opensource.polytechnique.org/                                   *
  *                                                                         *
  *  This program is free software; you can redistribute it and/or modify   *
@@ -498,8 +498,8 @@ class XnetGrpModule extends PLModule
             $users = array_keys(Env::v('add_users'));
             $former_users = XDB::fetchColumn('SELECT  uid
                                                 FROM  group_former_members
-                                               WHERE  remember = TRUE AND uid IN {?}',
-                                             $users);
+                                               WHERE  remember = TRUE AND asso_id = {?} AND uid IN {?}',
+                                             $globals->asso('id'), $users);
             $new_users = array_diff($users, $former_users);
 
             foreach ($former_users as $uid) {
@@ -514,10 +514,8 @@ class XnetGrpModule extends PLModule
 
             $data = array();
             foreach ($new_users as $uid) {
-                $data[] = XDB::format('({?}, {?})', $globals->asso('id'), $uid);
+                Group::subscribe($globals->asso('id'), $uid);
             }
-            XDB::rawExecute('INSERT INTO  group_members (asso_id, uid)
-                                  VALUES  ' . implode(',', $data));
         }
 
         if (Env::has('add_nonusers')) {
@@ -552,9 +550,7 @@ class XnetGrpModule extends PLModule
                                    VALUES  ({?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, \'xnet\', \'disabled\')',
                              $hruid, $display_name, $full_name, $directory_name, $sort_name, $firstname, $lastname, $email);
                 $uid = XDB::insertId();
-                XDB::execute('INSERT INTO  group_members (asso_id, uid)
-                                   VALUES  ({?}, {?})',
-                             $globals->asso('id'), $uid);
+                Group::subscribe($globals->asso('id'), $uid);
             }
         }
 
@@ -729,7 +725,7 @@ class XnetGrpModule extends PLModule
         $this->removeSubscriptionRequest($user->id());
         Group::subscribe($globals->asso('id'), $user->id());
 
-        if (XDB::affectedRows() == 1) {
+        if (XDB::affectedRows() == 1 && $user->forlifeEmail()) {
             $mailer = new PlMailer();
             $mailer->addTo($user->forlifeEmail());
             $mailer->setFrom('"' . S::user()->fullName() . '" <' . S::user()->forlifeEmail() . '>');
@@ -792,12 +788,14 @@ class XnetGrpModule extends PLModule
                 S::assert_xsrf_token();
 
                 $this->removeSubscriptionRequest($user->id());
-                $mailer = new PlMailer();
-                $mailer->addTo($user->forlifeEmail());
-                $mailer->setFrom('"' . S::user()->fullName() . '" <' . S::user()->forlifeEmail() . '>');
-                $mailer->setSubject('['.$globals->asso('nom').'] Demande d\'inscription annulée');
-                $mailer->setTxtBody(Env::v('motif'));
-                $mailer->send();
+                if ($user->forlifeEmail()) {
+                    $mailer = new PlMailer();
+                    $mailer->addTo($user->forlifeEmail());
+                    $mailer->setFrom('"' . S::user()->fullName() . '" <' . S::user()->forlifeEmail() . '>');
+                    $mailer->setSubject('['.$globals->asso('nom').'] Demande d\'inscription annulée');
+                    $mailer->setTxtBody(Env::v('motif'));
+                    $mailer->send();
+                }
                 $page->killSuccess("La demande de {$user->fullName()} a bien été refusée.");
             } else {
                 $page->assign('show_form', true);
@@ -831,9 +829,13 @@ class XnetGrpModule extends PLModule
                          S::i('uid'), $globals->asso('id'));
             $admins = $globals->asso()->iterToNotify();
             $admin = $admins->next();
-            $to = $admin->bestEmail();
-            while ($admin = $admins->next()) {
-                $to .= ', ' . $admin->bestEmail();
+            if (!is_null($admin)) {
+                $to = $admin->bestEmail();
+                while ($admin = $admins->next()) {
+                    $to .= ', ' . $admin->bestEmail();
+                }
+            } else {
+                $to = '';
             }
 
             $append = "\n"
@@ -1079,6 +1081,28 @@ class XnetGrpModule extends PLModule
 
             Group::subscribe($globals->asso('id'), $user->id());
             $this->removeSubscriptionRequest($user->id());
+            if ($user->isActive() && $user->bestEmail()) {
+                $mailer = new PlMailer('xnetgrp/forced-subscription.mail.tpl');
+                $mailer->addTo($user->bestEmail());
+                $mailer->assign('group', $globals->asso('nom'));
+                $mailer->assign('anim', S::user()->fullname());
+                $mailer->assign('diminutif', $globals->asso('diminutif'));
+                $mailer->send();
+            }
+
+            // Check if the group has more than 1000 members, if so, disable the "send mail" function.
+            $full_count = XDB::fetchOneCell('SELECT COUNT(*)
+                                               FROM group_members
+                                              WHERE asso_id = {?}',
+                                $globals->asso('id'));
+            if ($full_count > 999) {
+                XDB::execute(
+                    "UPDATE groups
+                    SET disable_mails = 1
+                    WHERE id = {?}",
+                    $globals->asso('id')); 
+            }
+            
             if ($suggest_account_activation) {
                 pl_redirect('member/suggest/' . $user->login() . '/' . $email . '/' . $globals->asso('nom'));
             } else {
@@ -1198,6 +1222,11 @@ class XnetGrpModule extends PLModule
             $mailer->send();
         }
 
+        $nl = Newsletter::forGroup($globals->asso('shortname'));
+        if (!is_null($nl)) {
+            $nl->unsubscribe(null, S::i('uid'));
+        }
+
         $domain = $globals->asso('mail_domain');
         if (!$domain) {
             return true;
@@ -1755,5 +1784,5 @@ class XnetGrpModule extends PLModule
     }
 }
 
-// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
+// vim:set et sw=4 sts=4 sws=4 foldmethod=marker fenc=utf-8:
 ?>