AXLetter module.
[platal.git] / modules / axletter.php
CommitLineData
8da0d3c1 1<?php
2/***************************************************************************
3 * Copyright (C) 2003-2007 Polytechnique.org *
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 AXLetterModule extends PLModule
23{
24 function handlers()
25 {
26 return array(
27 'ax' => $this->make_hook('index', AUTH_COOKIE),
28 'ax/show' => $this->make_hook('show', AUTH_COOKIE),
29 'ax/edit' => $this->make_hook('submit', AUTH_MDP),
30 'ax/edit/cancel' => $this->make_hook('cancel', AUTH_MDP),
31 'ax/edit/valid' => $this->make_hook('valid', AUTH_MDP),
32 'admin/axletter/rights' => $this->make_hook('admin_rights', AUTH_MDP, 'admin'),
33 );
34 }
35
36 function handler_index(&$page, $action = null, $hash = null)
37 {
38 require_once dirname(__FILE__) . '/axletter/axletter.inc.php';
39
40 $page->changeTpl('axletter/index.tpl');
41 $page->assign('xorg_title','Polytechnique.org - Envois de l\'AX');
42
43 switch ($action) {
44 case 'out': AXLetter::unsubscribe($hash); break;
45 case 'in': AXLetter::subscribe(); break;
46 default: ;
47 }
48
49 $perm = AXLetter::hasPerms();
50 if ($perm) {
51 $waiting = AXLetter::awaiting();
52 if ($waiting) {
53 $new = new AXLetter($waiting);
54 $page->assign('new', $new);
55 }
56 }
57 $page->assign('axs', AXLetter::subscriptionState());
58 $page->assign('ax_list', AXLetter::listSent());
59 $page->assign('ax_rights', $perm);
60 }
61
62 function handler_submit(&$page, $action = null)
63 {
64 require_once dirname(__FILE__) . '/axletter/axletter.inc.php';
65 if (!AXLetter::hasPerms()) {
66 return PL_FORBIDDEN;
67 }
68
69 $page->changeTpl('axletter/edit.tpl');
70
71 $saved = Post::i('saved');
72 $new = false;
73 $id = Post::i('id');
74 $shortname = trim(Post::v('shortname'));
75 $subject = trim(Post::v('subject'));
76 $title = trim(Post::v('title'));
77 $body = rtrim(Post::v('body'));
78 $signature = trim(Post::v('signature'));
79 $promo_min = Post::i('promo_min');
80 $promo_max = Post::i('promo_max');
81 $echeance = Post::has('echeance_date') ? Post::v('echeance_date') . ' ' . Post::v('echeance_time')
82 : Post::v('echeance');
83 $echeance_date = Post::v('echeance_date');
84 $echeance_time = Post::v('echeance_time');
85
86 if (!$id) {
87 $res = XDB::query("SELECT * FROM axletter WHERE FIND_IN_SET('new', bits)");
88 if ($res->numRows()) {
89 extract($res->fetchOneAssoc(), EXTR_OVERWRITE);
90 $saved = true;
91 } else {
92 XDB::execute("INSERT INTO axletter SET id = NULL");
93 $id = XDB::insertId();
94 }
95 if (!$echeance || $echeance == '0000-00-00 00:00:00') {
96 $saved = false;
97 $new = true;
98 }
99 } elseif (Post::has('valid')) {
100 if (!$subject && $title) {
101 $subject = $title;
102 }
103 if (!$title && $subject) {
104 $title = $subject;
105 }
106 if (!$subject || !$title || !$body) {
107 $page->trig("L'article doit avoir un sujet et un contenu");
108 Post::kill('valid');
109 }
110 if (($promo_min > $promo_max && $promo_max != 0)||
111 ($promo_min != 0 && ($promo_min <= 1900 || $promo_min >= 2020)) ||
112 ($promo_max != 0 && ($promo_max <= 1900 || $promo_max >= 2020)))
113 {
114 $page->trig("L'intervalle de promotions n'est pas valide");
115 Post::kill('valid');
116 }
117 if (empty($shortname)) {
118 $page->trig("L'annonce doit avoir un nom raccourci pour simplifier la navigation dans les archives");
119 Post::kill('valid');
120 } elseif (!preg_match('/^[a-z][-a-z0-9]*[a-z0-9]$/', $shortname)) {
121 $page->trig("Le nom raccourci n'est pas valide, il doit comporter au moins 2 caractères et n'être composé "
122 . "que de chiffres, lettres et tirets");
123 Post::kill('valid');
124 } elseif ($shortname != Post::v('old_shortname')) {
125 $res = XDB::query("SELECT id FROM axletter WHERE shortname = {?}", $shortname);
126 if ($res->numRows() && $res->fetchOneCell() != $id) {
127 $page->trig("Le nom $shortname est déjà utilisé, merci d'en choisir un autre");
128 $shortname = Post::v('old_shortname');
129 if (empty($shortname)) {
130 Post::kill('valid');
131 }
132 }
133 }
134
135 switch (@Post::v('valid')) {
136 case 'Aperçu':
137 require_once dirname(__FILE__) . '/axletter/axletter.inc.php';
138 $al = new AXLetter(array($id, $shortname, $subject, $title, $body, $signature,
139 $promo_min, $promo_max, $echeance, 0, 'new'));
140 $al->toHtml($page, S::v('prenom'), S::v('nom'), S::v('femme'));
141 break;
142
143 case 'Confirmer':
144 XDB::execute("REPLACE INTO axletter
145 SET id = {?}, shortname = {?}, subject = {?}, title = {?}, body = {?},
146 signature = {?}, promo_min = {?}, promo_max = {?}, echeance = {?}",
147 $id, $shortname, $subject, $title, $body, $signature, $promo_min, $promo_max, $echeance);
148 $saved = true;
149 $echeance_date = null;
150 $echeance_time = null;
151 pl_redirect('ax');
152 break;
153 }
154 }
155 $page->assign('id', $id);
156 $page->assign('shortname', $shortname);
157 $page->assign('subject', $subject);
158 $page->assign('title', $title);
159 $page->assign('body', $body);
160 $page->assign('signature', $signature);
161 $page->assign('promo_min', $promo_min);
162 $page->assign('promo_max', $promo_max);
163 $page->assign('echeance', $echeance);
164 $page->assign('echeance_date', $echeance_date);
165 $page->assign('echeance_time', $echeance_time);
166 $page->assign('saved', $saved);
167 $page->assign('new', $new);
168 $page->assign('is_xorg', S::has_perms());
169
170 if (!$saved) {
171 $select = '';
172 $time = time() + 3600 * 24 * 2;
173 for ($i = 0 ; $i < 15 ; $i++) {
174 $time += 3600 * 24;
175 $p_stamp = date('Ymd', $time);
176 $year = date('Y', $time);
177 $month = date('m', $time);
178 $day = date('d', $time);
179
180 if ($p_stamp == $echeance_date) {
181 $sel = ' selected="selected"';
182 } else {
183 $sel = '';
184 }
185 $select .= "<option value=\"$p_stamp\"$sel> $day / $month / $year</option>\n";
186 }
187 $page->assign('echeance_date', $select);
188 $select = '';
189 for ($i = 0 ; $i < 24 ; $i++) {
190 $stamp = sprintf('%02d:00:00', $i);
191 if ($stamp == $echeance_time) {
192 $sel = ' selected="selected"';
193 } else {
194 $sel = '';
195 }
196 $select .= "<option value=\"$stamp\"$sel>{$i}h</option>\n";
197 }
198 $page->assign('echeance_time', $select);
199 }
200 }
201
202 function handler_cancel(&$page, $force = null)
203 {
204 require_once dirname(__FILE__) . '/axletter/axletter.inc.php';
205 if (!AXLetter::hasPerms()) {
206 return PL_FORBIDDEN;
207 }
208
209 $url = parse_url($_SERVER['HTTP_REFERER']);
210 if ($force != 'force' && trim($url['path'], '/') != 'ax/edit') {
211 return PL_FORBIDDEN;
212 }
213
214 $waiting = AXLetter::awaiting();
215 if (!$waiting) {
216 $page->kill("Aucune lettre en attente");
217 return;
218 }
219 $al = new AXLetter($waiting);
220 if (!$al->invalid()) {
221 $page->kill("Une erreur est survenue lors de l'annulation de l'envoi");
222 return;
223 }
224
225 $page->kill("L'envoi de l'annonce {$al->title()} est annulé");
226 }
227
228 function handler_valid(&$page, $force = null)
229 {
230 require_once dirname(__FILE__) . '/axletter/axletter.inc.php';
231 if (!AXLetter::hasPerms()) {
232 return PL_FORBIDDEN;
233 }
234
235 $url = parse_url($_SERVER['HTTP_REFERER']);
236 if ($force != 'force' && trim($url['path'], '/') != 'ax/edit') {
237 return PL_FORBIDDEN;
238 }
239
240 $waiting = AXLetter::awaiting();
241 if (!$waiting) {
242 $page->kill("Aucune lettre en attente");
243 return;
244 }
245 $al = new AXLetter($waiting);
246 if (!$al->valid()) {
247 $page->kill("Une erreur est survenue lors de la validation de l'envoi");
248 return;
249 }
250
251 $page->kill("L'envoi de l'annonce aura lieu dans l'heure qui vient.");
252 }
253
254 function handler_show(&$page, $nid = 'last')
255 {
256 require_once dirname(__FILE__) . '/axletter/axletter.inc.php';
257 $page->changeTpl('axletter/show.tpl');
258
259 $nl = new AXLetter($nid);
260 if (Get::has('text')) {
261 $nl->toText($page, S::v('prenom'), S::v('nom'), S::v('femme'));
262 } else {
263 $nl->toHtml($page, S::v('prenom'), S::v('nom'), S::v('femme'));
264 }
265 if (Post::has('send')) {
266 $nl->sendTo(S::v('prenom'), S::v('nom'),
267 S::v('bestalias'), S::v('femme'),
268 S::v('mail_fmt') != 'texte');
269 }
270 }
271}
272
273?>