8da0d3c1 |
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 | |
8da0d3c1 |
76 | public function valid() |
77 | { |
78 | return XDB::execute("UPDATE axletter |
79 | SET echeance = NOW() |
80 | WHERE id = {?}", $this->_id); |
81 | } |
82 | |
83 | public function invalid() |
84 | { |
85 | return XDB::execute("UPDATE axletter |
86 | SET bits = 'invalid', date = CURDATE() |
87 | WHERE id = {?}", $this->_id); |
88 | } |
89 | |
90 | protected function setSent() |
91 | { |
92 | XDB::execute("UPDATE axletter |
93 | SET bits='sent', date=CURDATE() |
94 | WHERE id={?}", $this->_id); |
95 | } |
96 | |
a0f05027 |
97 | protected function getAllRecipients() |
98 | { |
99 | return "SELECT ni.user_id, IF(ni.user_id = 0, ni.email, a.alias) AS alias, |
100 | IF(ni.user_id = 0, ni.prenom, u.prenom) AS prenom, |
101 | IF(ni.user_id = 0, ni.nom, IF(u.nom_usage='', u.nom, u.nom_usage)) AS nom, |
102 | FIND_IN_SET('femme', IF(ni.user_id = 0, ni.flag, u.flags)) AS sexe, |
103 | IF(ni.user_id = 0, 'html', q.core_mail_fmt) AS pref, |
104 | IF(ni.user_id = 0, ni.hash, 0) AS hash |
105 | FROM axletter_ins AS ni |
106 | LEFT JOIN auth_user_md5 AS u USING(user_id) |
107 | LEFT JOIN auth_user_quick AS q ON(q.user_id = u.user_id) |
108 | LEFT JOIN aliases AS a ON(u.user_id=a.id AND FIND_IN_SET('bestalias',a.flags)) |
109 | LEFT JOIN emails AS e ON(e.uid=u.user_id AND e.flags='active') |
4e25428d |
110 | WHERE ni.last < {?} AND {$this->subscriptionWhere()} |
111 | AND (e.email IS NOT NULL OR ni.user_id = 0) |
a0f05027 |
112 | GROUP BY u.user_id"; |
113 | } |
114 | |
8da0d3c1 |
115 | static public function subscriptionState($uid = null) |
116 | { |
117 | $user = is_null($uid) ? S::v('uid') : $uid; |
118 | $res = XDB::query("SELECT 1 |
119 | FROM axletter_ins |
120 | WHERE user_id={?}", $user); |
121 | return $res->fetchOneCell(); |
122 | } |
123 | |
a0f05027 |
124 | static public function unsubscribe($uid = null, $hash = false) |
8da0d3c1 |
125 | { |
126 | $user = is_null($uid) ? S::v('uid') : $uid; |
a0f05027 |
127 | $field = !$hash ? 'user_id' : 'hash'; |
128 | if (is_null($uid) && $hash) { |
129 | return false; |
130 | } |
131 | $res = XDB::query("SELECT * |
132 | FROM axletter_ins |
133 | WHERE $field={?}", $user); |
134 | if (!$res->numRows()) { |
135 | return false; |
136 | } |
8da0d3c1 |
137 | XDB::execute("DELETE FROM axletter_ins |
a0f05027 |
138 | WHERE $field = {?}", $user); |
139 | return true; |
8da0d3c1 |
140 | } |
141 | |
142 | static public function subscribe($uid = null) |
143 | { |
144 | $user = is_null($uid) ? S::v('uid') : $uid; |
145 | XDB::execute("REPLACE INTO axletter_ins (user_id,last) |
146 | VALUES ({?}, 0)", $user); |
147 | } |
148 | |
149 | static public function hasPerms() |
150 | { |
151 | if (S::has_perms()) { |
152 | return true; |
153 | } |
154 | $res = XDB::query("SELECT 1 |
155 | FROM axletter_rights |
156 | WHERE user_id = {?}", S::i('uid')); |
157 | return $res->fetchOneCell(); |
158 | } |
159 | |
0d75939a |
160 | static public function grantPerms($uid) |
161 | { |
162 | if (!is_numeric($uid)) { |
163 | $res = XDB::query("SELECT id FROM aliases WHERE alias = {?}", $uid); |
164 | $uid = $res->fetchOneCell(); |
165 | } |
166 | if (!$uid) { |
167 | return false; |
168 | } |
169 | return XDB::execute("INSERT IGNORE INTO axletter_rights SET user_id = {?}", $uid); |
170 | } |
171 | |
172 | static public function revokePerms($uid) |
173 | { |
174 | if (!is_numeric($uid)) { |
175 | $res = XDB::query("SELECT id FROM aliases WHERE alias = {?}", $uid); |
176 | $uid = $res->fetchOneCell(); |
177 | } |
178 | if (!$uid) { |
179 | return false; |
180 | } |
181 | return XDB::execute("DELETE FROM axletter_rights WHERE user_id = {?}", $uid); |
182 | } |
183 | |
8da0d3c1 |
184 | protected function subscriptionTable() |
185 | { |
186 | return 'axletter_ins'; |
187 | } |
188 | |
189 | protected function subscriptionWhere() |
190 | { |
4e25428d |
191 | if (!$this->_promo_min && !$this->_promo_max) { |
192 | return '1'; |
193 | } |
194 | $where = array(); |
195 | if ($this->_promo_min) { |
196 | $where[] = "((ni.user_id = 0 AND ni.promo >= {$this->_promo_min}) OR (ni.user_id != 0 AND u.promo >= {$this->_promo_min}))"; |
197 | } |
198 | if ($this->_promo_max) { |
199 | $where[] = "((ni.user_id = 0 AND ni.promo <= {$this->_promo_max}) OR (ni.user_id != 0 AND u.promo <= {$this->_promo_max}))"; |
200 | } |
201 | return implode(' AND ', $where); |
8da0d3c1 |
202 | } |
203 | |
204 | static public function awaiting() |
205 | { |
a0f05027 |
206 | $res = XDB::query("SELECT * |
8da0d3c1 |
207 | FROM axletter |
208 | WHERE FIND_IN_SET('new', bits)"); |
a0f05027 |
209 | if ($res->numRows()) { |
210 | return new AXLetter($res->fetchOneRow()); |
211 | } |
212 | return null; |
213 | } |
214 | |
215 | static public function toSend() |
216 | { |
217 | $res = XDB::query("SELECT * |
218 | FROM axletter |
219 | WHERE FIND_IN_SET('new', bits) AND echeance <= NOW() AND echeance != 0"); |
220 | if ($res->numRows()) { |
221 | return new AXLetter($res->fetchOneRow()); |
222 | } |
223 | return null; |
8da0d3c1 |
224 | } |
225 | |
226 | static public function listSent() |
227 | { |
228 | $res = XDB::query("SELECT IF(shortname IS NULL, id, shortname) as id, date, subject AS titre |
229 | FROM axletter |
230 | WHERE NOT (FIND_IN_SET('new', bits)) |
231 | ORDER BY date DESC"); |
232 | return $res->fetchAllAssoc(); |
233 | } |
234 | |
235 | static public function listAll() |
236 | { |
237 | $res = XDB::query("SELECT IF(shortname IS NULL, id, shortname) as id, date, subject AS titre |
238 | FROM axletter |
239 | ORDER BY date DESC"); |
240 | return $res->fetchAllAssoc(); |
241 | } |
242 | } |
243 | |
244 | // vim:set et sw=4 sts=4 sws=4: |
245 | ?> |