Add club managers logic to Club model

This commit is contained in:
Alma Armas 2020-08-01 14:22:52 +00:00
parent b708fa8421
commit 98045ec121

View file

@ -2,8 +2,8 @@
namespace openvk\Web\Models\Entities; namespace openvk\Web\Models\Entities;
use openvk\Web\Util\DateTime; use openvk\Web\Util\DateTime;
use openvk\Web\Models\RowModel; use openvk\Web\Models\RowModel;
use openvk\Web\Models\Entities\User; use openvk\Web\Models\Entities\{User, Manager};
use openvk\Web\Models\Repositories\{Users, Clubs, Albums}; use openvk\Web\Models\Repositories\{Users, Clubs, Albums, Managers};
use Nette\Database\Table\{ActiveRow, GroupedSelection}; use Nette\Database\Table\{ActiveRow, GroupedSelection};
use Chandler\Database\DatabaseConnection as DB; use Chandler\Database\DatabaseConnection as DB;
use Chandler\Security\User as ChandlerUser; use Chandler\Security\User as ChandlerUser;
@ -265,6 +265,20 @@ class Club extends RowModel
foreach($rels as $rel) { foreach($rels as $rel) {
$rel = (new Users)->get($rel->user); $rel = (new Users)->get($rel->user);
if(!$rel) continue; if(!$rel) continue;
yield $rel;
}
}
function getManagersWithComment(int $page = 1): \Traversable
{
$rels = $this->getRecord()->related("group_coadmins.club")->where("comment IS NOT NULL")->page($page, 10);
foreach($rels as $rel) {
$rel = (new Managers)->get($rel->id);
if(!$rel) continue;
yield $rel;
} }
} }
@ -272,6 +286,11 @@ class Club extends RowModel
{ {
return sizeof($this->getRecord()->related("group_coadmins.club")) + 1; return sizeof($this->getRecord()->related("group_coadmins.club")) + 1;
} }
function getManagersCountWithComment(): int
{
return sizeof($this->getRecord()->related("group_coadmins.club")->where("comment IS NOT NULL")) + 1;
}
function addManager(User $user, ?string $comment = NULL): void function addManager(User $user, ?string $comment = NULL): void
{ {
@ -293,7 +312,7 @@ class Club extends RowModel
function canBeModifiedBy(User $user): bool function canBeModifiedBy(User $user): bool
{ {
$id = $user->getId(); $id = $user->getId();
if($this->getRecord()->owner === $id) if($this->getOwner()->getId() === $id)
return true; return true;
return !is_null($this->getRecord()->related("group_coadmins.club")->where("user", $id)->fetch()); return !is_null($this->getRecord()->related("group_coadmins.club")->where("user", $id)->fetch());