Refactor albums

This commit is contained in:
Alma Armas 2021-01-27 17:41:21 +00:00
parent 1e5f3f395b
commit 1d46bbb6d6

View file

@ -1,48 +1,30 @@
<?php declare(strict_types=1); <?php declare(strict_types=1);
namespace openvk\Web\Models\Entities; namespace openvk\Web\Models\Entities;
use openvk\Web\Util\DateTime;
use openvk\Web\Models\RowModel;
use openvk\Web\Models\Repositories\Photos; use openvk\Web\Models\Repositories\Photos;
use openvk\Web\Models\Repositories\Clubs;
use openvk\Web\Models\Repositories\Users;
use Chandler\Database\DatabaseConnection;
class Album extends RowModel class Album extends MediaCollection
{ {
const SPECIAL_AVATARS = 16; const SPECIAL_AVATARS = 16;
const SPECIAL_WALL = 32; const SPECIAL_WALL = 32;
protected $tableName = "albums"; protected $tableName = "albums";
protected $relTableName = "album_relations";
protected $entityTableName = "photos";
protected $entityClassName = 'openvk\Web\Models\Entities\Photo';
function getOwner(): RowModel protected $specialNames = [
{ 16 => "_avatar_album",
$oid = $this->getRecord()->owner; 32 => "_wall_album",
if($oid > 0) 64 => "_saved_photos_album",
return (new Users)->get($oid); ];
else
return (new Clubs)->get($oid * -1);
}
function getPrettyId(): string function getCoverURL(): ?string
{ {
return $this->getRecord()->owner . "_" . $this->getRecord()->id; $coverPhoto = $this->getCoverPhoto();
} if(!$coverPhoto)
return "/assets/packages/static/openvk/img/camera_200.png";
function getName(): string return $coverPhoto->getURL();
{
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;
} }
function getCoverPhoto(): ?Photo function getCoverPhoto(): ?Photo
@ -62,69 +44,26 @@ class Album extends RowModel
function getPhotos(int $page = 1, ?int $perPage = NULL): \Traversable function getPhotos(int $page = 1, ?int $perPage = NULL): \Traversable
{ {
$perPage = $perPage ?? OPENVK_DEFAULT_PER_PAGE; return $this->fetch($page, $perPage);
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);
}
} }
function getPhotosCount(): int function getPhotosCount(): int
{ {
return sizeof($this->getRecord()->related("album_relations.album")); return $this->size();
}
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;
} }
function addPhoto(Photo $photo): void function addPhoto(Photo $photo): void
{ {
DatabaseConnection::i()->getContext()->table("album_relations")->insert([ $this->add($photo);
"album" => $this->getRecord()->id,
"photo" => $photo->getId(),
]);
} }
function removePhoto(Photo $photo): void function removePhoto(Photo $photo): void
{ {
DatabaseConnection::i()->getContext()->table("album_relations")->where([ $this->remove($photo);
"album" => $this->getRecord()->id,
"photo" => $photo->getId(),
])->delete();
} }
function hasPhoto(Photo $photo): bool function hasPhoto(Photo $photo): bool
{ {
$rel = DatabaseConnection::i()->getContext()->table("album_relations")->where([ return $this->has($photo);
"album" => $this->getRecord()->id,
"photo" => $photo->getId(),
])->fetch();
return !is_null($rel);
} }
use Traits\TOwnable;
} }