178a0d39d815a06db2454b38637229215054038d
[platal.git] / modules / axletter / axletter.inc.php
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
22 require_once("massmailer.inc.php");
23
24 class AXLetter extends MassMailer
25 {
26 public $_body;
27 public $_signature;
28 public $_promo_min;
29 public $_promo_max;
30 public $_echeance;
31 public $_date;
32 public $_bits;
33
34 function __construct($id)
35 {
36 parent::__construct('axletter/letter.tpl', 'ax.css', 'ax/show');
37 $this->_head = '<cher> <prenom>,';
38
39 if (!is_array($id)) {
40 if ($id == 'last') {
41 $res = XDB::query("SELECT *
42 FROM axletter
43 WHERE FIND_IN_SET('sent', bits)
44 ORDER BY id DESC");
45 } else {
46 $res = XDB::query("SELECT *
47 FROM axletter
48 WHERE id = {?} OR shortname = {?}", $id, $id);
49 }
50 if (!$res->numRows()) {
51 $this->_id = null;
52 return;
53 }
54 $id = $res->fetchOneRow();
55 }
56 list($this->_id, $this->_shortname, $this->_title_mail, $this->_title,
57 $this->_body, $this->_signature, $this->_promo_min, $this->_promo_max,
58 $this->_echeance, $this->_date, $this->_bits) = $id;
59 }
60
61 protected function assignData(&$smarty)
62 {
63 $smarty->assign_by_ref('am', $this);
64 }
65
66 public function body($format)
67 {
68 return format_text($this->_body, $format);
69 }
70
71 public function signature($format)
72 {
73 return format_text($this->_signature, $format, 10);
74 }
75
76 static public function create($subject, $title, $body, $signature, $promo_min, $promo_max, $date, $shortname = null)
77 {
78 $id = AXLetter::awaiting();
79 if ($id) {
80 return new AXLetter($id);
81 }
82 XDB::execute("INSERT INTO axletter (shortname, echeance, promo_min, promo_max,
83 subject, title, body, signature,
84 subject_ini, title_ini, body_ini, signature_ini)
85 VALUES ({?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?}, {?})",
86 $shortname, $date, $promo_min, $promo_max,
87 $subject, $title, $body, $signature, $subject, $title, $body, $signature);
88 return new AXLetter(XDB::insertId());
89 }
90
91 public function update($subject, $title, $body, $signature, $promo_min, $promo_max, $date, $shortname = null)
92 {
93 $this->_shortname = $shortname;
94 $this->_title = $title;
95 $this->_title_mail = $subject;
96 $this->_body = $body;
97 $this->_signature = $signature;
98 $this->_promo_min = $promo_min;
99 $this->_promo_max = $promo_max;
100 $this->_date = $date;
101 return XDB::execute("UPDATE axletter (shortname, subject, title, body, signature, promo_min, promo_max, echeance)
102 SET shorname={?}, subject={?}, title={?}, body={?}, signature={?},
103 promo_min={?}, promo_max={?}, echeance={?}
104 WHERE id = {?}",
105 $shortname, $subject, $title, $body, $signature, $promo_min, $promo_max, $date, $this->_id);
106 }
107
108 public function valid()
109 {
110 return XDB::execute("UPDATE axletter
111 SET echeance = NOW()
112 WHERE id = {?}", $this->_id);
113 }
114
115 public function invalid()
116 {
117 return XDB::execute("UPDATE axletter
118 SET bits = 'invalid', date = CURDATE()
119 WHERE id = {?}", $this->_id);
120 }
121
122 protected function setSent()
123 {
124 XDB::execute("UPDATE axletter
125 SET bits='sent', date=CURDATE()
126 WHERE id={?}", $this->_id);
127 }
128
129 static public function subscriptionState($uid = null)
130 {
131 $user = is_null($uid) ? S::v('uid') : $uid;
132 $res = XDB::query("SELECT 1
133 FROM axletter_ins
134 WHERE user_id={?}", $user);
135 return $res->fetchOneCell();
136 }
137
138 static public function unsubscribe($uid = null)
139 {
140 $user = is_null($uid) ? S::v('uid') : $uid;
141 XDB::execute("DELETE FROM axletter_ins
142 WHERE user_id={?} OR hash = {?}", $user, $user);
143 }
144
145 static public function subscribe($uid = null)
146 {
147 $user = is_null($uid) ? S::v('uid') : $uid;
148 XDB::execute("REPLACE INTO axletter_ins (user_id,last)
149 VALUES ({?}, 0)", $user);
150 }
151
152 static public function hasPerms()
153 {
154 if (S::has_perms()) {
155 return true;
156 }
157 $res = XDB::query("SELECT 1
158 FROM axletter_rights
159 WHERE user_id = {?}", S::i('uid'));
160 return $res->fetchOneCell();
161 }
162
163 protected function subscriptionTable()
164 {
165 return 'axletter_ins';
166 }
167
168 protected function subscriptionWhere()
169 {
170 return 'ni.last';
171 }
172
173 static public function awaiting()
174 {
175 $res = XDB::query("SELECT id
176 FROM axletter
177 WHERE FIND_IN_SET('new', bits)");
178 return $res->fetchOneCell();
179 }
180
181 static public function listSent()
182 {
183 $res = XDB::query("SELECT IF(shortname IS NULL, id, shortname) as id, date, subject AS titre
184 FROM axletter
185 WHERE NOT (FIND_IN_SET('new', bits))
186 ORDER BY date DESC");
187 return $res->fetchAllAssoc();
188 }
189
190 static public function listAll()
191 {
192 $res = XDB::query("SELECT IF(shortname IS NULL, id, shortname) as id, date, subject AS titre
193 FROM axletter
194 ORDER BY date DESC");
195 return $res->fetchAllAssoc();
196 }
197 }
198
199 // vim:set et sw=4 sts=4 sws=4:
200 ?>