2025-01-31 18:20:13 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2021-10-07 11:48:55 +03:00
|
|
|
namespace openvk\Web\Models\Entities;
|
2025-01-31 18:20:13 +03:00
|
|
|
|
2021-10-07 11:48:55 +03:00
|
|
|
use openvk\Web\Util\DateTime;
|
|
|
|
use openvk\Web\Models\RowModel;
|
|
|
|
use openvk\Web\Models\Entities\User;
|
|
|
|
use Nette\Utils\{Image, ImageException};
|
|
|
|
|
|
|
|
class Gift extends RowModel
|
|
|
|
{
|
2025-01-31 18:20:13 +03:00
|
|
|
public const IMAGE_MAXSIZE = 131072;
|
|
|
|
public const IMAGE_BINARY = 0;
|
|
|
|
public const IMAGE_BASE64 = 1;
|
|
|
|
public const IMAGE_URL = 2;
|
|
|
|
|
|
|
|
public const PERIOD_IGNORE = 0;
|
|
|
|
public const PERIOD_SET = 1;
|
|
|
|
public const PERIOD_SET_IF_NONE = 2;
|
|
|
|
|
2021-10-07 11:48:55 +03:00
|
|
|
protected $tableName = "gifts";
|
2025-01-31 18:20:13 +03:00
|
|
|
|
|
|
|
public function getName(): string
|
2021-10-07 11:48:55 +03:00
|
|
|
{
|
|
|
|
return $this->getRecord()->internal_name;
|
|
|
|
}
|
2025-01-31 18:20:13 +03:00
|
|
|
|
|
|
|
public function getPrice(): int
|
2021-10-07 11:48:55 +03:00
|
|
|
{
|
|
|
|
return $this->getRecord()->price;
|
|
|
|
}
|
2025-01-31 18:20:13 +03:00
|
|
|
|
|
|
|
public function getUsages(): int
|
2021-10-07 11:48:55 +03:00
|
|
|
{
|
|
|
|
return $this->getRecord()->usages;
|
|
|
|
}
|
2025-01-31 18:20:13 +03:00
|
|
|
|
|
|
|
public function getUsagesBy(User $user, ?int $since = null): int
|
2021-10-07 11:48:55 +03:00
|
|
|
{
|
|
|
|
$sent = $this->getRecord()
|
|
|
|
->related("gift_user_relations.gift")
|
|
|
|
->where("sender", $user->getId())
|
|
|
|
->where("sent >= ?", $since ?? $this->getRecord()->limit_period ?? 0);
|
2025-01-31 18:20:13 +03:00
|
|
|
|
2021-10-07 11:48:55 +03:00
|
|
|
return sizeof($sent);
|
|
|
|
}
|
2025-01-31 18:20:13 +03:00
|
|
|
|
|
|
|
public function getUsagesLeft(User $user): float
|
2021-10-07 11:48:55 +03:00
|
|
|
{
|
2025-01-31 18:20:13 +03:00
|
|
|
if ($this->getLimit() === INF) {
|
2021-10-07 11:48:55 +03:00
|
|
|
return INF;
|
2025-01-31 18:20:13 +03:00
|
|
|
}
|
|
|
|
|
2021-10-07 11:48:55 +03:00
|
|
|
return max(0, $this->getLimit() - $this->getUsagesBy($user));
|
|
|
|
}
|
2025-01-31 18:20:13 +03:00
|
|
|
|
|
|
|
public function getImage(int $type = 0): /* ?binary */ string
|
2021-10-07 11:48:55 +03:00
|
|
|
{
|
2025-01-31 18:20:13 +03:00
|
|
|
switch ($type) {
|
2021-10-07 11:48:55 +03:00
|
|
|
default:
|
|
|
|
case static::IMAGE_BINARY:
|
|
|
|
return $this->getRecord()->image ?? "";
|
|
|
|
break;
|
|
|
|
case static::IMAGE_BASE64:
|
|
|
|
return "data:image/png;base64," . base64_encode($this->getRecord()->image ?? "");
|
|
|
|
break;
|
|
|
|
case static::IMAGE_URL:
|
|
|
|
return "/gift" . $this->getId() . "_" . $this->getUpdateDate()->timestamp() . ".png";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2025-01-31 18:20:13 +03:00
|
|
|
|
|
|
|
public function getLimit(): float
|
2021-10-07 11:48:55 +03:00
|
|
|
{
|
|
|
|
$limit = $this->getRecord()->limit;
|
2025-01-31 18:20:13 +03:00
|
|
|
|
2021-10-07 11:48:55 +03:00
|
|
|
return !$limit ? INF : (float) $limit;
|
|
|
|
}
|
2025-01-31 18:20:13 +03:00
|
|
|
|
|
|
|
public function getLimitResetTime(): ?DateTime
|
2021-10-07 11:48:55 +03:00
|
|
|
{
|
2025-01-31 18:20:13 +03:00
|
|
|
return is_null($t = $this->getRecord()->limit_period) ? null : new DateTime($t);
|
2021-10-07 11:48:55 +03:00
|
|
|
}
|
2025-01-31 18:20:13 +03:00
|
|
|
|
|
|
|
public function getUpdateDate(): DateTime
|
2021-10-07 11:48:55 +03:00
|
|
|
{
|
|
|
|
return new DateTime($this->getRecord()->updated);
|
|
|
|
}
|
2025-01-31 18:20:13 +03:00
|
|
|
|
|
|
|
public function canUse(User $user): bool
|
2021-10-07 11:48:55 +03:00
|
|
|
{
|
|
|
|
return $this->getUsagesLeft($user) > 0;
|
|
|
|
}
|
2025-01-31 18:20:13 +03:00
|
|
|
|
|
|
|
public function isFree(): bool
|
2021-10-07 11:48:55 +03:00
|
|
|
{
|
|
|
|
return $this->getPrice() === 0;
|
|
|
|
}
|
2025-01-31 18:20:13 +03:00
|
|
|
|
|
|
|
public function used(): void
|
2021-10-07 11:48:55 +03:00
|
|
|
{
|
|
|
|
$this->stateChanges("usages", $this->getUsages() + 1);
|
|
|
|
$this->save();
|
|
|
|
}
|
2025-01-31 18:20:13 +03:00
|
|
|
|
|
|
|
public function setName(string $name): void
|
2021-10-07 11:48:55 +03:00
|
|
|
{
|
|
|
|
$this->stateChanges("internal_name", $name);
|
|
|
|
}
|
2025-01-31 18:20:13 +03:00
|
|
|
|
|
|
|
public function setImage(string $file): bool
|
2021-10-07 11:48:55 +03:00
|
|
|
{
|
|
|
|
$imgBlob;
|
|
|
|
try {
|
|
|
|
$image = Image::fromFile($file);
|
|
|
|
$image->resize(512, 512, Image::SHRINK_ONLY);
|
2025-01-31 18:20:13 +03:00
|
|
|
|
2021-10-07 11:48:55 +03:00
|
|
|
$imgBlob = $image->toString(Image::PNG);
|
2025-01-31 18:20:13 +03:00
|
|
|
} catch (ImageException $ex) {
|
2021-10-07 11:48:55 +03:00
|
|
|
return false;
|
|
|
|
}
|
2025-01-31 18:20:13 +03:00
|
|
|
|
|
|
|
if (strlen($imgBlob) > (2 ** 24 - 1)) {
|
2021-10-07 11:48:55 +03:00
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
$this->stateChanges("updated", time());
|
|
|
|
$this->stateChanges("image", $imgBlob);
|
|
|
|
}
|
2025-01-31 18:20:13 +03:00
|
|
|
|
2021-10-07 11:48:55 +03:00
|
|
|
return true;
|
|
|
|
}
|
2025-01-31 18:20:13 +03:00
|
|
|
|
|
|
|
public function setLimit(?float $limit = null, int $periodBehaviour = 0): void
|
2021-10-07 11:48:55 +03:00
|
|
|
{
|
|
|
|
$limit ??= $this->getLimit();
|
2025-01-31 18:20:13 +03:00
|
|
|
$limit = $limit === INF ? null : (int) $limit;
|
2021-10-07 11:48:55 +03:00
|
|
|
$this->stateChanges("limit", $limit);
|
2025-01-31 18:20:13 +03:00
|
|
|
|
|
|
|
if (!$limit) {
|
|
|
|
$this->stateChanges("limit_period", null);
|
2021-10-07 11:48:55 +03:00
|
|
|
return;
|
|
|
|
}
|
2025-01-31 18:20:13 +03:00
|
|
|
|
|
|
|
switch ($periodBehaviour) {
|
2021-10-07 11:48:55 +03:00
|
|
|
default:
|
|
|
|
case static::PERIOD_IGNORE:
|
|
|
|
break;
|
2025-01-31 18:20:13 +03:00
|
|
|
|
2021-10-07 11:48:55 +03:00
|
|
|
case static::PERIOD_SET:
|
|
|
|
$this->stateChanges("limit_period", time());
|
|
|
|
break;
|
2025-01-31 18:20:13 +03:00
|
|
|
|
2021-10-07 11:48:55 +03:00
|
|
|
case static::PERIOD_SET_IF_NONE:
|
2025-01-31 18:20:13 +03:00
|
|
|
if (is_null($this->getRecord()) || is_null($this->getRecord()->limit_period)) {
|
2021-10-07 11:48:55 +03:00
|
|
|
$this->stateChanges("limit_period", time());
|
2025-01-31 18:20:13 +03:00
|
|
|
}
|
|
|
|
|
2021-10-07 11:48:55 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2025-01-31 18:20:13 +03:00
|
|
|
|
|
|
|
public function delete(bool $softly = true): void
|
2021-10-07 11:48:55 +03:00
|
|
|
{
|
|
|
|
$this->getRecord()->related("gift_relations.gift")->delete();
|
2025-01-31 18:20:13 +03:00
|
|
|
|
2021-10-07 11:48:55 +03:00
|
|
|
parent::delete($softly);
|
|
|
|
}
|
|
|
|
}
|