Merge branch 'master' of /home/git/platal into profile_edit
[platal.git] / modules / axletter / axletter.inc.php
1 <?php
2 /***************************************************************************
3 * Copyright (C) 2003-2007 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.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 shortname = {?}", $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 return "SELECT ni.user_id, IF(ni.user_id = 0, ni.email, a.alias) AS alias,
103 IF(ni.user_id = 0, ni.prenom, u.prenom) AS prenom,
104 IF(ni.user_id = 0, ni.nom, IF(u.nom_usage='', u.nom, u.nom_usage)) AS nom,
105 FIND_IN_SET('femme', IF(ni.user_id = 0, ni.flag, u.flags)) AS sexe,
106 IF(ni.user_id = 0, 'html', q.core_mail_fmt) AS pref,
107 IF(ni.user_id = 0, ni.hash, 0) AS hash
108 FROM axletter_ins AS ni
109 LEFT JOIN auth_user_md5 AS u USING(user_id)
110 LEFT JOIN auth_user_quick AS q ON(q.user_id = u.user_id)
111 LEFT JOIN aliases AS a ON(u.user_id=a.id AND FIND_IN_SET('bestalias',a.flags))
112 LEFT JOIN emails AS e ON(e.uid=u.user_id AND e.flags='active')
113 WHERE ni.last < {?} AND {$this->subscriptionWhere()}
114 AND (e.email IS NOT NULL OR ni.user_id = 0)
115 GROUP BY u.user_id";
116 }
117
118 static public function subscriptionState($uid = null)
119 {
120 $user = is_null($uid) ? S::v('uid') : $uid;
121 $res = XDB::query("SELECT 1
122 FROM axletter_ins
123 WHERE user_id={?}", $user);
124 return $res->fetchOneCell();
125 }
126
127 static public function unsubscribe($uid = null, $hash = false)
128 {
129 $user = is_null($uid) ? S::v('uid') : $uid;
130 $field = !$hash ? 'user_id' : 'hash';
131 if (is_null($uid) && $hash) {
132 return false;
133 }
134 $res = XDB::query("SELECT *
135 FROM axletter_ins
136 WHERE $field={?}", $user);
137 if (!$res->numRows()) {
138 return false;
139 }
140 XDB::execute("DELETE FROM axletter_ins
141 WHERE $field = {?}", $user);
142 return true;
143 }
144
145 static public function subscribe($uid = null)
146 {
147 $user = is_null($uid) ? S::v('uid') : $uid;
148 XDB::execute("REPLACE INTO axletter_ins (user_id,last)
149 VALUES ({?}, 0)", $user);
150 }
151
152 static public function hasPerms()
153 {
154 if (S::has_perms()) {
155 return true;
156 }
157 $res = XDB::query("SELECT 1
158 FROM axletter_rights
159 WHERE user_id = {?}", S::i('uid'));
160 return $res->fetchOneCell();
161 }
162
163 static public function grantPerms($uid)
164 {
165 if (!is_numeric($uid)) {
166 $res = XDB::query("SELECT id FROM aliases WHERE alias = {?}", $uid);
167 $uid = $res->fetchOneCell();
168 }
169 if (!$uid) {
170 return false;
171 }
172 return XDB::execute("INSERT IGNORE INTO axletter_rights SET user_id = {?}", $uid);
173 }
174
175 static public function revokePerms($uid)
176 {
177 if (!is_numeric($uid)) {
178 $res = XDB::query("SELECT id FROM aliases WHERE alias = {?}", $uid);
179 $uid = $res->fetchOneCell();
180 }
181 if (!$uid) {
182 return false;
183 }
184 return XDB::execute("DELETE FROM axletter_rights WHERE user_id = {?}", $uid);
185 }
186
187 protected function subscriptionWhere()
188 {
189 if (!$this->_promo_min && !$this->_promo_max) {
190 return '1';
191 }
192 $where = array();
193 if ($this->_promo_min) {
194 $where[] = "((ni.user_id = 0 AND ni.promo >= {$this->_promo_min}) OR (ni.user_id != 0 AND u.promo >= {$this->_promo_min}))";
195 }
196 if ($this->_promo_max) {
197 $where[] = "((ni.user_id = 0 AND ni.promo <= {$this->_promo_max}) OR (ni.user_id != 0 AND u.promo <= {$this->_promo_max}))";
198 }
199 return implode(' AND ', $where);
200 }
201
202 static public function awaiting()
203 {
204 $res = XDB::query("SELECT *
205 FROM axletter
206 WHERE FIND_IN_SET('new', bits)");
207 if ($res->numRows()) {
208 return new AXLetter($res->fetchOneRow());
209 }
210 return null;
211 }
212
213 static public function toSend()
214 {
215 $res = XDB::query("SELECT *
216 FROM axletter
217 WHERE FIND_IN_SET('new', bits) AND echeance <= NOW() AND echeance != 0");
218 if ($res->numRows()) {
219 return new AXLetter($res->fetchOneRow());
220 }
221 return null;
222 }
223
224 static public function listSent()
225 {
226 $res = XDB::query("SELECT IF(shortname IS NULL, id, shortname) as id, date, subject AS titre
227 FROM axletter
228 WHERE NOT (FIND_IN_SET('new', bits))
229 ORDER BY date DESC");
230 return $res->fetchAllAssoc();
231 }
232
233 static public function listAll()
234 {
235 $res = XDB::query("SELECT IF(shortname IS NULL, id, shortname) as id, date, subject AS titre
236 FROM axletter
237 ORDER BY date DESC");
238 return $res->fetchAllAssoc();
239 }
240 }
241
242 // vim:set et sw=4 sts=4 sws=4 enc=utf-8:
243 ?>