2025-01-31 18:20:13 +03:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2024-12-20 17:34:29 +03:00
|
|
|
namespace openvk\Web\Models\Entities\UserInfoEntities;
|
2025-01-31 18:20:13 +03:00
|
|
|
|
2024-12-20 17:34:29 +03:00
|
|
|
use openvk\Web\Models\RowModel;
|
|
|
|
use openvk\Web\Models\Repositories\Users;
|
|
|
|
use Chandler\Database\DatabaseConnection;
|
|
|
|
|
|
|
|
class AdditionalField extends RowModel
|
|
|
|
{
|
|
|
|
protected $tableName = "additional_fields";
|
|
|
|
|
2025-01-31 18:20:13 +03:00
|
|
|
public const PLACE_CONTACTS = 0;
|
|
|
|
public const PLACE_INTERESTS = 1;
|
2024-12-20 17:34:29 +03:00
|
|
|
|
2025-01-31 18:20:13 +03:00
|
|
|
public function getOwner(): int
|
2024-12-20 17:34:29 +03:00
|
|
|
{
|
|
|
|
return (int) $this->getRecord()->owner;
|
|
|
|
}
|
|
|
|
|
2025-01-31 18:20:13 +03:00
|
|
|
public function getName(bool $tr = true): string
|
2024-12-20 17:34:29 +03:00
|
|
|
{
|
|
|
|
$orig_name = $this->getRecord()->name;
|
|
|
|
$name = $orig_name;
|
2025-01-31 18:20:13 +03:00
|
|
|
if ($tr && $name[0] === "_") {
|
2024-12-20 17:34:29 +03:00
|
|
|
$name = tr("custom_field_" . substr($name, 1));
|
2025-01-31 18:20:13 +03:00
|
|
|
}
|
2024-12-20 17:34:29 +03:00
|
|
|
|
2025-01-31 18:20:13 +03:00
|
|
|
if (str_contains($name, "custom_field")) {
|
2024-12-20 17:34:29 +03:00
|
|
|
return $orig_name;
|
2025-01-31 18:20:13 +03:00
|
|
|
}
|
2024-12-20 17:34:29 +03:00
|
|
|
|
|
|
|
return $name;
|
|
|
|
}
|
|
|
|
|
2025-01-31 18:20:13 +03:00
|
|
|
public function getContent(): string
|
2024-12-20 17:34:29 +03:00
|
|
|
{
|
|
|
|
return $this->getRecord()->text;
|
|
|
|
}
|
|
|
|
|
2025-01-31 18:20:13 +03:00
|
|
|
public function getPlace(): string
|
2024-12-20 17:34:29 +03:00
|
|
|
{
|
2025-01-31 18:20:13 +03:00
|
|
|
switch ($this->getRecord()->place) {
|
2024-12-20 17:34:29 +03:00
|
|
|
case AdditionalField::PLACE_CONTACTS:
|
|
|
|
return "contact";
|
|
|
|
case AdditionalField::PLACE_INTERESTS:
|
|
|
|
return "interest";
|
|
|
|
}
|
|
|
|
|
|
|
|
return "contact";
|
|
|
|
}
|
|
|
|
|
2025-01-31 18:20:13 +03:00
|
|
|
public function isContact(): bool
|
2024-12-20 17:34:29 +03:00
|
|
|
{
|
|
|
|
return $this->getRecord()->place == AdditionalField::PLACE_CONTACTS;
|
|
|
|
}
|
|
|
|
|
2025-01-31 18:20:13 +03:00
|
|
|
public function toVkApiStruct(): object
|
2024-12-20 17:34:29 +03:00
|
|
|
{
|
|
|
|
return (object) [
|
|
|
|
"type" => $this->getRecord()->place,
|
|
|
|
"name" => $this->getName(),
|
2025-01-31 18:20:13 +03:00
|
|
|
"text" => $this->getContent(),
|
2024-12-20 17:34:29 +03:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2025-01-31 18:20:13 +03:00
|
|
|
public static function getById(int $id)
|
2024-12-20 17:34:29 +03:00
|
|
|
{
|
|
|
|
$ctx = DatabaseConnection::i()->getContext();
|
|
|
|
$entry = $ctx->table("additional_fields")->where("id", $id)->fetch();
|
|
|
|
|
2025-01-31 18:20:13 +03:00
|
|
|
if (!$entry) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2024-12-20 17:34:29 +03:00
|
|
|
return new AdditionalField($entry);
|
|
|
|
}
|
|
|
|
|
2025-01-31 18:20:13 +03:00
|
|
|
public static function getByOwner(int $owner): \Traversable
|
2024-12-20 17:34:29 +03:00
|
|
|
{
|
|
|
|
$ctx = DatabaseConnection::i()->getContext();
|
|
|
|
$entries = $ctx->table("additional_fields")->where("owner", $owner);
|
|
|
|
|
2025-01-31 18:20:13 +03:00
|
|
|
foreach ($entries as $entry) {
|
2024-12-20 17:34:29 +03:00
|
|
|
yield new AdditionalField($entry);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-01-31 18:20:13 +03:00
|
|
|
public static function getCountByOwner(int $owner): \Traversable
|
2024-12-20 17:34:29 +03:00
|
|
|
{
|
|
|
|
return DatabaseConnection::i()->getContext()->table("additional_fields")->where("owner", $owner)->count();
|
|
|
|
}
|
|
|
|
|
2025-01-31 18:20:13 +03:00
|
|
|
public static function resetByOwner(int $owner): bool
|
2024-12-20 17:34:29 +03:00
|
|
|
{
|
|
|
|
DatabaseConnection::i()->getContext()->table("additional_fields")->where("owner", $owner)->delete();
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|