admin/emails/broken: name the downloaded file "broken.csv"
[platal.git] / modules / email.php
index 632524c..b78cfe7 100644 (file)
@@ -41,7 +41,7 @@ class EmailModule extends PLModule
             'admin/emails/duplicated' => $this->make_hook('duplicated',  AUTH_PASSWD, 'admin'),
             'admin/emails/watch'      => $this->make_hook('duplicated',  AUTH_PASSWD, 'admin'),
             'admin/emails/lost'       => $this->make_hook('lost',        AUTH_PASSWD, 'admin'),
-            'admin/emails/broken'     => $this->make_hook('broken_addr', AUTH_PASSWD, 'admin'),
+            'admin/emails/broken'     => $this->make_hook('broken_addr', AUTH_PASSWD, 'admin,edit_directory'),
         );
     }
 
@@ -867,6 +867,7 @@ class EmailModule extends PLModule
                 sort($broken_list);
 
                 foreach ($broken_list as $email) {
+                    $email = trim($email);
                     $userobj = null;
                     if ($user = mark_broken_email($email, true)) {
                         $userobj = User::getSilentWithUID($user['uid']);
@@ -907,14 +908,23 @@ class EmailModule extends PLModule
                                WHERE  flags = \'active\' AND broken_level = 1
                                       AND DATE_ADD(last, INTERVAL 1 YEAR) < CURDATE()');
 
+                // Sort $broken_user_list with (promo, sortname, pid)
+                $sortable_array = array();
+                foreach ($broken_user_list as $pid => $mails) {
+                    $profile = $broken_user_profiles[$pid];
+                    $sortable_array[$pid] = array($profile->promo(), $profile->sortName(), $pid);
+                }
+                asort($sortable_array);
+
                 // Output the list of users with recently broken addresses,
                 // along with the count of valid redirections.
-                pl_cached_content_headers('text/x-csv', 1);
+                pl_cached_content_headers('text/x-csv', null, 1, 'broken.csv');
 
                 $csv = fopen('php://output', 'w');
                 fputcsv($csv, array('nom', 'promo', 'bounces', 'nbmails', 'url', 'corps', 'job', 'networking'), ';');
                 $corpsList = DirEnum::getOptions(DirEnum::CURRENTCORPS);
-                foreach ($broken_user_list as $pid => $mails) {
+                foreach (array_keys($sortable_array) as $pid) {
+                    $mails = $broken_user_list[$pid];
                     $profile = $broken_user_profiles[$pid];
                     $current_corps = $profile->getCorpsName();
                     $jobs = $profile->getJobs();