226 lines
		
	
	
		
			5.8 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
			
		
		
	
	
			226 lines
		
	
	
		
			5.8 KiB
		
	
	
	
		
			PHP
		
	
	
	
	
	
<?php
 | 
						|
 | 
						|
require_once "AutoStoring.php";
 | 
						|
 | 
						|
class Group extends AutoStoring
 | 
						|
{
 | 
						|
    private int $_id; // Group's ID (assigned by SleekDB)
 | 
						|
    private string $name; // Group's name
 | 
						|
    private bool $unique; // Indicates if name is unique or not
 | 
						|
    private string $owner; // Group owner's nickname
 | 
						|
    private string $description; // Group description
 | 
						|
    private array $editors; // Nicknames of users able to manage the group
 | 
						|
    private array $members; // Nickname of group members
 | 
						|
    private array $games; // Game IDs assigned to this group
 | 
						|
    private GroupMgr $groupMgr; // Reference to GroupMgr object managing this group
 | 
						|
 | 
						|
    // --------------
 | 
						|
 | 
						|
    // store modifications to the database
 | 
						|
    public function storeMods() : void
 | 
						|
    {
 | 
						|
        $this->groupMgr->updateGroup($this);
 | 
						|
    }
 | 
						|
 | 
						|
    // --------------
 | 
						|
 | 
						|
    function __construct(GroupMgr &$groupMgr, string $name, string $description, string $owner, int $id = -1, bool $unique = true, array $editors = [], array $members = [], array $games = [])
 | 
						|
    {
 | 
						|
        parent::__construct();
 | 
						|
 | 
						|
        $this->_id = $id;
 | 
						|
        $this->name = $name;
 | 
						|
        $this->unique = $unique;
 | 
						|
        $this->description = $description;
 | 
						|
        $this->owner = $owner;
 | 
						|
        $this->editors = $editors;
 | 
						|
        $this->members = $members;
 | 
						|
        $this->games = $games;
 | 
						|
        $this->groupMgr = &$groupMgr;
 | 
						|
    }
 | 
						|
 | 
						|
    // Create Group from array
 | 
						|
    static function fromArray(GroupMgr &$groupMgr, array $a): Group
 | 
						|
    {
 | 
						|
        $id = $a["_id"] ?? -1;
 | 
						|
        return new Group($groupMgr, $a["groupname"], $a["description"], $a["owner"], $id, $a["unique"], $a["editors"], $a["users"], $a["games"]);
 | 
						|
    }
 | 
						|
 | 
						|
    // Convert Group to array
 | 
						|
    function toArray(array $omit = []): array
 | 
						|
    {
 | 
						|
        $a = [
 | 
						|
            "_id" => $this->_id,
 | 
						|
            "groupname" => $this->name,
 | 
						|
            "unique" => $this->unique,
 | 
						|
            "description" => $this->description,
 | 
						|
            "owner" => $this->owner,
 | 
						|
            "editors" => $this->editors,
 | 
						|
            "users" => $this->members,
 | 
						|
            "games" => $this->games
 | 
						|
        ];
 | 
						|
 | 
						|
        foreach ($omit as $field) {
 | 
						|
            unset($a[$field]);
 | 
						|
        }
 | 
						|
 | 
						|
        return $a;
 | 
						|
    }
 | 
						|
 | 
						|
    // Get group's ID.
 | 
						|
    function getID(): int
 | 
						|
    {
 | 
						|
        return $this->_id;
 | 
						|
    }
 | 
						|
 | 
						|
    // Get group's name.
 | 
						|
    function getName(): string
 | 
						|
    {
 | 
						|
        return $this->name;
 | 
						|
    }
 | 
						|
 | 
						|
    // Set group's name.
 | 
						|
    function setName(string $name): void
 | 
						|
    {
 | 
						|
        $this->name = $name;
 | 
						|
        $this->storeMods();
 | 
						|
    }
 | 
						|
 | 
						|
    // Tell if group is unique
 | 
						|
    function isUnique(): bool
 | 
						|
    {
 | 
						|
        return $this->unique;
 | 
						|
    }
 | 
						|
 | 
						|
    // Get group's description.
 | 
						|
    function getDescription(): string
 | 
						|
    {
 | 
						|
        return $this->description;
 | 
						|
    }
 | 
						|
 | 
						|
    // Set group's description.
 | 
						|
    function setDescription(string $description): void
 | 
						|
    {
 | 
						|
        $this->description = $description;
 | 
						|
        $this->storeMods();
 | 
						|
    }
 | 
						|
 | 
						|
    // Get group's owner.
 | 
						|
    function getOwner(): string
 | 
						|
    {
 | 
						|
        return $this->owner;
 | 
						|
    }
 | 
						|
 | 
						|
    // Set group's owner.
 | 
						|
    function setOwner(string $owner): void
 | 
						|
    {
 | 
						|
        $this->owner = $owner;
 | 
						|
        $this->storeMods();
 | 
						|
    }
 | 
						|
 | 
						|
    // Get list of editors.
 | 
						|
    function getEditors(): array
 | 
						|
    {
 | 
						|
        return $this->editors;
 | 
						|
    }
 | 
						|
 | 
						|
    // Set editors.
 | 
						|
    function setEditors(array $editors): void
 | 
						|
    {
 | 
						|
        $this->editors = $editors;
 | 
						|
        $this->storeMods();
 | 
						|
    }
 | 
						|
 | 
						|
    // Get group members.
 | 
						|
    function getMembers(): array
 | 
						|
    {
 | 
						|
        return $this->members;
 | 
						|
    }
 | 
						|
 | 
						|
    // Set group members.
 | 
						|
    function setMembers(array $members): void
 | 
						|
    {
 | 
						|
        $this->members = $members;
 | 
						|
        $this->storeMods();
 | 
						|
    }
 | 
						|
 | 
						|
    // Get games.
 | 
						|
    function getGames(): array
 | 
						|
    {
 | 
						|
        return $this->games;
 | 
						|
    }
 | 
						|
 | 
						|
    // Set games.
 | 
						|
    function setGames(array $games): void
 | 
						|
    {
 | 
						|
        $this->games = $games;
 | 
						|
        $this->storeMods();
 | 
						|
    }
 | 
						|
 | 
						|
    // Include/exclude members.
 | 
						|
    function changeMembers(array $nicknames_add, array $nicknames_remove): void
 | 
						|
    {
 | 
						|
        foreach ($nicknames_add as $nickname) { // add members
 | 
						|
            alter_array_contents($this->members, $nickname, null);
 | 
						|
        }
 | 
						|
        foreach ($nicknames_remove as $nickname) { // remove members
 | 
						|
            alter_array_contents($this->members, null, $nickname); // delete from members
 | 
						|
            alter_array_contents($this->editors, null, $nickname); // delete from editors
 | 
						|
        }
 | 
						|
 | 
						|
        $this->storeMods(); // store changes
 | 
						|
    }
 | 
						|
 | 
						|
    // Add members
 | 
						|
    function addMembers(array $nicknames) : void {
 | 
						|
        $this->changeMembers($nicknames, []);
 | 
						|
    }
 | 
						|
 | 
						|
    // Remove members
 | 
						|
    function removeMembers(array $nicknames) : void {
 | 
						|
        $this->changeMembers([], $nicknames);
 | 
						|
    }
 | 
						|
 | 
						|
    // Include/exclude games.
 | 
						|
    function changeGames(array $gameids_add, array $gameids_remove): void
 | 
						|
    {
 | 
						|
        foreach ($gameids_add as $gameid) { // add games
 | 
						|
            alter_array_contents($this->games, $gameid, null);
 | 
						|
        }
 | 
						|
        foreach ($gameids_remove as $gameid) { // remove games
 | 
						|
            alter_array_contents($this->games, null, $gameid);
 | 
						|
        }
 | 
						|
 | 
						|
        $this->storeMods(); // store changes
 | 
						|
    }
 | 
						|
 | 
						|
    // Returns whether the user is an editor of this group.
 | 
						|
    function isUserEditor(string $nickname): bool
 | 
						|
    {
 | 
						|
        return in_array($nickname, $this->editors);
 | 
						|
    }
 | 
						|
 | 
						|
    // Returns whether the user is an editor or the owner of the group.
 | 
						|
    function isUserContributor(string $nickname): bool
 | 
						|
    {
 | 
						|
        return $this->isUserEditor($nickname) || ($this->owner === $nickname);
 | 
						|
    }
 | 
						|
 | 
						|
    // Returns if user is member of the group.
 | 
						|
    function isMember(string $nickname): bool
 | 
						|
    {
 | 
						|
        return in_array($nickname, $this->members);
 | 
						|
    }
 | 
						|
 | 
						|
    // Return if game is assigned to this group.
 | 
						|
    function isGameAssigned(string $gameid): bool
 | 
						|
    {
 | 
						|
        return in_array($gameid, $this->games);
 | 
						|
    }
 | 
						|
 | 
						|
    // Get groups unique name.
 | 
						|
    function getUniqueName(): string
 | 
						|
    {
 | 
						|
        return $this->name . ($this->unique ? "" : ("#" . $this->_id));
 | 
						|
    }
 | 
						|
} |