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 @@