Merge commit 'origin/master' into fusionax
[platal.git] / include / newsletter.inc.php
CommitLineData
0337d704 1<?php
2/***************************************************************************
8d84c630 3 * Copyright (C) 2003-2009 Polytechnique.org *
0337d704 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
16cd99fb 22require_once("massmailer.inc.php");
0337d704 23
0337d704 24// {{{ class NewsLetter
25
4882f4a5 26class NewsLetter extends MassMailer
0337d704 27{
4882f4a5 28 public $_date;
29 public $_cats = Array();
30 public $_arts = Array();
0337d704 31
4882f4a5 32 function __construct($id = null)
0337d704 33 {
b71f7275 34 parent::__construct('newsletter/nl.mail.tpl', 'nl.css', 'nl/show', 'newsletter', 'newsletter_ins');
ed76a506 35 if (isset($id)) {
36 if ($id == 'last') {
37 $res = XDB::query("SELECT MAX(id) FROM newsletter WHERE bits!='new'");
0337d704 38 $id = $res->fetchOneCell();
ed76a506 39 }
40 $res = XDB::query("SELECT * FROM newsletter WHERE id={?} OR short_name={?} LIMIT 1", $id, $id);
41 } else {
42 $res = XDB::query("SELECT * FROM newsletter WHERE bits='new'");
43 if (!$res->numRows()) {
4882f4a5 44 Newsletter::create();
ed76a506 45 }
46 $res = XDB::query("SELECT * FROM newsletter WHERE bits='new'");
ad6ca77d 47 }
9da70671
SJ
48 if ($res->numRows() != 1) {
49 throw new MailNotFound();
50 }
ed76a506 51 $nl = $res->fetchOneAssoc();
0337d704 52
ed76a506 53 $this->_id = $nl['id'];
54 $this->_shortname = $nl['short_name'];
55 $this->_date = $nl['date'];
56 $this->_title = $nl['titre'];
8269d2d6 57 $this->_title_mail = $nl['titre_mail'];
ed76a506 58 $this->_head = $nl['head'];
0337d704 59
ed76a506 60 $res = XDB::iterRow("SELECT cid,titre FROM newsletter_cat ORDER BY pos");
61 while (list($cid, $title) = $res->next()) {
62 $this->_cats[$cid] = $title;
63 }
eaf30d86 64
ed76a506 65 $res = XDB::iterRow(
0337d704 66 "SELECT a.title,a.body,a.append,a.aid,a.cid,a.pos
67 FROM newsletter_art AS a
68 INNER JOIN newsletter AS n USING(id)
69 LEFT JOIN newsletter_cat AS c ON(a.cid=c.cid)
70 WHERE a.id={?}
71 ORDER BY c.pos,a.pos", $this->_id);
ed76a506 72 while (list($title, $body, $append, $aid, $cid, $pos) = $res->next()) {
73 $this->_arts[$cid]["a$aid"] = new NLArticle($title, $body, $append, $aid, $cid, $pos);
74 }
0337d704 75 }
76
4882f4a5 77 public function save()
0337d704 78 {
8269d2d6 79 XDB::execute('UPDATE newsletter SET date={?},titre={?},titre_mail={?},head={?},short_name={?} WHERE id={?}',
80 $this->_date, $this->_title, $this->_title_mail, $this->_head, $this->_shortname,$this->_id);
ed76a506 81 }
eaf30d86 82
4882f4a5 83 public function getArt($aid)
0337d704 84 {
11f44323 85 foreach ($this->_arts as $key=>$artlist) {
86 if (isset($artlist["a$aid"])) {
0337d704 87 return $artlist["a$aid"];
88 }
11f44323 89 }
90 return null;
0337d704 91 }
92
4882f4a5 93 public function saveArticle(&$a)
0337d704 94 {
11f44323 95 if ($a->_aid>=0) {
96 XDB::execute('REPLACE INTO newsletter_art (id,aid,cid,pos,title,body,append)
97 VALUES ({?},{?},{?},{?},{?},{?},{?})',
98 $this->_id, $a->_aid, $a->_cid, $a->_pos,
99 $a->_title, $a->_body, $a->_append);
100 $this->_arts['a'.$a->_aid] = $a;
101 } else {
102 XDB::execute('INSERT INTO newsletter_art
103 SELECT {?},MAX(aid)+1,{?},'.($a->_pos ? intval($a->_pos) : 'MAX(pos)+1').',{?},{?},{?}
104 FROM newsletter_art AS a
105 WHERE a.id={?}',
106 $this->_id, $a->_cid, $a->_title, $a->_body, $a->_append, $this->_id);
107 $this->_arts['a'.$a->_aid] = $a;
108 }
0337d704 109 }
eaf30d86 110
4882f4a5 111 public function delArticle($aid)
0337d704 112 {
11f44323 113 XDB::execute('DELETE FROM newsletter_art WHERE id={?} AND aid={?}', $this->_id, $aid);
114 foreach ($this->_arts as $key=>$art) {
115 unset($this->_arts[$key]["a$aid"]);
116 }
0337d704 117 }
118
4882f4a5 119 protected function assignData(&$smarty)
120 {
121 $smarty->assign_by_ref('nl', $this);
122 }
0337d704 123
4882f4a5 124 protected function setSent()
0337d704 125 {
16cd99fb 126 XDB::execute("UPDATE newsletter SET bits='sent' WHERE id={?}", $this->_id);
eaf30d86 127 }
0337d704 128
16cd99fb 129 static public function subscriptionState($uid = null)
0337d704 130 {
16cd99fb 131 $user = is_null($uid) ? S::v('uid') : $uid;
132 $res = XDB::query("SELECT 1
133 FROM newsletter_ins
134 WHERE user_id={?}", $user);
135 return $res->fetchOneCell();
eaf30d86
PH
136 }
137
16cd99fb 138 static public function unsubscribe($uid = null)
4882f4a5 139 {
16cd99fb 140 $user = is_null($uid) ? S::v('uid') : $uid;
141 XDB::execute("DELETE FROM newsletter_ins
142 WHERE user_id={?}", $user);
0337d704 143 }
144
16cd99fb 145 static public function subscribe($uid = null)
4882f4a5 146 {
16cd99fb 147 $user = is_null($uid) ? S::v('uid') : $uid;
148 XDB::execute("REPLACE INTO newsletter_ins (user_id,last)
149 VALUES ({?}, 0)", $user);
4882f4a5 150 }
151
16cd99fb 152 protected function subscriptionWhere()
4882f4a5 153 {
16cd99fb 154 return '1';
4882f4a5 155 }
156
157 static public function create()
158 {
159 XDB::execute("INSERT INTO newsletter
160 SET bits='new',date=NOW(),titre='to be continued',titre_mail='to be continued'");
161 }
162
163 static public function listSent()
164 {
165 $res = XDB::query("SELECT IF(short_name IS NULL, id,short_name) as id,date,titre_mail AS titre
166 FROM newsletter
167 WHERE bits!='new'
168 ORDER BY date DESC");
169 return $res->fetchAllAssoc();
170 }
171
172 static public function listAll()
173 {
174 $res = XDB::query("SELECT IF(short_name IS NULL, id,short_name) as id,date,titre_mail AS titre
175 FROM newsletter
176 ORDER BY date DESC");
177 return $res->fetchAllAssoc();
178 }
0337d704 179}
180
181// }}}
182// {{{ class NLArticle
183
184class NLArticle
185{
186 // {{{ properties
eaf30d86 187
0337d704 188 var $_aid;
189 var $_cid;
190 var $_pos;
191 var $_title;
192 var $_body;
193 var $_append;
194
195 // }}}
196 // {{{ constructor
eaf30d86 197
4882f4a5 198 function __construct($title='', $body='', $append='', $aid=-1, $cid=0, $pos=0)
0337d704 199 {
4882f4a5 200 $this->_body = $body;
201 $this->_title = $title;
202 $this->_append = $append;
203 $this->_aid = $aid;
204 $this->_cid = $cid;
205 $this->_pos = $pos;
0337d704 206 }
207
208 // }}}
209 // {{{ function title()
210
4882f4a5 211 public function title()
0337d704 212 { return trim($this->_title); }
213
214 // }}}
215 // {{{ function body()
eaf30d86 216
4882f4a5 217 public function body()
0337d704 218 { return trim($this->_body); }
eaf30d86 219
0337d704 220 // }}}
221 // {{{ function append()
eaf30d86 222
4882f4a5 223 public function append()
0337d704 224 { return trim($this->_append); }
225
226 // }}}
227 // {{{ function toText()
228
e0e77c5a 229 public function toText($hash = null, $login = null)
0337d704 230 {
4882f4a5 231 $title = '*'.$this->title().'*';
dcdcd18a 232 $body = MiniWiki::WikiToText($this->_body, true);
02fdd1c8 233 $app = MiniWiki::WikiToText($this->_append,false,4);
e0e77c5a
FB
234 $text = trim("$title\n\n$body\n\n$app")."\n";
235 if (!is_null($hash) && !is_null($login)) {
236 $text = str_replace('%HASH%', "$hash/$login", $text);
237 } else {
238 $text = str_replace('%HASH%', '', $text);
239 }
240 return $text;
0337d704 241 }
242
243 // }}}
244 // {{{ function toHtml()
245
e0e77c5a 246 public function toHtml($hash = null, $login = null)
0337d704 247 {
493b6abe 248 $title = "<h2 class='xorg_nl'><a id='art{$this->_aid}'></a>".pl_entities($this->title()).'</h2>';
02fdd1c8 249 $body = MiniWiki::WikiToHTML($this->_body);
250 $app = MiniWiki::WikiToHTML($this->_append);
eaf30d86 251
4882f4a5 252 $art = "$title\n";
253 $art .= "<div class='art'>\n$body\n";
254 if ($app) {
0337d704 255 $art .= "<div class='app'>$app</div>";
256 }
4882f4a5 257 $art .= "</div>\n";
e0e77c5a
FB
258 if (!is_null($hash) && !is_null($login)) {
259 $art = str_replace('%HASH%', "$hash/$login", $art);
260 } else {
261 $art = str_replace('%HASH%', '', $art);
262 }
eaf30d86 263
4882f4a5 264 return $art;
0337d704 265 }
266
267 // }}}
268 // {{{ function check()
269
4882f4a5 270 public function check()
0337d704 271 {
02fdd1c8 272 $text = MiniWiki::WikiToText($this->_body);
4882f4a5 273 $arr = explode("\n",wordwrap($text,68));
274 $c = 0;
275 foreach ($arr as $line) {
0337d704 276 if (trim($line)) {
277 $c++;
278 }
279 }
4882f4a5 280 return $c<9;
0337d704 281 }
282
283 // }}}
284}
285
286// }}}
0337d704 287
a7de4ef7 288// vim:set et sw=4 sts=4 sws=4 enc=utf-8:
0337d704 289?>