2 /***************************************************************************
3 * Copyright (C) 2003-2008 Polytechnique.org *
4 * http://opensource.polytechnique.org/ *
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. *
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. *
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 *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
22 require_once("massmailer.inc.php");
24 // {{{ class NewsLetter
26 class NewsLetter
extends MassMailer
29 public $_cats = Array();
30 public $_arts = Array();
32 function __construct($id = null
)
34 parent
::__construct('newsletter/nl.mail.tpl', 'nl.css', 'nl/show', 'newsletter', 'newsletter_ins');
37 $res = XDB
::query("SELECT MAX(id) FROM newsletter WHERE bits!='new'");
38 $id = $res->fetchOneCell();
40 $res = XDB
::query("SELECT * FROM newsletter WHERE id={?} OR short_name={?} LIMIT 1", $id, $id);
42 $res = XDB
::query("SELECT * FROM newsletter WHERE bits='new'");
43 if (!$res->numRows()) {
46 $res = XDB
::query("SELECT * FROM newsletter WHERE bits='new'");
48 $nl = $res->fetchOneAssoc();
50 $this->_id
= $nl['id'];
51 $this->_shortname
= $nl['short_name'];
52 $this->_date
= $nl['date'];
53 $this->_title
= $nl['titre'];
54 $this->_title_mail
= $nl['titre_mail'];
55 $this->_head
= $nl['head'];
57 $res = XDB
::iterRow("SELECT cid,titre FROM newsletter_cat ORDER BY pos");
58 while (list($cid, $title) = $res->next()) {
59 $this->_cats
[$cid] = $title;
63 "SELECT a.title,a.body,a.append,a.aid,a.cid,a.pos
64 FROM newsletter_art AS a
65 INNER JOIN newsletter AS n USING(id)
66 LEFT JOIN newsletter_cat AS c ON(a.cid=c.cid)
68 ORDER BY c.pos,a.pos", $this->_id
);
69 while (list($title, $body, $append, $aid, $cid, $pos) = $res->next()) {
70 $this->_arts
[$cid]["a$aid"] = new NLArticle($title, $body, $append, $aid, $cid, $pos);
74 public function save()
76 XDB
::execute('UPDATE newsletter SET date={?},titre={?},titre_mail={?},head={?},short_name={?} WHERE id={?}',
77 $this->_date
, $this->_title
, $this->_title_mail
, $this->_head
, $this->_shortname
,$this->_id
);
80 public function getArt($aid)
82 foreach ($this->_arts
as $key=>$artlist) {
83 if (isset($artlist["a$aid"])) {
84 return $artlist["a$aid"];
90 public function saveArticle(&$a)
93 XDB
::execute('REPLACE INTO newsletter_art (id,aid,cid,pos,title,body,append)
94 VALUES ({?},{?},{?},{?},{?},{?},{?})',
95 $this->_id
, $a->_aid
, $a->_cid
, $a->_pos
,
96 $a->_title
, $a->_body
, $a->_append
);
97 $this->_arts
['a'.$a->_aid
] = $a;
99 XDB
::execute('INSERT INTO newsletter_art
100 SELECT {?},MAX(aid)+1,{?},'.($a->_pos ?
intval($a->_pos
) : 'MAX(pos)+1').',{?},{?},{?}
101 FROM newsletter_art AS a
103 $this->_id
, $a->_cid
, $a->_title
, $a->_body
, $a->_append
, $this->_id
);
104 $this->_arts
['a'.$a->_aid
] = $a;
108 public function delArticle($aid)
110 XDB
::execute('DELETE FROM newsletter_art WHERE id={?} AND aid={?}', $this->_id
, $aid);
111 foreach ($this->_arts
as $key=>$art) {
112 unset($this->_arts
[$key]["a$aid"]);
116 protected function assignData(&$smarty)
118 $smarty->assign_by_ref('nl', $this);
121 protected function setSent()
123 XDB
::execute("UPDATE newsletter SET bits='sent' WHERE id={?}", $this->_id
);
126 static public function subscriptionState($uid = null
)
128 $user = is_null($uid) ? S
::v('uid') : $uid;
129 $res = XDB
::query("SELECT 1
131 WHERE user_id={?}", $user);
132 return $res->fetchOneCell();
135 static public function unsubscribe($uid = null
)
137 $user = is_null($uid) ? S
::v('uid') : $uid;
138 XDB
::execute("DELETE FROM newsletter_ins
139 WHERE user_id={?}", $user);
142 static public function subscribe($uid = null
)
144 $user = is_null($uid) ? S
::v('uid') : $uid;
145 XDB
::execute("REPLACE INTO newsletter_ins (user_id,last)
146 VALUES ({?}, 0)", $user);
149 protected function subscriptionWhere()
154 static public function create()
156 XDB
::execute("INSERT INTO newsletter
157 SET bits='new',date=NOW(),titre='to be continued',titre_mail='to be continued'");
160 static public function listSent()
162 $res = XDB
::query("SELECT IF(short_name IS NULL, id,short_name) as id,date,titre_mail AS titre
165 ORDER BY date DESC");
166 return $res->fetchAllAssoc();
169 static public function listAll()
171 $res = XDB
::query("SELECT IF(short_name IS NULL, id,short_name) as id,date,titre_mail AS titre
173 ORDER BY date DESC");
174 return $res->fetchAllAssoc();
179 // {{{ class NLArticle
195 function __construct($title='', $body='', $append='', $aid=-1, $cid=0, $pos=0)
197 $this->_body
= $body;
198 $this->_title
= $title;
199 $this->_append
= $append;
206 // {{{ function title()
208 public function title()
209 { return trim($this->_title
); }
212 // {{{ function body()
214 public function body()
215 { return trim($this->_body
); }
218 // {{{ function append()
220 public function append()
221 { return trim($this->_append
); }
224 // {{{ function toText()
226 public function toText($hash = null
, $login = null
)
228 $title = '*'.$this->title().'*';
229 $body = MiniWiki
::WikiToText($this->_body
, true
);
230 $app = MiniWiki
::WikiToText($this->_append
,false
,4);
231 $text = trim("$title\n\n$body\n\n$app")."\n";
232 if (!is_null($hash) && !is_null($login)) {
233 $text = str_replace('%HASH%', "$hash/$login", $text);
235 $text = str_replace('%HASH%', '', $text);
241 // {{{ function toHtml()
243 public function toHtml($hash = null
, $login = null
)
245 $title = "<h2 class='xorg_nl'><a id='art{$this->_aid}'></a>".pl_entities($this->title()).'</h2>';
246 $body = MiniWiki
::WikiToHTML($this->_body
);
247 $app = MiniWiki
::WikiToHTML($this->_append
);
250 $art .= "<div class='art'>\n$body\n";
252 $art .= "<div class='app'>$app</div>";
255 if (!is_null($hash) && !is_null($login)) {
256 $art = str_replace('%HASH%', "$hash/$login", $art);
258 $art = str_replace('%HASH%', '', $art);
265 // {{{ function check()
267 public function check()
269 $text = MiniWiki
::WikiToText($this->_body
);
270 $arr = explode("\n",wordwrap($text,68));
272 foreach ($arr as $line) {
285 // vim:set et sw=4 sts=4 sws=4 enc=utf-8: