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
34 * @link http://galette.tuxfamily.org
35 * @since Available since 0.7.5dev - 2013-02-13
38 namespace Galette\Repository
;
40 use Galette\Entity\Reminder
;
41 use Galette\Filters\MembersList
;
43 use Zend\Db\Sql\Expression
;
51 * @author Johan Cwiklinski <johan@x-tnd.be>
52 * @copyright 2013-2014 The Galette Team
53 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL License 3.0 or (at your option) any later version
54 * @link http://galette.tuxfamily.org
55 * @since Available since 0.7.5dev - 2013-02-13
60 const TABLE
= 'reminders';
61 const PK
= 'reminder_id';
71 * @param array $selected Selected types for sending
73 public function __construct($selected)
75 if (isset($selected) && is_array($selected)) {
76 $this->selected
= array_map('intval', $selected);
78 $this->selected
= array(Reminder
::IMPENDING
, Reminder
::LATE
);
85 * @param Db $zdb Database instance
86 * @param string $type Reminder type
90 private function loadToRemind($zdb, $type)
92 $this->toremind
= array();
93 $select = $zdb->select(Members
::TABLE
, 'a');
95 array('r' => PREFIX_DB
. self
::TABLE
),
96 'a.' . Members
::PK
. '=r.reminder_dest',
98 'last_reminder' => new Expression('MAX(reminder_date)'),
103 array('p' => PREFIX_DB
. Members
::TABLE
),
104 'a.parent_id=p.' . Members
::PK
,
107 )->where('(a.email_adh != \'\' OR p.email_adh != \'\')')
108 ->where('a.activite_adh=true')
109 ->where('a.bool_exempt_adh=false');
111 if ($type === Reminder
::LATE
) {
112 $select->where
->LessThan(
114 date('Y-m-d', time())
117 $now = new \
DateTime();
118 $duedate = new \
DateTime();
119 $duedate->modify('+1 month');
120 $select->where
->greaterThanOrEqualTo(
122 $now->format('Y-m-d')
125 $duedate->format('Y-m-d')
129 $select->group('a.id_adh')->group('r.reminder_type');
131 $results = $zdb->execute($select);
133 foreach ($results as $r) {
134 if ($r->reminder_type
=== null ||
(int)$r->reminder_type
=== $type) {
135 $date_checked = false;
137 $due_date = new \
DateTime($r->date_echeance
);
138 $now = new \
DateTime();
141 case Reminder
::IMPENDING
:
142 //reminders 30 days and 7 days before
143 $first = clone $due_date;
144 $second = clone $due_date;
145 $first->modify('-1 month');
146 $second->modify('-7 day');
147 if ($now >= $first ||
$now >= $second) {
148 if ($r->last_reminder
== '') {
149 $date_checked = true;
151 $last_reminder = new \
DateTime($r->last_reminder
);
152 if ($now >= $second && $second > $last_reminder) {
153 $date_checked = true;
159 //reminders 30 days and 60 days after
160 $first = clone $due_date;
161 $second = clone $due_date;
162 $first->modify('1 month');
163 $second->modify('2 month');
164 if ($r->last_reminder
== null ||
$now >= $second ||
$now >= $first) {
165 if ($r->last_reminder
== '') {
166 $date_checked = true;
168 $last_reminder = new \
DateTime($r->last_reminder
);
169 if ($now >= $second && $second > $last_reminder) {
170 $date_checked = true;
179 $this->toremind
[] = $r->$pk;
183 'Reminder does not suits current requested type ' .
192 * Get the list of reminders
194 * @param Db $zdb Database instance
195 * @param boolean $nomail Get reminders for members who do not have email address
199 public function getList($zdb, $nomail = false)
201 $this->types
= array();
202 $this->reminders
= array();
205 foreach ($this->selected
as $s) {
206 $this->loadToRemind($zdb, $s);
208 if (count($this->toremind
) > 0) {
211 $members = $m->getArrayList(
220 $this->types
[$s] = $members;
224 if (is_array($this->types
)) {
225 foreach ($this->types
as $type => $members) {
227 if (is_array($members)) {
228 foreach ($members as $member) {
229 $reminder = new Reminder();
230 $reminder->type
= $type;
231 $reminder->dest
= $member;
233 $this->reminders
[] = $reminder;
238 return $this->reminders
;