2 /***************************************************************************
3 * Copyright (C) 2003-2010 Polytechnique.org *
4 * http://opensource.polytechnique.org/ *
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. *
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. *
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 *
19 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA *
20 ***************************************************************************/
22 require_once("massmailer.inc.php");
24 class AXLetter
extends MassMailer
37 function __construct($id)
39 parent
::__construct('axletter/letter.mail.tpl', 'ax.css', 'ax/show', 'axletter', 'axletter_ins');
40 $this->_head
= '<cher> <prenom>,';
44 $res = XDB
::query("SELECT *
46 WHERE FIND_IN_SET('sent', bits)
49 $res = XDB
::query("SELECT *
51 WHERE id = {?} OR short_name = {?}", $id, $id);
53 if (!$res->numRows()) {
54 throw new MailNotFound();
56 $id = $res->fetchOneRow();
58 list($this->_id
, $this->_shortname
, $this->_title_mail
, $this->_title
,
59 $this->_body
, $this->_signature
, $this->_promo_min
, $this->_promo_max
,
60 $this->_subset_to
, $this->_subset_rm
, $this->_echeance
, $this->_date
, $this->_bits
) = $id;
61 if ($this->_date
== '0000-00-00') {
64 $this->_subset_to
= ($this->_subset_to ?
explode("\n", $this->_subset_to
) : null
);
65 $this->_subset
= (count($this->_subset_to
) > 0);
68 protected function assignData(&$smarty)
70 $smarty->assign_by_ref('am', $this);
73 public function body($format)
75 return format_text($this->_body
, $format);
78 public function signature($format)
80 return format_text($this->_signature
, $format, 10);
83 public function valid()
85 return XDB
::execute("UPDATE axletter
87 WHERE id = {?}", $this->_id
);
90 public function invalid()
92 return XDB
::execute("UPDATE axletter
93 SET bits = 'invalid', date = CURDATE()
94 WHERE id = {?}", $this->_id
);
97 protected function setSent()
99 XDB
::execute("UPDATE axletter
100 SET bits='sent', date=CURDATE()
101 WHERE id={?}", $this->_id
);
104 static public function subscriptionState($uid = null
)
106 $user = is_null($uid) ? S
::v('uid') : $uid;
107 $res = XDB
::query("SELECT 1
109 WHERE uid={?}", $user);
110 return $res->fetchOneCell();
113 static public function unsubscribe($uid = null
, $hash = false
)
115 $user = is_null($uid) ? S
::v('uid') : $uid;
116 $field = !$hash ?
'uid' : 'hash';
117 if (is_null($uid) && $hash) {
120 $res = XDB
::query("SELECT uid
122 WHERE $field={?}", $user);
123 if ($res->numRows() != 1) {
126 XDB
::execute("DELETE FROM axletter_ins
127 WHERE $field = {?}", $user);
131 static public function subscribe($uid = null
)
133 $user = is_null($uid) ? S
::v('uid') : $uid;
134 XDB
::execute("REPLACE INTO axletter_ins (uid,last)
135 VALUES ({?}, 0)", $user);
138 static public function hasPerms()
143 $res = XDB
::query("SELECT COUNT(*)
145 WHERE uid = {?}", S
::i('uid'));
146 return ($res->fetchOneCell() > 0);
149 static public function grantPerms($uid)
151 if (!is_numeric($uid)) {
152 $res = XDB
::query("SELECT uid FROM aliases WHERE alias = {?}", $uid);
153 $uid = $res->fetchOneCell();
158 return XDB
::execute("INSERT IGNORE INTO axletter_rights SET uid = {?}", $uid);
161 static public function revokePerms($uid)
163 if (!is_numeric($uid)) {
164 $res = XDB
::query("SELECT uid FROM aliases WHERE alias = {?}", $uid);
165 $uid = $res->fetchOneCell();
170 return XDB
::execute("DELETE FROM axletter_rights WHERE uid = {?}", $uid);
173 protected function subscriptionWhere()
175 if (!$this->_promo_min
&& !$this->_promo_max
&& !$this->_subset
) {
179 if ($this->_promo_min
) {
180 $where[] = "((ni.uid = 0 AND ni.promo >= {$this->_promo_min}) OR (ni.uid != 0 AND u.promo >= {$this->_promo_min}))";
182 if ($this->_promo_max
) {
183 $where[] = "((ni.uid = 0 AND ni.promo <= {$this->_promo_max}) OR (ni.uid != 0 AND u.promo <= {$this->_promo_max}))";
185 if ($this->_subset
) {
186 require_once("emails.inc.php");
187 $ids = ids_from_mails($this->_subset_to
);
188 $ids_list = implode(',', $ids);
189 if(count($ids) > 0) {
190 if ($this->_subset_rm
) {
191 $where[] = "ni.uid NOT IN ($ids_list)";
193 $where[] = "ni.uid IN ($ids_list)";
200 return implode(' AND ', $where);
203 static public function awaiting()
205 $res = XDB
::query("SELECT *
207 WHERE FIND_IN_SET('new', bits)");
208 if ($res->numRows()) {
209 return new AXLetter($res->fetchOneRow());
214 static public function toSend()
216 $res = XDB
::query("SELECT *
218 WHERE FIND_IN_SET('new', bits) AND echeance <= NOW() AND echeance != 0");
219 if ($res->numRows()) {
220 return new AXLetter($res->fetchOneRow());
225 static public function listSent()
227 $res = XDB
::query("SELECT IF(short_name IS NULL, id, short_name) as id, date, subject AS titre
229 WHERE NOT FIND_IN_SET('new', bits) AND NOT FIND_IN_SET('invalid', bits)
230 ORDER BY date DESC");
231 return $res->fetchAllAssoc();
234 static public function listAll()
236 $res = XDB
::query("SELECT IF(short_name IS NULL, id, short_name) as id, date, subject AS titre
238 ORDER BY date DESC");
239 return $res->fetchAllAssoc();
243 // vim:set et sw=4 sts=4 sws=4 enc=utf-8: