Revert "Initial work on the rewriting of the profile/edit page"
[platal.git] / include / newsletter.inc.php
CommitLineData
0337d704 1<?php
2/***************************************************************************
5ddeb07c 3 * Copyright (C) 2003-2007 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 {
fc9d9368 34 parent::__construct('newsletter/nl.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 }
ed76a506 48 $nl = $res->fetchOneAssoc();
0337d704 49
ed76a506 50 $this->_id = $nl['id'];
51 $this->_shortname = $nl['short_name'];
52 $this->_date = $nl['date'];
53 $this->_title = $nl['titre'];
8269d2d6 54 $this->_title_mail = $nl['titre_mail'];
ed76a506 55 $this->_head = $nl['head'];
0337d704 56
ed76a506 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;
60 }
ad6ca77d 61
ed76a506 62 $res = XDB::iterRow(
0337d704 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)
67 WHERE a.id={?}
68 ORDER BY c.pos,a.pos", $this->_id);
ed76a506 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);
71 }
0337d704 72 }
73
4882f4a5 74 public function save()
0337d704 75 {
8269d2d6 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);
ed76a506 78 }
4882f4a5 79
80 public function getArt($aid)
0337d704 81 {
11f44323 82 foreach ($this->_arts as $key=>$artlist) {
83 if (isset($artlist["a$aid"])) {
0337d704 84 return $artlist["a$aid"];
85 }
11f44323 86 }
87 return null;
0337d704 88 }
89
4882f4a5 90 public function saveArticle(&$a)
0337d704 91 {
11f44323 92 if ($a->_aid>=0) {
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;
98 } else {
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
102 WHERE a.id={?}',
103 $this->_id, $a->_cid, $a->_title, $a->_body, $a->_append, $this->_id);
104 $this->_arts['a'.$a->_aid] = $a;
105 }
0337d704 106 }
4882f4a5 107
108 public function delArticle($aid)
0337d704 109 {
11f44323 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"]);
113 }
0337d704 114 }
115
4882f4a5 116 protected function assignData(&$smarty)
117 {
118 $smarty->assign_by_ref('nl', $this);
119 }
0337d704 120
4882f4a5 121 protected function setSent()
0337d704 122 {
16cd99fb 123 XDB::execute("UPDATE newsletter SET bits='sent' WHERE id={?}", $this->_id);
124 }
0337d704 125
16cd99fb 126 static public function subscriptionState($uid = null)
0337d704 127 {
16cd99fb 128 $user = is_null($uid) ? S::v('uid') : $uid;
129 $res = XDB::query("SELECT 1
130 FROM newsletter_ins
131 WHERE user_id={?}", $user);
132 return $res->fetchOneCell();
133 }
134
135 static public function unsubscribe($uid = null)
4882f4a5 136 {
16cd99fb 137 $user = is_null($uid) ? S::v('uid') : $uid;
138 XDB::execute("DELETE FROM newsletter_ins
139 WHERE user_id={?}", $user);
0337d704 140 }
141
16cd99fb 142 static public function subscribe($uid = null)
4882f4a5 143 {
16cd99fb 144 $user = is_null($uid) ? S::v('uid') : $uid;
145 XDB::execute("REPLACE INTO newsletter_ins (user_id,last)
146 VALUES ({?}, 0)", $user);
4882f4a5 147 }
148
16cd99fb 149 protected function subscriptionWhere()
4882f4a5 150 {
16cd99fb 151 return '1';
4882f4a5 152 }
153
154 static public function create()
155 {
156 XDB::execute("INSERT INTO newsletter
157 SET bits='new',date=NOW(),titre='to be continued',titre_mail='to be continued'");
158 }
159
160 static public function listSent()
161 {
162 $res = XDB::query("SELECT IF(short_name IS NULL, id,short_name) as id,date,titre_mail AS titre
163 FROM newsletter
164 WHERE bits!='new'
165 ORDER BY date DESC");
166 return $res->fetchAllAssoc();
167 }
168
169 static public function listAll()
170 {
171 $res = XDB::query("SELECT IF(short_name IS NULL, id,short_name) as id,date,titre_mail AS titre
172 FROM newsletter
173 ORDER BY date DESC");
174 return $res->fetchAllAssoc();
175 }
0337d704 176}
177
178// }}}
179// {{{ class NLArticle
180
181class NLArticle
182{
183 // {{{ properties
184
185 var $_aid;
186 var $_cid;
187 var $_pos;
188 var $_title;
189 var $_body;
190 var $_append;
191
192 // }}}
193 // {{{ constructor
194
4882f4a5 195 function __construct($title='', $body='', $append='', $aid=-1, $cid=0, $pos=0)
0337d704 196 {
4882f4a5 197 $this->_body = $body;
198 $this->_title = $title;
199 $this->_append = $append;
200 $this->_aid = $aid;
201 $this->_cid = $cid;
202 $this->_pos = $pos;
0337d704 203 }
204
205 // }}}
206 // {{{ function title()
207
4882f4a5 208 public function title()
0337d704 209 { return trim($this->_title); }
210
211 // }}}
212 // {{{ function body()
213
4882f4a5 214 public function body()
0337d704 215 { return trim($this->_body); }
216
217 // }}}
218 // {{{ function append()
219
4882f4a5 220 public function append()
0337d704 221 { return trim($this->_append); }
222
223 // }}}
224 // {{{ function toText()
225
4882f4a5 226 public function toText()
0337d704 227 {
4882f4a5 228 $title = '*'.$this->title().'*';
dcdcd18a 229 $body = MiniWiki::WikiToText($this->_body, true);
02fdd1c8 230 $app = MiniWiki::WikiToText($this->_append,false,4);
4882f4a5 231 return trim("$title\n\n$body\n\n$app")."\n";
0337d704 232 }
233
234 // }}}
235 // {{{ function toHtml()
236
4882f4a5 237 public function toHtml()
0337d704 238 {
493b6abe 239 $title = "<h2 class='xorg_nl'><a id='art{$this->_aid}'></a>".pl_entities($this->title()).'</h2>';
02fdd1c8 240 $body = MiniWiki::WikiToHTML($this->_body);
241 $app = MiniWiki::WikiToHTML($this->_append);
ad6ca77d 242
4882f4a5 243 $art = "$title\n";
244 $art .= "<div class='art'>\n$body\n";
245 if ($app) {
0337d704 246 $art .= "<div class='app'>$app</div>";
247 }
4882f4a5 248 $art .= "</div>\n";
ad6ca77d 249
4882f4a5 250 return $art;
0337d704 251 }
252
253 // }}}
254 // {{{ function check()
255
4882f4a5 256 public function check()
0337d704 257 {
02fdd1c8 258 $text = MiniWiki::WikiToText($this->_body);
4882f4a5 259 $arr = explode("\n",wordwrap($text,68));
260 $c = 0;
261 foreach ($arr as $line) {
0337d704 262 if (trim($line)) {
263 $c++;
264 }
265 }
4882f4a5 266 return $c<9;
0337d704 267 }
268
269 // }}}
270}
271
272// }}}
0337d704 273
a7de4ef7 274// vim:set et sw=4 sts=4 sws=4 enc=utf-8:
0337d704 275?>