"Fixed" user editor in the broom closet.

This commit is contained in:
flash 2023-01-02 20:09:38 +00:00
parent ed2201090c
commit d9c3ca1e5a
3 changed files with 58 additions and 118 deletions

View file

@ -52,21 +52,14 @@ if(!empty($_POST['role']) && is_array($_POST['role']) && CSRF::validateRequest()
return;
}
$roleColour = new Colour;
if(!empty($_POST['role']['colour']['inherit'])) {
$roleColour->setInherit(true);
$roleColour = Colour::none();
} else {
foreach(['red', 'green', 'blue'] as $key) {
$value = (int)($_POST['role']['colour'][$key] ?? -1);
try {
$roleColour->{'set' . ucfirst($key)}($value);
} catch(\Exception $ex){
echo $ex->getMessage();
return;
}
}
$roleColour = Colour::fromRgb(
(int)($_POST['role']['colour']['red'] ?? -1),
(int)($_POST['role']['colour']['green'] ?? -1),
(int)($_POST['role']['colour']['blue'] ?? -1)
);
}
$roleDescription = $_POST['role']['description'] ?? '';

View file

@ -94,8 +94,8 @@ if(CSRF::validateRequest() && $canEdit) {
}
if(!empty($_POST['user']) && is_array($_POST['user'])) {
$setUsername = (string)($_POST['user']['username'] ?? '');
$setEMailAddress = (string)($_POST['user']['email'] ?? '');
//$setUsername = (string)($_POST['user']['username'] ?? '');
//$setEMailAddress = (string)($_POST['user']['email'] ?? '');
$setCountry = (string)($_POST['user']['country'] ?? '');
$setTitle = (string)($_POST['user']['title'] ?? '');
@ -105,20 +105,20 @@ if(CSRF::validateRequest() && $canEdit) {
$userInfo->setDisplayRole(UserRole::byId($displayRole));
} catch(UserRoleNotFoundException $ex) {}
$usernameValidation = User::validateUsername($setUsername);
$emailValidation = User::validateEMailAddress($setEMailAddress);
//$usernameValidation = User::validateUsername($setUsername);
//$emailValidation = User::validateEMailAddress($setEMailAddress);
$countryValidation = strlen($setCountry) === 2
&& ctype_alpha($setCountry)
&& ctype_upper($setCountry);
if(!empty($usernameValidation))
$notices[] = User::usernameValidationErrorString($usernameValidation);
//if(!empty($usernameValidation))
// $notices[] = User::usernameValidationErrorString($usernameValidation);
if(!empty($emailValidation)) {
$notices[] = $emailValidation === 'in-use'
? 'This e-mail address has already been used!'
: 'This e-mail address is invalid!';
}
// if(!empty($emailValidation)) {
// $notices[] = $emailValidation === 'in-use'
// ? 'This e-mail address has already been used!'
// : 'This e-mail address is invalid!';
// }
if(!$countryValidation)
$notices[] = 'Country code was invalid.';
@ -127,8 +127,9 @@ if(CSRF::validateRequest() && $canEdit) {
$notices[] = 'User title was invalid.';
if(empty($notices))
$userInfo->setUsername((string)($_POST['user']['username'] ?? ''))
->setEMailAddress((string)($_POST['user']['email'] ?? ''))
$userInfo
// ->setUsername((string)($_POST['user']['username'] ?? ''))
// ->setEMailAddress((string)($_POST['user']['email'] ?? ''))
->setCountry((string)($_POST['user']['country'] ?? ''))
->setTitle((string)($_POST['user']['title'] ?? ''))
->setDisplayRole(UserRole::byId((int)($_POST['user']['display_role'] ?? 0)));
@ -137,15 +138,12 @@ if(CSRF::validateRequest() && $canEdit) {
if(!empty($_POST['colour']) && is_array($_POST['colour'])) {
$setColour = null;
if(!empty($_POST['colour']['enable'])) {
$setColour = new Colour;
if(!empty($_POST['colour']['enable']))
try {
$setColour->setHex((string)($_POST['colour']['hex'] ?? ''));
$setColour = Colour::fromHex((string)($_POST['colour']['hex'] ?? ''));
} catch(\Exception $ex) {
$notices[] = $ex->getMessage();
}
}
if(empty($notices))
$userInfo->setColour($setColour);

View file

@ -14,7 +14,7 @@ class Colour {
private int $raw = 0;
public function __construct(?int $raw = 0) {
$this->setRaw($raw ?? 0);
$this->raw = ($raw ?? 0) & 0x7FFFFFFF;
}
public static function none(): self {
@ -22,93 +22,12 @@ class Colour {
}
public static function fromRgb(int $red, int $green, int $blue): self {
return (new Colour)->setRed($red)->setGreen($green)->setBlue($blue);
$raw = (($red & 0xFF) << 16)
| (($green & 0xFF) << 8)
| ($blue & 0xFF);
return new Colour($raw);
}
public static function fromHex(string $hex): self {
return (new Colour)->setHex($hex);
}
public function getRaw(): int {
return $this->raw;
}
public function setRaw(int $raw): self {
if($raw < 0 || $raw > 0x7FFFFFFF)
throw new InvalidArgumentException('Invalid raw colour.');
$this->raw = $raw;
return $this;
}
public function getInherit(): bool {
return ($this->getRaw() & self::FLAG_INHERIT) > 0;
}
public function setInherit(bool $inherit): self {
$raw = $this->getRaw();
if($inherit)
$raw |= self::FLAG_INHERIT;
else
$raw &= ~self::FLAG_INHERIT;
$this->setRaw($raw);
return $this;
}
public function getRed(): int {
return ($this->getRaw() & 0xFF0000) >> 16;
}
public function setRed(int $red): self {
if($red < 0 || $red > 0xFF)
throw new InvalidArgumentException('Invalid red value.');
$raw = $this->getRaw();
$raw &= ~0xFF0000;
$raw |= $red << 16;
$this->setRaw($raw);
return $this;
}
public function getGreen(): int {
return ($this->getRaw() & 0xFF00) >> 8;
}
public function setGreen(int $green): self {
if($green < 0 || $green > 0xFF)
throw new InvalidArgumentException('Invalid green value.');
$raw = $this->getRaw();
$raw &= ~0xFF00;
$raw |= $green << 8;
$this->setRaw($raw);
return $this;
}
public function getBlue(): int {
return ($this->getRaw() & 0xFF);
}
public function setBlue(int $blue): self {
if($blue < 0 || $blue > 0xFF)
throw new InvalidArgumentException('Invalid blue value.');
$raw = $this->getRaw();
$raw &= ~0xFF;
$raw |= $blue;
$this->setRaw($raw);
return $this;
}
public function getLuminance(): float {
return self::LUMINANCE_WEIGHT_RED * $this->getRed()
+ self::LUMINANCE_WEIGHT_GREEN * $this->getGreen()
+ self::LUMINANCE_WEIGHT_BLUE * $this->getBlue();
}
public function getHex(): string {
return str_pad(dechex($this->getRaw() & 0xFFFFFF), 6, '0', STR_PAD_LEFT);
}
public function setHex(string $hex): self {
if($hex[0] === '#')
$hex = mb_substr($hex, 1);
@ -123,7 +42,37 @@ class Colour {
throw new InvalidArgumentException('Argument is not a hex string.');
}
return $this->setRaw(hexdec($hex));
return new Colour(hexdec($hex));
}
public function getRaw(): int {
return $this->raw;
}
public function getInherit(): bool {
return ($this->getRaw() & self::FLAG_INHERIT) > 0;
}
public function getRed(): int {
return ($this->getRaw() & 0xFF0000) >> 16;
}
public function getGreen(): int {
return ($this->getRaw() & 0xFF00) >> 8;
}
public function getBlue(): int {
return ($this->getRaw() & 0xFF);
}
public function getLuminance(): float {
return self::LUMINANCE_WEIGHT_RED * $this->getRed()
+ self::LUMINANCE_WEIGHT_GREEN * $this->getGreen()
+ self::LUMINANCE_WEIGHT_BLUE * $this->getBlue();
}
public function getHex(): string {
return str_pad(dechex($this->getRaw() & 0xFFFFFF), 6, '0', STR_PAD_LEFT);
}
public function getCSS(): string {