]> git.agnieray.net Git - galette.git/blob - galette/lib/Galette/Filters/MailingsList.php
4c4b06a2b3d876907ad4088059653821879149e0
[galette.git] / galette / lib / Galette / Filters / MailingsList.php
1 <?php
2
3 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
4
5 /**
6 * Mailiungs history lists filters and paginator
7 *
8 * PHP version 5
9 *
10 * Copyright © 2016 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 Filters
28 * @package Galette
29 *
30 * @author Johan Cwiklinski <johan@x-tnd.be>
31 * @copyright 2016 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 * @version SVN: $Id$
34 * @link http://galette.tuxfamily.org
35 * @since 2016-11-26
36 */
37
38 namespace Galette\Filters;
39
40 use Analog\Analog;
41 use Galette\Core\Pagination;
42 use Galette\Core\MailingHistory;
43
44 /**
45 * Mailings history lists filters and paginator
46 *
47 * @name ContributionsList
48 * @category Filters
49 * @package Galette
50 *
51 * @author Johan Cwiklinski <johan@x-tnd.be>
52 * @copyright 2016 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 */
56
57 class MailingsList extends Pagination
58 {
59 const ORDERBY_DATE = 0;
60 const ORDERBY_SENDER = 1;
61 const ORDERBY_SUBJECT = 2;
62 const ORDERBY_SENT = 3;
63
64 //filters
65 private $start_date_filter = null;
66 private $end_date_filter = null;
67 private $sender_filter = 0;
68 private $sent_filter = MailingHistory::FILTER_DC_SENT;
69 private $subject_filter = null;
70
71 protected $list_fields = array(
72 'start_date_filter',
73 'raw_start_date_filter',
74 'end_date_filter',
75 'raw_end_date_filter',
76 'sender_filter',
77 'sent_filter',
78 'subject_filter'
79 );
80
81 /**
82 * Default constructor
83 */
84 public function __construct()
85 {
86 $this->reinit();
87 }
88
89 /**
90 * Returns the field we want to default set order to
91 *
92 * @return string field name
93 */
94 protected function getDefaultOrder()
95 {
96 return self::ORDERBY_DATE;
97 }
98
99 /**
100 * Reinit default parameters
101 *
102 * @return void
103 */
104 public function reinit()
105 {
106 parent::reinit();
107 $this->start_date_filter = null;
108 $this->end_date_filter = null;
109 $this->sender_filter = 0;
110 $this->sent_filter = MailingHistory::FILTER_DC_SENT;
111 $this->subject_filter = null;
112 }
113
114 /**
115 * Global getter method
116 *
117 * @param string $name name of the property we want to retrive
118 *
119 * @return object the called property
120 */
121 public function __get($name)
122 {
123 Analog::log(
124 '[MailingsList] Getting property `' . $name . '`',
125 Analog::DEBUG
126 );
127
128 if (in_array($name, $this->pagination_fields)) {
129 return parent::__get($name);
130 } else {
131 if (in_array($name, $this->list_fields)) {
132 switch ($name) {
133 case 'raw_start_date_filter':
134 return $this->start_date_filter;
135 break;
136 case 'raw_end_date_filter':
137 return $this->end_date_filter;
138 break;
139 case 'start_date_filter':
140 case 'end_date_filter':
141 try {
142 if ($this->$name !== null) {
143 $d = new \DateTime($this->$name);
144 return $d->format(__("Y-m-d"));
145 }
146 } catch (\Exception $e) {
147 //oops, we've got a bad date :/
148 Analog::log(
149 'Bad date (' . $this->$name . ') | ' .
150 $e->getMessage(),
151 Analog::INFO
152 );
153 return $this->$name;
154 }
155 break;
156 default:
157 return $this->$name;
158 }
159 } else {
160 Analog::log(
161 '[MailingsList] Unable to get proprety `' .$name . '`',
162 Analog::WARNING
163 );
164 }
165 }
166 }
167
168 /**
169 * Global setter method
170 *
171 * @param string $name name of the property we want to assign a value to
172 * @param object $value a relevant value for the property
173 *
174 * @return void
175 */
176 public function __set($name, $value)
177 {
178 if (in_array($name, $this->pagination_fields)) {
179 parent::__set($name, $value);
180 } else {
181 Analog::log(
182 '[MailingsList] Setting property `' . $name . '`',
183 Analog::DEBUG
184 );
185
186 switch ($name) {
187 case 'start_date_filter':
188 case 'end_date_filter':
189 try {
190 if ($value !== '') {
191 $y = \DateTime::createFromFormat(__("Y"), $value);
192 if ($y !== false) {
193 $month = 1;
194 $day = 1;
195 if ($name === 'end_date_filter') {
196 $month = 12;
197 $day = 31;
198 }
199 $y->setDate(
200 $y->format('Y'),
201 $month,
202 $day
203 );
204 $this->$name = $y->format('Y-m-d');
205 }
206
207 $ym = \DateTime::createFromFormat(__("Y-m"), $value);
208 if ($y === false && $ym !== false) {
209 $day = 1;
210 if ($name === 'end_date_filter') {
211 $day = $ym->format('t');
212 }
213 $ym->setDate(
214 $ym->format('Y'),
215 $ym->format('m'),
216 $day
217 );
218 $this->$name = $ym->format('Y-m-d');
219 }
220
221 $d = \DateTime::createFromFormat(__("Y-m-d"), $value);
222 if ($y === false && $ym === false && $d !== false) {
223 $this->$name = $d->format('Y-m-d');
224 }
225
226 if ($y === false && $ym === false && $d === false) {
227 $formats = array(
228 __("Y"),
229 __("Y-m"),
230 __("Y-m-d"),
231 );
232
233 $field = null;
234 if ($name === 'start_date_filter') {
235 $field = _T("start date filter");
236 }
237 if ($name === 'end_date_filter') {
238 $field = _T("end date filter");
239 }
240
241 throw new \Exception(
242 str_replace(
243 array('%field', '%format'),
244 array(
245 $field,
246 implode(', ', $formats)
247 ),
248 _T("Unknown date format for %field.<br/>Know formats are: %formats")
249 )
250 );
251 }
252 } else {
253 $this->$name = null;
254 }
255 } catch (\Exception $e) {
256 Analog::log(
257 'Wrong date format. field: ' . $key .
258 ', value: ' . $value . ', expected fmt: ' .
259 __("Y-m-d") . ' | ' . $e->getMessage(),
260 Analog::INFO
261 );
262 throw $e;
263 }
264 break;
265 default:
266 $this->$name = $value;
267 break;
268 }
269 }
270 }
271 }