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