]> git.agnieray.net Git - galette.git/blob - galette/lib/Galette/Entity/Reminder.php
9cff34da922c9b81509f4885920ace18bcc0bf39
[galette.git] / galette / lib / Galette / Entity / Reminder.php
1 <?php
2
3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
4
5 /**
6 * Reminders
7 *
8 * PHP version 5
9 *
10 * Copyright © 2013-2014 The Galette Team
11 *
12 * This file is part of Galette (http://galette.tuxfamily.org).
13 *
14 * Galette is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation, either version 3 of the License, or
17 * (at your option) any later version.
18 *
19 * Galette is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with Galette. If not, see <http://www.gnu.org/licenses/>.
26 *
27 * @category Entity
28 * @package Galette
29 *
30 * @author Johan Cwiklinski <johan@x-tnd.be>
31 * @copyright 2013-2014 The Galette Team
32 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
33 * @link http://galette.tuxfamily.org
34 * @since Available since 0.7.5dev - 2013-02-11
35 */
36
37 namespace Galette\Entity;
38
39 use Analog\Analog;
40 use Laminas\Db\Sql\Expression;
41 use Galette\Core\GaletteMail;
42 use Galette\Entity\Texts;
43 use Galette\Core\Db;
44 use Galette\Core\History;
45
46 /**
47 * Reminders
48 *
49 * @category Entity
50 * @name Reminder
51 * @package Galette
52 * @author Johan Cwiklinski <johan@x-tnd.be>
53 * @copyright 2009-2014 The Galette Team
54 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
55 * @link http://galette.tuxfamily.org
56 * @since Available since 0.7.5dev - 2013-02-11
57 */
58
59 class Reminder
60 {
61 const TABLE = 'reminders';
62 const PK = 'reminder_id';
63
64 private $id;
65 private $type;
66 private $dest;
67 private $date;
68 private $success;
69 private $nomail;
70 private $comment;
71 private $replaces;
72 private $msg;
73
74 const IMPENDING = 1;
75 const LATE = 2;
76
77 /**
78 * Main constructor
79 *
80 * @param mixed $args Arguments
81 */
82 public function __construct($args = null)
83 {
84 if ($args !== null) {
85 if (is_int($args)) {
86 $this->load($args);
87 } elseif (is_object($args)) {
88 $this->loadFromRs($args);
89 } else {
90 Analog::log(
91 __METHOD__ . ': unknonw arg',
92 Analog::WARNING
93 );
94 }
95 }
96 }
97
98 /**
99 * Load a reminder from its id
100 *
101 * @param int $id Identifier
102 *
103 * @return void
104 */
105 private function load($id)
106 {
107 global $zdb;
108 try {
109 $select = $zdb->select(self::TABLE);
110 $select->limit(1)
111 ->where(self::PK . ' = ' . $id);
112
113 $results = $zdb->execute($select);
114 $this->loadFromRs($results->current());
115 } catch (\Exception $e) {
116 Analog::log(
117 'An error occurred loading reminder #' . $id . "Message:\n" .
118 $e->getMessage(),
119 Analog::ERROR
120 );
121 }
122 }
123
124 /**
125 * Load reminder from a db ResultSet
126 *
127 * @param ResultSet $rs ResultSet
128 *
129 * @return void
130 */
131 private function loadFromRs($rs)
132 {
133 global $zdb;
134
135 try {
136 $pk = self::PK;
137 $this->id = $rs->$pk;
138 $this->type = $rs->reminder_type;
139 $this->dest = new Adherent($zdb, (int)$rs->reminder_dest);
140 $this->date = $rs->reminder_date;
141 $this->success = $rs->reminder_success;
142 $this->nomail = $rs->reminder_nomail;
143 $this->comment = $rs->reminder_comment;
144 } catch (\Exception $e) {
145 Analog::log(
146 __METHOD__ . ': incorrect ResultSet. Error: ' . $e->getMessage(),
147 Analog::ERROR
148 );
149 Analog::log(
150 print_r($rs, true),
151 Analog::INFO
152 );
153 }
154 }
155
156 /**
157 * Store reminder in database and history
158 *
159 * @param Db $zdb Database instance
160 *
161 * @return boolean
162 */
163 private function store($zdb)
164 {
165 $now = new \DateTime();
166 $data = array(
167 'reminder_type' => $this->type,
168 'reminder_dest' => $this->dest->id,
169 'reminder_date' => $now->format('Y-m-d'),
170 'reminder_success' => ($this->success) ?
171 true :
172 ($zdb->isPostgres() ? 'false' : 0),
173 'reminder_nomail' => ($this->nomail) ?
174 true :
175 ($zdb->isPostgres() ? 'false' : 0)
176 );
177 try {
178 $insert = $zdb->insert(self::TABLE);
179 $insert->values($data);
180
181 $add = $zdb->execute($insert);
182 if (!$add->count() > 0) {
183 Analog::log('Reminder not stored!', Analog::ERROR);
184 return false;
185 }
186 return true;
187 } catch (\Exception $e) {
188 Analog::log(
189 'An error occurred storing reminder: ' . $e->getMessage() .
190 "\n" . print_r($data, true),
191 Analog::ERROR
192 );
193 return false;
194 }
195 }
196
197 /**
198 * Was reminder sent successfully?
199 *
200 * @return boolean
201 */
202 public function isSuccess()
203 {
204 return $this->success;
205 }
206
207 /**
208 * Did member had an email when reminder was sent?
209 *
210 * @return boolean
211 */
212 public function hasMail()
213 {
214 return !$this->nomail;
215 }
216
217 /**
218 * Send the reminder
219 *
220 * @param Texts $texts Text object
221 * @param History $hist History
222 * @param Db $zdb Database instance
223 *
224 * @return boolean
225 */
226 public function send(Texts $texts, History $hist, Db $zdb)
227 {
228 global $preferences;
229
230 $type_name = 'late';
231 if ($this->type === self::IMPENDING) {
232 $type_name = 'impending';
233 }
234
235 if ($this->hasMail()) {
236 $texts->setReplaces($this->replaces);
237
238 $texts->getTexts(
239 $type_name . 'duedate',
240 $this->dest->language
241 );
242
243 $mail = new GaletteMail($preferences);
244 $mail->setSubject($texts->getSubject());
245 $mail->setRecipients(
246 array(
247 $this->dest->getEmail() => $this->dest->sname
248 )
249 );
250 $mail->setMessage($texts->getBody());
251 $sent = $mail->send();
252
253 $details = str_replace(
254 array(
255 '%name',
256 '%mail',
257 '%days'
258 ),
259 array(
260 $this->dest->sname,
261 $this->dest->getEmail(),
262 $this->dest->days_remaining
263 ),
264 _T("%name <%mail> (%days days)")
265 );
266
267 if ($sent == GaletteMail::MAIL_SENT) {
268 $this->success = true;
269 $msg = '';
270 if ($type_name == 'late') {
271 $msg = _T("Sent reminder email for late membership");
272 } else {
273 $msg = _T("Sent reminder email for impending membership");
274 }
275 $this->msg = $details;
276 $hist->add($msg, $details);
277 } else {
278 $this->success = false;
279 if ($type_name == 'late') {
280 $msg = _T("A problem happened while sending late membership email");
281 } else {
282 $msg = _T("A problem happened while sending impending membership email");
283 }
284 $this->msg = $details;
285 $hist->add($str, $details);
286 }
287 } else {
288 $this->success = false;
289 $this->nomail = true;
290 $str = str_replace(
291 '%membership',
292 $type_name,
293 _T("Unable to send %membership reminder (no email address).")
294 );
295 $details = str_replace(
296 array(
297 '%name',
298 '%id',
299 '%days'
300 ),
301 array(
302 $this->dest->sname,
303 $this->dest->id,
304 $this->dest->days_remaining
305 ),
306 _T("%name (#%id - %days days)")
307 );
308 $hist->add($str, $details);
309 $this->msg = $this->dest->sname;
310 }
311 //store reminder in database
312 $this->store($zdb);
313 return $this->success;
314 }
315
316 /**
317 * Retrieve message
318 *
319 * @return string
320 */
321 public function getMessage()
322 {
323 return $this->msg;
324 }
325
326 /**
327 * Getter
328 *
329 * @param string $name Property name
330 *
331 * @return mixed
332 */
333 public function __get($name)
334 {
335 switch ($name) {
336 case 'member_id':
337 return $this->dest->id;
338 break;
339 default:
340 Analog::log(
341 'Unable to get Reminder property ' . $name,
342 Analog::WARNING
343 );
344 break;
345 }
346 }
347
348 /**
349 * Setter
350 *
351 * @param string $name Property name
352 * @param mixed $value Property value
353 *
354 * @return void
355 */
356 public function __set($name, $value)
357 {
358 switch ($name) {
359 case 'type':
360 if (
361 $value === self::IMPENDING
362 || $value === self::LATE
363 ) {
364 $this->type = $value;
365 } else {
366 throw new \UnexpectedValueException(
367 'Unknown type!'
368 );
369 }
370 break;
371 case 'dest':
372 if ($this->type !== null && $value instanceof Adherent) {
373 $this->dest = $value;
374 $this->replaces['login_adh'] = $value->login;
375 $this->replaces['name_adh'] = custom_html_entity_decode($value->sname);
376 $this->replaces['firstname_adh'] = custom_html_entity_decode($value->surname);
377 $this->replaces['lastname_adh'] = custom_html_entity_decode($value->name);
378 if ($value->getEmail() != '') {
379 $this->nomail = false;
380 }
381 if ($this->type === self::LATE) {
382 $this->replaces['days_expired'] = $value->days_remaining * -1;
383 }
384 if ($this->type === self::IMPENDING) {
385 $this->replaces['days_remaining'] = $value->days_remaining;
386 }
387 } else {
388 if (!$value instanceof Adherent) {
389 throw new \UnexpectedValueException(
390 'Please provide a member object.'
391 );
392 } else {
393 throw new \UnderflowException(
394 'Please set reminder type first.'
395 );
396 }
397 }
398 break;
399 default:
400 Analog::log(
401 'Unable to set property ' . $name,
402 Analog::WARNING
403 );
404 break;
405 }
406 }
407 }