3c6cf3d22569ce7fd154c4aec702a9e3e9480e7c
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
= preg_split("/\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 protected function getAllRecipients()
106 return "SELECT ni.user_id, IF(ni.user_id = 0, NULL, u.hruid) AS hruid,
107 IF(ni.user_id = 0, ni.email, CONCAT(a.alias, '@{$globals->mail->domain}')) AS alias,
108 IF(ni.user_id = 0, ni.prenom, u.prenom) AS prenom,
109 IF(ni.user_id = 0, ni.nom, IF(u.nom_usage='', u.nom, u.nom_usage)) AS nom,
110 FIND_IN_SET('femme', IF(ni.user_id = 0, ni.flag, u.flags)) AS sexe,
111 IF(ni.user_id = 0, 'html', q.core_mail_fmt) AS pref,
112 IF(ni.user_id = 0, ni.hash, 0) AS hash
113 FROM axletter_ins AS ni
114 LEFT JOIN auth_user_md5 AS u USING(user_id)
115 LEFT JOIN auth_user_quick AS q ON(q.user_id = u.user_id)
116 LEFT JOIN aliases AS a ON(u.user_id=a.id AND FIND_IN_SET('bestalias',a.flags))
117 LEFT JOIN emails AS e ON(e.uid=u.user_id AND e.flags='active')
118 WHERE ni.last < {?} AND {$this->subscriptionWhere()}
119 AND (e.email IS NOT NULL OR FIND_IN_SET('googleapps', u.mail_storage) OR ni.user_id = 0)
123 static public function subscriptionState($uid = null
)
125 $user = is_null($uid) ? S
::v('uid') : $uid;
126 $res = XDB
::query("SELECT 1
128 WHERE user_id={?}", $user);
129 return $res->fetchOneCell();
132 static public function unsubscribe($uid = null
, $hash = false
)
134 $user = is_null($uid) ? S
::v('uid') : $uid;
135 $field = !$hash ?
'user_id' : 'hash';
136 if (is_null($uid) && $hash) {
139 $res = XDB
::query("SELECT *
141 WHERE $field={?}", $user);
142 if (!$res->numRows()) {
145 XDB
::execute("DELETE FROM axletter_ins
146 WHERE $field = {?}", $user);
150 static public function subscribe($uid = null
)
152 $user = is_null($uid) ? S
::v('uid') : $uid;
153 XDB
::execute("REPLACE INTO axletter_ins (user_id,last)
154 VALUES ({?}, 0)", $user);
157 static public function hasPerms()
162 $res = XDB
::query("SELECT COUNT(*)
164 WHERE user_id = {?}", S
::i('uid'));
165 return ($res->fetchOneCell() > 0);
168 static public function grantPerms($uid)
170 if (!is_numeric($uid)) {
171 $res = XDB
::query("SELECT id FROM aliases WHERE alias = {?}", $uid);
172 $uid = $res->fetchOneCell();
177 return XDB
::execute("INSERT IGNORE INTO axletter_rights SET user_id = {?}", $uid);
180 static public function revokePerms($uid)
182 if (!is_numeric($uid)) {
183 $res = XDB
::query("SELECT id FROM aliases WHERE alias = {?}", $uid);
184 $uid = $res->fetchOneCell();
189 return XDB
::execute("DELETE FROM axletter_rights WHERE user_id = {?}", $uid);
192 protected function subscriptionWhere()
194 if (!$this->_promo_min
&& !$this->_promo_max
&& !$this->_subset
) {
198 if ($this->_promo_min
) {
199 $where[] = "((ni.user_id = 0 AND ni.promo >= {$this->_promo_min}) OR (ni.user_id != 0 AND u.promo >= {$this->_promo_min}))";
201 if ($this->_promo_max
) {
202 $where[] = "((ni.user_id = 0 AND ni.promo <= {$this->_promo_max}) OR (ni.user_id != 0 AND u.promo <= {$this->_promo_max}))";
204 if ($this->_subset
) {
205 require_once("emails.inc.php");
206 print_r($this->_subset_to
);
207 $ids = idsFromMails($this->_subset_to
);
209 $ids_list = implode(',', $ids);
210 $where[] = "ni.user_id IN ($ids_list)";
211 // TODO : force use of the adresses given by AX, not "canonical" ones ?
213 return implode(' AND ', $where);
216 static public function awaiting()
218 $res = XDB
::query("SELECT *
220 WHERE FIND_IN_SET('new', bits)");
221 if ($res->numRows()) {
222 return new AXLetter($res->fetchOneRow());
227 static public function toSend()
229 $res = XDB
::query("SELECT *
231 WHERE FIND_IN_SET('new', bits) AND echeance <= NOW() AND echeance != 0");
232 if ($res->numRows()) {
233 return new AXLetter($res->fetchOneRow());
238 static public function listSent()
240 $res = XDB
::query("SELECT IF(short_name IS NULL, id, short_name) as id, date, subject AS titre
242 WHERE NOT FIND_IN_SET('new', bits) AND NOT FIND_IN_SET('invalid', bits)
243 ORDER BY date DESC");
244 return $res->fetchAllAssoc();
247 static public function listAll()
249 $res = XDB
::query("SELECT IF(short_name IS NULL, id, short_name) as id, date, subject AS titre
251 ORDER BY date DESC");
252 return $res->fetchAllAssoc();
256 // vim:set et sw=4 sts=4 sws=4 enc=utf-8: