web-base/Core/Objects/DatabaseEntity/Group.class.php

57 lines
1.6 KiB
PHP
Raw Normal View History

2022-06-20 19:52:31 +02:00
<?php
2022-11-18 18:06:46 +01:00
namespace Core\Objects\DatabaseEntity;
2022-06-20 19:52:31 +02:00
use Core\Driver\SQL\SQL;
2022-11-18 18:06:46 +01:00
use Core\Objects\DatabaseEntity\Attribute\MaxLength;
2022-11-20 17:13:53 +01:00
use Core\Objects\DatabaseEntity\Controller\DatabaseEntity;
use Core\Objects\DatabaseEntity\Controller\NMRelation;
2022-06-20 19:52:31 +02:00
class Group extends DatabaseEntity {
2022-11-20 17:13:53 +01:00
const ADMIN = 1;
const MODERATOR = 3;
const SUPPORT = 2;
const GROUPS = [
self::ADMIN => "Administrator",
self::MODERATOR => "Moderator",
self::SUPPORT => "Support",
];
2022-06-20 19:52:31 +02:00
#[MaxLength(32)] public string $name;
#[MaxLength(10)] public string $color;
2022-11-20 17:13:53 +01:00
public function __construct(?int $id, string $name, string $color) {
2022-06-20 19:52:31 +02:00
parent::__construct($id);
2022-11-20 17:13:53 +01:00
$this->name = $name;
$this->color = $color;
2022-06-20 19:52:31 +02:00
}
public function getMembers(SQL $sql): array {
2023-01-10 22:12:05 +01:00
$nmTable = User::getHandler($sql)->getNMRelation("groups")->getTableName();
2023-01-07 15:34:05 +01:00
$users = User::findBy(User::createBuilder($sql, false)
->innerJoin($nmTable, "user_id", "User.id")
->whereEq("group_id", $this->id));
2023-01-07 15:34:05 +01:00
return User::toJsonArray($users, ["id", "name", "fullName", "profilePicture"]);
}
2023-01-09 20:27:01 +01:00
public static function getPredefinedValues(): array {
return [
new Group(Group::ADMIN, Group::GROUPS[Group::ADMIN], "#dc3545"),
new Group(Group::MODERATOR, Group::GROUPS[Group::MODERATOR], "#28a745"),
new Group(Group::SUPPORT, Group::GROUPS[Group::SUPPORT], "#007bff"),
];
}
2024-04-02 15:33:00 +02:00
public function delete(SQL $sql): bool {
if (parent::delete($sql)) {
$handler = User::getHandler($sql);
$table = $handler->getNMRelation("groups")->getTableName();
return $sql->delete($table)->whereEq("group_id", $this->id)->execute();
} else {
return false;
}
}
2022-06-20 19:52:31 +02:00
}