3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
10 * Copyright © 2013-2021 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-2021 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
;
39 use Galette\Features\Replacements
;
42 use Galette\Core\GaletteMail
;
44 use Galette\Core\History
;
52 * @author Johan Cwiklinski <johan@x-tnd.be>
53 * @copyright 2009-2021 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
63 public const TABLE
= 'reminders';
64 public const PK
= 'reminder_id';
71 private $success = false;
77 public const IMPENDING
= 1;
78 public const LATE
= 2;
83 * @param mixed $args Arguments
85 public function __construct($args = null)
90 } elseif (is_object($args)) {
91 $this->loadFromRs($args);
94 __METHOD__
. ': unknonw arg',
102 * Load a reminder from its id
104 * @param int $id Identifier
108 private function load($id)
112 $select = $zdb->select(self
::TABLE
);
114 ->where(self
::PK
. ' = ' . $id);
116 $results = $zdb->execute($select);
117 $this->loadFromRs($results->current());
118 } catch (Throwable
$e) {
120 'An error occurred loading reminder #' . $id . "Message:\n" .
128 * Load reminder from a db ResultSet
130 * @param ResultSet $rs ResultSet
134 private function loadFromRs($rs)
140 $this->id
= $rs->$pk;
141 $this->type
= $rs->reminder_type
;
142 $this->dest
= new Adherent($zdb, (int)$rs->reminder_dest
);
143 $this->date
= $rs->reminder_date
;
144 $this->success
= $rs->reminder_success
;
145 $this->nomail
= $rs->reminder_nomail
;
146 $this->comment
= $rs->reminder_comment
;
147 } catch (Throwable
$e) {
149 __METHOD__
. ': incorrect ResultSet. Error: ' . $e->getMessage(),
160 * Store reminder in database and history
162 * @param Db $zdb Database instance
166 private function store($zdb)
168 $now = new \
DateTime();
170 'reminder_type' => $this->type
,
171 'reminder_dest' => $this->dest
->id
,
172 'reminder_date' => $now->format('Y-m-d'),
173 'reminder_success' => ($this->success
) ?
175 ($zdb->isPostgres() ?
'false' : 0),
176 'reminder_nomail' => ($this->nomail
) ?
178 ($zdb->isPostgres() ?
'false' : 0)
181 $insert = $zdb->insert(self
::TABLE
);
182 $insert->values($data);
184 $add = $zdb->execute($insert);
185 if (!($add->count() > 0)) {
186 Analog
::log('Reminder not stored!', Analog
::ERROR
);
190 } catch (Throwable
$e) {
192 'An error occurred storing reminder: ' . $e->getMessage() .
193 "\n" . print_r($data, true),
201 * Was reminder sent successfully?
205 public function isSuccess()
207 return $this->success
;
211 * Did member had an email when reminder was sent?
215 public function hasMail()
217 return !$this->nomail
;
223 * @param Texts $texts Text object
224 * @param History $hist History
225 * @param Db $zdb Database instance
229 public function send(Texts
$texts, History
$hist, Db
$zdb)
233 $this->success
= false;
236 if ($this->type
=== self
::IMPENDING
) {
237 $type_name = 'impending';
240 if ($this->hasMail()) {
241 $texts->setMember($this->dest
)
242 ->setNoContribution();
245 $type_name . 'duedate',
246 $this->dest
->language
249 $mail = new GaletteMail($preferences);
250 $mail->setSubject($texts->getSubject());
251 $mail->setRecipients(
253 $this->dest
->getEmail() => $this->dest
->sname
256 $mail->setMessage($texts->getBody());
257 $sent = $mail->send();
259 $details = str_replace(
267 $this->dest
->getEmail(),
268 $this->dest
->days_remaining
270 _T("%name <%mail> (%days days)")
273 if ($sent == GaletteMail
::MAIL_SENT
) {
274 $this->success
= true;
276 if ($type_name == 'late') {
277 $msg = _T("Sent reminder email for late membership");
279 $msg = _T("Sent reminder email for impending membership");
281 $this->msg
= $details;
282 $hist->add($msg, $details);
284 if ($type_name == 'late') {
285 $msg = _T("A problem happened while sending late membership email");
287 $msg = _T("A problem happened while sending impending membership email");
289 $this->msg
= $details;
290 $hist->add($msg, $details);
293 $this->nomail
= true;
297 _T("Unable to send %membership reminder (no email address).")
299 $details = str_replace(
308 $this->dest
->days_remaining
310 _T("%name (#%id - %days days)")
312 $hist->add($str, $details);
313 $this->msg
= $this->dest
->sname
;
315 //store reminder in database
317 return $this->success
;
325 public function getMessage()
333 * @param string $name Property name
337 public function __get($name)
341 return $this->dest
->id
;
347 'Unable to get Reminder property ' . $name,
357 * @param string $name Property name
358 * @param mixed $value Property value
362 public function __set($name, $value)
367 $value === self
::IMPENDING
368 ||
$value === self
::LATE
370 $this->type
= $value;
372 throw new \
UnexpectedValueException(
378 if ($this->type
!== null && $value instanceof Adherent
) {
379 $this->dest
= $value;
381 if ($value->getEmail() != '') {
382 $this->nomail
= false;
385 if (!$value instanceof Adherent
) {
386 throw new \
UnexpectedValueException(
387 'Please provide a member object.'
390 throw new \
UnderflowException(
391 'Please set reminder type first.'
398 'Unable to set property ' . $name,