2 /***************************************************************************
3 * Copyright (C) 2003-2007 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
34 function __construct($id)
36 parent
::__construct('axletter/letter.tpl', 'ax.css', 'ax/show');
37 $this->_head
= '<cher> <prenom>,';
41 $res = XDB
::query("SELECT *
43 WHERE FIND_IN_SET('sent', bits)
46 $res = XDB
::query("SELECT *
48 WHERE id = {?} OR shortname = {?}", $id, $id);
50 if (!$res->numRows()) {
54 $id = $res->fetchOneRow();
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;
61 protected function assignData(&$smarty)
63 $smarty->assign_by_ref('am', $this);
66 public function body($format)
68 return format_text($this->_body
, $format);
71 public function signature($format)
73 return format_text($this->_signature
, $format, 10);
76 public function valid()
78 return XDB
::execute("UPDATE axletter
80 WHERE id = {?}", $this->_id
);
83 public function invalid()
85 return XDB
::execute("UPDATE axletter
86 SET bits = 'invalid', date = CURDATE()
87 WHERE id = {?}", $this->_id
);
90 protected function setSent()
92 XDB
::execute("UPDATE axletter
93 SET bits='sent', date=CURDATE()
94 WHERE id={?}", $this->_id
);
97 protected function getAllRecipients()
99 return "SELECT ni.user_id, IF(ni.user_id = 0, ni.email, a.alias) AS alias,
100 IF(ni.user_id = 0, ni.prenom, u.prenom) AS prenom,
101 IF(ni.user_id = 0, ni.nom, IF(u.nom_usage='', u.nom, u.nom_usage)) AS nom,
102 FIND_IN_SET('femme', IF(ni.user_id = 0, ni.flag, u.flags)) AS sexe,
103 IF(ni.user_id = 0, 'html', q.core_mail_fmt) AS pref,
104 IF(ni.user_id = 0, ni.hash, 0) AS hash
105 FROM axletter_ins AS ni
106 LEFT JOIN auth_user_md5 AS u USING(user_id)
107 LEFT JOIN auth_user_quick AS q ON(q.user_id = u.user_id)
108 LEFT JOIN aliases AS a ON(u.user_id=a.id AND FIND_IN_SET('bestalias',a.flags))
109 LEFT JOIN emails AS e ON(e.uid=u.user_id AND e.flags='active')
110 WHERE ni.last < {?} AND {$this->subscriptionWhere()}
111 AND (e.email IS NOT NULL OR ni.user_id = 0)
115 static public function subscriptionState($uid = null
)
117 $user = is_null($uid) ? S
::v('uid') : $uid;
118 $res = XDB
::query("SELECT 1
120 WHERE user_id={?}", $user);
121 return $res->fetchOneCell();
124 static public function unsubscribe($uid = null
, $hash = false
)
126 $user = is_null($uid) ? S
::v('uid') : $uid;
127 $field = !$hash ?
'user_id' : 'hash';
128 if (is_null($uid) && $hash) {
131 $res = XDB
::query("SELECT *
133 WHERE $field={?}", $user);
134 if (!$res->numRows()) {
137 XDB
::execute("DELETE FROM axletter_ins
138 WHERE $field = {?}", $user);
142 static public function subscribe($uid = null
)
144 $user = is_null($uid) ? S
::v('uid') : $uid;
145 XDB
::execute("REPLACE INTO axletter_ins (user_id,last)
146 VALUES ({?}, 0)", $user);
149 static public function hasPerms()
151 if (S
::has_perms()) {
154 $res = XDB
::query("SELECT 1
156 WHERE user_id = {?}", S
::i('uid'));
157 return $res->fetchOneCell();
160 static public function grantPerms($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("INSERT IGNORE INTO axletter_rights SET user_id = {?}", $uid);
172 static public function revokePerms($uid)
174 if (!is_numeric($uid)) {
175 $res = XDB
::query("SELECT id FROM aliases WHERE alias = {?}", $uid);
176 $uid = $res->fetchOneCell();
181 return XDB
::execute("DELETE FROM axletter_rights WHERE user_id = {?}", $uid);
184 protected function subscriptionTable()
186 return 'axletter_ins';
189 protected function subscriptionWhere()
191 if (!$this->_promo_min
&& !$this->_promo_max
) {
195 if ($this->_promo_min
) {
196 $where[] = "((ni.user_id = 0 AND ni.promo >= {$this->_promo_min}) OR (ni.user_id != 0 AND u.promo >= {$this->_promo_min}))";
198 if ($this->_promo_max
) {
199 $where[] = "((ni.user_id = 0 AND ni.promo <= {$this->_promo_max}) OR (ni.user_id != 0 AND u.promo <= {$this->_promo_max}))";
201 return implode(' AND ', $where);
204 static public function awaiting()
206 $res = XDB
::query("SELECT *
208 WHERE FIND_IN_SET('new', bits)");
209 if ($res->numRows()) {
210 return new AXLetter($res->fetchOneRow());
215 static public function toSend()
217 $res = XDB
::query("SELECT *
219 WHERE FIND_IN_SET('new', bits) AND echeance <= NOW() AND echeance != 0");
220 if ($res->numRows()) {
221 return new AXLetter($res->fetchOneRow());
226 static public function listSent()
228 $res = XDB
::query("SELECT IF(shortname IS NULL, id, shortname) as id, date, subject AS titre
230 WHERE NOT (FIND_IN_SET('new', bits))
231 ORDER BY date DESC");
232 return $res->fetchAllAssoc();
235 static public function listAll()
237 $res = XDB
::query("SELECT IF(shortname IS NULL, id, shortname) as id, date, subject AS titre
239 ORDER BY date DESC");
240 return $res->fetchAllAssoc();
244 // vim:set et sw=4 sts=4 sws=4: