add some properties
[phpbb.git] / phpBB / includes / functions_profile_fields.php
blob4a7c4fde7019a57b42359f89a883cd1e68ce56a9
1 <?php
2 /**
4 * @package phpBB3
5 * @version $Id$
6 * @copyright (c) 2005 phpBB Group
7 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
9 */
11 /**
12 * @ignore
14 if (!defined('IN_PHPBB'))
16 exit;
19 /**
20 * Custom Profile Fields
21 * @package phpBB3
23 class custom_profile
25 public static $profile_types = array(FIELD_INT => 'int', FIELD_STRING => 'string', FIELD_TEXT => 'text', FIELD_BOOL => 'bool', FIELD_DROPDOWN => 'dropdown', FIELD_DATE => 'date');
26 private $profile_cache = array();
27 private $options_lang = array();
29 /**
30 * Assign editable fields to template, mode can be profile (for profile change) or register (for registration)
31 * Called by ucp_profile and ucp_register
32 * @access public
34 public function generate_profile_fields($mode, $lang_id)
36 global $db, $template, $auth;
38 $sql_where = '';
39 switch ($mode)
41 case 'register':
42 // If the field is required we show it on the registration page and do not show hidden fields
43 $sql_where .= ' AND (f.field_show_on_reg = 1 OR f.field_required = 1) AND f.field_hide = 0';
44 break;
46 case 'profile':
47 // Show hidden fields to moderators/admins
48 if (!$auth->acl_gets('a_', 'm_') && !$auth->acl_getf_global('m_'))
50 $sql_where .= ' AND f.field_hide = 0';
52 break;
54 default:
55 trigger_error('Wrong profile mode specified', E_USER_ERROR);
56 break;
59 $sql = 'SELECT l.*, f.*
60 FROM ' . PROFILE_LANG_TABLE . ' l, ' . PROFILE_FIELDS_TABLE . " f
61 WHERE f.field_active = 1
62 $sql_where
63 AND l.lang_id = $lang_id
64 AND l.field_id = f.field_id
65 ORDER BY f.field_order";
66 $result = $db->sql_query($sql);
68 while ($row = $db->sql_fetchrow($result))
70 // Return templated field
71 $tpl_snippet = $this->process_field_row('change', $row);
73 // Some types are multivalue, we can't give them a field_id as we would not know which to pick
74 $type = (int) $row['field_type'];
76 $template->assign_block_vars('profile_fields', array(
77 'LANG_NAME' => $row['lang_name'],
78 'LANG_EXPLAIN' => $row['lang_explain'],
79 'FIELD' => $tpl_snippet,
80 'FIELD_ID' => ($type == FIELD_DATE || ($type == FIELD_BOOL && $row['field_length'] == '1')) ? '' : 'pf_' . $row['field_ident'],
81 'S_REQUIRED' => ($row['field_required']) ? true : false)
84 $db->sql_freeresult($result);
87 /**
88 * Validate entered profile field data
89 * @access public
91 public function validate_profile_field($field_type, &$field_value, $field_data)
93 switch ($field_type)
95 case FIELD_INT:
96 case FIELD_DROPDOWN:
97 $field_value = (int) $field_value;
98 break;
100 case FIELD_BOOL:
101 $field_value = (bool) $field_value;
102 break;
105 switch ($field_type)
107 case FIELD_DATE:
108 $field_validate = explode('-', $field_value);
110 $day = (isset($field_validate[0])) ? (int) $field_validate[0] : 0;
111 $month = (isset($field_validate[1])) ? (int) $field_validate[1] : 0;
112 $year = (isset($field_validate[2])) ? (int) $field_validate[2] : 0;
114 if ((!$day || !$month || !$year) && !$field_data['field_required'])
116 return false;
119 if ((!$day || !$month || !$year) && $field_data['field_required'])
121 return 'FIELD_REQUIRED';
124 if ($day < 0 || $day > 31 || $month < 0 || $month > 12 || ($year < 1901 && $year > 0) || $year > gmdate('Y', time()) + 50)
126 return 'FIELD_INVALID_DATE';
129 if (checkdate($month, $day, $year) === false)
131 return 'FIELD_INVALID_DATE';
133 break;
135 case FIELD_BOOL:
136 if (!$field_value && $field_data['field_required'])
138 return 'FIELD_REQUIRED';
140 break;
142 case FIELD_INT:
143 if (empty($field_value) && !$field_data['field_required'])
145 return false;
148 if ($field_value < $field_data['field_minlen'])
150 return 'FIELD_TOO_SMALL';
152 else if ($field_value > $field_data['field_maxlen'])
154 return 'FIELD_TOO_LARGE';
156 break;
158 case FIELD_DROPDOWN:
159 if ($field_value == $field_data['field_novalue'] && $field_data['field_required'])
161 return 'FIELD_REQUIRED';
163 break;
165 case FIELD_STRING:
166 case FIELD_TEXT:
167 if (empty($field_value) && !$field_data['field_required'])
169 return false;
171 else if (empty($field_value) && $field_data['field_required'])
173 return 'FIELD_REQUIRED';
176 if ($field_data['field_minlen'] && utf8_strlen($field_value) < $field_data['field_minlen'])
178 return 'FIELD_TOO_SHORT';
180 else if ($field_data['field_maxlen'] && utf8_strlen($field_value) > $field_data['field_maxlen'])
182 return 'FIELD_TOO_LONG';
185 if (!empty($field_data['field_validation']) && $field_data['field_validation'] != '.*')
187 $field_validate = ($field_type == FIELD_STRING) ? $field_value : bbcode_nl2br($field_value);
188 if (!preg_match('#^' . str_replace('\\\\', '\\', $field_data['field_validation']) . '$#i', $field_validate))
190 return 'FIELD_INVALID_CHARS';
193 break;
196 return false;
200 * Build profile cache, used for display
201 * @access private
203 private function build_cache()
205 global $db, $user, $auth;
207 $this->profile_cache = array();
209 // Display hidden/no_view fields for admin/moderator
210 $sql = 'SELECT l.*, f.*
211 FROM ' . PROFILE_LANG_TABLE . ' l, ' . PROFILE_FIELDS_TABLE . ' f
212 WHERE l.lang_id = ' . $user->get_iso_lang_id() . '
213 AND f.field_active = 1 ' .
214 ((!$auth->acl_gets('a_', 'm_') && !$auth->acl_getf_global('m_')) ? ' AND f.field_hide = 0 ' : '') . '
215 AND f.field_no_view = 0
216 AND l.field_id = f.field_id
217 ORDER BY f.field_order';
218 $result = $db->sql_query($sql);
220 while ($row = $db->sql_fetchrow($result))
222 $this->profile_cache[$row['field_ident']] = $row;
224 $db->sql_freeresult($result);
228 * Get language entries for options and store them here for later use
230 private function get_option_lang($field_id, $lang_id, $field_type, $preview)
232 global $db;
234 if ($preview)
236 $lang_options = (!is_array($this->vars['lang_options'])) ? explode("\n", $this->vars['lang_options']) : $this->vars['lang_options'];
238 // @todo: ref optimize
239 foreach ($lang_options as $num => $var)
241 $this->options_lang[$field_id][$lang_id][($num + 1)] = $var;
244 else
246 $sql = 'SELECT option_id, lang_value
247 FROM ' . PROFILE_FIELDS_LANG_TABLE . "
248 WHERE field_id = $field_id
249 AND lang_id = $lang_id
250 AND field_type = $field_type
251 ORDER BY option_id";
252 $result = $db->sql_query($sql);
254 // @todo: ref optimize
255 while ($row = $db->sql_fetchrow($result))
257 $this->options_lang[$field_id][$lang_id][($row['option_id'] + 1)] = $row['lang_value'];
259 $db->sql_freeresult($result);
264 * Submit profile field
265 * @access public
267 public function submit_cp_field($mode, $lang_id, &$cp_data, &$cp_error)
269 global $auth, $db, $user;
271 $sql_where = '';
272 switch ($mode)
274 case 'register':
275 // If the field is required we show it on the registration page and do not show hidden fields
276 $sql_where .= ' AND (f.field_show_on_reg = 1 OR f.field_required = 1) AND f.field_hide = 0';
277 break;
279 case 'profile':
280 // Show hidden fields to moderators/admins
281 if (!$auth->acl_gets('a_', 'm_') && !$auth->acl_getf_global('m_'))
283 $sql_where .= ' AND f.field_hide = 0';
285 break;
287 default:
288 trigger_error('Wrong profile mode specified', E_USER_ERROR);
289 break;
292 $sql = 'SELECT l.*, f.*
293 FROM ' . PROFILE_LANG_TABLE . ' l, ' . PROFILE_FIELDS_TABLE . " f
294 WHERE l.lang_id = $lang_id
295 AND f.field_active = 1
296 $sql_where
297 AND l.field_id = f.field_id
298 ORDER BY f.field_order";
299 $result = $db->sql_query($sql);
301 while ($row = $db->sql_fetchrow($result))
303 $cp_data['pf_' . $row['field_ident']] = $this->get_profile_field($row);
304 $check_value = $cp_data['pf_' . $row['field_ident']];
306 if (($cp_result = $this->validate_profile_field($row['field_type'], $check_value, $row)) !== false)
308 // If not and only showing common error messages, use this one
309 $error = '';
310 switch ($cp_result)
312 case 'FIELD_INVALID_DATE':
313 case 'FIELD_REQUIRED':
314 $error = sprintf($user->lang[$cp_result], $row['lang_name']);
315 break;
317 case 'FIELD_TOO_SHORT':
318 case 'FIELD_TOO_SMALL':
319 $error = sprintf($user->lang[$cp_result], $row['lang_name'], $row['field_minlen']);
320 break;
322 case 'FIELD_TOO_LONG':
323 case 'FIELD_TOO_LARGE':
324 $error = sprintf($user->lang[$cp_result], $row['lang_name'], $row['field_maxlen']);
325 break;
327 case 'FIELD_INVALID_CHARS':
328 switch ($row['field_validation'])
330 case '[0-9]+':
331 $error = sprintf($user->lang[$cp_result . '_NUMBERS_ONLY'], $row['lang_name']);
332 break;
334 case '[\w]+':
335 $error = sprintf($user->lang[$cp_result . '_ALPHA_ONLY'], $row['lang_name']);
336 break;
338 case '[\w_\+\. \-\[\]]+':
339 $error = sprintf($user->lang[$cp_result . '_SPACERS_ONLY'], $row['lang_name']);
340 break;
342 break;
345 if ($error != '')
347 $cp_error[] = $error;
351 $db->sql_freeresult($result);
355 * Assign fields to template, used for viewprofile, viewtopic and memberlist (if load setting is enabled)
356 * This is directly connected to the user -> mode == grab is to grab the user specific fields, mode == show is for assigning the row to the template
357 * @access public
359 public function generate_profile_fields_template($mode, $user_id = 0, $profile_row = false)
361 global $db;
363 if ($mode == 'grab')
365 if (!is_array($user_id))
367 $user_id = array($user_id);
370 if (!sizeof($this->profile_cache))
372 $this->build_cache();
375 if (!sizeof($user_id))
377 return array();
380 $sql = 'SELECT *
381 FROM ' . PROFILE_FIELDS_DATA_TABLE . '
382 WHERE ' . $db->sql_in_set('user_id', array_map('intval', $user_id));
383 $result = $db->sql_query($sql);
385 $field_data = array();
386 while ($row = $db->sql_fetchrow($result))
388 $field_data[$row['user_id']] = $row;
390 $db->sql_freeresult($result);
392 $user_fields = array();
394 // Go through the fields in correct order
395 foreach (array_keys($this->profile_cache) as $used_ident)
397 foreach ($field_data as $user_id => $row)
399 $user_fields[$user_id][$used_ident]['value'] = $row['pf_' . $used_ident];
400 $user_fields[$user_id][$used_ident]['data'] = $this->profile_cache[$used_ident];
404 return $user_fields;
406 else if ($mode == 'show')
408 // $profile_row == $user_fields[$row['user_id']];
409 $tpl_fields = array();
410 $tpl_fields['row'] = $tpl_fields['blockrow'] = array();
412 foreach ($profile_row as $ident => $ident_ary)
414 $value = $this->get_profile_value($ident_ary);
416 if ($value === NULL)
418 continue;
421 $tpl_fields['row'] += array(
422 'PROFILE_' . strtoupper($ident) . '_VALUE' => $value,
423 'PROFILE_' . strtoupper($ident) . '_TYPE' => $ident_ary['data']['field_type'],
424 'PROFILE_' . strtoupper($ident) . '_NAME' => $ident_ary['data']['lang_name'],
425 'PROFILE_' . strtoupper($ident) . '_EXPLAIN'=> $ident_ary['data']['lang_explain'],
427 'S_PROFILE_' . strtoupper($ident) => true
430 $tpl_fields['blockrow'][] = array(
431 'PROFILE_FIELD_VALUE' => $value,
432 'PROFILE_FIELD_TYPE' => $ident_ary['data']['field_type'],
433 'PROFILE_FIELD_NAME' => $ident_ary['data']['lang_name'],
434 'PROFILE_FIELD_EXPLAIN' => $ident_ary['data']['lang_explain'],
436 'S_PROFILE_' . strtoupper($ident) => true
440 return $tpl_fields;
442 else
444 trigger_error('Wrong mode for custom profile', E_USER_ERROR);
449 * Get Profile Value for display
451 private function get_profile_value($ident_ary)
453 $value = $ident_ary['value'];
454 $field_type = $ident_ary['data']['field_type'];
456 switch (self::$profile_types[$field_type])
458 case 'int':
459 if ($value == '')
461 return NULL;
463 return (int) $value;
464 break;
466 case 'string':
467 case 'text':
468 if (!$value)
470 return NULL;
473 $value = make_clickable($value);
474 $value = censor_text($value);
475 $value = bbcode_nl2br($value);
476 return $value;
477 break;
479 // case 'datetime':
480 case 'date':
481 $date = explode('-', $value);
482 $day = (isset($date[0])) ? (int) $date[0] : 0;
483 $month = (isset($date[1])) ? (int) $date[1] : 0;
484 $year = (isset($date[2])) ? (int) $date[2] : 0;
486 if (!$day && !$month && !$year)
488 return NULL;
490 else if ($day && $month && $year)
492 global $user;
493 // d/m/y 00:00 GMT isn't necessarily on the same d/m/y in the user's timezone, so add the timezone seconds
494 return $user->format_date(gmmktime(0, 0, 0, $month, $day, $year) + $user->timezone + $user->dst, $user->lang['DATE_FORMAT'], true);
497 return $value;
498 break;
500 case 'dropdown':
501 $field_id = $ident_ary['data']['field_id'];
502 $lang_id = $ident_ary['data']['lang_id'];
503 if (!isset($this->options_lang[$field_id][$lang_id]))
505 $this->get_option_lang($field_id, $lang_id, FIELD_DROPDOWN, false);
508 if ($value == $ident_ary['data']['field_novalue'])
510 return NULL;
513 $value = (int) $value;
515 // User not having a value assigned
516 if (!isset($this->options_lang[$field_id][$lang_id][$value]))
518 return NULL;
521 return $this->options_lang[$field_id][$lang_id][$value];
522 break;
524 case 'bool':
525 $field_id = $ident_ary['data']['field_id'];
526 $lang_id = $ident_ary['data']['lang_id'];
527 if (!isset($this->options_lang[$field_id][$lang_id]))
529 $this->get_option_lang($field_id, $lang_id, FIELD_BOOL, false);
532 if ($ident_ary['data']['field_length'] == 1)
534 return (isset($this->options_lang[$field_id][$lang_id][(int) $value])) ? $this->options_lang[$field_id][$lang_id][(int) $value] : NULL;
536 else if (!$value)
538 return NULL;
540 else
542 return $this->options_lang[$field_id][$lang_id][(int) ($value) + 1];
544 break;
546 default:
547 trigger_error('Unknown profile type', E_USER_ERROR);
548 break;
553 * Get field value for registration/profile
554 * @access private
556 private function get_var($field_validation, &$profile_row, $default_value, $preview)
558 global $user;
560 $profile_row['field_ident'] = (isset($profile_row['var_name'])) ? $profile_row['var_name'] : 'pf_' . $profile_row['field_ident'];
561 $user_ident = $profile_row['field_ident'];
562 // checkbox - only testing for isset
563 if ($profile_row['field_type'] == FIELD_BOOL && $profile_row['field_length'] == 2)
565 $value = (phpbb_request::is_set($profile_row['field_ident'])) ? true : ((!isset($user->profile_fields[$user_ident]) || $preview) ? $default_value : $user->profile_fields[$user_ident]);
567 else if ($profile_row['field_type'] == FIELD_INT)
569 if (phpbb_request::is_set($profile_row['field_ident']))
571 $value = (request_var($profile_row['field_ident'], '') === '') ? null : request_var($profile_row['field_ident'], $default_value);
573 else
575 if (!$preview && isset($user->profile_fields[$user_ident]) && is_null($user->profile_fields[$user_ident]))
577 $value = null;
579 else if (!isset($user->profile_fields[$user_ident]) || $preview)
581 $value = $default_value;
583 else
585 $value = $user->profile_fields[$user_ident];
589 return (is_null($value)) ? '' : (int) $value;
591 else
593 $value = (phpbb_request::is_set($profile_row['field_ident'])) ? request_var($profile_row['field_ident'], $default_value, true) : ((!isset($user->profile_fields[$user_ident]) || $preview) ? $default_value : $user->profile_fields[$user_ident]);
595 if (gettype($value) == 'string')
597 $value = utf8_normalize_nfc($value);
601 switch ($field_validation)
603 case 'int':
604 return (int) $value;
605 break;
608 return $value;
612 * Process int-type
613 * @access private
615 private function generate_int($profile_row, $preview = false)
617 global $template;
619 $profile_row['field_value'] = $this->get_var('int', $profile_row, $profile_row['field_default_value'], $preview);
620 $template->assign_block_vars(self::$profile_types[$profile_row['field_type']], array_change_key_case($profile_row, CASE_UPPER));
624 * Process date-type
625 * @access private
627 private function generate_date($profile_row, $preview = false)
629 global $user, $template;
631 $profile_row['field_ident'] = (isset($profile_row['var_name'])) ? $profile_row['var_name'] : 'pf_' . $profile_row['field_ident'];
632 $user_ident = $profile_row['field_ident'];
634 $now = getdate();
636 if (!phpbb_request::is_set($profile_row['field_ident'] . '_day'))
638 if ($profile_row['field_default_value'] == 'now')
640 $profile_row['field_default_value'] = sprintf('%2d-%2d-%4d', $now['mday'], $now['mon'], $now['year']);
642 list($day, $month, $year) = explode('-', ((!isset($user->profile_fields[$user_ident]) || $preview) ? $profile_row['field_default_value'] : $user->profile_fields[$user_ident]));
644 else
646 if ($preview && $profile_row['field_default_value'] == 'now')
648 $profile_row['field_default_value'] = sprintf('%2d-%2d-%4d', $now['mday'], $now['mon'], $now['year']);
649 list($day, $month, $year) = explode('-', ((!isset($user->profile_fields[$user_ident]) || $preview) ? $profile_row['field_default_value'] : $user->profile_fields[$user_ident]));
651 else
653 $day = request_var($profile_row['field_ident'] . '_day', 0);
654 $month = request_var($profile_row['field_ident'] . '_month', 0);
655 $year = request_var($profile_row['field_ident'] . '_year', 0);
659 $profile_row['s_day_options'] = '<option value="0"' . ((!$day) ? ' selected="selected"' : '') . '>--</option>';
660 for ($i = 1; $i < 32; $i++)
662 $profile_row['s_day_options'] .= '<option value="' . $i . '"' . (($i == $day) ? ' selected="selected"' : '') . ">$i</option>";
665 $profile_row['s_month_options'] = '<option value="0"' . ((!$month) ? ' selected="selected"' : '') . '>--</option>';
666 for ($i = 1; $i < 13; $i++)
668 $profile_row['s_month_options'] .= '<option value="' . $i . '"' . (($i == $month) ? ' selected="selected"' : '') . ">$i</option>";
671 $profile_row['s_year_options'] = '<option value="0"' . ((!$year) ? ' selected="selected"' : '') . '>--</option>';
672 for ($i = $now['year'] - 100; $i <= $now['year'] + 100; $i++)
674 $profile_row['s_year_options'] .= '<option value="' . $i . '"' . (($i == $year) ? ' selected="selected"' : '') . ">$i</option>";
676 unset($now);
678 $profile_row['field_value'] = 0;
679 $template->assign_block_vars(self::$profile_types[$profile_row['field_type']], array_change_key_case($profile_row, CASE_UPPER));
683 * Process bool-type
684 * @access private
686 private function generate_bool($profile_row, $preview = false)
688 global $template;
690 $value = $this->get_var('int', $profile_row, $profile_row['field_default_value'], $preview);
692 $profile_row['field_value'] = $value;
693 $template->assign_block_vars(self::$profile_types[$profile_row['field_type']], array_change_key_case($profile_row, CASE_UPPER));
695 if ($profile_row['field_length'] == 1)
697 if (!isset($this->options_lang[$profile_row['field_id']][$profile_row['lang_id']]) || !sizeof($this->options_lang[$profile_row['field_id']][$profile_row['lang_id']]))
699 $this->get_option_lang($profile_row['field_id'], $profile_row['lang_id'], FIELD_BOOL, $preview);
702 foreach ($this->options_lang[$profile_row['field_id']][$profile_row['lang_id']] as $option_id => $option_value)
704 $template->assign_block_vars('bool.options', array(
705 'OPTION_ID' => $option_id,
706 'CHECKED' => ($value == $option_id) ? ' checked="checked"' : '',
707 'VALUE' => $option_value)
714 * Process string-type
715 * @access private
717 private function generate_string($profile_row, $preview = false)
719 global $template;
721 $profile_row['field_value'] = $this->get_var('string', $profile_row, $profile_row['lang_default_value'], $preview);
722 $template->assign_block_vars(self::$profile_types[$profile_row['field_type']], array_change_key_case($profile_row, CASE_UPPER));
726 * Process text-type
727 * @access private
729 private function generate_text($profile_row, $preview = false)
731 global $template, $user;
733 $field_length = explode('|', $profile_row['field_length']);
734 $profile_row['field_rows'] = $field_length[0];
735 $profile_row['field_cols'] = $field_length[1];
737 $profile_row['field_value'] = $this->get_var('string', $profile_row, $profile_row['lang_default_value'], $preview);
738 $template->assign_block_vars(self::$profile_types[$profile_row['field_type']], array_change_key_case($profile_row, CASE_UPPER));
742 * Process dropdown-type
743 * @access private
745 private function generate_dropdown($profile_row, $preview = false)
747 global $user, $template;
749 $value = $this->get_var('int', $profile_row, $profile_row['field_default_value'], $preview);
751 if (!isset($this->options_lang[$profile_row['field_id']]) || !isset($this->options_lang[$profile_row['field_id']][$profile_row['lang_id']]) || !sizeof($this->options_lang[$profile_row['field_id']][$profile_row['lang_id']]))
753 $this->get_option_lang($profile_row['field_id'], $profile_row['lang_id'], FIELD_DROPDOWN, $preview);
756 $profile_row['field_value'] = $value;
757 $template->assign_block_vars(self::$profile_types[$profile_row['field_type']], array_change_key_case($profile_row, CASE_UPPER));
759 foreach ($this->options_lang[$profile_row['field_id']][$profile_row['lang_id']] as $option_id => $option_value)
761 $template->assign_block_vars('dropdown.options', array(
762 'OPTION_ID' => $option_id,
763 'SELECTED' => ($value == $option_id) ? ' selected="selected"' : '',
764 'VALUE' => $option_value)
770 * Return Templated value/field. Possible values for $mode are:
771 * change == user is able to set/enter profile values; preview == just show the value
772 * @access private
774 private function process_field_row($mode, $profile_row)
776 global $template;
778 $preview = ($mode == 'preview') ? true : false;
780 // set template filename
781 $template->set_filenames(array(
782 'cp_body' => 'custom_profile_fields.html')
785 // empty previously filled blockvars
786 foreach (self::$profile_types as $field_case => $field_type)
788 $template->destroy_block_vars($field_type);
791 // Assign template variables
792 $type_func = 'generate_' . self::$profile_types[$profile_row['field_type']];
793 $this->$type_func($profile_row, $preview);
795 // Return templated data
796 return $template->assign_display('cp_body');
800 * Build Array for user insertion into custom profile fields table
802 public static function build_insert_sql_array($cp_data)
804 global $db, $user, $auth;
806 $sql_not_in = array();
807 foreach ($cp_data as $key => $null)
809 $sql_not_in[] = (strncmp($key, 'pf_', 3) === 0) ? substr($key, 3) : $key;
812 $sql = 'SELECT f.field_type, f.field_ident, f.field_default_value, l.lang_default_value
813 FROM ' . PROFILE_LANG_TABLE . ' l, ' . PROFILE_FIELDS_TABLE . ' f
814 WHERE l.lang_id = ' . $user->get_iso_lang_id() . '
815 ' . ((sizeof($sql_not_in)) ? ' AND ' . $db->sql_in_set('f.field_ident', $sql_not_in, true) : '') . '
816 AND l.field_id = f.field_id';
817 $result = $db->sql_query($sql);
819 while ($row = $db->sql_fetchrow($result))
821 if ($row['field_default_value'] == 'now' && $row['field_type'] == FIELD_DATE)
823 $now = getdate();
824 $row['field_default_value'] = sprintf('%2d-%2d-%4d', $now['mday'], $now['mon'], $now['year']);
827 $cp_data['pf_' . $row['field_ident']] = (in_array($row['field_type'], array(FIELD_TEXT, FIELD_STRING))) ? $row['lang_default_value'] : $row['field_default_value'];
829 $db->sql_freeresult($result);
831 return $cp_data;
835 * Get profile field value on submit
836 * @access private
838 private function get_profile_field($profile_row)
840 $var_name = 'pf_' . $profile_row['field_ident'];
842 switch ($profile_row['field_type'])
844 case FIELD_DATE:
846 if (!phpbb_request::is_set($var_name . '_day'))
848 if ($profile_row['field_default_value'] == 'now')
850 $now = getdate();
851 $profile_row['field_default_value'] = sprintf('%2d-%2d-%4d', $now['mday'], $now['mon'], $now['year']);
853 list($day, $month, $year) = explode('-', $profile_row['field_default_value']);
855 else
857 $day = request_var($var_name . '_day', 0);
858 $month = request_var($var_name . '_month', 0);
859 $year = request_var($var_name . '_year', 0);
862 $var = sprintf('%2d-%2d-%4d', $day, $month, $year);
863 break;
865 case FIELD_BOOL:
866 // Checkbox
867 if ($profile_row['field_length'] == 2)
869 $var = phpbb_request::is_set($var_name) ? 1 : 0;
871 else
873 $var = request_var($var_name, (int) $profile_row['field_default_value']);
875 break;
877 case FIELD_STRING:
878 case FIELD_TEXT:
879 $var = utf8_normalize_nfc(request_var($var_name, (string) $profile_row['field_default_value'], true));
880 break;
882 case FIELD_INT:
883 if (phpbb_request::is_set($var_name) && request_var($var_name, '') === '')
885 $var = NULL;
887 else
889 $var = request_var($var_name, (int) $profile_row['field_default_value']);
891 break;
893 case FIELD_DROPDOWN:
894 $var = request_var($var_name, (int) $profile_row['field_default_value']);
895 break;
897 default:
898 $var = request_var($var_name, $profile_row['field_default_value']);
899 break;
902 return $var;
907 * Custom Profile Fields ACP
908 * @package phpBB3
910 class custom_profile_admin extends custom_profile
912 public $vars = array();
915 * Return possible validation options
917 public function validate_options()
919 global $user;
921 $validate_ary = array('CHARS_ANY' => '.*', 'NUMBERS_ONLY' => '[0-9]+', 'ALPHA_ONLY' => '[\w]+', 'ALPHA_SPACERS' => '[\w_\+\. \-\[\]]+');
923 $validate_options = '';
924 foreach ($validate_ary as $lang => $value)
926 $selected = ($this->vars['field_validation'] == $value) ? ' selected="selected"' : '';
927 $validate_options .= '<option value="' . $value . '"' . $selected . '>' . $user->lang[$lang] . '</option>';
930 return $validate_options;
934 * Get string options for second step in ACP
936 public function get_string_options()
938 global $user;
940 $options = array(
941 0 => array('TITLE' => $user->lang['FIELD_LENGTH'], 'FIELD' => '<input type="text" name="field_length" size="5" value="' . $this->vars['field_length'] . '" />'),
942 1 => array('TITLE' => $user->lang['MIN_FIELD_CHARS'], 'FIELD' => '<input type="text" name="field_minlen" size="5" value="' . $this->vars['field_minlen'] . '" />'),
943 2 => array('TITLE' => $user->lang['MAX_FIELD_CHARS'], 'FIELD' => '<input type="text" name="field_maxlen" size="5" value="' . $this->vars['field_maxlen'] . '" />'),
944 3 => array('TITLE' => $user->lang['FIELD_VALIDATION'], 'FIELD' => '<select name="field_validation">' . $this->validate_options() . '</select>')
947 return $options;
951 * Get text options for second step in ACP
953 public function get_text_options()
955 global $user;
957 $options = array(
958 0 => array('TITLE' => $user->lang['FIELD_LENGTH'], 'FIELD' => '<input name="rows" size="5" value="' . $this->vars['rows'] . '" /> ' . $user->lang['ROWS'] . '</dd><dd><input name="columns" size="5" value="' . $this->vars['columns'] . '" /> ' . $user->lang['COLUMNS'] . ' <input type="hidden" name="field_length" value="' . $this->vars['field_length'] . '" />'),
959 1 => array('TITLE' => $user->lang['MIN_FIELD_CHARS'], 'FIELD' => '<input type="text" name="field_minlen" size="10" value="' . $this->vars['field_minlen'] . '" />'),
960 2 => array('TITLE' => $user->lang['MAX_FIELD_CHARS'], 'FIELD' => '<input type="text" name="field_maxlen" size="10" value="' . $this->vars['field_maxlen'] . '" />'),
961 3 => array('TITLE' => $user->lang['FIELD_VALIDATION'], 'FIELD' => '<select name="field_validation">' . $this->validate_options() . '</select>')
964 return $options;
968 * Get int options for second step in ACP
970 public function get_int_options()
972 global $user;
974 $options = array(
975 0 => array('TITLE' => $user->lang['FIELD_LENGTH'], 'FIELD' => '<input type="text" name="field_length" size="5" value="' . $this->vars['field_length'] . '" />'),
976 1 => array('TITLE' => $user->lang['MIN_FIELD_NUMBER'], 'FIELD' => '<input type="text" name="field_minlen" size="5" value="' . $this->vars['field_minlen'] . '" />'),
977 2 => array('TITLE' => $user->lang['MAX_FIELD_NUMBER'], 'FIELD' => '<input type="text" name="field_maxlen" size="5" value="' . $this->vars['field_maxlen'] . '" />'),
978 3 => array('TITLE' => $user->lang['DEFAULT_VALUE'], 'FIELD' => '<input type="post" name="field_default_value" value="' . $this->vars['field_default_value'] . '" />')
981 return $options;
985 * Get bool options for second step in ACP
987 public function get_bool_options()
989 global $user, $lang_defs;
991 $default_lang_id = $lang_defs['iso'][phpbb::$config['default_lang']];
993 $profile_row = array(
994 'var_name' => 'field_default_value',
995 'field_id' => 1,
996 'lang_name' => $this->vars['lang_name'],
997 'lang_explain' => $this->vars['lang_explain'],
998 'lang_id' => $default_lang_id,
999 'field_default_value' => $this->vars['field_default_value'],
1000 'field_ident' => 'field_default_value',
1001 'field_type' => FIELD_BOOL,
1002 'field_length' => $this->vars['field_length'],
1003 'lang_options' => $this->vars['lang_options']
1006 $options = array(
1007 0 => array('TITLE' => $user->lang['FIELD_TYPE'], 'EXPLAIN' => $user->lang['BOOL_TYPE_EXPLAIN'], 'FIELD' => '<label><input type="radio" class="radio" name="field_length" value="1"' . (($this->vars['field_length'] == 1) ? ' checked="checked"' : '') . ' onchange="document.getElementById(\'add_profile_field\').submit();" /> ' . $user->lang['RADIO_BUTTONS'] . '</label><label><input type="radio" class="radio" name="field_length" value="2"' . (($this->vars['field_length'] == 2) ? ' checked="checked"' : '') . ' onchange="document.getElementById(\'add_profile_field\').submit();" /> ' . $user->lang['CHECKBOX'] . '</label>'),
1008 1 => array('TITLE' => $user->lang['DEFAULT_VALUE'], 'FIELD' => $this->process_field_row('preview', $profile_row))
1011 return $options;
1015 * Get dropdown options for second step in ACP
1017 public function get_dropdown_options()
1019 global $user, $lang_defs;
1021 $default_lang_id = $lang_defs['iso'][phpbb::$config['default_lang']];
1023 $profile_row[0] = array(
1024 'var_name' => 'field_default_value',
1025 'field_id' => 1,
1026 'lang_name' => $this->vars['lang_name'],
1027 'lang_explain' => $this->vars['lang_explain'],
1028 'lang_id' => $default_lang_id,
1029 'field_default_value' => $this->vars['field_default_value'],
1030 'field_ident' => 'field_default_value',
1031 'field_type' => FIELD_DROPDOWN,
1032 'lang_options' => $this->vars['lang_options']
1035 $profile_row[1] = $profile_row[0];
1036 $profile_row[1]['var_name'] = 'field_novalue';
1037 $profile_row[1]['field_ident'] = 'field_novalue';
1038 $profile_row[1]['field_default_value'] = $this->vars['field_novalue'];
1040 $options = array(
1041 0 => array('TITLE' => $user->lang['DEFAULT_VALUE'], 'FIELD' => $this->process_field_row('preview', $profile_row[0])),
1042 1 => array('TITLE' => $user->lang['NO_VALUE_OPTION'], 'EXPLAIN' => $user->lang['NO_VALUE_OPTION_EXPLAIN'], 'FIELD' => $this->process_field_row('preview', $profile_row[1]))
1045 return $options;
1049 * Get date options for second step in ACP
1051 public function get_date_options()
1053 global $user, $lang_defs;
1055 $default_lang_id = $lang_defs['iso'][phpbb::$config['default_lang']];
1057 $profile_row = array(
1058 'var_name' => 'field_default_value',
1059 'lang_name' => $this->vars['lang_name'],
1060 'lang_explain' => $this->vars['lang_explain'],
1061 'lang_id' => $default_lang_id,
1062 'field_default_value' => $this->vars['field_default_value'],
1063 'field_ident' => 'field_default_value',
1064 'field_type' => FIELD_DATE,
1065 'field_length' => $this->vars['field_length']
1068 $always_now = request_var('always_now', -1);
1069 if ($always_now == -1)
1071 $s_checked = ($this->vars['field_default_value'] == 'now') ? true : false;
1073 else
1075 $s_checked = ($always_now) ? true : false;
1078 $options = array(
1079 0 => array('TITLE' => $user->lang['DEFAULT_VALUE'], 'FIELD' => $this->process_field_row('preview', $profile_row)),
1080 1 => array('TITLE' => $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();" /> ' . $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();" /> ' . $user->lang['NO'] . '</label>'),
1083 return $options;