Replaces the remaining Content-Type headers with calls to pl_content_header*.
[platal.git] / modules / axletter / axletter.inc.php
CommitLineData
8da0d3c1 1<?php
2/***************************************************************************
9f5bd98e 3 * Copyright (C) 2003-2010 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;
1f58ab8e 32 public $_subset_rm;
8da0d3c1 33 public $_echeance;
34 public $_date;
35 public $_bits;
36
37 function __construct($id)
38 {
b71f7275 39 parent::__construct('axletter/letter.mail.tpl', 'ax.css', 'ax/show', 'axletter', 'axletter_ins');
8da0d3c1 40 $this->_head = '<cher> <prenom>,';
41
42 if (!is_array($id)) {
43 if ($id == 'last') {
44 $res = XDB::query("SELECT *
45 FROM axletter
46 WHERE FIND_IN_SET('sent', bits)
47 ORDER BY id DESC");
48 } else {
49 $res = XDB::query("SELECT *
50 FROM axletter
b35106ba 51 WHERE id = {?} OR short_name = {?}", $id, $id);
8da0d3c1 52 }
53 if (!$res->numRows()) {
4a71cf67 54 throw new MailNotFound();
8da0d3c1 55 }
56 $id = $res->fetchOneRow();
57 }
58 list($this->_id, $this->_shortname, $this->_title_mail, $this->_title,
59 $this->_body, $this->_signature, $this->_promo_min, $this->_promo_max,
1f58ab8e 60 $this->_subset_to, $this->_subset_rm, $this->_echeance, $this->_date, $this->_bits) = $id;
9b242f17 61 if ($this->_date == '0000-00-00') {
62 $this->_date = 0;
63 }
c1134ef2 64 $this->_subset_to = ($this->_subset_to ? explode("\n", $this->_subset_to) : null);
1605f171 65 $this->_subset = (count($this->_subset_to) > 0);
8da0d3c1 66 }
67
68 protected function assignData(&$smarty)
69 {
70 $smarty->assign_by_ref('am', $this);
71 }
72
73 public function body($format)
74 {
75 return format_text($this->_body, $format);
76 }
77
78 public function signature($format)
79 {
80 return format_text($this->_signature, $format, 10);
81 }
82
8da0d3c1 83 public function valid()
84 {
85 return XDB::execute("UPDATE axletter
86 SET echeance = NOW()
87 WHERE id = {?}", $this->_id);
88 }
89
90 public function invalid()
91 {
92 return XDB::execute("UPDATE axletter
93 SET bits = 'invalid', date = CURDATE()
94 WHERE id = {?}", $this->_id);
95 }
96
97 protected function setSent()
98 {
99 XDB::execute("UPDATE axletter
100 SET bits='sent', date=CURDATE()
101 WHERE id={?}", $this->_id);
102 }
103
104 static public function subscriptionState($uid = null)
105 {
106 $user = is_null($uid) ? S::v('uid') : $uid;
107 $res = XDB::query("SELECT 1
108 FROM axletter_ins
c0c4edb5 109 WHERE uid={?}", $user);
8da0d3c1 110 return $res->fetchOneCell();
eaf30d86
PH
111 }
112
a0f05027 113 static public function unsubscribe($uid = null, $hash = false)
8da0d3c1 114 {
115 $user = is_null($uid) ? S::v('uid') : $uid;
c0c4edb5 116 $field = !$hash ? 'uid' : 'hash';
a0f05027 117 if (is_null($uid) && $hash) {
118 return false;
119 }
120 $res = XDB::query("SELECT *
121 FROM axletter_ins
122 WHERE $field={?}", $user);
123 if (!$res->numRows()) {
124 return false;
125 }
8da0d3c1 126 XDB::execute("DELETE FROM axletter_ins
a0f05027 127 WHERE $field = {?}", $user);
128 return true;
8da0d3c1 129 }
130
131 static public function subscribe($uid = null)
132 {
133 $user = is_null($uid) ? S::v('uid') : $uid;
c0c4edb5 134 XDB::execute("REPLACE INTO axletter_ins (uid,last)
8da0d3c1 135 VALUES ({?}, 0)", $user);
136 }
137
138 static public function hasPerms()
139 {
dd70cd28 140 if (S::admin()) {
8da0d3c1 141 return true;
142 }
40b6aa0f 143 $res = XDB::query("SELECT COUNT(*)
8da0d3c1 144 FROM axletter_rights
c0c4edb5 145 WHERE uid = {?}", S::i('uid'));
40b6aa0f 146 return ($res->fetchOneCell() > 0);
8da0d3c1 147 }
148
0d75939a 149 static public function grantPerms($uid)
150 {
151 if (!is_numeric($uid)) {
fe13bc1d 152 $res = XDB::query("SELECT uid FROM aliases WHERE alias = {?}", $uid);
0d75939a 153 $uid = $res->fetchOneCell();
154 }
155 if (!$uid) {
156 return false;
157 }
c0c4edb5 158 return XDB::execute("INSERT IGNORE INTO axletter_rights SET uid = {?}", $uid);
0d75939a 159 }
160
161 static public function revokePerms($uid)
162 {
163 if (!is_numeric($uid)) {
fe13bc1d 164 $res = XDB::query("SELECT uid FROM aliases WHERE alias = {?}", $uid);
0d75939a 165 $uid = $res->fetchOneCell();
eaf30d86 166 }
0d75939a 167 if (!$uid) {
168 return false;
169 }
c0c4edb5 170 return XDB::execute("DELETE FROM axletter_rights WHERE uid = {?}", $uid);
0d75939a 171 }
172
8da0d3c1 173 protected function subscriptionWhere()
174 {
1605f171 175 if (!$this->_promo_min && !$this->_promo_max && !$this->_subset) {
4e25428d 176 return '1';
177 }
178 $where = array();
179 if ($this->_promo_min) {
c0c4edb5 180 $where[] = "((ni.uid = 0 AND ni.promo >= {$this->_promo_min}) OR (ni.uid != 0 AND u.promo >= {$this->_promo_min}))";
4e25428d 181 }
182 if ($this->_promo_max) {
c0c4edb5 183 $where[] = "((ni.uid = 0 AND ni.promo <= {$this->_promo_max}) OR (ni.uid != 0 AND u.promo <= {$this->_promo_max}))";
4e25428d 184 }
1605f171
RB
185 if ($this->_subset) {
186 require_once("emails.inc.php");
180627f3 187 $ids = ids_from_mails($this->_subset_to);
1605f171 188 $ids_list = implode(',', $ids);
c1134ef2 189 if(count($ids) > 0) {
1f58ab8e 190 if ($this->_subset_rm) {
c0c4edb5 191 $where[] = "ni.uid NOT IN ($ids_list)";
1f58ab8e 192 } else {
c0c4edb5 193 $where[] = "ni.uid IN ($ids_list)";
1f58ab8e 194 }
f0b77ba6
RB
195 } else {
196 // No valid email
197 $where[] = "0";
198 }
1605f171 199 }
4e25428d 200 return implode(' AND ', $where);
8da0d3c1 201 }
202
203 static public function awaiting()
204 {
a0f05027 205 $res = XDB::query("SELECT *
8da0d3c1 206 FROM axletter
207 WHERE FIND_IN_SET('new', bits)");
a0f05027 208 if ($res->numRows()) {
209 return new AXLetter($res->fetchOneRow());
210 }
211 return null;
212 }
213
214 static public function toSend()
215 {
216 $res = XDB::query("SELECT *
217 FROM axletter
218 WHERE FIND_IN_SET('new', bits) AND echeance <= NOW() AND echeance != 0");
219 if ($res->numRows()) {
220 return new AXLetter($res->fetchOneRow());
221 }
222 return null;
8da0d3c1 223 }
224
225 static public function listSent()
eaf30d86 226 {
b35106ba 227 $res = XDB::query("SELECT IF(short_name IS NULL, id, short_name) as id, date, subject AS titre
8da0d3c1 228 FROM axletter
5b46c60b 229 WHERE NOT FIND_IN_SET('new', bits) AND NOT FIND_IN_SET('invalid', bits)
8da0d3c1 230 ORDER BY date DESC");
231 return $res->fetchAllAssoc();
232 }
eaf30d86 233
8da0d3c1 234 static public function listAll()
eaf30d86 235 {
b35106ba 236 $res = XDB::query("SELECT IF(short_name IS NULL, id, short_name) as id, date, subject AS titre
8da0d3c1 237 FROM axletter
238 ORDER BY date DESC");
239 return $res->fetchAllAssoc();
240 }
241}
242
a7de4ef7 243// vim:set et sw=4 sts=4 sws=4 enc=utf-8:
8da0d3c1 244?>