Use a template to format the newsletter and use the power of PlMailer
[platal.git] / include / newsletter.inc.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2006 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
22 // {{{ requires + defines
23
24 require_once("xorg.misc.inc.php");
25
26 if (isset($page)) {
27 $page->addCssLink('nl.css');
28 }
29
30 define('FEMME', 1);
31 define('HOMME', 0);
32
33 // }}}
34 // {{{ class NewsLetter
35
36 class NewsLetter
37 {
38 // {{{ properties
39
40 var $_id;
41 var $_shortname;
42 var $_date;
43 var $_title;
44 var $_title_mail;
45 var $_head;
46 var $_cats = Array();
47 var $_arts = Array();
48
49 // }}}
50 // {{{ constructor
51
52 function NewsLetter($id=null)
53 {
54 if (isset($id)) {
55 if ($id == 'last') {
56 $res = XDB::query("SELECT MAX(id) FROM newsletter WHERE bits!='new'");
57 $id = $res->fetchOneCell();
58 }
59 $res = XDB::query("SELECT * FROM newsletter WHERE id={?} OR short_name={?} LIMIT 1", $id, $id);
60 } else {
61 $res = XDB::query("SELECT * FROM newsletter WHERE bits='new'");
62 if (!$res->numRows()) {
63 insert_new_nl();
64 }
65 $res = XDB::query("SELECT * FROM newsletter WHERE bits='new'");
66 }
67 $nl = $res->fetchOneAssoc();
68
69 $this->_id = $nl['id'];
70 $this->_shortname = $nl['short_name'];
71 $this->_date = $nl['date'];
72 $this->_title = $nl['titre'];
73 $this->_title_mail = $nl['titre_mail'];
74 $this->_head = $nl['head'];
75
76 $res = XDB::iterRow("SELECT cid,titre FROM newsletter_cat ORDER BY pos");
77 while (list($cid, $title) = $res->next()) {
78 $this->_cats[$cid] = $title;
79 }
80
81 $res = XDB::iterRow(
82 "SELECT a.title,a.body,a.append,a.aid,a.cid,a.pos
83 FROM newsletter_art AS a
84 INNER JOIN newsletter AS n USING(id)
85 LEFT JOIN newsletter_cat AS c ON(a.cid=c.cid)
86 WHERE a.id={?}
87 ORDER BY c.pos,a.pos", $this->_id);
88 while (list($title, $body, $append, $aid, $cid, $pos) = $res->next()) {
89 $this->_arts[$cid]["a$aid"] = new NLArticle($title, $body, $append, $aid, $cid, $pos);
90 }
91 }
92
93 // }}}
94 // {{{ function setSent()
95
96 function setSent()
97 {
98 XDB::execute("UPDATE newsletter SET bits='sent' WHERE id={?}", $this->_id);
99 }
100
101 // }}}
102 // {{{ function save()
103
104 function save()
105 {
106 XDB::execute('UPDATE newsletter SET date={?},titre={?},titre_mail={?},head={?},short_name={?} WHERE id={?}',
107 $this->_date, $this->_title, $this->_title_mail, $this->_head, $this->_shortname,$this->_id);
108 }
109
110 // }}}
111 // {{{ function id()
112
113 function id()
114 {
115 return is_null($this->_shortname) ? $this->_id : $this->_shortname;
116 }
117
118 // }}}
119 // {{{ function title()
120
121 function title($mail = false) {
122 if ($mail) {
123 return $this->_title_mail;
124 }
125 return $this->_title;
126 }
127
128 // }}}
129 // {{{ function head()
130
131 function head($prenom = null, $nom = null, $sexe = null, $type = 'text')
132 {
133 if (is_null($prenom)) {
134 return $this->_head;
135 } else {
136 $head = $this->_head;
137 $head = str_replace('<cher>', $sexe ? 'Chère' : 'Cher', $head);
138 $head = str_replace('<prenom>', $prenom, $head);
139 $head = str_replace('<nom>', $nom, $head);
140 if ($type == 'text') {
141 $head = enriched_to_text($head,false,true,2,64);
142 } else {
143 $head = enriched_to_text($head, true);
144 }
145 return $head;
146 }
147 }
148
149 // }}}
150 // {{{ funciton getCss()
151
152 function getCss()
153 {
154 return file_get_contents(dirname(__FILE__) . '/../htdocs/css/nl.css');
155 }
156
157 // }}}
158 // {{{ function getArt()
159
160 function getArt($aid)
161 {
162 foreach ($this->_arts as $key=>$artlist) {
163 if (isset($artlist["a$aid"])) {
164 return $artlist["a$aid"];
165 }
166 }
167 return null;
168 }
169
170 // }}}
171 // {{{ function saveArticle()
172
173 function saveArticle(&$a)
174 {
175 if ($a->_aid>=0) {
176 XDB::execute('REPLACE INTO newsletter_art (id,aid,cid,pos,title,body,append)
177 VALUES ({?},{?},{?},{?},{?},{?},{?})',
178 $this->_id, $a->_aid, $a->_cid, $a->_pos,
179 $a->_title, $a->_body, $a->_append);
180 $this->_arts['a'.$a->_aid] = $a;
181 } else {
182 XDB::execute('INSERT INTO newsletter_art
183 SELECT {?},MAX(aid)+1,{?},'.($a->_pos ? intval($a->_pos) : 'MAX(pos)+1').',{?},{?},{?}
184 FROM newsletter_art AS a
185 WHERE a.id={?}',
186 $this->_id, $a->_cid, $a->_title, $a->_body, $a->_append, $this->_id);
187 $this->_arts['a'.$a->_aid] = $a;
188 }
189 }
190
191 // }}}
192 // {{{ function delArticle()
193
194 function delArticle($aid)
195 {
196 XDB::execute('DELETE FROM newsletter_art WHERE id={?} AND aid={?}', $this->_id, $aid);
197 foreach ($this->_arts as $key=>$art) {
198 unset($this->_arts[$key]["a$aid"]);
199 }
200 }
201
202 // }}}
203 // {{{ function toText()
204
205 function toText(&$page, $prenom,$nom,$sexe)
206 {
207 $page->assign('is_mail', false);
208 $page->assign('html_version', false);
209 $page->assign('prenom', $prenom);
210 $page->assign('nom', $nom);
211 $page->assign('sexe', $sexe);
212 $page->assign_by_ref('nl', $this);
213 }
214
215 // }}}
216 // {{{ function toHtml()
217
218 function toHtml(&$page, $prenom, $nom, $sexe)
219 {
220 $page->assign('prefix', 'nl/show/' . $this->id());
221 $page->assign('is_mail', false);
222 $page->assign('html_version', true);
223 $page->assign('prenom', $prenom);
224 $page->assign('nom', $nom);
225 $page->assign('sexe', $sexe);
226 $page->assign_by_ref('nl', $this);
227 }
228
229 // }}}
230 // {{{ function sendTo()
231
232 function sendTo($prenom, $nom, $login, $sex, $html)
233 {
234 global $globals;
235
236 $mailer = new PlMailer('newsletter/nl.tpl');
237 $mailer->assign('is_mail', true);
238 $mailer->assign('prenom', $prenom);
239 $mailer->assign('nom', $nom);
240 $mailer->assign('sexe', $sex);
241 $mailer->assign_by_ref('nl', $this);
242 $mailer->assign('prefix', null);
243 $mailer->addTo("\"$prenom $nom\" <$login@{$globals->mail->domain}>");
244 $mailer->send($html);
245 }
246
247 // }}}
248 }
249
250 // }}}
251 // {{{ class NLArticle
252
253 class NLArticle
254 {
255 // {{{ properties
256
257 var $_aid;
258 var $_cid;
259 var $_pos;
260 var $_title;
261 var $_body;
262 var $_append;
263
264 // }}}
265 // {{{ constructor
266
267 function NLArticle($title='', $body='', $append='', $aid=-1, $cid=0, $pos=0)
268 {
269 $this->_body = $body;
270 $this->_title = $title;
271 $this->_append = $append;
272 $this->_aid = $aid;
273 $this->_cid = $cid;
274 $this->_pos = $pos;
275 }
276
277 // }}}
278 // {{{ function title()
279
280 function title()
281 { return trim($this->_title); }
282
283 // }}}
284 // {{{ function body()
285
286 function body()
287 { return trim($this->_body); }
288
289 // }}}
290 // {{{ function append()
291
292 function append()
293 { return trim($this->_append); }
294
295 // }}}
296 // {{{ function toText()
297
298 function toText()
299 {
300 $title = '*'.$this->title().'*';
301 $body = enriched_to_text($this->_body,false,true);
302 $app = enriched_to_text($this->_append,false,false,4);
303 return trim("$title\n\n$body\n\n$app")."\n";
304 }
305
306 // }}}
307 // {{{ function toHtml()
308
309 function toHtml()
310 {
311 $title = "<h2 class='xorg_nl'><a id='art{$this->_aid}'></a>".htmlentities($this->title()).'</h2>';
312 $body = enriched_to_text($this->_body,true);
313 $app = enriched_to_text($this->_append,true);
314
315 $art = "$title\n";
316 $art .= "<div class='art'>\n$body\n";
317 if ($app) {
318 $art .= "<div class='app'>$app</div>";
319 }
320 $art .= "</div>\n";
321
322 return $art;
323 }
324
325 // }}}
326 // {{{ function check()
327
328 function check()
329 {
330 $text = enriched_to_text($this->_body);
331 $arr = explode("\n",wordwrap($text,68));
332 $c = 0;
333 foreach ($arr as $line) {
334 if (trim($line)) {
335 $c++;
336 }
337 }
338 return $c<9;
339 }
340
341 // }}}
342 }
343
344 // }}}
345 // {{{ Functions
346
347 function insert_new_nl()
348 {
349 XDB::execute("INSERT INTO newsletter SET bits='new',date=NOW(),titre='to be continued',titre_mail='to be continued'");
350 }
351
352 function get_nl_slist()
353 {
354 $res = XDB::query("SELECT IF(short_name IS NULL, id,short_name) as id,date,titre_mail AS titre FROM newsletter ORDER BY date DESC");
355 return $res->fetchAllAssoc();
356 }
357
358 function get_nl_list()
359 {
360 $res = XDB::query("SELECT IF(short_name IS NULL, id,short_name) as id,date,titre_mail AS titre FROM newsletter WHERE bits!='new' ORDER BY date DESC");
361 return $res->fetchAllAssoc();
362 }
363
364 function get_nl_state()
365 {
366 $res = XDB::query('SELECT 1 FROM newsletter_ins WHERE user_id={?}', S::v('uid'));
367 return $res->fetchOneCell();
368 }
369
370 function unsubscribe_nl()
371 {
372 XDB::execute('DELETE FROM newsletter_ins WHERE user_id={?}', S::v('uid'));
373 }
374
375 function subscribe_nl($uid=-1)
376 {
377 $user = ($uid == -1) ? S::v('uid') : $uid;
378 XDB::execute('REPLACE INTO newsletter_ins (user_id,last)
379 VALUES ({?}, 0)', $user);
380 }
381
382 function justify($text,$n)
383 {
384 $arr = explode("\n",wordwrap($text,$n));
385 $arr = array_map('trim',$arr);
386 $res = '';
387 foreach ($arr as $key => $line) {
388 $nxl = isset($arr[$key+1]) ? trim($arr[$key+1]) : '';
389 $nxl_split = preg_split('! +!',$nxl);
390 $nxw_len = count($nxl_split) ? strlen($nxl_split[0]) : 0;
391 $line = trim($line);
392
393 if (strlen($line)+1+$nxw_len < $n) {
394 $res .= "$line\n";
395 continue;
396 }
397
398 if (preg_match('![.:;]$!',$line)) {
399 $res .= "$line\n";
400 continue;
401 }
402
403 $tmp = preg_split('! +!',trim($line));
404 $words = count($tmp);
405 if ($words <= 1) {
406 $res .= "$line\n";
407 continue;
408 }
409
410 $len = array_sum(array_map('strlen',$tmp));
411 $empty = $n - $len;
412 $sw = floatval($empty) / floatval($words-1);
413
414 $cur = 0;
415 $l = '';
416 foreach ($tmp as $word) {
417 $l .= $word;
418 $cur += $sw + strlen($word);
419 $l = str_pad($l,intval($cur+0.5));
420 }
421 $res .= trim($l)."\n";
422 }
423 return trim($res);
424 }
425
426 function enriched_to_text($input,$html=false,$just=false,$indent=0,$width=68)
427 {
428 $text = trim($input);
429 if ($html) {
430 $text = htmlspecialchars($text);
431 $text = str_replace('[b]','<strong>', $text);
432 $text = str_replace('[/b]','</strong>', $text);
433 $text = str_replace('[i]','<em>', $text);
434 $text = str_replace('[/i]','</em>', $text);
435 $text = str_replace('[u]','<span style="text-decoration: underline">', $text);
436 $text = str_replace('[/u]','</span>', $text);
437 require_once('url_catcher.inc.php');
438 $text = url_catcher($text);
439 return nl2br($text);
440 } else {
441 $text = preg_replace('!\[\/?b\]!','*',$text);
442 $text = preg_replace('!\[\/?u\]!','_',$text);
443 $text = preg_replace('!\[\/?i\]!','/',$text);
444 $text = preg_replace('!(((https?|ftp)://|www\.)[^\r\n\t ]*)!','[\1]', $text);
445 $text = preg_replace('!(([a-zA-Z0-9\-_+.]*@[a-zA-Z0-9\-_+.]*)(?:\?[^\r\n\t ]*)?)!','[mailto:\1]', $text);
446 $text = $just ? justify($text,$width-$indent) : wordwrap($text,$width-$indent);
447 if($indent) {
448 $ind = str_pad('',$indent);
449 $text = $ind.str_replace("\n","\n$ind",$text);
450 }
451 return $text;
452 }
453 }
454
455 // }}}
456
457 // vim:set et sw=4 sts=4 sws=4:
458 ?>