"Fixed" user editor in the broom closet.
This commit is contained in:
parent
ed2201090c
commit
d9c3ca1e5a
3 changed files with 58 additions and 118 deletions
|
@ -52,21 +52,14 @@ if(!empty($_POST['role']) && is_array($_POST['role']) && CSRF::validateRequest()
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
$roleColour = new Colour;
|
|
||||||
|
|
||||||
if(!empty($_POST['role']['colour']['inherit'])) {
|
if(!empty($_POST['role']['colour']['inherit'])) {
|
||||||
$roleColour->setInherit(true);
|
$roleColour = Colour::none();
|
||||||
} else {
|
} else {
|
||||||
foreach(['red', 'green', 'blue'] as $key) {
|
$roleColour = Colour::fromRgb(
|
||||||
$value = (int)($_POST['role']['colour'][$key] ?? -1);
|
(int)($_POST['role']['colour']['red'] ?? -1),
|
||||||
|
(int)($_POST['role']['colour']['green'] ?? -1),
|
||||||
try {
|
(int)($_POST['role']['colour']['blue'] ?? -1)
|
||||||
$roleColour->{'set' . ucfirst($key)}($value);
|
);
|
||||||
} catch(\Exception $ex){
|
|
||||||
echo $ex->getMessage();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$roleDescription = $_POST['role']['description'] ?? '';
|
$roleDescription = $_POST['role']['description'] ?? '';
|
||||||
|
|
|
@ -94,8 +94,8 @@ if(CSRF::validateRequest() && $canEdit) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!empty($_POST['user']) && is_array($_POST['user'])) {
|
if(!empty($_POST['user']) && is_array($_POST['user'])) {
|
||||||
$setUsername = (string)($_POST['user']['username'] ?? '');
|
//$setUsername = (string)($_POST['user']['username'] ?? '');
|
||||||
$setEMailAddress = (string)($_POST['user']['email'] ?? '');
|
//$setEMailAddress = (string)($_POST['user']['email'] ?? '');
|
||||||
$setCountry = (string)($_POST['user']['country'] ?? '');
|
$setCountry = (string)($_POST['user']['country'] ?? '');
|
||||||
$setTitle = (string)($_POST['user']['title'] ?? '');
|
$setTitle = (string)($_POST['user']['title'] ?? '');
|
||||||
|
|
||||||
|
@ -105,20 +105,20 @@ if(CSRF::validateRequest() && $canEdit) {
|
||||||
$userInfo->setDisplayRole(UserRole::byId($displayRole));
|
$userInfo->setDisplayRole(UserRole::byId($displayRole));
|
||||||
} catch(UserRoleNotFoundException $ex) {}
|
} catch(UserRoleNotFoundException $ex) {}
|
||||||
|
|
||||||
$usernameValidation = User::validateUsername($setUsername);
|
//$usernameValidation = User::validateUsername($setUsername);
|
||||||
$emailValidation = User::validateEMailAddress($setEMailAddress);
|
//$emailValidation = User::validateEMailAddress($setEMailAddress);
|
||||||
$countryValidation = strlen($setCountry) === 2
|
$countryValidation = strlen($setCountry) === 2
|
||||||
&& ctype_alpha($setCountry)
|
&& ctype_alpha($setCountry)
|
||||||
&& ctype_upper($setCountry);
|
&& ctype_upper($setCountry);
|
||||||
|
|
||||||
if(!empty($usernameValidation))
|
//if(!empty($usernameValidation))
|
||||||
$notices[] = User::usernameValidationErrorString($usernameValidation);
|
// $notices[] = User::usernameValidationErrorString($usernameValidation);
|
||||||
|
|
||||||
if(!empty($emailValidation)) {
|
// if(!empty($emailValidation)) {
|
||||||
$notices[] = $emailValidation === 'in-use'
|
// $notices[] = $emailValidation === 'in-use'
|
||||||
? 'This e-mail address has already been used!'
|
// ? 'This e-mail address has already been used!'
|
||||||
: 'This e-mail address is invalid!';
|
// : 'This e-mail address is invalid!';
|
||||||
}
|
// }
|
||||||
|
|
||||||
if(!$countryValidation)
|
if(!$countryValidation)
|
||||||
$notices[] = 'Country code was invalid.';
|
$notices[] = 'Country code was invalid.';
|
||||||
|
@ -127,8 +127,9 @@ if(CSRF::validateRequest() && $canEdit) {
|
||||||
$notices[] = 'User title was invalid.';
|
$notices[] = 'User title was invalid.';
|
||||||
|
|
||||||
if(empty($notices))
|
if(empty($notices))
|
||||||
$userInfo->setUsername((string)($_POST['user']['username'] ?? ''))
|
$userInfo
|
||||||
->setEMailAddress((string)($_POST['user']['email'] ?? ''))
|
// ->setUsername((string)($_POST['user']['username'] ?? ''))
|
||||||
|
// ->setEMailAddress((string)($_POST['user']['email'] ?? ''))
|
||||||
->setCountry((string)($_POST['user']['country'] ?? ''))
|
->setCountry((string)($_POST['user']['country'] ?? ''))
|
||||||
->setTitle((string)($_POST['user']['title'] ?? ''))
|
->setTitle((string)($_POST['user']['title'] ?? ''))
|
||||||
->setDisplayRole(UserRole::byId((int)($_POST['user']['display_role'] ?? 0)));
|
->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'])) {
|
if(!empty($_POST['colour']) && is_array($_POST['colour'])) {
|
||||||
$setColour = null;
|
$setColour = null;
|
||||||
|
|
||||||
if(!empty($_POST['colour']['enable'])) {
|
if(!empty($_POST['colour']['enable']))
|
||||||
$setColour = new Colour;
|
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$setColour->setHex((string)($_POST['colour']['hex'] ?? ''));
|
$setColour = Colour::fromHex((string)($_POST['colour']['hex'] ?? ''));
|
||||||
} catch(\Exception $ex) {
|
} catch(\Exception $ex) {
|
||||||
$notices[] = $ex->getMessage();
|
$notices[] = $ex->getMessage();
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if(empty($notices))
|
if(empty($notices))
|
||||||
$userInfo->setColour($setColour);
|
$userInfo->setColour($setColour);
|
||||||
|
|
123
src/Colour.php
123
src/Colour.php
|
@ -14,7 +14,7 @@ class Colour {
|
||||||
private int $raw = 0;
|
private int $raw = 0;
|
||||||
|
|
||||||
public function __construct(?int $raw = 0) {
|
public function __construct(?int $raw = 0) {
|
||||||
$this->setRaw($raw ?? 0);
|
$this->raw = ($raw ?? 0) & 0x7FFFFFFF;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function none(): self {
|
public static function none(): self {
|
||||||
|
@ -22,93 +22,12 @@ class Colour {
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function fromRgb(int $red, int $green, int $blue): self {
|
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 {
|
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] === '#')
|
if($hex[0] === '#')
|
||||||
$hex = mb_substr($hex, 1);
|
$hex = mb_substr($hex, 1);
|
||||||
|
|
||||||
|
@ -123,7 +42,37 @@ class Colour {
|
||||||
throw new InvalidArgumentException('Argument is not a hex string.');
|
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 {
|
public function getCSS(): string {
|
||||||
|
|
Loading…
Reference in a new issue