[ Index ]

PHP Cross Reference of phpBB 3.0 Beta 3

title

Body

[close]

/includes/ -> functions_profile_fields.php (source)

   1  <?php
   2  /** 
   3  *
   4  * @package phpBB3
   5  * @version $Id: functions_profile_fields.php,v 1.46 2006/11/12 14:29:31 naderman Exp $
   6  * @copyright (c) 2005 phpBB Group 
   7  * @license http://opensource.org/licenses/gpl-license.php GNU Public License 
   8  *
   9  */
  10  
  11  /**
  12  * Custom Profile Fields
  13  * @package phpBB3
  14  */
  15  class custom_profile
  16  {
  17      var $profile_types = array(1 => 'int', 2 => 'string', 3 => 'text', 4 => 'bool', 5 => 'dropdown', 6 => 'date');
  18      var $profile_cache = array();
  19      var $options_lang = array();
  20  
  21      /**
  22      * Assign editable fields to template, mode can be profile (for profile change) or register (for registration)
  23      * Called by ucp_profile and ucp_register
  24      * @access public
  25      */
  26  	function generate_profile_fields($mode, $lang_id)
  27      {
  28          global $db, $template, $auth;
  29  
  30          $sql_where = '';
  31          switch ($mode)
  32          {
  33              case 'register':
  34                  // If the field is required we show it on the registration page and do not show hidden fields
  35                  $sql_where .= ' AND (f.field_show_on_reg = 1 OR f.field_required = 1) AND f.field_hide = 0';
  36              break;
  37  
  38              case 'profile':
  39                  // Show hidden fields to moderators/admins
  40                  if (!$auth->acl_gets('a_', 'm_'))
  41                  {
  42                      $sql_where .= ' AND f.field_hide = 0';
  43                  }
  44              break;
  45  
  46              default:
  47                  trigger_error('Wrong profile mode specified', E_USER_ERROR);
  48              break;
  49          }
  50  
  51          $sql = 'SELECT l.*, f.*
  52              FROM ' . PROFILE_LANG_TABLE . ' l, ' . PROFILE_FIELDS_TABLE . " f 
  53              WHERE f.field_active = 1
  54                  $sql_where
  55                  AND l.lang_id = $lang_id
  56                  AND l.field_id = f.field_id 
  57              ORDER BY f.field_order";
  58          $result = $db->sql_query($sql);
  59  
  60          while ($row = $db->sql_fetchrow($result))
  61          {
  62              // Return templated field
  63              $tpl_snippet = $this->process_field_row('change', $row);
  64  
  65              $template->assign_block_vars('profile_fields', array(
  66                  'LANG_NAME'        => $row['lang_name'],
  67                  'LANG_EXPLAIN'    => $row['lang_explain'],
  68                  'FIELD'            => $tpl_snippet,
  69                  'S_REQUIRED'    => ($row['field_required']) ? true : false)
  70              );
  71          }
  72          $db->sql_freeresult($result);
  73      }
  74  
  75      /**
  76      * Validate entered profile field data
  77      * @access public
  78      */
  79  	function validate_profile_field($field_type, &$field_value, $field_data)
  80      {
  81          switch ($field_type)
  82          {
  83              case FIELD_INT:
  84              case FIELD_DROPDOWN:
  85                  $field_value = (int) $field_value;
  86              break;
  87  
  88              case FIELD_BOOL:
  89                  $field_value = (bool) $field_value;
  90              break;
  91          }
  92  
  93          switch ($field_type)
  94          {
  95              case FIELD_DATE:
  96                  $field_validate = explode('-', $field_value);
  97                  
  98                  $day = (isset($field_validate[0])) ? (int) $field_validate[0] : 0;
  99                  $month = (isset($field_validate[1])) ? (int) $field_validate[1] : 0;
 100                  $year = (isset($field_validate[2])) ? (int) $field_validate[2] : 0;
 101  
 102                  if ((!$day || !$month || !$year) && !$field_data['field_required'])
 103                  {
 104                      return false;
 105                  }
 106  
 107                  if ((!$day || !$month || !$year) && $field_data['field_required'])
 108                  {
 109                      return 'FIELD_REQUIRED';
 110                  }
 111  
 112                  if ($day < 0 || $day > 31 || $month < 0 || $month > 12 || ($year < 1901 && $year > 0) || $year > gmdate('Y', time()))
 113                  {
 114                      return 'FIELD_INVALID_DATE';
 115                  }
 116  
 117                  if (checkdate($month, $day, $year) === false)
 118                  {
 119                      return 'FIELD_INVALID_DATE';
 120                  }
 121              break;
 122  
 123              case FIELD_BOOL:
 124                  if (!$field_value && $field_data['field_required'])
 125                  {
 126                      return 'FIELD_REQUIRED';
 127                  }
 128              break;
 129  
 130              case FIELD_INT:
 131                  if (empty($field_value) && !$field_data['field_required'])
 132                  {
 133                      return false;
 134                  }
 135  
 136                  if ($field_value < $field_data['field_minlen'])
 137                  {
 138                      return 'FIELD_TOO_SMALL';
 139                  }
 140                  else if ($field_value > $field_data['field_maxlen']) 
 141                  {
 142                      return 'FIELD_TOO_LARGE';
 143                  }
 144              break;
 145          
 146              case FIELD_DROPDOWN:
 147                  if ($field_value == $field_data['field_novalue'] && $field_data['field_required'])
 148                  {
 149                      return 'FIELD_REQUIRED';
 150                  }
 151              break;
 152              
 153              case FIELD_STRING:
 154              case FIELD_TEXT:
 155                  if (empty($field_value) && !$field_data['field_required'])
 156                  {
 157                      return false;
 158                  }
 159                  else if (empty($field_value) && $field_data['field_required'])
 160                  {
 161                      return 'FIELD_REQUIRED';
 162                  }
 163  
 164                  if ($field_data['field_minlen'] && utf8_strlen($field_value) < $field_data['field_minlen'])
 165                  {
 166                      return 'FIELD_TOO_SHORT';
 167                  }
 168                  else if ($field_data['field_maxlen'] && utf8_strlen($field_value) > $field_data['field_maxlen'])
 169                  {
 170                      return 'FIELD_TOO_LONG';
 171                  }
 172  
 173                  if (!empty($field_data['field_validation']) && $field_data['field_validation'] != '.*')
 174                  {
 175                      $field_validate = ($field_type == FIELD_STRING) ? $field_value : str_replace("\n", ' ', $field_value);
 176                      if (!preg_match('#^' . str_replace('\\\\', '\\', $field_data['field_validation']) . '$#i', $field_validate))
 177                      {
 178                          return 'FIELD_INVALID_CHARS';
 179                      }
 180                  }
 181              break;
 182          }
 183  
 184          return false;
 185      }
 186  
 187      /**
 188      * Build profile cache, used for display
 189      * @access private
 190      */
 191  	function build_cache()
 192      {
 193          global $db, $user, $auth;
 194  
 195          $this->profile_cache = array();
 196          
 197          // Display hidden/no_view fields for admin/moderator
 198          $sql = 'SELECT l.*, f.*
 199              FROM ' . PROFILE_LANG_TABLE . ' l, ' . PROFILE_FIELDS_TABLE . ' f 
 200              WHERE l.lang_id = ' . $user->get_iso_lang_id() . '
 201                  AND f.field_active = 1 ' .
 202                  ((!$auth->acl_gets('a_', 'm_')) ? '    AND f.field_hide = 0 ' : '') . '
 203                  AND f.field_no_view = 0
 204                  AND l.field_id = f.field_id 
 205              ORDER BY f.field_order';
 206          $result = $db->sql_query($sql);
 207  
 208          while ($row = $db->sql_fetchrow($result))
 209          {
 210              $this->profile_cache[$row['field_ident']] = $row;
 211          }
 212          $db->sql_freeresult($result);
 213      }
 214  
 215      /**
 216      * Get language entries for options and store them here for later use
 217      */
 218  	function get_option_lang($field_id, $lang_id, $field_type, $preview)
 219      {
 220          global $db;
 221  
 222          if ($preview)
 223          {
 224              $lang_options = (!is_array($this->vars['lang_options'])) ? explode("\n", $this->vars['lang_options']) : $this->vars['lang_options'];
 225              
 226              foreach ($lang_options as $num => $var)
 227              {
 228                  $this->options_lang[$field_id][$lang_id][($num + 1)] = $var;
 229              }
 230          }
 231          else
 232          {
 233              $sql = 'SELECT option_id, lang_value
 234                  FROM ' . PROFILE_FIELDS_LANG_TABLE . "
 235                      WHERE field_id = $field_id
 236                      AND lang_id = $lang_id
 237                      AND field_type = $field_type
 238                  ORDER BY option_id";
 239              $result = $db->sql_query($sql);
 240  
 241              while ($row = $db->sql_fetchrow($result))
 242              {
 243                  $this->options_lang[$field_id][$lang_id][($row['option_id'] + 1)] = $row['lang_value'];
 244              }
 245              $db->sql_freeresult($result);
 246          }
 247      }
 248  
 249      /**
 250      * Submit profile field
 251      * @access public
 252      */
 253  	function submit_cp_field($mode, $lang_id, &$cp_data, &$cp_error)
 254      {
 255          global $auth, $db, $user;
 256  
 257          $sql_where = '';
 258          switch ($mode)
 259          {
 260              case 'register':
 261                  // If the field is required we show it on the registration page and do not show hidden fields
 262                  $sql_where .= ' AND (f.field_show_on_reg = 1 OR f.field_required = 1) AND f.field_hide = 0';
 263              break;
 264  
 265              case 'profile':
 266                  // Show hidden fields to moderators/admins
 267                  if (!$auth->acl_gets('a_', 'm_'))
 268                  {
 269                      $sql_where .= ' AND f.field_hide = 0';
 270                  }
 271              break;
 272  
 273              default:
 274                  trigger_error('Wrong profile mode specified', E_USER_ERROR);
 275              break;
 276          }
 277  
 278          $sql = 'SELECT l.*, f.*
 279              FROM ' . PROFILE_LANG_TABLE . ' l, ' . PROFILE_FIELDS_TABLE . " f 
 280              WHERE l.lang_id = $lang_id
 281                  AND f.field_active = 1
 282                  $sql_where
 283                  AND l.field_id = f.field_id 
 284              ORDER BY f.field_order";
 285          $result = $db->sql_query($sql);
 286  
 287          while ($row = $db->sql_fetchrow($result))
 288          {
 289              $cp_data['pf_' . $row['field_ident']] = $this->get_profile_field($row);
 290              $check_value = $cp_data['pf_' . $row['field_ident']];
 291  
 292              if (($cp_result = $this->validate_profile_field($row['field_type'], $check_value, $row)) !== false)
 293              {
 294                  // If not and only showing common error messages, use this one
 295                  $error = '';
 296                  switch ($cp_result)
 297                  {
 298                      case 'FIELD_INVALID_DATE':
 299                      case 'FIELD_REQUIRED':
 300                          $error = sprintf($user->lang[$cp_result], $row['lang_name']);
 301                      break;
 302  
 303                      case 'FIELD_TOO_SHORT':
 304                      case 'FIELD_TOO_SMALL':
 305                          $error = sprintf($user->lang[$cp_result], $row['lang_name'], $row['field_minlen']);
 306                      break;
 307                      
 308                      case 'FIELD_TOO_LONG':
 309                      case 'FIELD_TOO_LARGE':
 310                          $error = sprintf($user->lang[$cp_result], $row['lang_name'], $row['field_maxlen']);
 311                      break;
 312                      
 313                      case 'FIELD_INVALID_CHARS':
 314                          switch ($row['field_validation'])
 315                          {
 316                              case '[0-9]+':
 317                                  $error = sprintf($user->lang[$cp_result . '_NUMBERS_ONLY'], $row['lang_name']);
 318                              break;
 319  
 320                              case '[\w]+':
 321                                  $error = sprintf($user->lang[$cp_result . '_ALPHA_ONLY'], $row['lang_name']);
 322                              break;
 323  
 324                              case '[\w_\+\. \-\[\]]+':
 325                                  $error = sprintf($user->lang[$cp_result . '_SPACERS_ONLY'], $row['lang_name']);
 326                              break;
 327                          }
 328                      break;
 329                  }
 330                  
 331                  if ($error != '')
 332                  {
 333                      $cp_error[] = $error;
 334                  }
 335              }
 336          }
 337          $db->sql_freeresult($result);
 338      }
 339  
 340      /**
 341      * Assign fields to template, used for viewprofile, viewtopic and memberlist (if load setting is enabled)
 342      * 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
 343      * @access public
 344      */
 345  	function generate_profile_fields_template($mode, $user_id = 0, $profile_row = false)
 346      {
 347          global $db;
 348  
 349          if ($mode == 'grab')
 350          {
 351              if (!is_array($user_id))
 352              {
 353                  $user_id = array($user_id);
 354              }
 355  
 356              if (!sizeof($this->profile_cache))
 357              {
 358                  $this->build_cache();
 359              }
 360  
 361              if (!sizeof($user_id))
 362              {
 363                  return array();
 364              }
 365  
 366              $sql = 'SELECT *
 367                  FROM ' . PROFILE_FIELDS_DATA_TABLE . '
 368                  WHERE ' . $db->sql_in_set('user_id', array_map('intval', $user_id));
 369              $result = $db->sql_query($sql);
 370  
 371              $field_data = array();
 372              while ($row = $db->sql_fetchrow($result))
 373              {
 374                  $field_data[$row['user_id']] = $row;
 375              }
 376              $db->sql_freeresult($result);
 377  
 378              $user_fields = array();
 379  
 380              // Go through the fields in correct order
 381              foreach (array_keys($this->profile_cache) as $used_ident)
 382              {
 383                  foreach ($field_data as $user_id => $row)
 384                  {
 385                      $user_fields[$user_id][$used_ident]['value'] = $row['pf_' . $used_ident];
 386                      $user_fields[$user_id][$used_ident]['data'] = $this->profile_cache[$used_ident];
 387                  }
 388              }
 389  
 390              return $user_fields;
 391          }
 392          else if ($mode == 'show')
 393          {
 394              // $profile_row == $user_fields[$row['user_id']];
 395              $tpl_fields = array();
 396              $tpl_fields['row'] = $tpl_fields['blockrow'] = array();
 397  
 398              foreach ($profile_row as $ident => $ident_ary)
 399              {
 400                  $value = $this->get_profile_value($ident_ary);
 401  
 402                  if ($value === NULL)
 403                  {
 404                      continue;
 405                  }
 406  
 407                  $tpl_fields['row'] += array(
 408                      'PROFILE_' . strtoupper($ident) . '_VALUE'    => $value,
 409                      'PROFILE_' . strtoupper($ident) . '_TYPE'    => $ident_ary['data']['field_type'],
 410                      'PROFILE_' . strtoupper($ident) . '_NAME'    => $ident_ary['data']['lang_name'],
 411                      'PROFILE_' . strtoupper($ident) . '_EXPLAIN'=> $ident_ary['data']['lang_explain'],
 412  
 413                      'S_PROFILE_' . strtoupper($ident)            => true
 414                  );
 415  
 416                  $tpl_fields['blockrow'][] = array(
 417                      'PROFILE_FIELD_VALUE'    => $value,
 418                      'PROFILE_FIELD_TYPE'    => $ident_ary['data']['field_type'],
 419                      'PROFILE_FIELD_NAME'    => $ident_ary['data']['lang_name'],
 420                      'PROFILE_FIELD_EXPLAIN'    => $ident_ary['data']['lang_explain'],
 421  
 422                      'S_PROFILE_' . strtoupper($ident)        => true
 423                  );
 424              }
 425          
 426              return $tpl_fields;
 427          }
 428          else
 429          {
 430              trigger_error('Wrong mode for custom profile', E_USER_ERROR);
 431          }
 432      }
 433  
 434      /**
 435      * Get Profile Value for display
 436      */
 437  	function get_profile_value($ident_ary)
 438      {
 439          $value = $ident_ary['value'];
 440          $field_type = $ident_ary['data']['field_type'];
 441  
 442          switch ($this->profile_types[$field_type])
 443          {
 444              case 'int':
 445                  if ($value == '')
 446                  {
 447                      return NULL;
 448                  }
 449                  return (int) $value;
 450              break;
 451  
 452              case 'string':
 453              case 'text':
 454                  if (!$value)
 455                  {
 456                      return NULL;
 457                  }
 458  
 459                  $value = make_clickable($value);
 460                  $value = censor_text($value);
 461                  $value = str_replace("\n", '<br />', $value);
 462                  return $value;
 463              break;
 464  
 465              // case 'datetime':
 466              case 'date':
 467                  $date = explode('-', $value);
 468                  $month = (isset($date[0])) ? (int) $date[0] : 0;
 469                  $day = (isset($date[1])) ? (int) $date[1] : 0;
 470                  $year = (isset($date[2])) ? (int) $date[2] : 0;
 471  
 472                  if (!$day && !$month && !$year)
 473                  {
 474                      return NULL;
 475                  }
 476                  else if ($day && $month && $year)
 477                  {
 478                      return sprintf('%4d-%02d-%02d', $year, $month, $day);
 479                  }
 480  
 481                  return $value;
 482              break;
 483  
 484              case 'dropdown':
 485                  $field_id = $ident_ary['data']['field_id'];
 486                  $lang_id = $ident_ary['data']['lang_id'];
 487                  if (!isset($this->options_lang[$field_id][$lang_id]))
 488                  {
 489                      $this->get_option_lang($field_id, $lang_id, FIELD_DROPDOWN, false);
 490                  }
 491  
 492                  if ($value == $ident_ary['data']['field_novalue'])
 493                  {
 494                      return NULL;
 495                  }
 496  
 497                  $value = (int) $value;
 498  
 499                  // User not having a value assigned
 500                  if (!isset($this->options_lang[$field_id][$lang_id][$value]))
 501                  {
 502                      return NULL;
 503                  }
 504  
 505                  return $this->options_lang[$field_id][$lang_id][$value];
 506              break;
 507  
 508              case 'bool':
 509                  $field_id = $ident_ary['data']['field_id'];
 510                  $lang_id = $ident_ary['data']['lang_id'];
 511                  if (!isset($this->options_lang[$field_id][$lang_id]))
 512                  {
 513                      $this->get_option_lang($field_id, $lang_id, FIELD_BOOL, false);
 514                  }
 515  
 516                  if ($ident_ary['data']['field_length'] == 1)
 517                  {
 518                      return (isset($this->options_lang[$field_id][$lang_id][(int) $value])) ? $this->options_lang[$field_id][$lang_id][(int) $value] : NULL;
 519                  }
 520                  else if (!$value)
 521                  {
 522                      return NULL;
 523                  }
 524                  else
 525                  {
 526                      return $this->options_lang[$field_id][$lang_id][(int) ($value + 1)];
 527                  }
 528              break;
 529  
 530              default:
 531                  trigger_error('Unknown profile type', E_USER_ERROR);
 532              break;
 533          }
 534      }
 535  
 536      /**
 537      * Get field value for registration/profile
 538      * @access private
 539      */
 540  	function get_var($field_validation, &$profile_row, $default_value, $preview)
 541      {
 542          global $user;
 543  
 544          $profile_row['field_ident'] = (isset($profile_row['var_name'])) ? $profile_row['var_name'] : 'pf_' . $profile_row['field_ident'];
 545          $user_ident = 'pf_' . str_replace('pf_', '', $profile_row['field_ident']);
 546  
 547          // checkbox - only testing for isset
 548          if ($profile_row['field_type'] == FIELD_BOOL && $profile_row['field_length'] == 2)
 549          {
 550              $value = (isset($_REQUEST[$profile_row['field_ident']])) ? true : ((!isset($user->profile_fields[$user_ident]) || $preview) ? $default_value : $user->profile_fields[$user_ident]);
 551          }
 552          else if ($profile_row['field_type'] == FIELD_INT)
 553          {
 554              if (isset($_REQUEST[$profile_row['field_ident']]))
 555              {
 556                  $value = ($_REQUEST[$profile_row['field_ident']] === '') ? NULL : request_var($profile_row['field_ident'], $default_value);
 557              }
 558              else
 559              {
 560                  if (!$preview && isset($user->profile_fields[$user_ident]) && is_null($user->profile_fields[$user_ident]))
 561                  {
 562                      $value = NULL;
 563                  }
 564                  else if (!isset($user->profile_fields[$user_ident]) || $preview)
 565                  {
 566                      $value = $default_value;
 567                  }
 568                  else
 569                  {
 570                      $value = $user->profile_fields[$user_ident];
 571                  }
 572              }
 573  
 574              return (is_null($value)) ? '' : (int) $value;
 575          }
 576          else
 577          {
 578              $value = (isset($_REQUEST[$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]);
 579              
 580              if (gettype($value) == 'string')
 581              {
 582                  utf8_normalize_nfc(&$value);
 583              }
 584          }
 585  
 586          switch ($field_validation)
 587          {
 588              case 'int':
 589                  return (int) $value;
 590              break;
 591          }
 592  
 593          return $value;
 594      }
 595  
 596      /**
 597      * Process int-type
 598      * @access private
 599      */
 600  	function generate_int($profile_row, $preview = false)
 601      {
 602          global $template;
 603  
 604          $profile_row['field_value'] = $this->get_var('int', $profile_row, $profile_row['field_default_value'], $preview);
 605          $template->assign_block_vars($this->profile_types[$profile_row['field_type']], array_change_key_case($profile_row, CASE_UPPER));
 606      }
 607  
 608      /**
 609      * Process date-type
 610      * @access private
 611      */
 612  	function generate_date($profile_row, $preview = false)
 613      {
 614          global $user, $template;
 615  
 616          $profile_row['field_ident'] = (isset($profile_row['var_name'])) ? $profile_row['var_name'] : 'pf_' . $profile_row['field_ident'];
 617          $user_ident = 'pf_' . str_replace('pf_', '', $profile_row['field_ident']);
 618  
 619          $now = getdate();
 620  
 621          if (!isset($_REQUEST[$profile_row['field_ident'] . '_day']))
 622          {
 623              if ($profile_row['field_default_value'] == 'now')
 624              {
 625                  $profile_row['field_default_value'] = sprintf('%2d-%2d-%4d', $now['mday'], $now['mon'], $now['year']);
 626              }
 627              list($day, $month, $year) = explode('-', ((!isset($user->profile_fields[$user_ident]) || $preview) ? $profile_row['field_default_value'] : $user->profile_fields[$user_ident]));
 628          }
 629          else
 630          {
 631              if ($preview && $profile_row['field_default_value'] == 'now')
 632              {
 633                  $profile_row['field_default_value'] = sprintf('%2d-%2d-%4d', $now['mday'], $now['mon'], $now['year']);
 634                  list($day, $month, $year) = explode('-', ((!isset($user->profile_fields[$user_ident]) || $preview) ? $profile_row['field_default_value'] : $user->profile_fields[$user_ident]));
 635              }
 636              else
 637              {
 638                  $day = request_var($profile_row['field_ident'] . '_day', 0);
 639                  $month = request_var($profile_row['field_ident'] . '_month', 0);
 640                  $year = request_var($profile_row['field_ident'] . '_year', 0);
 641              }
 642          }
 643  
 644          $profile_row['s_day_options'] = '<option value="0"' . ((!$day) ? ' selected="selected"' : '') . '>--</option>';
 645          for ($i = 1; $i < 32; $i++)
 646          {
 647              $profile_row['s_day_options'] .= '<option value="' . $i . '"' . (($i == $day) ? ' selected="selected"' : '') . ">$i</option>";
 648          }
 649  
 650          $profile_row['s_month_options'] = '<option value="0"' . ((!$month) ? ' selected="selected"' : '') . '>--</option>';
 651          for ($i = 1; $i < 13; $i++)
 652          {
 653              $profile_row['s_month_options'] .= '<option value="' . $i . '"' . (($i == $month) ? ' selected="selected"' : '') . ">$i</option>";
 654          }
 655  
 656          $profile_row['s_year_options'] = '<option value="0"' . ((!$year) ? ' selected="selected"' : '') . '>--</option>';
 657          for ($i = $now['year'] - 100; $i <= $now['year']; $i++)
 658          {
 659              $profile_row['s_year_options'] .= '<option value="' . $i . '"' . (($i == $year) ? ' selected="selected"' : '') . ">$i</option>";
 660          }
 661          unset($now);
 662          
 663          $profile_row['field_value'] = 0;
 664          $template->assign_block_vars($this->profile_types[$profile_row['field_type']], array_change_key_case($profile_row, CASE_UPPER));
 665      }
 666  
 667      /**
 668      * Process bool-type
 669      * @access private
 670      */
 671  	function generate_bool($profile_row, $preview = false)
 672      {
 673          global $template;
 674  
 675          $value = $this->get_var('int', $profile_row, $profile_row['field_default_value'], $preview);
 676  
 677          $profile_row['field_value'] = $value;
 678          $template->assign_block_vars($this->profile_types[$profile_row['field_type']], array_change_key_case($profile_row, CASE_UPPER));
 679  
 680          if ($profile_row['field_length'] == 1)
 681          {
 682              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']]))
 683              {
 684                  $this->get_option_lang($profile_row['field_id'], $profile_row['lang_id'], FIELD_BOOL, $preview);
 685              }
 686  
 687              foreach ($this->options_lang[$profile_row['field_id']][$profile_row['lang_id']] as $option_id => $option_value)
 688              {
 689                  $template->assign_block_vars('bool.options', array(
 690                      'OPTION_ID'    => $option_id,
 691                      'CHECKED'    => ($value == $option_id) ? ' checked="checked"' : '',
 692                      'VALUE'        => $option_value)
 693                  );
 694              }
 695          }
 696      }
 697  
 698      /**
 699      * Process string-type
 700      * @access private
 701      */
 702  	function generate_string($profile_row, $preview = false)
 703      {
 704          global $template;
 705  
 706          $profile_row['field_value'] = $this->get_var('string', $profile_row, $profile_row['lang_default_value'], $preview);
 707          $template->assign_block_vars($this->profile_types[$profile_row['field_type']], array_change_key_case($profile_row, CASE_UPPER));
 708      }
 709  
 710      /**
 711      * Process text-type
 712      * @access private
 713      */
 714  	function generate_text($profile_row, $preview = false)
 715      {
 716          global $template;
 717          global $user, $phpEx, $phpbb_root_path;
 718  
 719          $field_length = explode('|', $profile_row['field_length']);
 720          $profile_row['field_rows'] = $field_length[0];
 721          $profile_row['field_cols'] = $field_length[1];
 722  
 723          $profile_row['field_value'] = $this->get_var('string', $profile_row, $profile_row['lang_default_value'], $preview);
 724          $template->assign_block_vars($this->profile_types[$profile_row['field_type']], array_change_key_case($profile_row, CASE_UPPER));
 725      }
 726  
 727      /**
 728      * Process dropdown-type
 729      * @access private
 730      */
 731  	function generate_dropdown($profile_row, $preview = false)
 732      {
 733          global $user, $template;
 734  
 735          $value = $this->get_var('int', $profile_row, $profile_row['field_default_value'], $preview);
 736  
 737          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']]))
 738          {
 739              $this->get_option_lang($profile_row['field_id'], $profile_row['lang_id'], FIELD_DROPDOWN, $preview);
 740          }
 741  
 742          $profile_row['field_value'] = $value;
 743          $template->assign_block_vars($this->profile_types[$profile_row['field_type']], array_change_key_case($profile_row, CASE_UPPER));
 744  
 745          foreach ($this->options_lang[$profile_row['field_id']][$profile_row['lang_id']] as $option_id => $option_value)
 746          {
 747              $template->assign_block_vars('dropdown.options', array(
 748                  'OPTION_ID'    => $option_id,
 749                  'SELECTED'    => ($value == $option_id) ? ' selected="selected"' : '',
 750                  'VALUE'        => $option_value)
 751              );
 752          }
 753      }
 754  
 755      /**
 756      * Return Templated value/field. Possible values for $mode are:
 757      * change == user is able to set/enter profile values; preview == just show the value
 758      * @access private
 759      */
 760  	function process_field_row($mode, $profile_row)
 761      {
 762          global $template;
 763  
 764          $preview = ($mode == 'preview') ? true : false;
 765  
 766          // set template filename
 767          $template->set_filenames(array(
 768              'cp_body'        => 'custom_profile_fields.html')
 769          );
 770  
 771          // empty previously filled blockvars
 772          foreach ($this->profile_types as $field_case => $field_type)
 773          {
 774              $template->destroy_block_vars($field_type);
 775          }
 776  
 777          // Assign template variables
 778          $type_func = 'generate_' . $this->profile_types[$profile_row['field_type']];
 779          $this->$type_func($profile_row, $preview);
 780  
 781          // Return templated data
 782          return $template->assign_display('cp_body');
 783      }
 784  
 785      /**
 786      * Build Array for user insertion into custom profile fields table
 787      */
 788  	function build_insert_sql_array($cp_data)
 789      {
 790          global $db, $user, $auth;
 791  
 792          $sql_not_in = array();
 793          foreach ($cp_data as $key => $null)
 794          {
 795              $sql_not_in[] = (strncmp($key, 'pf_', 3) === 0) ? substr($key, 3) : $key;
 796          }
 797  
 798          $sql = 'SELECT f.field_type, f.field_ident, f.field_default_value, l.lang_default_value
 799              FROM ' . PROFILE_LANG_TABLE . ' l, ' . PROFILE_FIELDS_TABLE . ' f 
 800              WHERE l.lang_id = ' . $user->get_iso_lang_id() . ' 
 801                  ' . ((sizeof($sql_not_in)) ? ' AND ' . $db->sql_in_set('f.field_ident', $sql_not_in, true) : '') . '
 802                  AND l.field_id = f.field_id';
 803          $result = $db->sql_query($sql);
 804  
 805          while ($row = $db->sql_fetchrow($result))
 806          {
 807              if ($row['field_default_value'] == 'now' && $row['field_type'] == FIELD_DATE)
 808              {
 809                  $now = getdate();
 810                  $row['field_default_value'] = sprintf('%2d-%2d-%4d', $now['mday'], $now['mon'], $now['year']);
 811              }
 812  
 813              $cp_data['pf_' . $row['field_ident']] = (in_array($row['field_type'], array(FIELD_TEXT, FIELD_STRING))) ? $row['lang_default_value'] : $row['field_default_value'];
 814          }
 815          $db->sql_freeresult($result);
 816          
 817          return $cp_data;
 818      }
 819  
 820      /**
 821      * Get profile field value on submit
 822      * @access private
 823      */
 824  	function get_profile_field($profile_row)
 825      {
 826          global $phpbb_root_path, $phpEx;
 827          global $config;
 828          
 829          $var_name = 'pf_' . $profile_row['field_ident'];
 830          
 831          switch ($profile_row['field_type'])
 832          {
 833              case FIELD_DATE:
 834  
 835                  if (!isset($_REQUEST[$var_name . '_day']))
 836                  {
 837                      if ($profile_row['field_default_value'] == 'now')
 838                      {
 839                          $now = getdate();
 840                          $profile_row['field_default_value'] = sprintf('%2d-%2d-%4d', $now['mday'], $now['mon'], $now['year']);
 841                      }
 842                      list($day, $month, $year) = explode('-', $profile_row['field_default_value']);
 843                  }
 844                  else
 845                  {
 846                      $day = request_var($var_name . '_day', 0);
 847                      $month = request_var($var_name . '_month', 0);
 848                      $year = request_var($var_name . '_year', 0);
 849                  }
 850                  
 851                  $var = sprintf('%2d-%2d-%4d', $day, $month, $year);
 852              break;
 853  
 854              case FIELD_BOOL:
 855                  // Checkbox
 856                  if ($profile_row['field_length'] == 2)
 857                  {
 858                      $var = (isset($_REQUEST[$var_name])) ? 1 : 0;
 859                  }
 860                  else
 861                  {
 862                      $var = request_var($var_name, $profile_row['field_default_value']);
 863                  }
 864              break;
 865  
 866              case FIELD_STRING:
 867              case FIELD_TEXT:
 868                  $var = request_var($var_name, $profile_row['field_default_value'], true);
 869                  utf8_normalize_nfc(&$var);
 870              break;
 871  
 872              case FIELD_INT:
 873                  if (isset($_REQUEST[$var_name]) && $_REQUEST[$var_name] === '')
 874                  {
 875                      $var = NULL;
 876                  }
 877                  else
 878                  {
 879                      $var = request_var($var_name, $profile_row['field_default_value']);
 880                  }
 881              break;
 882  
 883              default:
 884                  $var = request_var($var_name, $profile_row['field_default_value']);
 885              break;
 886          }
 887  
 888          return $var;
 889      }
 890  }
 891  
 892  /**
 893  * Custom Profile Fields ACP
 894  * @package phpBB3
 895  */
 896  class custom_profile_admin extends custom_profile
 897  {
 898      var $vars = array();
 899  
 900      /**
 901      * Return possible validation options
 902      */
 903  	function validate_options()
 904      {
 905          global $user;
 906  
 907          $validate_ary = array('CHARS_ANY' => '.*', 'NUMBERS_ONLY' => '[0-9]+', 'ALPHA_ONLY' => '[\w]+', 'ALPHA_SPACERS' => '[\w_\+\. \-\[\]]+');
 908  
 909          $validate_options = '';
 910          foreach ($validate_ary as $lang => $value)
 911          {
 912              $selected = ($this->vars['field_validation'] == $value) ? ' selected="selected"' : '';
 913              $validate_options .= '<option value="' . $value . '"' . $selected . '>' . $user->lang[$lang] . '</option>';
 914          }
 915  
 916          return $validate_options;
 917      }
 918      
 919      /**
 920      * Get string options for second step in ACP
 921      */
 922  	function get_string_options()
 923      {
 924          global $user;
 925  
 926          $options = array(
 927              0 => array('TITLE' => $user->lang['FIELD_LENGTH'],        'FIELD' => '<input type="text" name="field_length" size="5" value="' . $this->vars['field_length'] . '" />'),
 928              1 => array('TITLE' => $user->lang['MIN_FIELD_CHARS'],    'FIELD' => '<input type="text" name="field_minlen" size="5" value="' . $this->vars['field_minlen'] . '" />'),
 929              2 => array('TITLE' => $user->lang['MAX_FIELD_CHARS'],    'FIELD' => '<input type="text" name="field_maxlen" size="5" value="' . $this->vars['field_maxlen'] . '" />'),
 930              3 => array('TITLE' => $user->lang['FIELD_VALIDATION'],    'FIELD' => '<select name="field_validation">' . $this->validate_options() . '</select>')
 931          );
 932  
 933          return $options;
 934      }
 935  
 936      /**
 937      * Get text options for second step in ACP
 938      */
 939  	function get_text_options()
 940      {
 941          global $user;
 942  
 943          $options = array(
 944              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'] . '" />'),
 945              1 => array('TITLE' => $user->lang['MIN_FIELD_CHARS'],    'FIELD' => '<input type="text" name="field_minlen" size="10" value="' . $this->vars['field_minlen'] . '" />'),
 946              2 => array('TITLE' => $user->lang['MAX_FIELD_CHARS'],    'FIELD' => '<input type="text" name="field_maxlen" size="10" value="' . $this->vars['field_maxlen'] . '" />'),
 947              3 => array('TITLE' => $user->lang['FIELD_VALIDATION'],    'FIELD' => '<select name="field_validation">' . $this->validate_options() . '</select>')
 948          );
 949  
 950          return $options;
 951      }
 952  
 953      /**
 954      * Get int options for second step in ACP
 955      */
 956  	function get_int_options()
 957      {
 958          global $user;
 959  
 960          $options = array(
 961              0 => array('TITLE' => $user->lang['FIELD_LENGTH'],        'FIELD' => '<input type="text" name="field_length" size="5" value="' . $this->vars['field_length'] . '" />'),
 962              1 => array('TITLE' => $user->lang['MIN_FIELD_NUMBER'],    'FIELD' => '<input type="text" name="field_minlen" size="5" value="' . $this->vars['field_minlen'] . '" />'),
 963              2 => array('TITLE' => $user->lang['MAX_FIELD_NUMBER'],    'FIELD' => '<input type="text" name="field_maxlen" size="5" value="' . $this->vars['field_maxlen'] . '" />'),
 964              3 => array('TITLE' => $user->lang['DEFAULT_VALUE'],        'FIELD' => '<input type="post" name="field_default_value" value="' . $this->vars['field_default_value'] . '" />')
 965          );
 966  
 967          return $options;
 968      }
 969  
 970      /**
 971      * Get bool options for second step in ACP
 972      */
 973  	function get_bool_options()
 974      {
 975          global $user, $config, $lang_defs;
 976  
 977          $default_lang_id = $lang_defs['iso'][$config['default_lang']];
 978  
 979          $profile_row = array(
 980              'var_name'                => 'field_default_value',
 981              'field_id'                => 1,
 982              'lang_name'                => $this->vars['lang_name'],
 983              'lang_explain'            => $this->vars['lang_explain'],
 984              'lang_id'                => $default_lang_id,
 985              'field_default_value'    => $this->vars['field_default_value'],
 986              'field_ident'            => 'field_default_value',
 987              'field_type'            => FIELD_BOOL,
 988              'field_length'            => $this->vars['field_length'],
 989              'lang_options'            => $this->vars['lang_options']
 990          );
 991  
 992          $options = array(
 993              0 => array('TITLE' => $user->lang['FIELD_TYPE'], 'EXPLAIN' => $user->lang['BOOL_TYPE_EXPLAIN'], 'FIELD' => '<input type="radio" class="radio" name="field_length" value="1"' . (($this->vars['field_length'] == 1) ? ' checked="checked"' : '') . ' />' . $user->lang['RADIO_BUTTONS'] . '&nbsp; &nbsp;<input type="radio" class="radio" name="field_length" value="2"' . (($this->vars['field_length'] == 2) ? ' checked="checked"' : '') . ' />' . $user->lang['CHECKBOX'] . '&nbsp; &nbsp;'),
 994              1 => array('TITLE' => $user->lang['DEFAULT_VALUE'], 'FIELD' => $this->process_field_row('preview', $profile_row))
 995          );
 996  
 997          return $options;
 998      }
 999  
1000      /**
1001      * Get dropdown options for second step in ACP
1002      */
1003  	function get_dropdown_options()
1004      {
1005          global $user, $config, $lang_defs;
1006  
1007          $default_lang_id = $lang_defs['iso'][$config['default_lang']];
1008  
1009          $profile_row[0] = array(
1010              'var_name'                => 'field_default_value',
1011              'field_id'                => 1,
1012              'lang_name'                => $this->vars['lang_name'],
1013              'lang_explain'            => $this->vars['lang_explain'],
1014              'lang_id'                => $default_lang_id,
1015              'field_default_value'    => $this->vars['field_default_value'],
1016              'field_ident'            => 'field_default_value',
1017              'field_type'            => FIELD_DROPDOWN,
1018              'lang_options'            => $this->vars['lang_options']
1019          );
1020  
1021          $profile_row[1] = $profile_row[0];
1022          $profile_row[1]['var_name'] = 'field_novalue';
1023          $profile_row[1]['field_ident'] = 'field_novalue';
1024          $profile_row[1]['field_default_value']    = $this->vars['field_novalue'];
1025  
1026          $options = array(
1027              0 => array('TITLE' => $user->lang['DEFAULT_VALUE'], 'FIELD' => $this->process_field_row('preview', $profile_row[0])),
1028              1 => array('TITLE' => $user->lang['NO_VALUE_OPTION'], 'EXPLAIN' => $user->lang['NO_VALUE_OPTION_EXPLAIN'], 'FIELD' => $this->process_field_row('preview', $profile_row[1]))
1029          );
1030  
1031          return $options;
1032      }
1033  
1034      /**
1035      * Get date options for second step in ACP
1036      */
1037  	function get_date_options()
1038      {
1039          global $user, $config, $lang_defs;
1040  
1041          $default_lang_id = $lang_defs['iso'][$config['default_lang']];
1042  
1043          $profile_row = array(
1044              'var_name'                => 'field_default_value',
1045              'lang_name'                => $this->vars['lang_name'],
1046              'lang_explain'            => $this->vars['lang_explain'],
1047              'lang_id'                => $default_lang_id,
1048              'field_default_value'    => $this->vars['field_default_value'],
1049              'field_ident'            => 'field_default_value',
1050              'field_type'            => FIELD_DATE,
1051              'field_length'            => $this->vars['field_length']
1052          );
1053  
1054          $always_now = request_var('always_now', 0);
1055          $s_checked = ($always_now || $this->vars['field_default_value'] == 'now') ? true : false;
1056  
1057          $options = array(
1058              0 => array('TITLE' => $user->lang['DEFAULT_VALUE'],    'FIELD' => $this->process_field_row('preview', $profile_row)),
1059              1 => array('TITLE' => $user->lang['ALWAYS_TODAY'],    'FIELD' => '<input type="radio" class="radio" name="always_now" value="1"' . (($s_checked) ? ' checked="checked"' : '') . ' onchange="document.getElementById(\'add_profile_field\').submit();" /> ' . $user->lang['YES'] . ' <input type="radio" class="radio" name="always_now" value="0"' . ((!$s_checked) ? ' checked="checked"' : '') . ' onchange="document.getElementById(\'add_profile_field\').submit();" /> ' . $user->lang['NO']),
1060          );
1061  
1062          return $options;
1063      }
1064  }
1065  
1066  ?>


Generated: Wed Nov 22 00:35:05 2006 Cross-referenced by PHPXref 0.6