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