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