3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
10 * Copyright © 2013-2014 The Galette Team
12 * This file is part of Galette (http://galette.tuxfamily.org).
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.
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.
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/>.
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
37 namespace Galette\Entity
;
40 use Laminas\Db\Sql\Expression
;
41 use Galette\Core\GaletteMail
;
42 use Galette\Entity\Texts
;
44 use Galette\Core\History
;
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
61 const TABLE
= 'reminders';
62 const PK
= 'reminder_id';
80 * @param mixed $args Arguments
82 public function __construct($args = null)
87 } elseif (is_object($args)) {
88 $this->loadFromRs($args);
91 __METHOD__
. ': unknonw arg',
99 * Load a reminder from its id
101 * @param int $id Identifier
105 private function load($id)
109 $select = $zdb->select(self
::TABLE
);
111 ->where(self
::PK
. ' = ' . $id);
113 $results = $zdb->execute($select);
114 $this->loadFromRs($results->current());
115 } catch (\Exception
$e) {
117 'An error occurred loading reminder #' . $id . "Message:\n" .
125 * Load reminder from a db ResultSet
127 * @param ResultSet $rs ResultSet
131 private function loadFromRs($rs)
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) {
146 __METHOD__
. ': incorrect ResultSet. Error: ' . $e->getMessage(),
157 * Store reminder in database and history
159 * @param Db $zdb Database instance
163 private function store($zdb)
165 $now = new \
DateTime();
167 'reminder_type' => $this->type
,
168 'reminder_dest' => $this->dest
->id
,
169 'reminder_date' => $now->format('Y-m-d'),
170 'reminder_success' => ($this->success
) ?
172 ($zdb->isPostgres() ?
'false' : 0),
173 'reminder_nomail' => ($this->nomail
) ?
175 ($zdb->isPostgres() ?
'false' : 0)
178 $insert = $zdb->insert(self
::TABLE
);
179 $insert->values($data);
181 $add = $zdb->execute($insert);
182 if (!$add->count() > 0) {
183 Analog
::log('Reminder not stored!', Analog
::ERROR
);
187 } catch (\Exception
$e) {
189 'An error occurred storing reminder: ' . $e->getMessage() .
190 "\n" . print_r($data, true),
198 * Was reminder sent successfully?
202 public function isSuccess()
204 return $this->success
;
208 * Did member had an email when reminder was sent?
212 public function hasMail()
214 return !$this->nomail
;
220 * @param Texts $texts Text object
221 * @param History $hist History
222 * @param Db $zdb Database instance
226 public function send(Texts
$texts, History
$hist, Db
$zdb)
231 if ($this->type
=== self
::IMPENDING
) {
232 $type_name = 'impending';
235 if ($this->hasMail()) {
236 $texts->setReplaces($this->replaces
);
239 $type_name . 'duedate',
240 $this->dest
->language
243 $mail = new GaletteMail($preferences);
244 $mail->setSubject($texts->getSubject());
245 $mail->setRecipients(
247 $this->dest
->getEmail() => $this->dest
->sname
250 $mail->setMessage($texts->getBody());
251 $sent = $mail->send();
253 $details = str_replace(
261 $this->dest
->getEmail(),
262 $this->dest
->days_remaining
264 _T("%name <%mail> (%days days)")
267 if ($sent == GaletteMail
::MAIL_SENT
) {
268 $this->success
= true;
270 if ($type_name == 'late') {
271 $msg = _T("Sent reminder email for late membership");
273 $msg = _T("Sent reminder email for impending membership");
275 $this->msg
= $details;
276 $hist->add($msg, $details);
278 $this->success
= false;
279 if ($type_name == 'late') {
280 $msg = _T("A problem happened while sending late membership email");
282 $msg = _T("A problem happened while sending impending membership email");
284 $this->msg
= $details;
285 $hist->add($str, $details);
288 $this->success
= false;
289 $this->nomail
= true;
293 _T("Unable to send %membership reminder (no email address).")
295 $details = str_replace(
304 $this->dest
->days_remaining
306 _T("%name (#%id - %days days)")
308 $hist->add($str, $details);
309 $this->msg
= $this->dest
->sname
;
311 //store reminder in database
313 return $this->success
;
321 public function getMessage()
329 * @param string $name Property name
333 public function __get($name)
337 return $this->dest
->id
;
341 'Unable to get Reminder property ' . $name,
351 * @param string $name Property name
352 * @param mixed $value Property value
356 public function __set($name, $value)
361 $value === self
::IMPENDING
362 ||
$value === self
::LATE
364 $this->type
= $value;
366 throw new \
UnexpectedValueException(
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;
381 if ($this->type
=== self
::LATE
) {
382 $this->replaces
['days_expired'] = $value->days_remaining
* -1;
384 if ($this->type
=== self
::IMPENDING
) {
385 $this->replaces
['days_remaining'] = $value->days_remaining
;
388 if (!$value instanceof Adherent
) {
389 throw new \
UnexpectedValueException(
390 'Please provide a member object.'
393 throw new \
UnderflowException(
394 'Please set reminder type first.'
401 'Unable to set property ' . $name,