[ Index ]

PHP Cross Reference of phpBB-3.3.3-deutsch

title

Body

[close]

/phpbb/profilefields/type/ -> type_bool.php (source)

   1  <?php
   2  /**
   3  *
   4  * This file is part of the phpBB Forum Software package.
   5  *
   6  * @copyright (c) phpBB Limited <https://www.phpbb.com>
   7  * @license GNU General Public License, version 2 (GPL-2.0)
   8  *
   9  * For full copyright and license information, please see
  10  * the docs/CREDITS.txt file.
  11  *
  12  */
  13  
  14  namespace phpbb\profilefields\type;
  15  
  16  class type_bool extends type_base
  17  {
  18      /**
  19      * Profile fields language helper
  20      * @var \phpbb\profilefields\lang_helper
  21      */
  22      protected $lang_helper;
  23  
  24      /**
  25      * Request object
  26      * @var \phpbb\request\request
  27      */
  28      protected $request;
  29  
  30      /**
  31      * Template object
  32      * @var \phpbb\template\template
  33      */
  34      protected $template;
  35  
  36      /**
  37      * User object
  38      * @var \phpbb\user
  39      */
  40      protected $user;
  41  
  42      /**
  43      * Construct
  44      *
  45      * @param    \phpbb\profilefields\lang_helper        $lang_helper    Profile fields language helper
  46      * @param    \phpbb\request\request        $request    Request object
  47      * @param    \phpbb\template\template    $template    Template object
  48      * @param    \phpbb\user                    $user        User object
  49      */
  50  	public function __construct(\phpbb\profilefields\lang_helper $lang_helper, \phpbb\request\request $request, \phpbb\template\template $template, \phpbb\user $user)
  51      {
  52          $this->lang_helper = $lang_helper;
  53          $this->request = $request;
  54          $this->template = $template;
  55          $this->user = $user;
  56      }
  57  
  58      /**
  59      * {@inheritDoc}
  60      */
  61  	public function get_name_short()
  62      {
  63          return 'bool';
  64      }
  65  
  66      /**
  67      * {@inheritDoc}
  68      */
  69  	public function get_options($default_lang_id, $field_data)
  70      {
  71          $profile_row = array(
  72              'var_name'                => 'field_default_value',
  73              'field_id'                => 1,
  74              'lang_name'                => $field_data['lang_name'],
  75              'lang_explain'            => $field_data['lang_explain'],
  76              'lang_id'                => $default_lang_id,
  77              'field_default_value'    => $field_data['field_default_value'],
  78              'field_ident'            => 'field_default_value',
  79              'field_type'            => $this->get_service_name(),
  80              'field_length'            => $field_data['field_length'],
  81              'lang_options'            => $field_data['lang_options'],
  82          );
  83  
  84          $options = array(
  85              0 => array('TITLE' => $this->user->lang['FIELD_TYPE'], 'EXPLAIN' => $this->user->lang['BOOL_TYPE_EXPLAIN'], 'FIELD' => '<label><input type="radio" class="radio" name="field_length" value="1"' . (($field_data['field_length'] == 1) ? ' checked="checked"' : '') . ' onchange="document.getElementById(\'add_profile_field\').submit();" /> ' . $this->user->lang['RADIO_BUTTONS'] . '</label><label><input type="radio" class="radio" name="field_length" value="2"' . (($field_data['field_length'] == 2) ? ' checked="checked"' : '') . ' onchange="document.getElementById(\'add_profile_field\').submit();" /> ' . $this->user->lang['CHECKBOX'] . '</label>'),
  86              1 => array('TITLE' => $this->user->lang['DEFAULT_VALUE'], 'FIELD' => $this->process_field_row('preview', $profile_row)),
  87          );
  88  
  89          return $options;
  90      }
  91  
  92      /**
  93      * {@inheritDoc}
  94      */
  95  	public function get_default_option_values()
  96      {
  97          return array(
  98              'field_length'        => 1,
  99              'field_minlen'        => 0,
 100              'field_maxlen'        => 0,
 101              'field_validation'    => '',
 102              'field_novalue'        => 0,
 103              'field_default_value'    => 0,
 104          );
 105      }
 106  
 107      /**
 108      * {@inheritDoc}
 109      */
 110  	public function get_default_field_value($field_data)
 111      {
 112          return $field_data['field_default_value'];
 113      }
 114  
 115      /**
 116      * {@inheritDoc}
 117      */
 118  	public function get_profile_field($profile_row)
 119      {
 120          $var_name = 'pf_' . $profile_row['field_ident'];
 121  
 122          // Checkbox
 123          if ($profile_row['field_length'] == 2)
 124          {
 125              return ($this->request->is_set($var_name)) ? 1 : 0;
 126          }
 127          else
 128          {
 129              return $this->request->variable($var_name, (int) $profile_row['field_default_value']);
 130          }
 131      }
 132  
 133      /**
 134      * {@inheritDoc}
 135      */
 136  	public function validate_profile_field(&$field_value, $field_data)
 137      {
 138          $field_value = (bool) $field_value;
 139  
 140          if (!$field_value && $field_data['field_required'])
 141          {
 142              return $this->user->lang('FIELD_REQUIRED', $this->get_field_name($field_data['lang_name']));
 143          }
 144  
 145          return false;
 146      }
 147  
 148      /**
 149      * {@inheritDoc}
 150      */
 151  	public function get_profile_value($field_value, $field_data)
 152      {
 153          $field_id = $field_data['field_id'];
 154          $lang_id = $field_data['lang_id'];
 155  
 156          if (!$this->lang_helper->is_set($field_id, $lang_id))
 157          {
 158              $this->lang_helper->load_option_lang($lang_id);
 159          }
 160  
 161          if (!$field_value && $field_data['field_show_novalue'])
 162          {
 163              $field_value = $field_data['field_default_value'];
 164          }
 165  
 166          if ($field_data['field_length'] == 1)
 167          {
 168              return ($this->lang_helper->is_set($field_id, $lang_id, (int) $field_value)) ? $this->lang_helper->get($field_id, $lang_id, (int) $field_value) : null;
 169          }
 170          else if (!$field_value)
 171          {
 172              return null;
 173          }
 174          else
 175          {
 176              return $this->lang_helper->is_set($field_id, $lang_id, $field_value + 1) ? $this->lang_helper->get($field_id, $lang_id, $field_value + 1) : null;
 177          }
 178      }
 179  
 180      /**
 181      * {@inheritDoc}
 182      */
 183  	public function get_profile_value_raw($field_value, $field_data)
 184      {
 185          if ($field_value == $field_data['field_novalue'] && !$field_data['field_show_novalue'])
 186          {
 187              return null;
 188          }
 189  
 190          if (!$field_value && $field_data['field_show_novalue'])
 191          {
 192              $field_value = $field_data['field_novalue'];
 193          }
 194  
 195          return $field_value;
 196      }
 197  
 198      /**
 199      * {@inheritDoc}
 200      */
 201  	public function generate_field($profile_row, $preview_options = false)
 202      {
 203          $profile_row['field_ident'] = (isset($profile_row['var_name'])) ? $profile_row['var_name'] : 'pf_' . $profile_row['field_ident'];
 204          $field_ident = $profile_row['field_ident'];
 205          $default_value = $profile_row['field_default_value'];
 206  
 207          // checkbox - set the value to "true" if it has been set to 1
 208          if ($profile_row['field_length'] == 2)
 209          {
 210              $value = ($this->request->is_set($field_ident) && $this->request->variable($field_ident, $default_value) == 1) ? true : ((!isset($this->user->profile_fields[$field_ident]) || $preview_options !== false) ? $default_value : $this->user->profile_fields[$field_ident]);
 211          }
 212          else
 213          {
 214              $value = ($this->request->is_set($field_ident)) ? $this->request->variable($field_ident, $default_value) : ((!isset($this->user->profile_fields[$field_ident]) || $preview_options !== false) ? $default_value : $this->user->profile_fields[$field_ident]);
 215          }
 216  
 217          $profile_row['field_value'] = (int) $value;
 218          $this->template->assign_block_vars('bool', array_change_key_case($profile_row, CASE_UPPER));
 219  
 220          if ($profile_row['field_length'] == 1)
 221          {
 222              if (!$this->lang_helper->is_set($profile_row['field_id'], $profile_row['lang_id'], 1))
 223              {
 224                  if ($preview_options)
 225                  {
 226                      $this->lang_helper->load_preview_options($profile_row['field_id'], $profile_row['lang_id'], $preview_options);
 227                  }
 228                  else
 229                  {
 230                      $this->lang_helper->load_option_lang($profile_row['lang_id']);
 231                  }
 232              }
 233  
 234              $options = $this->lang_helper->get($profile_row['field_id'], $profile_row['lang_id']);
 235              foreach ($options as $option_id => $option_value)
 236              {
 237                  $this->template->assign_block_vars('bool.options', array(
 238                      'OPTION_ID'    => $option_id,
 239                      'CHECKED'    => ($value == $option_id) ? ' checked="checked"' : '',
 240                      'VALUE'        => $option_value,
 241                  ));
 242              }
 243          }
 244      }
 245  
 246      /**
 247      * {@inheritDoc}
 248      */
 249  	public function get_field_ident($field_data)
 250      {
 251          return ($field_data['field_length'] == '1') ? '' : 'pf_' . $field_data['field_ident'];
 252      }
 253  
 254      /**
 255      * {@inheritDoc}
 256      */
 257  	public function get_database_column_type()
 258      {
 259          return 'TINT:2';
 260      }
 261  
 262      /**
 263      * {@inheritDoc}
 264      */
 265  	public function get_language_options($field_data)
 266      {
 267          $options = array(
 268              'lang_name'        => 'string',
 269              'lang_options'    => 'two_options',
 270          );
 271  
 272          if ($field_data['lang_explain'])
 273          {
 274              $options['lang_explain'] = 'text';
 275          }
 276  
 277          return $options;
 278      }
 279  
 280      /**
 281      * {@inheritDoc}
 282      */
 283  	public function get_language_options_input($field_data)
 284      {
 285          $field_data['l_lang_name']            = $this->request->variable('l_lang_name', array(0 => ''), true);
 286          $field_data['l_lang_explain']        = $this->request->variable('l_lang_explain', array(0 => ''), true);
 287          $field_data['l_lang_default_value']    = $this->request->variable('l_lang_default_value', array(0 => ''), true);
 288  
 289          /**
 290          * @todo check if this line is correct...
 291          $field_data['l_lang_default_value']    = $this->request->variable('l_lang_default_value', array(0 => array('')), true);
 292          */
 293          $field_data['l_lang_options']    = $this->request->variable('l_lang_options', array(0 => array('')), true);
 294  
 295          return $field_data;
 296      }
 297  
 298      /**
 299      * {@inheritDoc}
 300      */
 301  	public function prepare_options_form(&$exclude_options, &$visibility_options)
 302      {
 303          $exclude_options[1][] = 'lang_options';
 304  
 305          return $this->request->variable('lang_options', array(''), true);
 306      }
 307  
 308      /**
 309      * {@inheritDoc}
 310      */
 311  	public function validate_options_on_submit($error, $field_data)
 312      {
 313          if (empty($field_data['lang_options'][0]) || empty($field_data['lang_options'][1]))
 314          {
 315              $error[] = $this->user->lang['NO_FIELD_ENTRIES'];
 316          }
 317  
 318          return $error;
 319      }
 320  
 321      /**
 322      * {@inheritDoc}
 323      */
 324  	public function get_excluded_options($key, $action, $current_value, &$field_data, $step)
 325      {
 326          if ($step == 2 && $key == 'field_default_value')
 327          {
 328              // 'field_length' == 1 defines radio buttons. Possible values are 1 or 2 only.
 329              // 'field_length' == 2 defines checkbox. Possible values are 0 or 1 only.
 330              // If we switch the type on step 2, we have to adjust field value.
 331              // 1 is a common value for the checkbox and radio buttons.
 332  
 333              // Adjust unchecked checkbox value.
 334              // If we return or save settings from 2nd/3rd page
 335              // and the checkbox is unchecked, set the value to 0.
 336              if ($this->request->is_set('step') && !$this->request->is_set($key))
 337              {
 338                  return 0;
 339              }
 340  
 341              // If we switch to the checkbox type but former radio buttons value was 2,
 342              // which is not the case for the checkbox, set it to 0 (unchecked).
 343              if ($field_data['field_length'] == 2 && $current_value == 2)
 344              {
 345                  return 0;
 346              }
 347              // If we switch to the radio buttons but the former checkbox value was 0,
 348              // which is not the case for the radio buttons, set it to 0.
 349              else if ($field_data['field_length'] == 1 && $current_value == 0)
 350              {
 351                  return 2;
 352              }
 353          }
 354  
 355          if ($key == 'l_lang_options' && $this->request->is_set($key))
 356          {
 357              $field_data[$key] = $this->request->variable($key, array(0 => array('')), true);
 358  
 359              return $current_value;
 360          }
 361  
 362          return parent::get_excluded_options($key, $action, $current_value, $field_data, $step);
 363      }
 364  
 365      /**
 366      * {@inheritDoc}
 367      */
 368  	public function prepare_hidden_fields($step, $key, $action, &$field_data)
 369      {
 370          if ($key == 'field_default_value')
 371          {
 372              $field_length = $this->request->variable('field_length', 0);
 373  
 374              // Do a simple is set check if using checkbox.
 375              if ($field_length == 2)
 376              {
 377                  return $this->request->is_set($key);
 378              }
 379              return $this->request->variable($key, $field_data[$key], true);
 380          }
 381  
 382          $default_lang_options = array(
 383              'l_lang_options'    => array(0 => array('')),
 384              'lang_options'        => array(0 => ''),
 385          );
 386  
 387          if (isset($default_lang_options[$key]) && $this->request->is_set($key))
 388          {
 389              return $this->request->variable($key, $default_lang_options[$key], true);
 390          }
 391  
 392          return parent::prepare_hidden_fields($step, $key, $action, $field_data);
 393      }
 394  
 395      /**
 396      * {@inheritDoc}
 397      */
 398  	public function display_options(&$template_vars, &$field_data)
 399      {
 400          // Initialize these array elements if we are creating a new field
 401          if (!count($field_data['lang_options']))
 402          {
 403              // No options have been defined for a boolean field.
 404              $field_data['lang_options'][0] = '';
 405              $field_data['lang_options'][1] = '';
 406          }
 407  
 408          $template_vars = array_merge($template_vars, array(
 409              'S_BOOL'                    => true,
 410              'L_LANG_OPTIONS_EXPLAIN'    => $this->user->lang['BOOL_ENTRIES_EXPLAIN'],
 411              'FIRST_LANG_OPTION'            => $field_data['lang_options'][0],
 412              'SECOND_LANG_OPTION'        => $field_data['lang_options'][1],
 413          ));
 414      }
 415  }


Generated: Sun Feb 14 20:08:31 2021 Cross-referenced by PHPXref 0.7.1