Merge branch 'platal-1.0.0'
[platal.git] / modules / newsletter.php
CommitLineData
e2efba7d 1<?php
2/***************************************************************************
9f5bd98e 3 * Copyright (C) 2003-2010 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(
eb5a266d
SJ
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),
30 'admin/newsletter' => $this->make_hook('admin_nl', AUTH_MDP, 'admin'),
31 'admin/newsletter/categories' => $this->make_hook('admin_nl_cat', AUTH_MDP, 'admin'),
e2efba7d 32 'admin/newsletter/edit' => $this->make_hook('admin_nl_edit', AUTH_MDP, 'admin'),
33 );
34 }
35
e2efba7d 36 function handler_nl(&$page, $action = null)
37 {
38 require_once 'newsletter.inc.php';
39
40 $page->changeTpl('newsletter/index.tpl');
46f272fe 41 $page->setTitle('Lettres mensuelles');
e2efba7d 42
43 switch ($action) {
b6ba1a04
RB
44 case 'out': NewsLetter::unsubscribe(); break;
45 case 'in': NewsLetter::subscribe(); break;
e2efba7d 46 default: ;
47 }
48
b6ba1a04
RB
49 $page->assign('nls', NewsLetter::subscriptionState());
50 $page->assign('nl_list', NewsLetter::listSent());
e2efba7d 51 }
52
53 function handler_nl_show(&$page, $nid = 'last')
54 {
55 $page->changeTpl('newsletter/show.tpl');
56
57 require_once 'newsletter.inc.php';
58
9da70671
SJ
59 try {
60 $nl = new NewsLetter($nid);
6d1747b3 61 $user =& S::user();
9da70671 62 if (Get::has('text')) {
6d1747b3 63 $nl->toText($page, $user);
9da70671 64 } else {
6d1747b3 65 $nl->toHtml($page, $user);
9da70671
SJ
66 }
67 if (Post::has('send')) {
6d1747b3 68 $nl->sendTo($user);
9da70671
SJ
69 }
70 } catch (MailNotFound $e) {
71 return PL_NOT_FOUND;
e2efba7d 72 }
73 }
74
75 function handler_nl_submit(&$page)
76 {
77 $page->changeTpl('newsletter/submit.tpl');
78
79 require_once 'newsletter.inc.php';
8f201b69
FB
80 $wp = new PlWikiPage('Xorg.LettreMensuelle');
81 $wp->buildCache();
e2efba7d 82
1970c12b 83 if (Post::has('see') || (Post::has('valid') && (!trim(Post::v('title')) || !trim(Post::v('body'))))) {
84 if (!Post::has('see')) {
a7d35093 85 $page->trigError("L'article doit avoir un titre et un contenu");
1970c12b 86 }
e2efba7d 87 $art = new NLArticle(Post::v('title'), Post::v('body'), Post::v('append'));
88 $page->assign('art', $art);
89 } elseif (Post::has('valid')) {
90 require_once('validations.inc.php');
5daf68f6 91 $art = new NLReq(S::user(), Post::v('title'),
e2efba7d 92 Post::v('body'), Post::v('append'));
93 $art->submit();
94 $page->assign('submited', true);
95 }
1970c12b 96 $page->addCssLink('nl.css');
e2efba7d 97 }
98
99 function handler_admin_nl(&$page, $new = false) {
100 $page->changeTpl('newsletter/admin.tpl');
46f272fe 101 $page->setTitle('Administration - Newsletter : liste');
e2efba7d 102 require_once("newsletter.inc.php");
eaf30d86 103
e2efba7d 104 if($new) {
b6ba1a04 105 NewsLetter::create();
4882f4a5 106 pl_redirect("admin/newsletter");
e2efba7d 107 }
eaf30d86 108
b6ba1a04 109 $page->assign('nl_list', NewsLetter::listAll());
e2efba7d 110 }
eaf30d86 111
e2efba7d 112 function handler_admin_nl_edit(&$page, $nid = 'last', $aid = null, $action = 'edit') {
113 $page->changeTpl('newsletter/edit.tpl');
3ad44e08 114 $page->addCssLink('nl.css');
e4705641
SJ
115 $page->setTitle('Administration - Newsletter : Édition');
116 require_once 'newsletter.inc.php';
eaf30d86 117
9e2a6a32 118 $nl = new NewsLetter($nid);
eaf30d86 119
e2efba7d 120 if($action == 'delete') {
121 $nl->delArticle($aid);
122 pl_redirect("admin/newsletter/edit/$nid");
123 }
eaf30d86 124
e2efba7d 125 if($aid == 'update') {
9e2a6a32
SJ
126 $nl->_title = Post::v('title');
127 $nl->_title_mail = Post::v('title_mail');
128 $nl->_date = Post::v('date');
129 $nl->_head = Post::v('head');
130 $nl->_shortname = strlen(Post::v('shortname')) ? Post::v('shortname') : null;
e2efba7d 131 if (preg_match('/^[-a-z0-9]*$/i', $nl->_shortname) && !is_numeric($nl->_shortname)) {
132 $nl->save();
133 } else {
9e2a6a32 134 $page->trigError("Le nom de la NL n'est pas valide.");
e2efba7d 135 pl_redirect('admin/newsletter/edit/' . $nl->_id);
136 }
137 }
eaf30d86 138
e2efba7d 139 if(Post::v('save')) {
140 $art = new NLArticle(Post::v('title'), Post::v('body'), Post::v('append'),
9e2a6a32 141 $aid, Post::v('cid'), Post::v('pos'));
e2efba7d 142 $nl->saveArticle($art);
143 pl_redirect("admin/newsletter/edit/$nid");
144 }
eaf30d86 145
9e2a6a32 146 if ($action == 'edit' && $aid != 'update') {
e2efba7d 147 $eaid = $aid;
e4705641 148 if (Post::has('title')) {
e2efba7d 149 $art = new NLArticle(Post::v('title'), Post::v('body'), Post::v('append'),
e4705641 150 $eaid, Post::v('cid'), Post::v('pos'));
e2efba7d 151 } else {
e4705641
SJ
152 $art = ($eaid == 'new') ? new NLArticle() : $nl->getArt($eaid);
153 }
154 if ($art && !$art->check()) {
155 $page->trigError("Cet article est trop long.");
e2efba7d 156 }
157 $page->assign('art', $art);
158 }
eaf30d86 159
9e2a6a32 160 if ($aid == 'blacklist_check') {
955109ba 161 global $globals;
9e2a6a32 162 $ips_to_check = array();
955109ba 163 $blacklist_host_resolution_count = 0;
9e2a6a32
SJ
164
165 foreach ($nl->_arts as $key => $articles) {
166 foreach ($articles as $article) {
955109ba 167 $article_ips = $article->getLinkIps($blacklist_host_resolution_count);
9e2a6a32
SJ
168 if (!empty($article_ips)) {
169 $ips_to_check[$article->title()] = $article_ips;
170 }
171 }
172 }
173
174 $page->assign('ips_to_check', $ips_to_check);
955109ba
SJ
175 if ($blacklist_host_resolution_count >= $globals->mail->blacklist_host_resolution_limit) {
176 $page->trigError("Toutes les url et adresses emails de la lettre"
9e2a6a32
SJ
177 . " n'ont pas été prises en compte car la"
178 . " limite du nombre de résolutions DNS"
179 . " autorisée a été atteinte.");
180 }
181 }
182
183 $page->assign_by_ref('nl', $nl);
e2efba7d 184 }
185
186 function handler_admin_nl_cat(&$page, $action = 'list', $id = null) {
46f272fe 187 $page->setTitle('Administration - Newsletter : Catégories');
a7de4ef7 188 $page->assign('title', 'Gestion des catégories de la newsletter');
e2efba7d 189 $table_editor = new PLTableEditor('admin/newsletter/categories','newsletter_cat','cid');
a7de4ef7 190 $table_editor->describe('titre','intitulé',true);
e2efba7d 191 $table_editor->describe('pos','position',true);
192 $table_editor->apply($page, $action, $id);
bc0903c7 193 }
e2efba7d 194}
195
a7de4ef7 196// vim:set et sw=4 sts=4 sws=4 foldmethod=marker enc=utf-8:
e2efba7d 197?>