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-13
37 namespace Galette\Repository
;
39 use Galette\Entity\Reminder
;
40 use Galette\Filters\MembersList
;
42 use Laminas\Db\Sql\Expression
;
50 * @author Johan Cwiklinski <johan@x-tnd.be>
51 * @copyright 2013-2014 The Galette Team
52 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
53 * @link http://galette.tuxfamily.org
54 * @since Available since 0.7.5dev - 2013-02-13
59 const TABLE
= 'reminders';
60 const PK
= 'reminder_id';
70 * @param array $selected Selected types for sending
72 public function __construct($selected = null)
74 if (isset($selected) && is_array($selected)) {
75 $this->selected
= array_map('intval', $selected);
77 $this->selected
= array(Reminder
::IMPENDING
, Reminder
::LATE
);
84 * @param Db $zdb Database instance
85 * @param string $type Reminder type
89 private function loadToRemind($zdb, $type)
91 $this->toremind
= array();
92 $select = $zdb->select(Members
::TABLE
, 'a');
94 array('r' => PREFIX_DB
. self
::TABLE
),
95 'a.' . Members
::PK
. '=r.reminder_dest',
97 'last_reminder' => new Expression('MAX(reminder_date)'),
102 array('p' => PREFIX_DB
. Members
::TABLE
),
103 'a.parent_id=p.' . Members
::PK
,
106 )->where('(a.email_adh != \'\' OR p.email_adh != \'\')')
107 ->where('a.activite_adh=true')
108 ->where('a.bool_exempt_adh=false');
110 if ($type === Reminder
::LATE
) {
111 $select->where
->LessThan(
113 date('Y-m-d', time())
116 $now = new \
DateTime();
117 $duedate = new \
DateTime();
118 $duedate->modify('+1 month');
119 $select->where
->greaterThanOrEqualTo(
121 $now->format('Y-m-d')
124 $duedate->format('Y-m-d')
128 $select->group('a.id_adh')->group('r.reminder_type')->group('p.email_adh');
130 $results = $zdb->execute($select);
132 foreach ($results as $r) {
133 if ($r->reminder_type
=== null ||
(int)$r->reminder_type
=== $type) {
134 $date_checked = false;
136 $due_date = new \
DateTime($r->date_echeance
);
137 $now = new \
DateTime();
140 case Reminder
::IMPENDING
:
141 //reminders 30 days and 7 days before
142 $first = clone $due_date;
143 $second = clone $due_date;
144 $first->modify('-1 month');
145 $second->modify('-7 day');
146 if ($now >= $first ||
$now >= $second) {
147 if ($r->last_reminder
== '') {
148 $date_checked = true;
150 $last_reminder = new \
DateTime($r->last_reminder
);
151 if ($now >= $second && $second > $last_reminder) {
152 $date_checked = true;
158 //reminders 30 days and 60 days after
159 $first = clone $due_date;
160 $second = clone $due_date;
161 $first->modify('1 month');
162 $second->modify('2 month');
163 if ($r->last_reminder
== null ||
$now >= $second ||
$now >= $first) {
164 if ($r->last_reminder
== '') {
165 $date_checked = true;
167 $last_reminder = new \
DateTime($r->last_reminder
);
168 if ($now >= $second && $second > $last_reminder) {
169 $date_checked = true;
178 $this->toremind
[] = $r->$pk;
182 'Reminder does not suits current requested type ' .
191 * Get the list of reminders
193 * @param Db $zdb Database instance
194 * @param boolean $nomail Get reminders for members who do not have email address
198 public function getList($zdb, $nomail = false)
200 $this->types
= array();
201 $this->reminders
= array();
204 foreach ($this->selected
as $s) {
205 $this->loadToRemind($zdb, $s);
207 if (count($this->toremind
) > 0) {
210 $members = $m->getArrayList(
219 $this->types
[$s] = $members;
223 if (is_array($this->types
)) {
224 foreach ($this->types
as $type => $members) {
226 if (is_array($members)) {
227 foreach ($members as $member) {
228 $reminder = new Reminder();
229 $reminder->type
= $type;
230 $reminder->dest
= $member;
232 $this->reminders
[] = $reminder;
237 return $this->reminders
;