2 /***************************************************************************
3 * Copyright (C) 2003-2009 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
36 function __construct($id)
38 parent
::__construct('axletter/letter.mail.tpl', 'ax.css', 'ax/show', 'axletter', 'axletter_ins');
39 $this->_head
= '<cher> <prenom>,';
43 $res = XDB
::query("SELECT *
45 WHERE FIND_IN_SET('sent', bits)
48 $res = XDB
::query("SELECT *
50 WHERE id = {?} OR short_name = {?}", $id, $id);
52 if (!$res->numRows()) {
53 throw new MailNotFound();
55 $id = $res->fetchOneRow();
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') {
63 $this->_subset_to
= explode("\n", $this->_subset_to
);
64 $this->_subset
= (count($this->_subset_to
) > 0);
67 protected function assignData(&$smarty)
69 $smarty->assign_by_ref('am', $this);
72 public function body($format)
74 return format_text($this->_body
, $format);
77 public function signature($format)
79 return format_text($this->_signature
, $format, 10);
82 public function valid()
84 return XDB
::execute("UPDATE axletter
86 WHERE id = {?}", $this->_id
);
89 public function invalid()
91 return XDB
::execute("UPDATE axletter
92 SET bits = 'invalid', date = CURDATE()
93 WHERE id = {?}", $this->_id
);
96 protected function setSent()
98 XDB
::execute("UPDATE axletter
99 SET bits='sent', date=CURDATE()
100 WHERE id={?}", $this->_id
);
103 static public function subscriptionState($uid = null
)
105 $user = is_null($uid) ? S
::v('uid') : $uid;
106 $res = XDB
::query("SELECT 1
108 WHERE user_id={?}", $user);
109 return $res->fetchOneCell();
112 static public function unsubscribe($uid = null
, $hash = false
)
114 $user = is_null($uid) ? S
::v('uid') : $uid;
115 $field = !$hash ?
'user_id' : 'hash';
116 if (is_null($uid) && $hash) {
119 $res = XDB
::query("SELECT *
121 WHERE $field={?}", $user);
122 if (!$res->numRows()) {
125 XDB
::execute("DELETE FROM axletter_ins
126 WHERE $field = {?}", $user);
130 static public function subscribe($uid = null
)
132 $user = is_null($uid) ? S
::v('uid') : $uid;
133 XDB
::execute("REPLACE INTO axletter_ins (user_id,last)
134 VALUES ({?}, 0)", $user);
137 static public function hasPerms()
142 $res = XDB
::query("SELECT COUNT(*)
144 WHERE user_id = {?}", S
::i('uid'));
145 return ($res->fetchOneCell() > 0);
148 static public function grantPerms($uid)
150 if (!is_numeric($uid)) {
151 $res = XDB
::query("SELECT id FROM aliases WHERE alias = {?}", $uid);
152 $uid = $res->fetchOneCell();
157 return XDB
::execute("INSERT IGNORE INTO axletter_rights SET user_id = {?}", $uid);
160 static public function revokePerms($uid)
162 if (!is_numeric($uid)) {
163 $res = XDB
::query("SELECT id FROM aliases WHERE alias = {?}", $uid);
164 $uid = $res->fetchOneCell();
169 return XDB
::execute("DELETE FROM axletter_rights WHERE user_id = {?}", $uid);
172 protected function subscriptionWhere()
174 if (!$this->_promo_min
&& !$this->_promo_max
&& !$this->_subset
) {
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}))";
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}))";
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)";
195 return implode(' AND ', $where);
198 static public function awaiting()
200 $res = XDB
::query("SELECT *
202 WHERE FIND_IN_SET('new', bits)");
203 if ($res->numRows()) {
204 return new AXLetter($res->fetchOneRow());
209 static public function toSend()
211 $res = XDB
::query("SELECT *
213 WHERE FIND_IN_SET('new', bits) AND echeance <= NOW() AND echeance != 0");
214 if ($res->numRows()) {
215 return new AXLetter($res->fetchOneRow());
220 static public function listSent()
222 $res = XDB
::query("SELECT IF(short_name IS NULL, id, short_name) as id, date, subject AS titre
224 WHERE NOT FIND_IN_SET('new', bits) AND NOT FIND_IN_SET('invalid', bits)
225 ORDER BY date DESC");
226 return $res->fetchAllAssoc();
229 static public function listAll()
231 $res = XDB
::query("SELECT IF(short_name IS NULL, id, short_name) as id, date, subject AS titre
233 ORDER BY date DESC");
234 return $res->fetchAllAssoc();
238 // vim:set et sw=4 sts=4 sws=4 enc=utf-8: