Verzeichnisstruktur phpBB-3.1.0
- Veröffentlicht
- 27.10.2014
So funktioniert es
|
Auf das letzte Element klicken. Dies geht jeweils ein Schritt zurück |
Auf das Icon klicken, dies öffnet das Verzeichnis. Nochmal klicken schließt das Verzeichnis. |
|
(Beispiel Datei-Icons)
|
Auf das Icon klicken um den Quellcode anzuzeigen |
type_date.php
001 <?php
002 /**
003 *
004 * This file is part of the phpBB Forum Software package.
005 *
006 * @copyright (c) phpBB Limited <https://www.phpbb.com>
007 * @license GNU General Public License, version 2 (GPL-2.0)
008 *
009 * For full copyright and license information, please see
010 * the docs/CREDITS.txt file.
011 *
012 */
013
014 namespace phpbb\profilefields\type;
015
016 class type_date extends type_base
017 {
018 /**
019 * Request object
020 * @var \phpbb\request\request
021 */
022 protected $request;
023
024 /**
025 * Template object
026 * @var \phpbb\template\template
027 */
028 protected $template;
029
030 /**
031 * User object
032 * @var \phpbb\user
033 */
034 protected $user;
035
036 /**
037 * Construct
038 *
039 * @param \phpbb\request\request $request Request object
040 * @param \phpbb\template\template $template Template object
041 * @param \phpbb\user $user User object
042 */
043 public function __construct(\phpbb\request\request $request, \phpbb\template\template $template, \phpbb\user $user)
044 {
045 $this->request = $request;
046 $this->template = $template;
047 $this->user = $user;
048 }
049
050 /**
051 * {@inheritDoc}
052 */
053 public function get_name_short()
054 {
055 return 'date';
056 }
057
058 /**
059 * {@inheritDoc}
060 */
061 public function get_options($default_lang_id, $field_data)
062 {
063 $profile_row = array(
064 'var_name' => 'field_default_value',
065 'lang_name' => $field_data['lang_name'],
066 'lang_explain' => $field_data['lang_explain'],
067 'lang_id' => $default_lang_id,
068 'field_default_value' => $field_data['field_default_value'],
069 'field_ident' => 'field_default_value',
070 'field_type' => $this->get_service_name(),
071 'field_length' => $field_data['field_length'],
072 'lang_options' => $field_data['lang_options'],
073 );
074
075 $always_now = request_var('always_now', -1);
076 if ($always_now == -1)
077 {
078 $s_checked = ($field_data['field_default_value'] == 'now') ? true : false;
079 }
080 else
081 {
082 $s_checked = ($always_now) ? true : false;
083 }
084
085 $options = array(
086 0 => array('TITLE' => $this->user->lang['DEFAULT_VALUE'], 'FIELD' => $this->process_field_row('preview', $profile_row)),
087 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>'),
088 );
089
090 return $options;
091 }
092
093 /**
094 * {@inheritDoc}
095 */
096 public function get_default_option_values()
097 {
098 return array(
099 '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 = $now['year'] - 100; $i <= $now['year'] + 100; $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 }
375