Unsubscribes user from groupNL when unsubscribing from group.
[platal.git] / modules / xnetgrp.php
index af9c68d..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   *
@@ -245,6 +245,7 @@ class XnetGrpModule extends PLModule
                     $page->assign('pub', Post::v('pub'));
                     $page->assign('notif_unsub', Post::i('notif_unsub'));
                     $page->assign('descr', Post::t('descr'));
+                    $page->assign('disable_mails', Post::b('disable_mails'));
                     $page->assign('error', $error);
                     return;
                 }
@@ -261,7 +262,7 @@ class XnetGrpModule extends PLModule
                              descr={?}, site={?}, mail={?}, resp={?},
                              forum={?}, mail_domain={?}, ax={?}, axDate = {?}, pub={?},
                              sub_url={?}, inscriptible={?}, unsub_url={?},
-                             flags = {?}, welcome_msg = {?}
+                             flags = {?}, welcome_msg = {?}, disable_mails = {?}
                       WHERE  id={?}",
                       Post::v('nom'), Post::v('diminutif'),
                       Post::v('cat'), (Post::i('dom') == 0) ? null : Post::i('dom'),
@@ -271,6 +272,7 @@ class XnetGrpModule extends PLModule
                       Post::has('ax'), $axDate, Post::v('pub'),
                       Post::v('sub_url'), Post::v('inscriptible'),
                       Post::v('unsub_url'), $flags, Post::t('welcome_msg'),
+                      Post::b('disable_mails'),
                       $globals->asso('id'));
                 if (Post::v('mail_domain')) {
                     XDB::execute('INSERT IGNORE INTO  email_virtual_domains (name)
@@ -347,11 +349,15 @@ class XnetGrpModule extends PLModule
         $page->assign('pub', $globals->asso('pub'));
         $page->assign('notif_unsub', $globals->asso('notif_unsub'));
         $page->assign('notify_all', $globals->asso('notify_all'));
+        $page->assign('disable_mails', $globals->asso('disable_mails'));
     }
 
     function handler_mail($page)
     {
         global $globals;
+        if ($globals->asso('disable_mails')) {
+            return PL_FORBIDDEN;
+        }
 
         $page->changeTpl('xnetgrp/mail.tpl');
         $mmlist = new MMList(S::user(), $globals->asso('mail_domain'));
@@ -454,12 +460,25 @@ class XnetGrpModule extends PLModule
             $filename = $globals->asso('diminutif') . '.csv';
         }
         $users = $globals->asso()->getMembersFilter(null, new UFO_Name())->getUsers();
+        $admin = may_update();
         pl_cached_content_headers('text/x-csv', 'iso-8859-1', 1);
 
-        echo utf8_decode("Nom;Prénom;Sexe;Promotion;Commentaire\n");
+        echo utf8_decode("Nom;Prénom;Sexe;Promotion;Commentaire");
+        if ($admin) {
+            echo utf8_decode(";Société;Poste\n");
+        } else {
+            echo utf8_decode("\n");
+        }
         foreach ($users as $user) {
             $line = $user->lastName() . ';' . $user->firstName() . ';' . ($user->isFemale() ? 'F' : 'M')
-                  . ';' . $user->promo() . ';' . strtr($user->group_comm, ';', ',');
+                . ';' . $user->promo() . ';' . strtr($user->group_comm, ';', ',');
+            if ($admin) {
+                if ($user->hasProfile()) {
+                    $line .= ';' . $user->profile()->getMainJob()->company->name . ';' . $user->profile()->getMainJob()->description;
+                } else {
+                    $line .= ';;';
+                }
+            }
             echo utf8_decode($line) . "\n";
         }
         exit();
@@ -479,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) {
@@ -495,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')) {
@@ -533,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);
             }
         }
 
@@ -710,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() . '>');
@@ -773,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);
@@ -812,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"
@@ -1060,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 {
@@ -1179,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;
@@ -1736,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:
 ?>