add additional fields

This commit is contained in:
mrilyew 2024-12-19 18:34:12 +03:00
parent 996108289c
commit 813306f38e
10 changed files with 212 additions and 1 deletions

View file

@ -296,4 +296,49 @@ final class Account extends VKAPIRequestHandler
return $result;
}
function saveInterestsInfo(
string $interests = NULL,
string $fav_music = NULL,
string $fav_films = NULL,
string $fav_shows = NULL,
string $fav_books = NULL,
string $fav_quote = NULL,
string $fav_games = NULL,
string $about = NULL,
)
{
$this->requireUser();
$this->willExecuteWriteAction();
$user = $this->getUser();
$changes = 0;
$changes_array = [
"interests" => $interests,
"fav_music" => $fav_music,
"fav_films" => $fav_films,
"fav_books" => $fav_books,
"fav_shows" => $fav_shows,
"fav_quote" => $fav_quote,
"fav_games" => $fav_games,
"about" => $about,
];
foreach($changes_array as $change_name => $change_value) {
$set_name = "set".ucfirst($change_name);
$get_name = "get".str_replace("Fav", "Favorite", str_replace("_", "", ucfirst($change_name)));
if(!is_null($change_value) && $change_value !== $user->$get_name()) {
$user->$set_name(ovk_proc_strtr($change_value, 1000));
$changes += 1;
}
}
if($changes > 0) {
$user->save();
}
return (object) [
"changed" => (int)($changes > 0),
];
}
}

View file

@ -293,6 +293,17 @@ final class Users extends VKAPIRequestHandler
$response[$i]->blacklisted = (int)$this->getUser()->isBlacklistedBy($usr);
break;
case "custom_fields":
if(sizeof($usrs) > 1)
break;
$c_fields = \openvk\Web\Models\Entities\UserInfoEntities\AdditionalField::getByOwner($usr->getId());
$append_array = [];
foreach($c_fields as $c_field)
$append_array[] = $c_field->toVkApiStruct();
$response[$i]->custom_fields = $append_array;
break;
}
}

View file

