From 813306f38efd771e80b8572d060837ccd8cb7c80 Mon Sep 17 00:00:00 2001 From: mrilyew <99399973+mrilyew@users.noreply.github.com> Date: Thu, 19 Dec 2024 18:34:12 +0300 Subject: [PATCH] add additional fields --- VKAPI/Handlers/Account.php | 45 ++++++++++ VKAPI/Handlers/Users.php | 11 +++ Web/Models/Entities/User.php | 29 ++++++ .../UserInfoEntities/AdditionalField.php | 89 +++++++++++++++++++ Web/Presenters/UserPresenter.php | 1 + Web/Presenters/templates/User/View.xml | 12 +++ install/sqls/00053-new-fields.sql | 9 ++ locales/en.strings | 7 ++ locales/ru.strings | 7 ++ quirks.yml | 3 +- 10 files changed, 212 insertions(+), 1 deletion(-) create mode 100644 Web/Models/Entities/UserInfoEntities/AdditionalField.php diff --git a/VKAPI/Handlers/Account.php b/VKAPI/Handlers/Account.php index 3f25df35..d4144b90 100644 --- a/VKAPI/Handlers/Account.php +++ b/VKAPI/Handlers/Account.php @@ -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), + ]; + } } diff --git a/VKAPI/Handlers/Users.php b/VKAPI/Handlers/Users.php index bbb78a55..c47acb8e 100644 --- a/VKAPI/Handlers/Users.php +++ b/VKAPI/Handlers/Users.php @@ -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; } } diff --git a/Web/Models/Entities/User.php b/Web/Models/Entities/User.php index 43290424..a1f650ec 100644 --- a/Web/Models/Entities/User.php +++ b/Web/Models/Entities/User.php @@ -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; diff --git a/Web/Models/Entities/UserInfoEntities/AdditionalField.php b/Web/Models/Entities/UserInfoEntities/AdditionalField.php new file mode 100644 index 00000000..b2a936df --- /dev/null +++ b/Web/Models/Entities/UserInfoEntities/AdditionalField.php @@ -0,0 +1,89 @@ +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(); + } +} diff --git a/Web/Presenters/UserPresenter.php b/Web/Presenters/UserPresenter.php index 556d5e8c..3d070b1c 100644 --- a/Web/Presenters/UserPresenter.php +++ b/Web/Presenters/UserPresenter.php @@ -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; diff --git a/Web/Presenters/templates/User/View.xml b/Web/Presenters/templates/User/View.xml index 98c12cb1..9c013dd1 100644 --- a/Web/Presenters/templates/User/View.xml +++ b/Web/Presenters/templates/User/View.xml @@ -506,6 +506,12 @@ {_address}: {$user->getPhysicalAddress()} + {if $additionalFields} + + {$field->getName()}: + {$field->getContent()} + + {/if} {/capture} @@ -571,6 +577,12 @@ {_favorite_games}: {$user->getFavoriteGames()} + {if $additionalFields} + + {$field->getName()}: + {$field->getContent()} + + {/if} {_information_about}: {$user->getDescription()} diff --git a/install/sqls/00053-new-fields.sql b/install/sqls/00053-new-fields.sql index 318cba71..0b437e57 100644 --- a/install/sqls/00053-new-fields.sql +++ b/install/sqls/00053-new-fields.sql @@ -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`); diff --git a/locales/en.strings b/locales/en.strings index e7a3d1d6..d82e0794 100644 --- a/locales/en.strings +++ b/locales/en.strings @@ -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"; diff --git a/locales/ru.strings b/locales/ru.strings index 4dc068ff..531b6de8 100644 --- a/locales/ru.strings +++ b/locales/ru.strings @@ -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" = "К сожалению, нам пришлось заблокировать страницу пользователя $1."; diff --git a/quirks.yml b/quirks.yml index 8a1b7878..8fe09b2f 100644 --- a/quirks.yml +++ b/quirks.yml @@ -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 \ No newline at end of file +polls.max-opts: 10 +users.max-fields: 7