Merge remote-tracking branch 'p/ticket/10428' into develop-olympus

* p/ticket/10428:
  [ticket/10428] Documentation for optionget/optionset functions.
  [ticket/10428] Use phpbb_optionget/set in optionget/set for DRYness.
  [ticket/10428] Dispose of $this->keyvalues cache for optionget.
  [ticket/10428] Compare $data to false strictly.
This commit is contained in:
Andreas Fischer 2011-12-25 15:12:03 +01:00
commit b0fd3b2bf0
2 changed files with 68 additions and 50 deletions

View file

@ -2339,47 +2339,62 @@ class acp_users
} }
/** /**
* Optionset replacement for this module based on $user->optionset * Set option bit field for user options in a user row array.
*
* Optionset replacement for this module based on $user->optionset.
*
* @param array $user_row Row from the users table.
* @param int $key Option key, as defined in $user->keyoptions property.
* @param bool $value True to set the option, false to clear the option.
* @param int $data Current bit field value, or false to use $user_row['user_options']
* @return int|bool If $data is false, the bit field is modified and
* written back to $user_row['user_options'], and
* return value is true if the bit field changed and
* false otherwise. If $data is not false, the new
* bitfield value is returned.
*/ */
function optionset(&$user_row, $key, $value, $data = false) function optionset(&$user_row, $key, $value, $data = false)
{ {
global $user; global $user;
$var = ($data) ? $data : $user_row['user_options']; $var = ($data !== false) ? $data : $user_row['user_options'];
if ($value && !($var & 1 << $user->keyoptions[$key])) $new_var = phpbb_optionset($user->keyoptions[$key], $value, $var);
{
$var += 1 << $user->keyoptions[$key];
}
else if (!$value && ($var & 1 << $user->keyoptions[$key]))
{
$var -= 1 << $user->keyoptions[$key];
}
else
{
return ($data) ? $var : false;
}
if (!$data) if ($data === false)
{ {
$user_row['user_options'] = $var; if ($new_var != $var)
{
$user_row['user_options'] = $new_var;
return true; return true;
} }
else else
{ {
return $var; return false;
}
}
else
{
return $new_var;
} }
} }
/** /**
* Optionget replacement for this module based on $user->optionget * Get option bit field from user options in a user row array.
*
* Optionget replacement for this module based on $user->optionget.
*
* @param array $user_row Row from the users table.
* @param int $key option key, as defined in $user->keyoptions property.
* @param int $data bit field value to use, or false to use $user_row['user_options']
* @return bool true if the option is set in the bit field, false otherwise
*/ */
function optionget(&$user_row, $key, $data = false) function optionget(&$user_row, $key, $data = false)
{ {
global $user; global $user;
$var = ($data) ? $data : $user_row['user_options']; $var = ($data !== false) ? $data : $user_row['user_options'];
return ($var & 1 << $user->keyoptions[$key]) ? true : false; return phpbb_optionget($user->keyoptions[$key], $var);
} }
} }

View file

@ -1507,7 +1507,6 @@ class user extends session
// Able to add new options (up to id 31) // Able to add new options (up to id 31)
var $keyoptions = array('viewimg' => 0, 'viewflash' => 1, 'viewsmilies' => 2, 'viewsigs' => 3, 'viewavatars' => 4, 'viewcensors' => 5, 'attachsig' => 6, 'bbcode' => 8, 'smilies' => 9, 'popuppm' => 10, 'sig_bbcode' => 15, 'sig_smilies' => 16, 'sig_links' => 17); var $keyoptions = array('viewimg' => 0, 'viewflash' => 1, 'viewsmilies' => 2, 'viewsigs' => 3, 'viewavatars' => 4, 'viewcensors' => 5, 'attachsig' => 6, 'bbcode' => 8, 'smilies' => 9, 'popuppm' => 10, 'sig_bbcode' => 15, 'sig_smilies' => 16, 'sig_links' => 17);
var $keyvalues = array();
/** /**
* Constructor to set the lang path * Constructor to set the lang path
@ -2337,47 +2336,51 @@ class user extends session
} }
/** /**
* Get option bit field from user options * Get option bit field from user options.
*
* @param int $key option key, as defined in $keyoptions property.
* @param int $data bit field value to use, or false to use $this->data['user_options']
* @return bool true if the option is set in the bit field, false otherwise
*/ */
function optionget($key, $data = false) function optionget($key, $data = false)
{ {
if (!isset($this->keyvalues[$key])) $var = ($data !== false) ? $data : $this->data['user_options'];
{ return phpbb_optionget($this->keyoptions[$key], $var);
$var = ($data) ? $data : $this->data['user_options'];
$this->keyvalues[$key] = ($var & 1 << $this->keyoptions[$key]) ? true : false;
}
return $this->keyvalues[$key];
} }
/** /**
* Set option bit field for user options * Set option bit field for user options.
*
* @param int $key Option key, as defined in $keyoptions property.
* @param bool $value True to set the option, false to clear the option.
* @param int $data Current bit field value, or false to use $this->data['user_options']
* @return int|bool If $data is false, the bit field is modified and
* written back to $this->data['user_options'], and
* return value is true if the bit field changed and
* false otherwise. If $data is not false, the new
* bitfield value is returned.
*/ */
function optionset($key, $value, $data = false) function optionset($key, $value, $data = false)
{ {
$var = ($data) ? $data : $this->data['user_options']; $var = ($data !== false) ? $data : $this->data['user_options'];
if ($value && !($var & 1 << $this->keyoptions[$key])) $new_var = phpbb_optionset($this->keyoptions[$key], $value, $var);
{
$var += 1 << $this->keyoptions[$key];
}
else if (!$value && ($var & 1 << $this->keyoptions[$key]))
{
$var -= 1 << $this->keyoptions[$key];
}
else
{
return ($data) ? $var : false;
}
if (!$data) if ($data === false)
{ {
$this->data['user_options'] = $var; if ($new_var != $var)
{
$this->data['user_options'] = $new_var;
return true; return true;
} }
else else
{ {
return $var; return false;
}
}
else
{
return $new_var;
} }
} }