Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
22.39% |
30 / 134 |
|
20.00% |
3 / 15 |
CRAP | |
0.00% |
0 / 1 |
type_date | |
22.39% |
30 / 134 |
|
20.00% |
3 / 15 |
3462.72 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
get_name_short | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
get_options | |
0.00% |
0 / 20 |
|
0.00% |
0 / 1 |
42 | |||
get_default_option_values | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
2 | |||
get_default_field_value | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
get_profile_field | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
12 | |||
validate_profile_field | |
92.31% |
12 / 13 |
|
0.00% |
0 / 1 |
21.20 | |||
get_profile_value | |
100.00% |
12 / 12 |
|
100.00% |
1 / 1 |
12 | |||
get_profile_value_raw | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
4 | |||
generate_field | |
0.00% |
0 / 24 |
|
0.00% |
0 / 1 |
380 | |||
get_field_ident | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
get_database_column_type | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
get_language_options | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
6 | |||
get_excluded_options | |
0.00% |
0 / 18 |
|
0.00% |
0 / 1 |
56 | |||
prepare_hidden_fields | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
20 |
1 | <?php |
2 | /** |
3 | * |
4 | * This file is part of the phpBB Forum Software package. |
5 | * |
6 | * @copyright (c) phpBB Limited <https://www.phpbb.com> |
7 | * @license GNU General Public License, version 2 (GPL-2.0) |
8 | * |
9 | * For full copyright and license information, please see |
10 | * the docs/CREDITS.txt file. |
11 | * |
12 | */ |
13 | |
14 | namespace phpbb\profilefields\type; |
15 | |
16 | class type_date extends type_base |
17 | { |
18 | /** |
19 | * Request object |
20 | * @var \phpbb\request\request |
21 | */ |
22 | protected $request; |
23 | |
24 | /** |
25 | * Template object |
26 | * @var \phpbb\template\template |
27 | */ |
28 | protected $template; |
29 | |
30 | /** |
31 | * User object |
32 | * @var \phpbb\user |
33 | */ |
34 | protected $user; |
35 | |
36 | /** |
37 | * Construct |
38 | * |
39 | * @param \phpbb\request\request $request Request object |
40 | * @param \phpbb\template\template $template Template object |
41 | * @param \phpbb\user $user User object |
42 | */ |
43 | public function __construct(\phpbb\request\request $request, \phpbb\template\template $template, \phpbb\user $user) |
44 | { |
45 | $this->request = $request; |
46 | $this->template = $template; |
47 | $this->user = $user; |
48 | } |
49 | |
50 | /** |
51 | * {@inheritDoc} |
52 | */ |
53 | public function get_name_short() |
54 | { |
55 | return 'date'; |
56 | } |
57 | |
58 | /** |
59 | * {@inheritDoc} |
60 | */ |
61 | public function get_options($default_lang_id, $field_data) |
62 | { |
63 | $profile_row = array( |
64 | 'var_name' => 'field_default_value', |
65 | 'lang_name' => $field_data['lang_name'], |
66 | 'lang_explain' => $field_data['lang_explain'], |
67 | 'lang_id' => $default_lang_id, |
68 | 'field_default_value' => $field_data['field_default_value'], |
69 | 'field_ident' => 'field_default_value', |
70 | 'field_type' => $this->get_service_name(), |
71 | 'field_length' => $field_data['field_length'], |
72 | 'lang_options' => $field_data['lang_options'], |
73 | ); |
74 | |
75 | $always_now = $this->request->variable('always_now', -1); |
76 | if ($always_now == -1) |
77 | { |
78 | $s_checked = ($field_data['field_default_value'] == 'now') ? true : false; |
79 | } |
80 | else |
81 | { |
82 | $s_checked = ($always_now) ? true : false; |
83 | } |
84 | |
85 | $options = array( |
86 | 0 => array('TITLE' => $this->user->lang['DEFAULT_VALUE'], 'FIELD' => $this->process_field_row('preview', $profile_row)), |
87 | 1 => array('TITLE' => $this->user->lang['ALWAYS_TODAY'], 'FIELD' => '<label><input type="radio" class="radio" name="always_now" value="1"' . (($s_checked) ? ' checked="checked"' : '') . ' onchange="document.getElementById(\'add_profile_field\').submit();" /> ' . $this->user->lang['YES'] . '</label><label><input type="radio" class="radio" name="always_now" value="0"' . ((!$s_checked) ? ' checked="checked"' : '') . ' onchange="document.getElementById(\'add_profile_field\').submit();" /> ' . $this->user->lang['NO'] . '</label>'), |
88 | ); |
89 | |
90 | return $options; |
91 | } |
92 | |
93 | /** |
94 | * {@inheritDoc} |
95 | */ |
96 | public function get_default_option_values() |
97 | { |
98 | return array( |
99 | 'field_length' => 10, |
100 | 'field_minlen' => 10, |
101 | 'field_maxlen' => 10, |
102 | 'field_validation' => '', |
103 | 'field_novalue' => ' 0- 0- 0', |
104 | 'field_default_value' => ' 0- 0- 0', |
105 | ); |
106 | } |
107 | |
108 | /** |
109 | * {@inheritDoc} |
110 | */ |
111 | public function get_default_field_value($field_data) |
112 | { |
113 | if ($field_data['field_default_value'] == 'now') |
114 | { |
115 | $now = getdate(); |
116 | $field_data['field_default_value'] = sprintf('%2d-%2d-%4d', $now['mday'], $now['mon'], $now['year']); |
117 | } |
118 | |
119 | return $field_data['field_default_value']; |
120 | } |
121 | |
122 | /** |
123 | * {@inheritDoc} |
124 | */ |
125 | public function get_profile_field($profile_row) |
126 | { |
127 | $var_name = 'pf_' . $profile_row['field_ident']; |
128 | |
129 | if (!$this->request->is_set($var_name . '_day')) |
130 | { |
131 | if ($profile_row['field_default_value'] == 'now') |
132 | { |
133 | $now = getdate(); |
134 | $profile_row['field_default_value'] = sprintf('%2d-%2d-%4d', $now['mday'], $now['mon'], $now['year']); |
135 | } |
136 | list($day, $month, $year) = explode('-', $profile_row['field_default_value']); |
137 | } |
138 | else |
139 | { |
140 | $day = $this->request->variable($var_name . '_day', 0); |
141 | $month = $this->request->variable($var_name . '_month', 0); |
142 | $year = $this->request->variable($var_name . '_year', 0); |
143 | } |
144 | |
145 | return sprintf('%2d-%2d-%4d', $day, $month, $year); |
146 | } |
147 | |
148 | /** |
149 | * {@inheritDoc} |
150 | */ |
151 | public function validate_profile_field(&$field_value, $field_data) |
152 | { |
153 | $field_validate = explode('-', $field_value ?: ''); |
154 | |
155 | $day = (isset($field_validate[0])) ? (int) $field_validate[0] : 0; |
156 | $month = (isset($field_validate[1])) ? (int) $field_validate[1] : 0; |
157 | $year = (isset($field_validate[2])) ? (int) $field_validate[2] : 0; |
158 | |
159 | if ((!$day || !$month || !$year) && !$field_data['field_required']) |
160 | { |
161 | return false; |
162 | } |
163 | |
164 | if ((!$day || !$month || !$year) && $field_data['field_required']) |
165 | { |
166 | return $this->user->lang('FIELD_REQUIRED', $this->get_field_name($field_data['lang_name'])); |
167 | } |
168 | |
169 | if ($day < 0 || $day > 31 || $month < 0 || $month > 12 || ($year < 1901 && $year > 0) || $year > gmdate('Y', time()) + 50) |
170 | { |
171 | return $this->user->lang('FIELD_INVALID_DATE', $this->get_field_name($field_data['lang_name'])); |
172 | } |
173 | |
174 | if (checkdate($month, $day, $year) === false) |
175 | { |
176 | return $this->user->lang('FIELD_INVALID_DATE', $this->get_field_name($field_data['lang_name'])); |
177 | } |
178 | |
179 | return false; |
180 | } |
181 | |
182 | /** |
183 | * {@inheritDoc} |
184 | */ |
185 | public function get_profile_value($field_value, $field_data) |
186 | { |
187 | $date = explode('-', $field_value ?: ''); |
188 | $day = (isset($date[0])) ? (int) $date[0] : 0; |
189 | $month = (isset($date[1])) ? (int) $date[1] : 0; |
190 | $year = (isset($date[2])) ? (int) $date[2] : 0; |
191 | |
192 | if (!$day && !$month && !$year && !$field_data['field_show_novalue']) |
193 | { |
194 | return null; |
195 | } |
196 | else if ($day && $month && $year) |
197 | { |
198 | // Date should display as the same date for every user regardless of timezone |
199 | return $this->user->create_datetime() |
200 | ->setDate($year, $month, $day) |
201 | ->setTime(0, 0, 0) |
202 | ->format($this->user->lang['DATE_FORMAT'], true); |
203 | } |
204 | |
205 | return $field_value; |
206 | } |
207 | |
208 | /** |
209 | * {@inheritDoc} |
210 | */ |
211 | public function get_profile_value_raw($field_value, $field_data) |
212 | { |
213 | if (($field_value === '' || $field_value === null) && !$field_data['field_show_novalue']) |
214 | { |
215 | return null; |
216 | } |
217 | |
218 | return $field_value; |
219 | } |
220 | |
221 | /** |
222 | * {@inheritDoc} |
223 | */ |
224 | public function generate_field($profile_row, $preview_options = false) |
225 | { |
226 | $profile_row['field_ident'] = (isset($profile_row['var_name'])) ? $profile_row['var_name'] : 'pf_' . $profile_row['field_ident']; |
227 | $field_ident = $profile_row['field_ident']; |
228 | |
229 | $now = getdate(); |
230 | |
231 | if (!$this->request->is_set($profile_row['field_ident'] . '_day')) |
232 | { |
233 | if ($profile_row['field_default_value'] == 'now') |
234 | { |
235 | $profile_row['field_default_value'] = sprintf('%2d-%2d-%4d', $now['mday'], $now['mon'], $now['year']); |
236 | } |
237 | list($day, $month, $year) = explode('-', ((!isset($this->user->profile_fields[$field_ident]) || $preview_options !== false) ? $profile_row['field_default_value'] : $this->user->profile_fields[$field_ident])); |
238 | } |
239 | else |
240 | { |
241 | if ($preview_options !== false && $profile_row['field_default_value'] == 'now') |
242 | { |
243 | $profile_row['field_default_value'] = sprintf('%2d-%2d-%4d', $now['mday'], $now['mon'], $now['year']); |
244 | list($day, $month, $year) = explode('-', ((!isset($this->user->profile_fields[$field_ident]) || $preview_options !== false) ? $profile_row['field_default_value'] : $this->user->profile_fields[$field_ident])); |
245 | } |
246 | else |
247 | { |
248 | $day = $this->request->variable($profile_row['field_ident'] . '_day', 0); |
249 | $month = $this->request->variable($profile_row['field_ident'] . '_month', 0); |
250 | $year = $this->request->variable($profile_row['field_ident'] . '_year', 0); |
251 | } |
252 | } |
253 | |
254 | $profile_row['s_day_options'] = '<option value="0"' . ((!$day) ? ' selected="selected"' : '') . '>--</option>'; |
255 | for ($i = 1; $i < 32; $i++) |
256 | { |
257 | $profile_row['s_day_options'] .= '<option value="' . $i . '"' . (($i == $day) ? ' selected="selected"' : '') . ">$i</option>"; |
258 | } |
259 | |
260 | $profile_row['s_month_options'] = '<option value="0"' . ((!$month) ? ' selected="selected"' : '') . '>--</option>'; |
261 | for ($i = 1; $i < 13; $i++) |
262 | { |
263 | $profile_row['s_month_options'] .= '<option value="' . $i . '"' . (($i == $month) ? ' selected="selected"' : '') . ">$i</option>"; |
264 | } |
265 | |
266 | $profile_row['s_year_options'] = '<option value="0"' . ((!$year) ? ' selected="selected"' : '') . '>--</option>'; |
267 | for ($i = 1901; $i <= $now['year'] + 50; $i++) |
268 | { |
269 | $profile_row['s_year_options'] .= '<option value="' . $i . '"' . (($i == $year) ? ' selected="selected"' : '') . ">$i</option>"; |
270 | } |
271 | |
272 | $profile_row['field_value'] = 0; |
273 | $this->template->assign_block_vars('date', array_change_key_case($profile_row, CASE_UPPER)); |
274 | } |
275 | |
276 | /** |
277 | * {@inheritDoc} |
278 | */ |
279 | public function get_field_ident($field_data) |
280 | { |
281 | return ''; |
282 | } |
283 | |
284 | /** |
285 | * {@inheritDoc} |
286 | */ |
287 | public function get_database_column_type() |
288 | { |
289 | return 'VCHAR:10'; |
290 | } |
291 | |
292 | /** |
293 | * {@inheritDoc} |
294 | */ |
295 | public function get_language_options($field_data) |
296 | { |
297 | $options = array( |
298 | 'lang_name' => 'string', |
299 | ); |
300 | |
301 | if ($field_data['lang_explain']) |
302 | { |
303 | $options['lang_explain'] = 'text'; |
304 | } |
305 | |
306 | return $options; |
307 | } |
308 | |
309 | /** |
310 | * {@inheritDoc} |
311 | */ |
312 | public function get_excluded_options($key, $action, $current_value, &$field_data, $step) |
313 | { |
314 | if ($step == 2 && $key == 'field_default_value') |
315 | { |
316 | $always_now = $this->request->variable('always_now', -1); |
317 | |
318 | if ($always_now == 1 || ($always_now === -1 && $current_value == 'now')) |
319 | { |
320 | $now = getdate(); |
321 | |
322 | $field_data['field_default_value_day'] = $now['mday']; |
323 | $field_data['field_default_value_month'] = $now['mon']; |
324 | $field_data['field_default_value_year'] = $now['year']; |
325 | $current_value = 'now'; |
326 | $this->request->overwrite('field_default_value', $current_value, \phpbb\request\request_interface::POST); |
327 | } |
328 | else |
329 | { |
330 | if ($this->request->is_set('field_default_value_day')) |
331 | { |
332 | $field_data['field_default_value_day'] = $this->request->variable('field_default_value_day', 0); |
333 | $field_data['field_default_value_month'] = $this->request->variable('field_default_value_month', 0); |
334 | $field_data['field_default_value_year'] = $this->request->variable('field_default_value_year', 0); |
335 | $current_value = sprintf('%2d-%2d-%4d', $field_data['field_default_value_day'], $field_data['field_default_value_month'], $field_data['field_default_value_year']); |
336 | $this->request->overwrite('field_default_value', $current_value, \phpbb\request\request_interface::POST); |
337 | } |
338 | else |
339 | { |
340 | list($field_data['field_default_value_day'], $field_data['field_default_value_month'], $field_data['field_default_value_year']) = explode('-', $current_value); |
341 | } |
342 | } |
343 | |
344 | return $current_value; |
345 | } |
346 | |
347 | return parent::get_excluded_options($key, $action, $current_value, $field_data, $step); |
348 | } |
349 | |
350 | /** |
351 | * {@inheritDoc} |
352 | */ |
353 | public function prepare_hidden_fields($step, $key, $action, &$field_data) |
354 | { |
355 | if ($key == 'field_default_value') |
356 | { |
357 | $always_now = $this->request->variable('always_now', 0); |
358 | |
359 | if ($always_now) |
360 | { |
361 | return 'now'; |
362 | } |
363 | else if ($this->request->is_set('field_default_value_day')) |
364 | { |
365 | $field_data['field_default_value_day'] = $this->request->variable('field_default_value_day', 0); |
366 | $field_data['field_default_value_month'] = $this->request->variable('field_default_value_month', 0); |
367 | $field_data['field_default_value_year'] = $this->request->variable('field_default_value_year', 0); |
368 | return sprintf('%2d-%2d-%4d', $field_data['field_default_value_day'], $field_data['field_default_value_month'], $field_data['field_default_value_year']); |
369 | } |
370 | } |
371 | |
372 | return parent::prepare_hidden_fields($step, $key, $action, $field_data); |
373 | } |
374 | } |