Displays remaining lines and characters in nl article submission (Closes #908).
[platal.git] / modules / newsletter.php
CommitLineData
e2efba7d 1<?php
2/***************************************************************************
12262f13 3 * Copyright (C) 2003-2011 Polytechnique.org *
e2efba7d 4 * http://opensource.polytechnique.org/ *
5 * *
6 * This program is free software; you can redistribute it and/or modify *
7 * it under the terms of the GNU General Public License as published by *
8 * the Free Software Foundation; either version 2 of the License, or *
9 * (at your option) any later version. *
10 * *
11 * This program is distributed in the hope that it will be useful, *
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
14 * GNU General Public License for more details. *
15 * *
16 * You should have received a copy of the GNU General Public License *
17 * along with this program; if not, write to the Free Software *
18 * Foundation, Inc., *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
21
22class NewsletterModule extends PLModule
23{
24 function handlers()
25 {
26 return array(
84163d58
RB
27 'nl' => $this->make_hook('nl', AUTH_COOKIE),
28 'nl/show' => $this->make_hook('nl_show', AUTH_COOKIE),
29 'nl/submit' => $this->make_hook('nl_submit', AUTH_MDP),
0fd5be99 30 'nl/remaining' => $this->make_hook('nl_remaining', AUTH_MDP),
84163d58
RB
31 'admin/nls' => $this->make_hook('admin_nl_groups', AUTH_MDP, 'admin'),
32 'admin/newsletter' => $this->make_hook('admin_nl', AUTH_MDP, 'admin'),
33 'admin/newsletter/categories' => $this->make_hook('admin_nl_cat', AUTH_MDP, 'admin'),
34 'admin/newsletter/edit' => $this->make_hook('admin_nl_edit', AUTH_MDP, 'admin'),
35 'admin/newsletter/edit/delete' => $this->make_hook('delete', AUTH_MDP, 'admin'),
36 // Automatic mailing is disabled for X.org NL
37// 'admin/newsletter/edit/cancel' => $this->make_hook('cancel', AUTH_MDP, 'admin'),
38// 'admin/newsletter/edit/valid' => $this->make_hook('valid', AUTH_MDP, 'admin'),
e2efba7d 39 );
40 }
41
84163d58
RB
42 /** This function should return the adequate NewsLetter object for the current module.
43 */
44 protected function getNl()
e2efba7d 45 {
46 require_once 'newsletter.inc.php';
84163d58
RB
47 return NewsLetter::forGroup(NewsLetter::GROUP_XORG);
48 }
49
26ba053e 50 function handler_nl($page, $action = null, $hash = null)
84163d58
RB
51 {
52 $nl = $this->getNl();
53 if (!$nl) {
54 return PL_NOT_FOUND;
55 }
e2efba7d 56
57 $page->changeTpl('newsletter/index.tpl');
46f272fe 58 $page->setTitle('Lettres mensuelles');
e2efba7d 59
60 switch ($action) {
84163d58
RB
61 case 'out': $nl->unsubscribe($hash, $hash != null); break;
62 case 'in': $nl->subscribe(); break;
e2efba7d 63 default: ;
64 }
65
84163d58
RB
66 $page->assign_by_ref('nl', $nl);
67 $page->assign('nls', $nl->subscriptionState());
68 $page->assign('nl_list', $nl->listSentIssues(true));
e2efba7d 69 }
70
26ba053e 71 function handler_nl_show($page, $nid = 'last')
e2efba7d 72 {
73 $page->changeTpl('newsletter/show.tpl');
84163d58
RB
74 $nl = $this->getNl();
75 if (!$nl) {
76 return PL_NOT_FOUND;
77 }
e2efba7d 78
9da70671 79 try {
84163d58 80 $issue = $nl->getIssue($nid);
6d1747b3 81 $user =& S::user();
9da70671 82 if (Get::has('text')) {
84163d58 83 $issue->toText($page, $user);
9da70671 84 } else {
84163d58 85 $issue->toHtml($page, $user);
9da70671
SJ
86 }
87 if (Post::has('send')) {
84163d58 88 $issue->sendTo($user);
9da70671
SJ
89 }
90 } catch (MailNotFound $e) {
91 return PL_NOT_FOUND;
e2efba7d 92 }
93 }
94
26ba053e 95 function handler_nl_submit($page)
e2efba7d 96 {
97 $page->changeTpl('newsletter/submit.tpl');
98
84163d58
RB
99 $nl = $this->getNl();
100 if (!$nl) {
101 return PL_NOT_FOUND;
102 }
103
8f201b69
FB
104 $wp = new PlWikiPage('Xorg.LettreMensuelle');
105 $wp->buildCache();
e2efba7d 106
1970c12b 107 if (Post::has('see') || (Post::has('valid') && (!trim(Post::v('title')) || !trim(Post::v('body'))))) {
108 if (!Post::has('see')) {
a7d35093 109 $page->trigError("L'article doit avoir un titre et un contenu");
1970c12b 110 }
e2efba7d 111 $art = new NLArticle(Post::v('title'), Post::v('body'), Post::v('append'));
112 $page->assign('art', $art);
113 } elseif (Post::has('valid')) {
5daf68f6 114 $art = new NLReq(S::user(), Post::v('title'),
e2efba7d 115 Post::v('body'), Post::v('append'));
116 $art->submit();
117 $page->assign('submited', true);
118 }
84163d58 119 $page->addCssLink($nl->cssFile());
e2efba7d 120 }
121
0fd5be99
SJ
122 function handler_nl_remaining($page)
123 {
124 require_once 'newsletter.inc.php';
125
126 pl_content_headers('text/html');
127 $page->changeTpl('newsletter/remaining.tpl', NO_SKIN);
128
129 $article = new NLArticle('', Post::t('body'), '');
130 $rest = $article->remain();
131
132 $page->assign('too_long', $rest['remaining_lines'] < 0);
133 $page->assign('last_line', ($rest['remaining_lines'] == 0));
134 $page->assign('remaining', ($rest['remaining_lines'] == 0) ? $rest['remaining_characters_for_last_line'] : $rest['remaining_lines']);
135 }
136
26ba053e 137 function handler_admin_nl($page, $new = false) {
e2efba7d 138 $page->changeTpl('newsletter/admin.tpl');
46f272fe 139 $page->setTitle('Administration - Newsletter : liste');
84163d58
RB
140
141 $nl = $this->getNl();
142 if (!$nl) {
143 return PL_NOT_FOUND;
144 }
eaf30d86 145
2d1ed661 146 if ($new == 'new') {
84163d58
RB
147 $id = $nl->createPending();
148 pl_redirect($nl->adminPrefix() . '/edit/' . $id);
e2efba7d 149 }
eaf30d86 150
84163d58
RB
151 $page->assign_by_ref('nl', $nl);
152 $page->assign('nl_list', $nl->listAllIssues());
153 }
154
26ba053e 155 function handler_admin_nl_groups($page)
84163d58
RB
156 {
157 require_once 'newsletter.inc.php';
158
159 $page->changeTpl('newsletter/admin_all.tpl');
160 $page->setTitle('Administration - Newsletters : Liste des Newsletters');
161
162 $page->assign('nls', Newsletter::getAll());
e2efba7d 163 }
eaf30d86 164
26ba053e 165 function handler_admin_nl_edit($page, $nid = 'last', $aid = null, $action = 'edit') {
e2efba7d 166 $page->changeTpl('newsletter/edit.tpl');
84163d58 167 $page->addCssLink('nl.Polytechnique.org.css');
e4705641 168 $page->setTitle('Administration - Newsletter : Édition');
eaf30d86 169
84163d58
RB
170 $nl = $this->getNl();
171 if (!$nl) {
172 return PL_NOT_FOUND;
173 }
eaf30d86 174
84163d58
RB
175 try {
176 $issue = $nl->getIssue($nid, false);
177 } catch (MailNotFound $e) {
178 return PL_NOT_FOUND;
e2efba7d 179 }
eaf30d86 180
84163d58
RB
181 $ufb = $nl->getSubscribersUFB();
182 $ufb_keepenv = false; // Will be set to True if there were invalid modification to the UFB.
183
184 // Convert NLIssue error messages to human-readable errors
185 $error_msgs = array(
186 NLIssue::ERROR_INVALID_SHORTNAME => "Le nom court est invalide ou vide.",
187 NLIssue::ERROR_INVALID_UFC => "Le filtre des destinataires est invalide.",
188 NLIssue::ERROR_SQL_SAVE => "Une erreur est survenue en tentant de sauvegarder la lettre, merci de réessayer.",
189 );
190
191 // Update the current issue
2d1ed661 192 if($aid == 'update' && Post::has('submit')) {
84163d58
RB
193
194 // Save common fields
195 $issue->title = Post::s('title');
196 $issue->title_mail = Post::s('title_mail');
197 $issue->head = Post::s('head');
198 $issue->signature = Post::s('signature');
199
200 if ($issue->isEditable()) {
201 // Date and shortname may only be modified for pending NLs, otherwise all links get broken.
202 $issue->date = Post::s('date');
203 $issue->shortname = strlen(Post::blank('shortname')) ? null : Post::s('shortname');
204 $issue->sufb->updateFromEnv($ufb->getEnv());
205
206 if ($nl->automaticMailingEnabled()) {
207 $issue->send_before = preg_replace('/^(\d\d\d\d)(\d\d)(\d\d)$/', '\1-\2-\3', Post::v('send_before_date')) . ' ' . Post::i('send_before_time_Hour') . ':00:00';
208 }
209 }
210 $errors = $issue->save();
211 if (count($errors)) {
212 foreach ($errors as $error_code) {
213 $page->trigError($error_msgs[$error_code]);
214 }
e2efba7d 215 }
216 }
eaf30d86 217
84163d58
RB
218 // Delete an article
219 if($action == 'delete') {
220 $issue->delArticle($aid);
221 pl_redirect($nl->adminPrefix() . "/edit/$nid");
222 }
223
224 // Save an article
e2efba7d 225 if(Post::v('save')) {
226 $art = new NLArticle(Post::v('title'), Post::v('body'), Post::v('append'),
9e2a6a32 227 $aid, Post::v('cid'), Post::v('pos'));
84163d58
RB
228 $issue->saveArticle($art);
229 pl_redirect($nl->adminPrefix() . "/edit/$nid");
e2efba7d 230 }
eaf30d86 231
84163d58 232 // Edit an article
9e2a6a32 233 if ($action == 'edit' && $aid != 'update') {
e2efba7d 234 $eaid = $aid;
e4705641 235 if (Post::has('title')) {
e2efba7d 236 $art = new NLArticle(Post::v('title'), Post::v('body'), Post::v('append'),
e4705641 237 $eaid, Post::v('cid'), Post::v('pos'));
e2efba7d 238 } else {
84163d58 239 $art = ($eaid == 'new') ? new NLArticle() : $issue->getArt($eaid);
e4705641
SJ
240 }
241 if ($art && !$art->check()) {
242 $page->trigError("Cet article est trop long.");
e2efba7d 243 }
244 $page->assign('art', $art);
245 }
eaf30d86 246
84163d58 247 // Check blacklisted IPs
9e2a6a32 248 if ($aid == 'blacklist_check') {
955109ba 249 global $globals;
9e2a6a32 250 $ips_to_check = array();
955109ba 251 $blacklist_host_resolution_count = 0;
9e2a6a32 252
84163d58 253 foreach ($issue->arts as $key => $articles) {
9e2a6a32 254 foreach ($articles as $article) {
955109ba 255 $article_ips = $article->getLinkIps($blacklist_host_resolution_count);
9e2a6a32
SJ
256 if (!empty($article_ips)) {
257 $ips_to_check[$article->title()] = $article_ips;
258 }
259 }
260 }
261
262 $page->assign('ips_to_check', $ips_to_check);
955109ba
SJ
263 if ($blacklist_host_resolution_count >= $globals->mail->blacklist_host_resolution_limit) {
264 $page->trigError("Toutes les url et adresses emails de la lettre"
9e2a6a32
SJ
265 . " n'ont pas été prises en compte car la"
266 . " limite du nombre de résolutions DNS"
267 . " autorisée a été atteinte.");
268 }
269 }
270
84163d58
RB
271 if ($issue->state == NLIssue::STATE_SENT) {
272 $page->trigWarning("Cette lettre a déjà été envoyée ; il est recommandé de limiter les modifications au maximum (orthographe, adresses web et mail).");
273 }
274
275 $ufb->setEnv($issue->sufb->getEnv());
9e2a6a32 276 $page->assign_by_ref('nl', $nl);
84163d58
RB
277 $page->assign_by_ref('issue', $issue);
278 }
279
280 /** This handler will cancel the sending of the currently pending issue
281 * It is disabled for X.org mailings.
282 */
26ba053e 283 function handler_admin_nl_cancel($page, $nid, $force = null)
84163d58
RB
284 {
285 $nl = $this->getNl();
286 if (!$nl) {
287 return PL_NOT_FOUND;
288 }
289
290 if (!$nl->mayEdit() || !S::has_xsrf_token()) {
291 return PL_FORBIDDEN;
292 }
293
294 if (!$nid) {
295 $page->kill("La lettre n'a pas été spécifiée");
296 }
297
298 $issue = $nl->getIssue($nid);
299 if (!$issue) {
300 $page->kill("La lettre {$nid} n'existe pas.");
301 }
302 if (!$issue->cancelMailing()) {
303 $page->trigErrorRedirect("Une erreur est survenue lors de l'annulation de l'envoi.", $nl->adminPrefix());
304 }
305
306 $page->trigSuccessRedirect("L'envoi de l'annonce {$issue->title()} est annulé.", $nl->adminPrefix());
307 }
308
309 /** This handler will enable the sending of the currently pending issue
310 * It is disabled for X.org mailings.
311 */
26ba053e 312 function handler_admin_nl_valid($page, $nid, $force = null)
84163d58
RB
313 {
314 $nl = $this->getNl();
315 if (!$nl) {
316 return PL_NOT_FOUND;
317 }
318
319 if (!$nl->mayEdit() || !S::has_xsrf_token()) {
320 return PL_FORBIDDEN;
321 }
322
323 if (!$nid) {
324 $page->kill("La lettre n'a pas été spécifiée.");
325 }
326
327 $issue = $nl->getIssue($nid);
328 if (!$issue) {
329 $page->kill("La lettre {$nid} n'existe pas.");
330 }
331 if (!$issue->scheduleMailing()) {
332 $page->trigErrorRedirect("Une erreur est survenue lors de la validation de l'envoi.", $nl->adminPrefix());
333 }
334
335 $page->trigSuccessRedirect("L'envoi de la newsletter {$issue->title()} a été validé.", $nl->adminPrefix());
336 }
337
338 /** This handler will remove the given issue.
339 */
26ba053e 340 function handler_admin_nl_delete($page, $nid, $force = null)
84163d58
RB
341 {
342 $nl = $this->getNl();
343 if (!$nl) {
344 return PL_NOT_FOUND;
345 }
346
347 if (!$nl->mayEdit() || !S::has_xsrf_token()) {
348 return PL_FORBIDDEN;
349 }
350
351 if (!$nid) {
352 $page->kill("La lettre n'a pas été spécifiée.");
353 }
354
355 $issue = $nl->getIssue($nid);
356 if (!$issue) {
357 $page->kill("La lettre {$nid} n'existe pas");
358 }
359 if (!$issue->isEditable()) {
360 $page->trigErrorRedirect("La lette a été envoyée ou est en cours d'envoi, elle ne peut être supprimée.", $nl->adminPrefix());
361 }
362 if (!$issue->delete()) {
363 $page->trigErrorRedirect("Une erreur est survenue lors de la suppression de la lettre.", $nl->adminPrefix());
364 }
365
366 $page->trigSuccessRedirect("La lettre a bien été supprimée.", $nl->adminPrefix());
e2efba7d 367 }
368
26ba053e 369 function handler_admin_nl_cat($page, $action = 'list', $id = null) {
46f272fe 370 $page->setTitle('Administration - Newsletter : Catégories');
a7de4ef7 371 $page->assign('title', 'Gestion des catégories de la newsletter');
e2efba7d 372 $table_editor = new PLTableEditor('admin/newsletter/categories','newsletter_cat','cid');
84163d58 373 $table_editor->describe('title','intitulé',true);
e2efba7d 374 $table_editor->describe('pos','position',true);
375 $table_editor->apply($page, $action, $id);
bc0903c7 376 }
e2efba7d 377}
378
a7de4ef7 379// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
e2efba7d 380?>