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