From 1d46bbb6d6eff421b045536f81a8842472037a9e Mon Sep 17 00:00:00 2001 From: Alma Armas Date: Wed, 27 Jan 2021 17:41:21 +0000 Subject: [PATCH] Refactor albums --- Web/Models/Entities/Album.php | 103 +++++++--------------------------- 1 file changed, 21 insertions(+), 82 deletions(-) diff --git a/Web/Models/Entities/Album.php b/Web/Models/Entities/Album.php index 2ee36302..b20f0388 100644 --- a/Web/Models/Entities/Album.php +++ b/Web/Models/Entities/Album.php @@ -1,48 +1,30 @@ getRecord()->owner; - if($oid > 0) - return (new Users)->get($oid); - else - return (new Clubs)->get($oid * -1); - } + protected $specialNames = [ + 16 => "_avatar_album", + 32 => "_wall_album", + 64 => "_saved_photos_album", + ]; - function getPrettyId(): string + function getCoverURL(): ?string { - return $this->getRecord()->owner . "_" . $this->getRecord()->id; - } - - function getName(): string - { - switch($this->getRecord()->special_type) { - case Album::SPECIAL_AVATARS: - return "Изображения со страницы"; - case Album::SPECIAL_WALL: - return "Изображения со стены"; - default: - return $this->getRecord()->name; - } - } - - function getDescription(): ?string - { - return $this->getRecord()->description; + $coverPhoto = $this->getCoverPhoto(); + if(!$coverPhoto) + return "/assets/packages/static/openvk/img/camera_200.png"; + + return $coverPhoto->getURL(); } function getCoverPhoto(): ?Photo @@ -62,69 +44,26 @@ class Album extends RowModel function getPhotos(int $page = 1, ?int $perPage = NULL): \Traversable { - $perPage = $perPage ?? OPENVK_DEFAULT_PER_PAGE; - - foreach($this->getRecord()->related("album_relations.album")->page($page, $perPage)->order("photo ASC") as $rel) { - $photo = $rel->ref("photos", "photo"); - if(!$photo) continue; - - yield new Photo($photo); - } + return $this->fetch($page, $perPage); } function getPhotosCount(): int { - return sizeof($this->getRecord()->related("album_relations.album")); - } - - function getCreationTime(): DateTime - { - return new DateTime($this->getRecord()->created); - } - - function getPublicationTime(): DateTime - { - return $this->getCreationTime(); - } - - function getEditTime(): ?DateTime - { - $edited = $this->getRecord()->edited; - if(is_null($edited)) return NULL; - - return new DateTime($edited); - } - - function isCreatedBySystem(): bool - { - return $this->getRecord()->special_type !== 0; + return $this->size(); } function addPhoto(Photo $photo): void { - DatabaseConnection::i()->getContext()->table("album_relations")->insert([ - "album" => $this->getRecord()->id, - "photo" => $photo->getId(), - ]); + $this->add($photo); } function removePhoto(Photo $photo): void { - DatabaseConnection::i()->getContext()->table("album_relations")->where([ - "album" => $this->getRecord()->id, - "photo" => $photo->getId(), - ])->delete(); + $this->remove($photo); } function hasPhoto(Photo $photo): bool { - $rel = DatabaseConnection::i()->getContext()->table("album_relations")->where([ - "album" => $this->getRecord()->id, - "photo" => $photo->getId(), - ])->fetch(); - - return !is_null($rel); + return $this->has($photo); } - - use Traits\TOwnable; }