Improve mailings to subsets
[platal.git] / modules / axletter / axletter.inc.php
CommitLineData
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
22require_once("massmailer.inc.php");
23
24class 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 }
1605f171
RB
63 $this->_subset_to = preg_split("/\n/", $this->_subset_to);
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
2a3f4f57
RB
192 protected function subsetJoin()
193 {
194 if ($this->_subset) {
4a08dc73 195 return "INNER JOIN axletter_subsets AS c ON (c.letter_id = ".XDB::escape($this->_id)." AND ni.user_id = c.uid)";
2a3f4f57 196 }
4a08dc73 197 return '';
2a3f4f57
RB
198 }
199
8da0d3c1 200 protected function subscriptionWhere()
201 {
1605f171 202 if (!$this->_promo_min && !$this->_promo_max && !$this->_subset) {
4e25428d 203 return '1';
204 }
205 $where = array();
206 if ($this->_promo_min) {
207 $where[] = "((ni.user_id = 0 AND ni.promo >= {$this->_promo_min}) OR (ni.user_id != 0 AND u.promo >= {$this->_promo_min}))";
208 }
209 if ($this->_promo_max) {
210 $where[] = "((ni.user_id = 0 AND ni.promo <= {$this->_promo_max}) OR (ni.user_id != 0 AND u.promo <= {$this->_promo_max}))";
211 }
1605f171
RB
212 if ($this->_subset) {
213 require_once("emails.inc.php");
214 print_r($this->_subset_to);
215 $ids = idsFromMails($this->_subset_to);
216 print_r($ids);
217 $ids_list = implode(',', $ids);
218 $where[] = "ni.user_id IN ($ids_list)";
219 // TODO : force use of the adresses given by AX, not "canonical" ones ?
220 }
4e25428d 221 return implode(' AND ', $where);
8da0d3c1 222 }
223
224 static public function awaiting()
225 {
a0f05027 226 $res = XDB::query("SELECT *
8da0d3c1 227 FROM axletter
228 WHERE FIND_IN_SET('new', bits)");
a0f05027 229 if ($res->numRows()) {
230 return new AXLetter($res->fetchOneRow());
231 }
232 return null;
233 }
234
235 static public function toSend()
236 {
237 $res = XDB::query("SELECT *
238 FROM axletter
239 WHERE FIND_IN_SET('new', bits) AND echeance <= NOW() AND echeance != 0");
240 if ($res->numRows()) {
241 return new AXLetter($res->fetchOneRow());
242 }
243 return null;
8da0d3c1 244 }
245
246 static public function listSent()
eaf30d86 247 {
b35106ba 248 $res = XDB::query("SELECT IF(short_name IS NULL, id, short_name) as id, date, subject AS titre
8da0d3c1 249 FROM axletter
5b46c60b 250 WHERE NOT FIND_IN_SET('new', bits) AND NOT FIND_IN_SET('invalid', bits)
8da0d3c1 251 ORDER BY date DESC");
252 return $res->fetchAllAssoc();
253 }
eaf30d86 254
8da0d3c1 255 static public function listAll()
eaf30d86 256 {
b35106ba 257 $res = XDB::query("SELECT IF(short_name IS NULL, id, short_name) as id, date, subject AS titre
8da0d3c1 258 FROM axletter
259 ORDER BY date DESC");
260 return $res->fetchAllAssoc();
261 }
262}
263
a7de4ef7 264// vim:set et sw=4 sts=4 sws=4 enc=utf-8:
8da0d3c1 265?>