Removes the call to get_user_forlife in massmailer, and updates dependencies of metho...
[platal.git] / modules / axletter / axletter.inc.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2008 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.mail.tpl', 'ax.css', 'ax/show', 'axletter', 'axletter_ins');
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 short_name = {?}", $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 if ($this->_date == '0000-00-00') {
60 $this->_date = 0;
61 }
62 }
63
64 protected function assignData(&$smarty)
65 {
66 $smarty->assign_by_ref('am', $this);
67 }
68
69 public function body($format)
70 {
71 return format_text($this->_body, $format);
72 }
73
74 public function signature($format)
75 {
76 return format_text($this->_signature, $format, 10);
77 }
78
79 public function valid()
80 {
81 return XDB::execute("UPDATE axletter
82 SET echeance = NOW()
83 WHERE id = {?}", $this->_id);
84 }
85
86 public function invalid()
87 {
88 return XDB::execute("UPDATE axletter
89 SET bits = 'invalid', date = CURDATE()
90 WHERE id = {?}", $this->_id);
91 }
92
93 protected function setSent()
94 {
95 XDB::execute("UPDATE axletter
96 SET bits='sent', date=CURDATE()
97 WHERE id={?}", $this->_id);
98 }
99
100 protected function getAllRecipients()
101 {
102 global $globals;
103 return "SELECT ni.user_id, IF(ni.user_id = 0, NULL, u.hruid) AS hruid,
104 IF(ni.user_id = 0, ni.email, CONCAT(a.alias, '@{$globals->mail->domain}')) AS alias,
105 IF(ni.user_id = 0, ni.prenom, u.prenom) AS prenom,
106 IF(ni.user_id = 0, ni.nom, IF(u.nom_usage='', u.nom, u.nom_usage)) AS nom,
107 FIND_IN_SET('femme', IF(ni.user_id = 0, ni.flag, u.flags)) AS sexe,
108 IF(ni.user_id = 0, 'html', q.core_mail_fmt) AS pref,
109 IF(ni.user_id = 0, ni.hash, 0) AS hash
110 FROM axletter_ins AS ni
111 LEFT JOIN auth_user_md5 AS u USING(user_id)
112 LEFT JOIN auth_user_quick AS q ON(q.user_id = u.user_id)
113 LEFT JOIN aliases AS a ON(u.user_id=a.id AND FIND_IN_SET('bestalias',a.flags))
114 LEFT JOIN emails AS e ON(e.uid=u.user_id AND e.flags='active')
115 WHERE ni.last < {?} AND {$this->subscriptionWhere()}
116 AND (e.email IS NOT NULL OR FIND_IN_SET('googleapps', u.mail_storage) OR ni.user_id = 0)
117 GROUP BY u.user_id";
118 }
119
120 static public function subscriptionState($uid = null)
121 {
122 $user = is_null($uid) ? S::v('uid') : $uid;
123 $res = XDB::query("SELECT 1
124 FROM axletter_ins
125 WHERE user_id={?}", $user);
126 return $res->fetchOneCell();
127 }
128
129 static public function unsubscribe($uid = null, $hash = false)
130 {
131 $user = is_null($uid) ? S::v('uid') : $uid;
132 $field = !$hash ? 'user_id' : 'hash';
133 if (is_null($uid) && $hash) {
134 return false;
135 }
136 $res = XDB::query("SELECT *
137 FROM axletter_ins
138 WHERE $field={?}", $user);
139 if (!$res->numRows()) {
140 return false;
141 }
142 XDB::execute("DELETE FROM axletter_ins
143 WHERE $field = {?}", $user);
144 return true;
145 }
146
147 static public function subscribe($uid = null)
148 {
149 $user = is_null($uid) ? S::v('uid') : $uid;
150 XDB::execute("REPLACE INTO axletter_ins (user_id,last)
151 VALUES ({?}, 0)", $user);
152 }
153
154 static public function hasPerms()
155 {
156 if (S::has_perms()) {
157 return true;
158 }
159 $res = XDB::query("SELECT 1
160 FROM axletter_rights
161 WHERE user_id = {?}", S::i('uid'));
162 return $res->fetchOneCell();
163 }
164
165 static public function grantPerms($uid)
166 {
167 if (!is_numeric($uid)) {
168 $res = XDB::query("SELECT id FROM aliases WHERE alias = {?}", $uid);
169 $uid = $res->fetchOneCell();
170 }
171 if (!$uid) {
172 return false;
173 }
174 return XDB::execute("INSERT IGNORE INTO axletter_rights SET user_id = {?}", $uid);
175 }
176
177 static public function revokePerms($uid)
178 {
179 if (!is_numeric($uid)) {
180 $res = XDB::query("SELECT id FROM aliases WHERE alias = {?}", $uid);
181 $uid = $res->fetchOneCell();
182 }
183 if (!$uid) {
184 return false;
185 }
186 return XDB::execute("DELETE FROM axletter_rights WHERE user_id = {?}", $uid);
187 }
188
189 protected function subscriptionWhere()
190 {
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);
202 }
203
204 static public function awaiting()
205 {
206 $res = XDB::query("SELECT *
207 FROM axletter
208 WHERE FIND_IN_SET('new', bits)");
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;
224 }
225
226 static public function listSent()
227 {
228 $res = XDB::query("SELECT IF(short_name IS NULL, id, short_name) as id, date, subject AS titre
229 FROM axletter
230 WHERE NOT FIND_IN_SET('new', bits) AND NOT FIND_IN_SET('invalid', bits)
231 ORDER BY date DESC");
232 return $res->fetchAllAssoc();
233 }
234
235 static public function listAll()
236 {
237 $res = XDB::query("SELECT IF(short_name IS NULL, id, short_name) 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 enc=utf-8:
245 ?>