2020-06-07 19:04:43 +03:00
|
|
|
<?php declare(strict_types=1);
|
|
|
|
namespace openvk\Web\Models\Entities\Traits;
|
2022-12-11 20:30:01 +03:00
|
|
|
use openvk\Web\Models\Entities\{Attachable, Photo};
|
|
|
|
use openvk\Web\Util\Makima\Makima;
|
2020-06-07 19:04:43 +03:00
|
|
|
use Chandler\Database\DatabaseConnection;
|
|
|
|
|
|
|
|
trait TAttachmentHost
|
|
|
|
{
|
|
|
|
private function composeAttachmentRequestData(Attachable $attachment): array
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
"target_type" => get_class($this),
|
|
|
|
"target_id" => $this->getId(),
|
|
|
|
"attachable_type" => get_class($attachment),
|
|
|
|
"attachable_id" => $attachment->getId(),
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
function getChildren(): \Traversable
|
|
|
|
{
|
2020-06-11 12:55:43 +03:00
|
|
|
$sel = DatabaseConnection::i()->getContext()
|
|
|
|
->table("attachments")
|
|
|
|
->where("target_id", $this->getId())
|
|
|
|
->where("attachments.target_type", get_class($this));
|
2020-06-07 19:04:43 +03:00
|
|
|
foreach($sel as $rel) {
|
|
|
|
$repoName = $rel->attachable_type . "s";
|
|
|
|
$repoName = str_replace("Entities", "Repositories", $repoName);
|
|
|
|
$repo = new $repoName;
|
|
|
|
|
|
|
|
yield $repo->get($rel->attachable_id);
|
|
|
|
}
|
|
|
|
}
|
2022-12-11 20:30:01 +03:00
|
|
|
|
|
|
|
function getChildrenWithLayout(int $w, int $h = -1): object
|
|
|
|
{
|
|
|
|
if($h < 0)
|
|
|
|
$h = $w;
|
|
|
|
|
|
|
|
$children = $this->getChildren();
|
|
|
|
$skipped = $photos = $result = [];
|
|
|
|
foreach($children as $child) {
|
|
|
|
if($child instanceof Photo) {
|
|
|
|
$photos[] = $child;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$skipped[] = ["100%", "unset", $child, "unset"];
|
|
|
|
}
|
|
|
|
|
|
|
|
$height = "unset";
|
|
|
|
$width = $w;
|
|
|
|
if(sizeof($photos) < 2) {
|
|
|
|
if(isset($photos[0]))
|
|
|
|
$result[] = ["100%", "unset", $photos[0], "unset"];
|
|
|
|
} else {
|
|
|
|
$mak = new Makima($photos);
|
|
|
|
$layout = $mak->computeMasonryLayout($w, $h);
|
|
|
|
$height = $layout->height;
|
|
|
|
$width = $layout->width;
|
|
|
|
for($i = 0; $i < sizeof($photos); $i++) {
|
|
|
|
$tile = $layout->tiles[$i];
|
|
|
|
$result[] = [$tile->width . "px", $tile->height . "px", $photos[$i], "left"];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return (object) [
|
|
|
|
"width" => $width . "px",
|
|
|
|
"height" => $height . "px",
|
|
|
|
"tiles" => array_merge($result, $skipped),
|
|
|
|
];
|
|
|
|
}
|
2020-06-07 19:04:43 +03:00
|
|
|
|
|
|
|
function attach(Attachable $attachment): void
|
|
|
|
{
|
|
|
|
DatabaseConnection::i()->getContext()
|
|
|
|
->table("attachments")
|
|
|
|
->insert($this->composeAttachmentRequestData($attachment));
|
|
|
|
}
|
|
|
|
|
|
|
|
function detach(Attachable $attachment): bool
|
|
|
|
{
|
|
|
|
$res = DatabaseConnection::i()->getContext()
|
|
|
|
->table("attachments")
|
|
|
|
->where($this->composeAttachmentRequestData($attachment))
|
|
|
|
->delete();
|
|
|
|
|
|
|
|
return $res > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
function unwire(): void
|
|
|
|
{
|
|
|
|
$this->getRecord()
|
|
|
|
->related("attachments.target_id")
|
|
|
|
->where("attachments.target_type", get_class($this))
|
|
|
|
->delete();
|
|
|
|
}
|
|
|
|
}
|