Moving to GitHub.
[platal.git] / modules / newsletter.php
index 7ea958e..230265e 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /***************************************************************************
- *  Copyright (C) 2003-2011 Polytechnique.org                              *
+ *  Copyright (C) 2003-2014 Polytechnique.org                              *
  *  http://opensource.polytechnique.org/                                   *
  *                                                                         *
  *  This program is free software; you can redistribute it and/or modify   *
@@ -34,6 +34,7 @@ class NewsletterModule extends PLModule
             'admin/newsletter/categories'  => $this->make_hook('admin_nl_cat',    AUTH_PASSWD, 'admin'),
             'admin/newsletter/edit'        => $this->make_hook('admin_nl_edit',   AUTH_PASSWD, 'admin'),
             'admin/newsletter/edit/delete' => $this->make_hook('admin_nl_delete', AUTH_PASSWD, 'admin'),
+            'stat/newsletter'              => $this->make_hook('stat_nl',         AUTH_PASSWD, 'admin')
             // Automatic mailing is disabled for X.org NL
 //            'admin/newsletter/edit/cancel' => $this->make_hook('cancel', AUTH_PASSWD, 'admin'),
 //            'admin/newsletter/edit/valid'  => $this->make_hook('valid',  AUTH_PASSWD, 'admin'),
@@ -216,14 +217,35 @@ class NewsletterModule extends PLModule
         $page->assign('nl_list', $nl->listAllIssues());
     }
 
-    function handler_admin_nl_groups($page)
+    function handler_admin_nl_groups($page, $sort = 'id', $order = 'ASC')
     {
         require_once 'newsletter.inc.php';
 
+        static $titles = array(
+            'id'         => 'Id',
+            'group_name' => 'Groupe',
+            'name'       => 'Titre',
+            'criteria'   => 'Critères actifs'
+        );
+        static $next_orders = array(
+            'ASC'  => 'DESC',
+            'DESC' => 'ASC'
+        );
+
+        if (!array_key_exists($sort, $titles)) {
+            $sort = 'id';
+        }
+        if (!in_array($order, array('ASC', 'DESC'))) {
+            $order = 'ASC';
+        }
+
         $page->changeTpl('newsletter/admin_all.tpl');
         $page->setTitle('Administration - Newsletters : Liste des Newsletters');
-
-        $page->assign('nls', Newsletter::getAll());
+        $page->assign('nls', Newsletter::getAll($sort, $order));
+        $page->assign('sort', $sort);
+        $page->assign('order', $order);
+        $page->assign('next_order', $next_orders[$order]);
+        $page->assign('titles', $titles);
     }
 
     function handler_admin_nl_edit($page, $nid = 'last', $aid = null, $action = 'edit') {
@@ -247,6 +269,7 @@ class NewsletterModule extends PLModule
 
         // Convert NLIssue error messages to human-readable errors
         $error_msgs = array(
+            NLIssue::ERROR_INVALID_REPLY_TO => "L'adresse de réponse est invalide.",
             NLIssue::ERROR_INVALID_SHORTNAME => "Le nom court est invalide ou vide.",
             NLIssue::ERROR_INVALID_UFC => "Le filtre des destinataires est invalide.",
             NLIssue::ERROR_TOO_LONG_UFC => "Le nombre de matricules AX renseigné est trop élevé.",
@@ -261,6 +284,7 @@ class NewsletterModule extends PLModule
             $issue->title_mail = Post::s('title_mail');
             $issue->head       = Post::s('head');
             $issue->signature  = Post::s('signature');
+            $issue->reply_to   = Post::s('reply_to');
 
             if ($issue->isEditable()) {
                 // Date and shortname may only be modified for pending NLs, otherwise all links get broken.
@@ -396,6 +420,11 @@ class NewsletterModule extends PLModule
         if (!$issue) {
             $page->kill("La lettre {$nid} n'existe pas.");
         }
+
+        if ($issue->isEmpty()) {
+            $page->trigErrorRedirect("La lettre étant vide, il n'est pas possible de l'envoyer.", $nl->adminPrefix());
+        }
+
         if (!$issue->scheduleMailing()) {
             $page->trigErrorRedirect("Une erreur est survenue lors de la validation de l'envoi.", $nl->adminPrefix());
         }
@@ -467,7 +496,53 @@ class NewsletterModule extends PLModule
         $table_editor->on_delete(null, null);
         $table_editor->apply($page, $action, $id);
     }
+
+    function handler_stat_nl($page)
+    {
+        $nl = $this->getNl();
+        if (!$nl) {
+            return PL_NOT_FOUND;
+        }
+        if (!$nl->mayEdit()) {
+            return PL_FORBIDDEN;
+        }
+
+        $page->setTitle('Statistiques - Newsletter');
+        $page->changeTpl('newsletter/statistics.tpl');
+
+        $data = array();
+        foreach (Profile::$cycles as $grade => $name) {
+            $data[$name] = array();
+            list($min, $max) = Profile::extremePromotions($grade);
+            $bound = (((int)($min / 10)) + 1) * 10;
+            while ($bound <= $max) {
+                $data[$name][$min . ' - ' . $bound] = array(
+                    'count'        => $nl->subscriberCount(null, null, $grade, $min, $bound),
+                    'lost'         => $nl->subscriberCount(true, null, $grade, $min, $bound),
+                    'count_female' => $nl->subscriberCount(null, User::GENDER_FEMALE, $grade, $min, $bound),
+                    'lost_female'  => $nl->subscriberCount(true, User::GENDER_FEMALE, $grade, $min, $bound)
+                );
+                $min = $bound + 1;
+                $bound += 10;
+            }
+            $bound -= 9;
+            if ($bound <= $max) {
+                $data[$name][$bound . ' - ' . $max] = array(
+                    'count'        => $nl->subscriberCount(null, null, $grade, $bound, $max),
+                    'lost'         => $nl->subscriberCount(true, null, $grade, $bound, $max),
+                    'count_female' => $nl->subscriberCount(null, User::GENDER_FEMALE, $grade, $bound, $max),
+                    'lost_female'  => $nl->subscriberCount(true, User::GENDER_FEMALE, $grade, $bound, $max)
+                );
+            }
+        }
+        $page->assign_by_ref('nl', $nl);
+        $page->assign('count', $nl->subscriberCount());
+        $page->assign('lost', $nl->lostSubscriberCount());
+        $page->assign('count_female', $nl->subscriberCount(null, User::GENDER_FEMALE));
+        $page->assign('lost_female', $nl->lostSubscriberCount(User::GENDER_FEMALE));
+        $page->assign('data', $data);
+    }
 }
 
-// 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:
 ?>