Commit | Line | Data |
---|---|---|
8da0d3c1 | 1 | <?php |
2 | /*************************************************************************** | |
8d84c630 | 3 | * Copyright (C) 2003-2009 Polytechnique.org * |
8da0d3c1 | 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; | |
2a3f4f57 | 30 | public $_subset; |
1605f171 | 31 | public $_subset_to; |
1f58ab8e | 32 | public $_subset_rm; |
8da0d3c1 | 33 | public $_echeance; |
34 | public $_date; | |
35 | public $_bits; | |
36 | ||
37 | function __construct($id) | |
38 | { | |
b71f7275 | 39 | parent::__construct('axletter/letter.mail.tpl', 'ax.css', 'ax/show', 'axletter', 'axletter_ins'); |
8da0d3c1 | 40 | $this->_head = '<cher> <prenom>,'; |
41 | ||
42 | if (!is_array($id)) { | |
43 | if ($id == 'last') { | |
44 | $res = XDB::query("SELECT * | |
45 | FROM axletter | |
46 | WHERE FIND_IN_SET('sent', bits) | |
47 | ORDER BY id DESC"); | |
48 | } else { | |
49 | $res = XDB::query("SELECT * | |
50 | FROM axletter | |
b35106ba | 51 | WHERE id = {?} OR short_name = {?}", $id, $id); |
8da0d3c1 | 52 | } |
53 | if (!$res->numRows()) { | |
4a71cf67 | 54 | throw new MailNotFound(); |
8da0d3c1 | 55 | } |
56 | $id = $res->fetchOneRow(); | |
57 | } | |
58 | list($this->_id, $this->_shortname, $this->_title_mail, $this->_title, | |
59 | $this->_body, $this->_signature, $this->_promo_min, $this->_promo_max, | |
1f58ab8e | 60 | $this->_subset_to, $this->_subset_rm, $this->_echeance, $this->_date, $this->_bits) = $id; |
9b242f17 | 61 | if ($this->_date == '0000-00-00') { |
62 | $this->_date = 0; | |
63 | } | |
c1134ef2 | 64 | $this->_subset_to = ($this->_subset_to ? explode("\n", $this->_subset_to) : null); |
1605f171 | 65 | $this->_subset = (count($this->_subset_to) > 0); |
8da0d3c1 | 66 | } |
67 | ||
68 | protected function assignData(&$smarty) | |
69 | { | |
70 | $smarty->assign_by_ref('am', $this); | |
71 | } | |
72 | ||
73 | public function body($format) | |
74 | { | |
75 | return format_text($this->_body, $format); | |
76 | } | |
77 | ||
78 | public function signature($format) | |
79 | { | |
80 | return format_text($this->_signature, $format, 10); | |
81 | } | |
82 | ||
8da0d3c1 | 83 | public function valid() |
84 | { | |
85 | return XDB::execute("UPDATE axletter | |
86 | SET echeance = NOW() | |
87 | WHERE id = {?}", $this->_id); | |
88 | } | |
89 | ||
90 | public function invalid() | |
91 | { | |
92 | return XDB::execute("UPDATE axletter | |
93 | SET bits = 'invalid', date = CURDATE() | |
94 | WHERE id = {?}", $this->_id); | |
95 | } | |
96 | ||
97 | protected function setSent() | |
98 | { | |
99 | XDB::execute("UPDATE axletter | |
100 | SET bits='sent', date=CURDATE() | |
101 | WHERE id={?}", $this->_id); | |
102 | } | |
103 | ||
a0f05027 | 104 | protected function getAllRecipients() |
105 | { | |
74f583b3 VZ |
106 | global $globals; |
107 | return "SELECT ni.user_id, IF(ni.user_id = 0, NULL, u.hruid) AS hruid, | |
108 | IF(ni.user_id = 0, ni.email, CONCAT(a.alias, '@{$globals->mail->domain}')) AS alias, | |
a0f05027 | 109 | IF(ni.user_id = 0, ni.prenom, u.prenom) AS prenom, |
110 | IF(ni.user_id = 0, ni.nom, IF(u.nom_usage='', u.nom, u.nom_usage)) AS nom, | |
111 | FIND_IN_SET('femme', IF(ni.user_id = 0, ni.flag, u.flags)) AS sexe, | |
112 | IF(ni.user_id = 0, 'html', q.core_mail_fmt) AS pref, | |
113 | IF(ni.user_id = 0, ni.hash, 0) AS hash | |
114 | FROM axletter_ins AS ni | |
115 | LEFT JOIN auth_user_md5 AS u USING(user_id) | |
116 | LEFT JOIN auth_user_quick AS q ON(q.user_id = u.user_id) | |
117 | LEFT JOIN aliases AS a ON(u.user_id=a.id AND FIND_IN_SET('bestalias',a.flags)) | |
118 | LEFT JOIN emails AS e ON(e.uid=u.user_id AND e.flags='active') | |
4e25428d | 119 | WHERE ni.last < {?} AND {$this->subscriptionWhere()} |
27614f4c | 120 | AND (e.email IS NOT NULL OR FIND_IN_SET('googleapps', u.mail_storage) OR ni.user_id = 0) |
a0f05027 | 121 | GROUP BY u.user_id"; |
eaf30d86 | 122 | } |
a0f05027 | 123 | |
8da0d3c1 | 124 | static public function subscriptionState($uid = null) |
125 | { | |
126 | $user = is_null($uid) ? S::v('uid') : $uid; | |
127 | $res = XDB::query("SELECT 1 | |
128 | FROM axletter_ins | |
129 | WHERE user_id={?}", $user); | |
130 | return $res->fetchOneCell(); | |
eaf30d86 PH |
131 | } |
132 | ||
a0f05027 | 133 | static public function unsubscribe($uid = null, $hash = false) |
8da0d3c1 | 134 | { |
135 | $user = is_null($uid) ? S::v('uid') : $uid; | |
a0f05027 | 136 | $field = !$hash ? 'user_id' : 'hash'; |
137 | if (is_null($uid) && $hash) { | |
138 | return false; | |
139 | } | |
140 | $res = XDB::query("SELECT * | |
141 | FROM axletter_ins | |
142 | WHERE $field={?}", $user); | |
143 | if (!$res->numRows()) { | |
144 | return false; | |
145 | } | |
8da0d3c1 | 146 | XDB::execute("DELETE FROM axletter_ins |
a0f05027 | 147 | WHERE $field = {?}", $user); |
148 | return true; | |
8da0d3c1 | 149 | } |
150 | ||
151 | static public function subscribe($uid = null) | |
152 | { | |
153 | $user = is_null($uid) ? S::v('uid') : $uid; | |
154 | XDB::execute("REPLACE INTO axletter_ins (user_id,last) | |
155 | VALUES ({?}, 0)", $user); | |
156 | } | |
157 | ||
158 | static public function hasPerms() | |
159 | { | |
dd70cd28 | 160 | if (S::admin()) { |
8da0d3c1 | 161 | return true; |
162 | } | |
40b6aa0f | 163 | $res = XDB::query("SELECT COUNT(*) |
8da0d3c1 | 164 | FROM axletter_rights |
165 | WHERE user_id = {?}", S::i('uid')); | |
40b6aa0f | 166 | return ($res->fetchOneCell() > 0); |
8da0d3c1 | 167 | } |
168 | ||
0d75939a | 169 | static public function grantPerms($uid) |
170 | { | |
171 | if (!is_numeric($uid)) { | |
172 | $res = XDB::query("SELECT id FROM aliases WHERE alias = {?}", $uid); | |
173 | $uid = $res->fetchOneCell(); | |
174 | } | |
175 | if (!$uid) { | |
176 | return false; | |
177 | } | |
178 | return XDB::execute("INSERT IGNORE INTO axletter_rights SET user_id = {?}", $uid); | |
179 | } | |
180 | ||
181 | static public function revokePerms($uid) | |
182 | { | |
183 | if (!is_numeric($uid)) { | |
184 | $res = XDB::query("SELECT id FROM aliases WHERE alias = {?}", $uid); | |
185 | $uid = $res->fetchOneCell(); | |
eaf30d86 | 186 | } |
0d75939a | 187 | if (!$uid) { |
188 | return false; | |
189 | } | |
190 | return XDB::execute("DELETE FROM axletter_rights WHERE user_id = {?}", $uid); | |
191 | } | |
192 | ||
8da0d3c1 | 193 | protected function subscriptionWhere() |
194 | { | |
1605f171 | 195 | if (!$this->_promo_min && !$this->_promo_max && !$this->_subset) { |
4e25428d | 196 | return '1'; |
197 | } | |
198 | $where = array(); | |
199 | if ($this->_promo_min) { | |
200 | $where[] = "((ni.user_id = 0 AND ni.promo >= {$this->_promo_min}) OR (ni.user_id != 0 AND u.promo >= {$this->_promo_min}))"; | |
201 | } | |
202 | if ($this->_promo_max) { | |
203 | $where[] = "((ni.user_id = 0 AND ni.promo <= {$this->_promo_max}) OR (ni.user_id != 0 AND u.promo <= {$this->_promo_max}))"; | |
204 | } | |
1605f171 RB |
205 | if ($this->_subset) { |
206 | require_once("emails.inc.php"); | |
180627f3 | 207 | $ids = ids_from_mails($this->_subset_to); |
1605f171 | 208 | $ids_list = implode(',', $ids); |
c1134ef2 | 209 | if(count($ids) > 0) { |
1f58ab8e RB |
210 | if ($this->_subset_rm) { |
211 | $where[] = "ni.user_id NOT IN ($ids_list)"; | |
212 | } else { | |
213 | $where[] = "ni.user_id IN ($ids_list)"; | |
214 | } | |
f0b77ba6 RB |
215 | } else { |
216 | // No valid email | |
217 | $where[] = "0"; | |
218 | } | |
1605f171 | 219 | } |
4e25428d | 220 | return implode(' AND ', $where); |
8da0d3c1 | 221 | } |
222 | ||
223 | static public function awaiting() | |
224 | { | |
a0f05027 | 225 | $res = XDB::query("SELECT * |
8da0d3c1 | 226 | FROM axletter |
227 | WHERE FIND_IN_SET('new', bits)"); | |
a0f05027 | 228 | if ($res->numRows()) { |
229 | return new AXLetter($res->fetchOneRow()); | |
230 | } | |
231 | return null; | |
232 | } | |
233 | ||
234 | static public function toSend() | |
235 | { | |
236 | $res = XDB::query("SELECT * | |
237 | FROM axletter | |
238 | WHERE FIND_IN_SET('new', bits) AND echeance <= NOW() AND echeance != 0"); | |
239 | if ($res->numRows()) { | |
240 | return new AXLetter($res->fetchOneRow()); | |
241 | } | |
242 | return null; | |
8da0d3c1 | 243 | } |
244 | ||
245 | static public function listSent() | |
eaf30d86 | 246 | { |
b35106ba | 247 | $res = XDB::query("SELECT IF(short_name IS NULL, id, short_name) as id, date, subject AS titre |
8da0d3c1 | 248 | FROM axletter |
5b46c60b | 249 | WHERE NOT FIND_IN_SET('new', bits) AND NOT FIND_IN_SET('invalid', bits) |
8da0d3c1 | 250 | ORDER BY date DESC"); |
251 | return $res->fetchAllAssoc(); | |
252 | } | |
eaf30d86 | 253 | |
8da0d3c1 | 254 | static public function listAll() |
eaf30d86 | 255 | { |
b35106ba | 256 | $res = XDB::query("SELECT IF(short_name IS NULL, id, short_name) as id, date, subject AS titre |
8da0d3c1 | 257 | FROM axletter |
258 | ORDER BY date DESC"); | |
259 | return $res->fetchAllAssoc(); | |
260 | } | |
261 | } | |
262 | ||
a7de4ef7 | 263 | // vim:set et sw=4 sts=4 sws=4 enc=utf-8: |
8da0d3c1 | 264 | ?> |