| 1 | <?php |
| 2 | /*************************************************************************** |
| 3 | * Copyright (C) 2003-2009 Polytechnique.org * |
| 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 | |
| 22 | require_once("massmailer.inc.php"); |
| 23 | |
| 24 | class AXLetter extends MassMailer |
| 25 | { |
| 26 | public $_body; |
| 27 | public $_signature; |
| 28 | public $_promo_min; |
| 29 | public $_promo_max; |
| 30 | public $_echeance; |
| 31 | public $_date; |
| 32 | public $_bits; |
| 33 | |
| 34 | function __construct($id) |
| 35 | { |
| 36 | parent::__construct('axletter/letter.mail.tpl', 'ax.css', 'ax/show', 'axletter', 'axletter_ins'); |
| 37 | $this->_head = '<cher> <prenom>,'; |
| 38 | |
| 39 | if (!is_array($id)) { |
| 40 | if ($id == 'last') { |
| 41 | $res = XDB::query("SELECT * |
| 42 | FROM axletter |
| 43 | WHERE FIND_IN_SET('sent', bits) |
| 44 | ORDER BY id DESC"); |
| 45 | } else { |
| 46 | $res = XDB::query("SELECT * |
| 47 | FROM axletter |
| 48 | WHERE id = {?} OR short_name = {?}", $id, $id); |
| 49 | } |
| 50 | if (!$res->numRows()) { |
| 51 | throw new MailNotFound(); |
| 52 | } |
| 53 | $id = $res->fetchOneRow(); |
| 54 | } |
| 55 | list($this->_id, $this->_shortname, $this->_title_mail, $this->_title, |
| 56 | $this->_body, $this->_signature, $this->_promo_min, $this->_promo_max, |
| 57 | $this->_echeance, $this->_date, $this->_bits) = $id; |
| 58 | if ($this->_date == '0000-00-00') { |
| 59 | $this->_date = 0; |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | protected function assignData(&$smarty) |
| 64 | { |
| 65 | $smarty->assign_by_ref('am', $this); |
| 66 | } |
| 67 | |
| 68 | public function body($format) |
| 69 | { |
| 70 | return format_text($this->_body, $format); |
| 71 | } |
| 72 | |
| 73 | public function signature($format) |
| 74 | { |
| 75 | return format_text($this->_signature, $format, 10); |
| 76 | } |
| 77 | |
| 78 | public function valid() |
| 79 | { |
| 80 | return XDB::execute("UPDATE axletter |
| 81 | SET echeance = NOW() |
| 82 | WHERE id = {?}", $this->_id); |
| 83 | } |
| 84 | |
| 85 | public function invalid() |
| 86 | { |
| 87 | return XDB::execute("UPDATE axletter |
| 88 | SET bits = 'invalid', date = CURDATE() |
| 89 | WHERE id = {?}", $this->_id); |
| 90 | } |
| 91 | |
| 92 | protected function setSent() |
| 93 | { |
| 94 | XDB::execute("UPDATE axletter |
| 95 | SET bits='sent', date=CURDATE() |
| 96 | WHERE id={?}", $this->_id); |
| 97 | } |
| 98 | |
| 99 | static public function subscriptionState($uid = null) |
| 100 | { |
| 101 | $user = is_null($uid) ? S::v('uid') : $uid; |
| 102 | $res = XDB::query("SELECT 1 |
| 103 | FROM axletter_ins |
| 104 | WHERE user_id={?}", $user); |
| 105 | return $res->fetchOneCell(); |
| 106 | } |
| 107 | |
| 108 | static public function unsubscribe($uid = null, $hash = false) |
| 109 | { |
| 110 | $user = is_null($uid) ? S::v('uid') : $uid; |
| 111 | $field = !$hash ? 'user_id' : 'hash'; |
| 112 | if (is_null($uid) && $hash) { |
| 113 | return false; |
| 114 | } |
| 115 | $res = XDB::query("SELECT * |
| 116 | FROM axletter_ins |
| 117 | WHERE $field={?}", $user); |
| 118 | if (!$res->numRows()) { |
| 119 | return false; |
| 120 | } |
| 121 | XDB::execute("DELETE FROM axletter_ins |
| 122 | WHERE $field = {?}", $user); |
| 123 | return true; |
| 124 | } |
| 125 | |
| 126 | static public function subscribe($uid = null) |
| 127 | { |
| 128 | $user = is_null($uid) ? S::v('uid') : $uid; |
| 129 | XDB::execute("REPLACE INTO axletter_ins (user_id,last) |
| 130 | VALUES ({?}, 0)", $user); |
| 131 | } |
| 132 | |
| 133 | static public function hasPerms() |
| 134 | { |
| 135 | if (S::has_perms()) { |
| 136 | return true; |
| 137 | } |
| 138 | $res = XDB::query("SELECT 1 |
| 139 | FROM axletter_rights |
| 140 | WHERE user_id = {?}", S::i('uid')); |
| 141 | return $res->fetchOneCell(); |
| 142 | } |
| 143 | |
| 144 | static public function grantPerms($uid) |
| 145 | { |
| 146 | if (!is_numeric($uid)) { |
| 147 | $res = XDB::query("SELECT id FROM aliases WHERE alias = {?}", $uid); |
| 148 | $uid = $res->fetchOneCell(); |
| 149 | } |
| 150 | if (!$uid) { |
| 151 | return false; |
| 152 | } |
| 153 | return XDB::execute("INSERT IGNORE INTO axletter_rights SET user_id = {?}", $uid); |
| 154 | } |
| 155 | |
| 156 | static public function revokePerms($uid) |
| 157 | { |
| 158 | if (!is_numeric($uid)) { |
| 159 | $res = XDB::query("SELECT id FROM aliases WHERE alias = {?}", $uid); |
| 160 | $uid = $res->fetchOneCell(); |
| 161 | } |
| 162 | if (!$uid) { |
| 163 | return false; |
| 164 | } |
| 165 | return XDB::execute("DELETE FROM axletter_rights WHERE user_id = {?}", $uid); |
| 166 | } |
| 167 | |
| 168 | protected function subscriptionWhere() |
| 169 | { |
| 170 | if (!$this->_promo_min && !$this->_promo_max) { |
| 171 | return '1'; |
| 172 | } |
| 173 | $where = array(); |
| 174 | if ($this->_promo_min) { |
| 175 | $where[] = "((ni.user_id = 0 AND ni.promo >= {$this->_promo_min}) OR (ni.user_id != 0 AND u.promo >= {$this->_promo_min}))"; |
| 176 | } |
| 177 | if ($this->_promo_max) { |
| 178 | $where[] = "((ni.user_id = 0 AND ni.promo <= {$this->_promo_max}) OR (ni.user_id != 0 AND u.promo <= {$this->_promo_max}))"; |
| 179 | } |
| 180 | return implode(' AND ', $where); |
| 181 | } |
| 182 | |
| 183 | static public function awaiting() |
| 184 | { |
| 185 | $res = XDB::query("SELECT * |
| 186 | FROM axletter |
| 187 | WHERE FIND_IN_SET('new', bits)"); |
| 188 | if ($res->numRows()) { |
| 189 | return new AXLetter($res->fetchOneRow()); |
| 190 | } |
| 191 | return null; |
| 192 | } |
| 193 | |
| 194 | static public function toSend() |
| 195 | { |
| 196 | $res = XDB::query("SELECT * |
| 197 | FROM axletter |
| 198 | WHERE FIND_IN_SET('new', bits) AND echeance <= NOW() AND echeance != 0"); |
| 199 | if ($res->numRows()) { |
| 200 | return new AXLetter($res->fetchOneRow()); |
| 201 | } |
| 202 | return null; |
| 203 | } |
| 204 | |
| 205 | static public function listSent() |
| 206 | { |
| 207 | $res = XDB::query("SELECT IF(short_name IS NULL, id, short_name) as id, date, subject AS titre |
| 208 | FROM axletter |
| 209 | WHERE NOT FIND_IN_SET('new', bits) AND NOT FIND_IN_SET('invalid', bits) |
| 210 | ORDER BY date DESC"); |
| 211 | return $res->fetchAllAssoc(); |
| 212 | } |
| 213 | |
| 214 | static public function listAll() |
| 215 | { |
| 216 | $res = XDB::query("SELECT IF(short_name IS NULL, id, short_name) as id, date, subject AS titre |
| 217 | FROM axletter |
| 218 | ORDER BY date DESC"); |
| 219 | return $res->fetchAllAssoc(); |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | // vim:set et sw=4 sts=4 sws=4 enc=utf-8: |
| 224 | ?> |