@ -228,6 +228,11 @@ class User extends RowModel
return $this->getRecord()->about;
}
function getAbout(): ?string
{
return $this->getRecord()->about;
}
function getStatus(): ?string
{
return $this->getRecord()->status;
@ -430,6 +435,30 @@ class User extends RowModel
return $this->getRecord()->address;
}
function getAdditionalFields(bool $split = false): array
{
$all = \openvk\Web\Models\Entities\UserInfoEntities\AdditionalField::getByOwner($this->getId());
$result = [
"interests" => [],
"contacts" => [],
];
if($split) {
foreach($all as $field) {
if($field->getPlace() == "contact")
$result["contacts"][] = $field;
else if($field->getPlace() == "interest")
$result["interests"][] = $field;
}
} else {
$result = [];
foreach($all as $field)
$result[] = $field;
}
return $result;
}
function getNotificationOffset(): int
{
return $this->getRecord()->notification_offset;

View file

@ -0,0 +1,89 @@
<?php declare(strict_types=1);
namespace openvk\Web\Models\Entities\UserInfoEntities;
use openvk\Web\Models\RowModel;
use openvk\Web\Models\Repositories\Users;
use Chandler\Database\DatabaseConnection;
class AdditionalField extends RowModel
{
protected $tableName = "additional_fields";
const PLACE_CONTACTS = 0;
const PLACE_INTERESTS = 1;
function getOwner(): int
{
return (int) $this->getRecord()->owner;
}
function getName(): string
{
$orig_name = $this->getRecord()->name;
$name = $orig_name;
if($name[0] === "_")
$name = tr("custom_fav_" . substr($name, 1));
if(str_contains($name, "custom_fav"))
return $orig_name;
return $name;
}
function getContent(): string
{
return $this->getRecord()->text;
}
function getPlace(): string
{
switch($this->getRecord()->place) {
case AdditionalField::PLACE_CONTACTS:
return "contact";
case AdditionalField::PLACE_INTERESTS:
return "interest";
}
return "contact";
}
function toVkApiStruct(): object
{
return (object) [
"type" => $this->getRecord()->place,
"name" => $this->getName(),
"text" => $this->getContent()
];
}
static function getById(int $id)
{
$ctx = DatabaseConnection::i()->getContext();
$entry = $ctx->table("additional_fields")->where([
"id" => $id,
])->fetch();
if(!$entry)
return NULL;
return new AdditionalField($entry);
}
static function getByOwner(int $owner): \Traversable
{
$ctx = DatabaseConnection::i()->getContext();
$entries = $ctx->table("additional_fields")->where([
"owner" => $owner,
]);
foreach($entries as $entry) {
yield new AdditionalField($entry);
}
}
static function getCountByOwner(int $owner): \Traversable
{
return DatabaseConnection::i()->getContext()->table("additional_fields")->where([
"owner" => $owner,
])->count();
}
}

View file

@ -64,6 +64,7 @@ final class UserPresenter extends OpenVKPresenter
$this->template->audios = (new Audios)->getRandomThreeAudiosByEntityId($user->getId());
$this->template->audiosCount = (new Audios)->getUserCollectionSize($user);
$this->template->audioStatus = $user->getCurrentAudioStatus();
$this->template->additionalFields = $user->getAdditionalFields(true);
$this->template->user = $user;

View file

@ -506,6 +506,12 @@
<td class="label"><span class="nobold">{_address}:</span></td>
<td class="data">{$user->getPhysicalAddress()}</td>
</tr>
{if $additionalFields}
<tr n:foreach="$additionalFields['contacts'] as $field">
<td class="label"><span class="nobold">{$field->getName()}:</span></td>
<td class="data">{$field->getContent()}</td>
</tr>
{/if}
</tbody>
</table>
{/capture}
@ -571,6 +577,12 @@
<td class="label"><span class="nobold">{_favorite_games}: </span></td>
<td class="data">{$user->getFavoriteGames()}</td>
</tr>
{if $additionalFields}
<tr n:foreach="$additionalFields['interests'] as $field">
<td class="label"><span class="nobold">{$field->getName()}:</span></td>
<td class="data">{$field->getContent()}</td>
</tr>
{/if}
<tr n:if="!is_null($user->getDescription())">
<td class="label"><span class="nobold">{_information_about}: </span></td>
<td class="data">{$user->getDescription()}</td>

View file

@ -1 +1,10 @@
ALTER TABLE `profiles` ADD `fav_games` MEDIUMTEXT NULL DEFAULT NULL AFTER `fav_quote`;
CREATE TABLE `additional_fields` (
`id` BIGINT(20) NOT NULL AUTO_INCREMENT,
`owner` BIGINT(20) UNSIGNED NOT NULL,
`name` VARCHAR(255) COLLATE utf8mb4_unicode_520_ci NOT NULL,
`text` MEDIUMTEXT COLLATE utf8mb4_unicode_520_ci NOT NULL,
`place` TINYINT(3) UNSIGNED NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE = InnoDB;
ALTER TABLE `additional_fields` ADD INDEX(`owner`);

View file

@ -153,6 +153,13 @@
"favorite_books" = "Favorite books";
"favorite_quotes" = "Favorite quotes";
"favorite_games" = "Favorite games";
"custom_fav_favorite_performers" = "Favourite performers";
"custom_fav_favorite_content_makers" = "Favourite content-makers";
"custom_fav_favorite_anime" = "Favourite anime";
"custom_fav_favorite_manga" = "Favourite manga";
"custom_fav_favorite_vtubers" = "Favourite vtubers";
"custom_fav_favorite_albums" = "Favourite music albums";
"information_about" = "About";
"updated_at" = "Updated at $1";

View file

@ -137,6 +137,13 @@
"favorite_books" = "Любимые книги";
"favorite_quotes" = "Любимые цитаты";
"favorite_games" = "Любимые игры";
"custom_fav_favorite_performers" = "Любимые исполнители";
"custom_fav_favorite_content_makers" = "Любимые контент-мейкеры";
"custom_fav_favorite_anime" = "Любимые аниме";
"custom_fav_favorite_manga" = "Любимая манга";
"custom_fav_favorite_vtubers" = "Любимые витуберы";
"custom_fav_favorite_albums" = "Любимые альбомы";
"information_about" = "О себе";
"updated_at" = "Обновлено $1";
"user_banned" = "К сожалению, нам пришлось заблокировать страницу пользователя <b>$1</b>.";

View file

@ -45,4 +45,5 @@ comments.allow-graffiti: 0
# + Set this option to any non-negative number to be this limit
wall.repost-liking-recursion-limit: 10
polls.max-opts: 10
polls.max-opts: 10
users.max-fields: 7