PageRenderTime 54ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/phpBB/phpbb/profilefields/type/type_date.php

https://github.com/VSEphpbb/phpbb
PHP | 374 lines | 253 code | 48 blank | 73 comment | 65 complexity | 40e51c66a8a1ed13090f4732f602ec20 MD5 | raw file
  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. namespace phpbb\profilefields\type;
  14. class type_date extends type_base
  15. {
  16. /**
  17. * Request object
  18. * @var \phpbb\request\request
  19. */
  20. protected $request;
  21. /**
  22. * Template object
  23. * @var \phpbb\template\template
  24. */
  25. protected $template;
  26. /**
  27. * User object
  28. * @var \phpbb\user
  29. */
  30. protected $user;
  31. /**
  32. * Construct
  33. *
  34. * @param \phpbb\request\request $request Request object
  35. * @param \phpbb\template\template $template Template object
  36. * @param \phpbb\user $user User object
  37. */
  38. public function __construct(\phpbb\request\request $request, \phpbb\template\template $template, \phpbb\user $user)
  39. {
  40. $this->request = $request;
  41. $this->template = $template;
  42. $this->user = $user;
  43. }
  44. /**
  45. * {@inheritDoc}
  46. */
  47. public function get_name_short()
  48. {
  49. return 'date';
  50. }
  51. /**
  52. * {@inheritDoc}
  53. */
  54. public function get_options($default_lang_id, $field_data)
  55. {
  56. $profile_row = array(
  57. 'var_name' => 'field_default_value',
  58. 'lang_name' => $field_data['lang_name'],
  59. 'lang_explain' => $field_data['lang_explain'],
  60. 'lang_id' => $default_lang_id,
  61. 'field_default_value' => $field_data['field_default_value'],
  62. 'field_ident' => 'field_default_value',
  63. 'field_type' => $this->get_service_name(),
  64. 'field_length' => $field_data['field_length'],
  65. 'lang_options' => $field_data['lang_options'],
  66. );
  67. $always_now = request_var('always_now', -1);
  68. if ($always_now == -1)
  69. {
  70. $s_checked = ($field_data['field_default_value'] == 'now') ? true : false;
  71. }
  72. else
  73. {
  74. $s_checked = ($always_now) ? true : false;
  75. }
  76. $options = array(
  77. 0 => array('TITLE' => $this->user->lang['DEFAULT_VALUE'], 'FIELD' => $this->process_field_row('preview', $profile_row)),
  78. 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>'),
  79. );
  80. return $options;
  81. }
  82. /**
  83. * {@inheritDoc}
  84. */
  85. public function get_default_option_values()
  86. {
  87. return array(
  88. 'field_length' => 10,
  89. 'field_minlen' => 10,
  90. 'field_maxlen' => 10,
  91. 'field_validation' => '',
  92. 'field_novalue' => ' 0- 0- 0',
  93. 'field_default_value' => ' 0- 0- 0',
  94. );
  95. }
  96. /**
  97. * {@inheritDoc}
  98. */
  99. public function get_default_field_value($field_data)
  100. {
  101. if ($field_data['field_default_value'] == 'now')
  102. {
  103. $now = getdate();
  104. $field_data['field_default_value'] = sprintf('%2d-%2d-%4d', $now['mday'], $now['mon'], $now['year']);
  105. }
  106. return $field_data['field_default_value'];
  107. }
  108. /**
  109. * {@inheritDoc}
  110. */
  111. public function get_profile_field($profile_row)
  112. {
  113. $var_name = 'pf_' . $profile_row['field_ident'];
  114. if (!$this->request->is_set($var_name . '_day'))
  115. {
  116. if ($profile_row['field_default_value'] == 'now')
  117. {
  118. $now = getdate();
  119. $profile_row['field_default_value'] = sprintf('%2d-%2d-%4d', $now['mday'], $now['mon'], $now['year']);
  120. }
  121. list($day, $month, $year) = explode('-', $profile_row['field_default_value']);
  122. }
  123. else
  124. {
  125. $day = $this->request->variable($var_name . '_day', 0);
  126. $month = $this->request->variable($var_name . '_month', 0);
  127. $year = $this->request->variable($var_name . '_year', 0);
  128. }
  129. return sprintf('%2d-%2d-%4d', $day, $month, $year);
  130. }
  131. /**
  132. * {@inheritDoc}
  133. */
  134. public function validate_profile_field(&$field_value, $field_data)
  135. {
  136. $field_validate = explode('-', $field_value);
  137. $day = (isset($field_validate[0])) ? (int) $field_validate[0] : 0;
  138. $month = (isset($field_validate[1])) ? (int) $field_validate[1] : 0;
  139. $year = (isset($field_validate[2])) ? (int) $field_validate[2] : 0;
  140. if ((!$day || !$month || !$year) && !$field_data['field_required'])
  141. {
  142. return false;
  143. }
  144. if ((!$day || !$month || !$year) && $field_data['field_required'])
  145. {
  146. return $this->user->lang('FIELD_REQUIRED', $this->get_field_name($field_data['lang_name']));
  147. }
  148. if ($day < 0 || $day > 31 || $month < 0 || $month > 12 || ($year < 1901 && $year > 0) || $year > gmdate('Y', time()) + 50)
  149. {
  150. return $this->user->lang('FIELD_INVALID_DATE', $this->get_field_name($field_data['lang_name']));
  151. }
  152. if (checkdate($month, $day, $year) === false)
  153. {
  154. return $this->user->lang('FIELD_INVALID_DATE', $this->get_field_name($field_data['lang_name']));
  155. }
  156. return false;
  157. }
  158. /**
  159. * {@inheritDoc}
  160. */
  161. public function get_profile_value($field_value, $field_data)
  162. {
  163. $date = explode('-', $field_value);
  164. $day = (isset($date[0])) ? (int) $date[0] : 0;
  165. $month = (isset($date[1])) ? (int) $date[1] : 0;
  166. $year = (isset($date[2])) ? (int) $date[2] : 0;
  167. if (!$day && !$month && !$year && !$field_data['field_show_novalue'])
  168. {
  169. return null;
  170. }
  171. else if ($day && $month && $year)
  172. {
  173. // Date should display as the same date for every user regardless of timezone
  174. return $this->user->create_datetime()
  175. ->setDate($year, $month, $day)
  176. ->setTime(0, 0, 0)
  177. ->format($this->user->lang['DATE_FORMAT'], true);
  178. }
  179. return $field_value;
  180. }
  181. /**
  182. * {@inheritDoc}
  183. */
  184. public function get_profile_value_raw($field_value, $field_data)
  185. {
  186. if (($field_value === '' || $field_value === null) && !$field_data['field_show_novalue'])
  187. {
  188. return null;
  189. }
  190. return $field_value;
  191. }
  192. /**
  193. * {@inheritDoc}
  194. */
  195. public function generate_field($profile_row, $preview_options = false)
  196. {
  197. $profile_row['field_ident'] = (isset($profile_row['var_name'])) ? $profile_row['var_name'] : 'pf_' . $profile_row['field_ident'];
  198. $field_ident = $profile_row['field_ident'];
  199. $now = getdate();
  200. if (!$this->request->is_set($profile_row['field_ident'] . '_day'))
  201. {
  202. if ($profile_row['field_default_value'] == 'now')
  203. {
  204. $profile_row['field_default_value'] = sprintf('%2d-%2d-%4d', $now['mday'], $now['mon'], $now['year']);
  205. }
  206. 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]));
  207. }
  208. else
  209. {
  210. if ($preview_options !== false && $profile_row['field_default_value'] == 'now')
  211. {
  212. $profile_row['field_default_value'] = sprintf('%2d-%2d-%4d', $now['mday'], $now['mon'], $now['year']);
  213. 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]));
  214. }
  215. else
  216. {
  217. $day = $this->request->variable($profile_row['field_ident'] . '_day', 0);
  218. $month = $this->request->variable($profile_row['field_ident'] . '_month', 0);
  219. $year = $this->request->variable($profile_row['field_ident'] . '_year', 0);
  220. }
  221. }
  222. $profile_row['s_day_options'] = '<option value="0"' . ((!$day) ? ' selected="selected"' : '') . '>--</option>';
  223. for ($i = 1; $i < 32; $i++)
  224. {
  225. $profile_row['s_day_options'] .= '<option value="' . $i . '"' . (($i == $day) ? ' selected="selected"' : '') . ">$i</option>";
  226. }
  227. $profile_row['s_month_options'] = '<option value="0"' . ((!$month) ? ' selected="selected"' : '') . '>--</option>';
  228. for ($i = 1; $i < 13; $i++)
  229. {
  230. $profile_row['s_month_options'] .= '<option value="' . $i . '"' . (($i == $month) ? ' selected="selected"' : '') . ">$i</option>";
  231. }
  232. $profile_row['s_year_options'] = '<option value="0"' . ((!$year) ? ' selected="selected"' : '') . '>--</option>';
  233. for ($i = 1901; $i <= $now['year'] + 50; $i++)
  234. {
  235. $profile_row['s_year_options'] .= '<option value="' . $i . '"' . (($i == $year) ? ' selected="selected"' : '') . ">$i</option>";
  236. }
  237. $profile_row['field_value'] = 0;
  238. $this->template->assign_block_vars('date', array_change_key_case($profile_row, CASE_UPPER));
  239. }
  240. /**
  241. * {@inheritDoc}
  242. */
  243. public function get_field_ident($field_data)
  244. {
  245. return '';
  246. }
  247. /**
  248. * {@inheritDoc}
  249. */
  250. public function get_database_column_type()
  251. {
  252. return 'VCHAR:10';
  253. }
  254. /**
  255. * {@inheritDoc}
  256. */
  257. public function get_language_options($field_data)
  258. {
  259. $options = array(
  260. 'lang_name' => 'string',
  261. );
  262. if ($field_data['lang_explain'])
  263. {
  264. $options['lang_explain'] = 'text';
  265. }
  266. return $options;
  267. }
  268. /**
  269. * {@inheritDoc}
  270. */
  271. public function get_excluded_options($key, $action, $current_value, &$field_data, $step)
  272. {
  273. if ($step == 2 && $key == 'field_default_value')
  274. {
  275. $always_now = $this->request->variable('always_now', -1);
  276. if ($always_now == 1 || ($always_now === -1 && $current_value == 'now'))
  277. {
  278. $now = getdate();
  279. $field_data['field_default_value_day'] = $now['mday'];
  280. $field_data['field_default_value_month'] = $now['mon'];
  281. $field_data['field_default_value_year'] = $now['year'];
  282. $current_value = 'now';
  283. $this->request->overwrite('field_default_value', $current_value, \phpbb\request\request_interface::POST);
  284. }
  285. else
  286. {
  287. if ($this->request->is_set('field_default_value_day'))
  288. {
  289. $field_data['field_default_value_day'] = $this->request->variable('field_default_value_day', 0);
  290. $field_data['field_default_value_month'] = $this->request->variable('field_default_value_month', 0);
  291. $field_data['field_default_value_year'] = $this->request->variable('field_default_value_year', 0);
  292. $current_value = sprintf('%2d-%2d-%4d', $field_data['field_default_value_day'], $field_data['field_default_value_month'], $field_data['field_default_value_year']);
  293. $this->request->overwrite('field_default_value', $current_value, \phpbb\request\request_interface::POST);
  294. }
  295. else
  296. {
  297. list($field_data['field_default_value_day'], $field_data['field_default_value_month'], $field_data['field_default_value_year']) = explode('-', $current_value);
  298. }
  299. }
  300. return $current_value;
  301. }
  302. return parent::get_excluded_options($key, $action, $current_value, $field_data, $step);
  303. }
  304. /**
  305. * {@inheritDoc}
  306. */
  307. public function prepare_hidden_fields($step, $key, $action, &$field_data)
  308. {
  309. if ($key == 'field_default_value')
  310. {
  311. $always_now = $this->request->variable('always_now', 0);
  312. if ($always_now)
  313. {
  314. return 'now';
  315. }
  316. else if ($this->request->is_set('field_default_value_day'))
  317. {
  318. $field_data['field_default_value_day'] = $this->request->variable('field_default_value_day', 0);
  319. $field_data['field_default_value_month'] = $this->request->variable('field_default_value_month', 0);
  320. $field_data['field_default_value_year'] = $this->request->variable('field_default_value_year', 0);
  321. return sprintf('%2d-%2d-%4d', $field_data['field_default_value_day'], $field_data['field_default_value_month'], $field_data['field_default_value_year']);
  322. }
  323. }
  324. return parent::prepare_hidden_fields($step, $key, $action, $field_data);
  325. }
  326. }