Fixes axletter.
[platal.git] / modules / axletter / axletter.inc.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2009 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 $_subset;
31 public $_subset_to;
32 public $_echeance;
33 public $_date;
34 public $_bits;
35
36 function __construct($id)
37 {
38 parent::__construct('axletter/letter.mail.tpl', 'ax.css', 'ax/show', 'axletter', 'axletter_ins');
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
50 WHERE id = {?} OR short_name = {?}", $id, $id);
51 }
52 if (!$res->numRows()) {
53 throw new MailNotFound();
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,
59 $this->_subset_to, $this->_echeance, $this->_date, $this->_bits) = $id;
60 if ($this->_date == '0000-00-00') {
61 $this->_date = 0;
62 }
63 $this->_subset_to = ($this->_subset_to ? explode("\n", $this->_subset_to) : null);
64 $this->_subset = (count($this->_subset_to) > 0);
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
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
103 protected function getAllRecipients()
104 {
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,
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')
118 WHERE ni.last < {?} AND {$this->subscriptionWhere()}
119 AND (e.email IS NOT NULL OR FIND_IN_SET('googleapps', u.mail_storage) OR ni.user_id = 0)
120 GROUP BY u.user_id";
121 }
122
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();
130 }
131
132 static public function unsubscribe($uid = null, $hash = false)
133 {
134 $user = is_null($uid) ? S::v('uid') : $uid;
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 }
145 XDB::execute("DELETE FROM axletter_ins
146 WHERE $field = {?}", $user);
147 return true;
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 {
159 if (S::admin()) {
160 return true;
161 }
162 $res = XDB::query("SELECT COUNT(*)
163 FROM axletter_rights
164 WHERE user_id = {?}", S::i('uid'));
165 return ($res->fetchOneCell() > 0);
166 }
167
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();
185 }
186 if (!$uid) {
187 return false;
188 }
189 return XDB::execute("DELETE FROM axletter_rights WHERE user_id = {?}", $uid);
190 }
191
192 protected function subscriptionWhere()
193 {
194 if (!$this->_promo_min && !$this->_promo_max && !$this->_subset) {
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 }
204 if ($this->_subset) {
205 require_once("emails.inc.php");
206 $ids = ids_from_mails($this->_subset_to);
207 $ids_list = implode(',', $ids);
208 if(count($ids) > 0) {
209 $where[] = "ni.user_id IN ($ids_list)";
210 } else {
211 // No valid email
212 $where[] = "0";
213 }
214 }
215 return implode(' AND ', $where);
216 }
217
218 static public function awaiting()
219 {
220 $res = XDB::query("SELECT *
221 FROM axletter
222 WHERE FIND_IN_SET('new', bits)");
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;
238 }
239
240 static public function listSent()
241 {
242 $res = XDB::query("SELECT IF(short_name IS NULL, id, short_name) as id, date, subject AS titre
243 FROM axletter
244 WHERE NOT FIND_IN_SET('new', bits) AND NOT FIND_IN_SET('invalid', bits)
245 ORDER BY date DESC");
246 return $res->fetchAllAssoc();
247 }
248
249 static public function listAll()
250 {
251 $res = XDB::query("SELECT IF(short_name IS NULL, id, short_name) as id, date, subject AS titre
252 FROM axletter
253 ORDER BY date DESC");
254 return $res->fetchAllAssoc();
255 }
256 }
257
258 // vim:set et sw=4 sts=4 sws=4 enc=utf-8:
259 ?>