Skip to content

Commit

Permalink
feat(profile): fav games & custom fields (#1193)
Browse files Browse the repository at this point in the history
* add fav games

* add additional fields

* add gui 4 dis

* add maxlength
  • Loading branch information
mrilyew authored Dec 20, 2024
1 parent 4c9c650 commit 0aecc29
Show file tree
Hide file tree
Showing 14 changed files with 456 additions and 13 deletions.
47 changes: 46 additions & 1 deletion VKAPI/Handlers/Account.php
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ function unban(int $owner_id): int
return 1;

$entity = get_entity_by_id($owner_id);
if(!$entity || $entity->isDeleted())
if(!$entity)
return 0;

if(!$entity->isBlacklistedBy($this->getUser()))
Expand All @@ -296,4 +296,49 @@ function getBanned(int $offset = 0, int $count = 100, string $fields = ""): obje

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),
];
}
}
26 changes: 25 additions & 1 deletion VKAPI/Handlers/Users.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,14 @@ function get(string $user_ids = "0", string $fields = "", int $offset = 0, int $
if($authuser == NULL) $authuser = $this->getUser();

$users = new UsersRepo;
if($user_ids == "0")
if($user_ids == "0") {
if(!$authuser) {
return [];
}

$user_ids = (string) $authuser->getId();
}


$usrs = explode(',', $user_ids);
$response = array();
Expand Down Expand Up @@ -198,6 +204,13 @@ function get(string $user_ids = "0", string $fields = "", int $offset = 0, int $

$response[$i]->quotes = $usr->getFavoriteQuote();
break;
case "games":
if(!$canView) {
break;
}

$response[$i]->games = $usr->getFavoriteGames();
break;
case "email":
if(!$canView) {
break;
Expand Down Expand Up @@ -280,6 +293,17 @@ function get(string $user_ids = "0", string $fields = "", int $offset = 0, int $

$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;
}
}

Expand Down
37 changes: 37 additions & 0 deletions Web/Models/Entities/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,11 @@ function getDescription(): ?string
return $this->getRecord()->about;
}

function getAbout(): ?string
{
return $this->getRecord()->about;
}

function getStatus(): ?string
{
return $this->getRecord()->status;
Expand Down Expand Up @@ -415,6 +420,11 @@ function getFavoriteQuote(): ?string
return $this->getRecord()->fav_quote;
}

function getFavoriteGames(): ?string
{
return $this->getRecord()->fav_games;
}

function getCity(): ?string
{
return $this->getRecord()->city;
Expand All @@ -425,6 +435,30 @@ function getPhysicalAddress(): ?string
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;
Expand Down Expand Up @@ -1432,6 +1466,9 @@ function toVkApiStruct(?User $user = NULL, string $fields = ''): object

$res->blacklisted = (int)$user->isBlacklistedBy($this);
break;
case "games":
$res->games = $this->getFavoriteGames();
break;
}
}

Expand Down
95 changes: 95 additions & 0 deletions Web/Models/Entities/UserInfoEntities/AdditionalField.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
<?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(bool $tr = true): string
{
$orig_name = $this->getRecord()->name;
$name = $orig_name;
if($tr && $name[0] === "_")
$name = tr("custom_field_" . substr($name, 1));

if(str_contains($name, "custom_field"))
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 isContact(): bool
{
return $this->getRecord()->place == AdditionalField::PLACE_CONTACTS;
}

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();
}

static function resetByOwner(int $owner): bool
{
DatabaseConnection::i()->getContext()->table("additional_fields")->where("owner", $owner)->delete();

return true;
}
}
56 changes: 47 additions & 9 deletions Web/Presenters/UserPresenter.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ function renderView(int $id): void
$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;

Expand Down Expand Up @@ -251,13 +252,14 @@ function renderEdit(): void
else
$user->setWebsite((!parse_url($website, PHP_URL_SCHEME) ? "https://" : "") . $website);
} elseif($_GET['act'] === "interests") {
$user->setInterests(empty($this->postParam("interests")) ? NULL : ovk_proc_strtr($this->postParam("interests"), 300));
$user->setFav_Music(empty($this->postParam("fav_music")) ? NULL : ovk_proc_strtr($this->postParam("fav_music"), 300));
$user->setFav_Films(empty($this->postParam("fav_films")) ? NULL : ovk_proc_strtr($this->postParam("fav_films"), 300));
$user->setFav_Shows(empty($this->postParam("fav_shows")) ? NULL : ovk_proc_strtr($this->postParam("fav_shows"), 300));
$user->setFav_Books(empty($this->postParam("fav_books")) ? NULL : ovk_proc_strtr($this->postParam("fav_books"), 300));
$user->setFav_Quote(empty($this->postParam("fav_quote")) ? NULL : ovk_proc_strtr($this->postParam("fav_quote"), 300));
$user->setAbout(empty($this->postParam("about")) ? NULL : ovk_proc_strtr($this->postParam("about"), 300));
$user->setInterests(empty($this->postParam("interests")) ? NULL : ovk_proc_strtr($this->postParam("interests"), 1000));
$user->setFav_Music(empty($this->postParam("fav_music")) ? NULL : ovk_proc_strtr($this->postParam("fav_music"), 1000));
$user->setFav_Films(empty($this->postParam("fav_films")) ? NULL : ovk_proc_strtr($this->postParam("fav_films"), 1000));
$user->setFav_Shows(empty($this->postParam("fav_shows")) ? NULL : ovk_proc_strtr($this->postParam("fav_shows"), 1000));
$user->setFav_Books(empty($this->postParam("fav_books")) ? NULL : ovk_proc_strtr($this->postParam("fav_books"), 1000));
$user->setFav_Quote(empty($this->postParam("fav_quote")) ? NULL : ovk_proc_strtr($this->postParam("fav_quote"), 1000));
$user->setFav_Games(empty($this->postParam("fav_games")) ? NULL : ovk_proc_strtr($this->postParam("fav_games"), 1000));
$user->setAbout(empty($this->postParam("about")) ? NULL : ovk_proc_strtr($this->postParam("about"), 1000));
} elseif($_GET["act"] === "backdrop") {
if($this->postParam("subact") === "remove") {
$user->unsetBackDropPictures();
Expand Down Expand Up @@ -295,10 +297,46 @@ function renderEdit(): void
$this->returnJson([
"success" => true
]);
} elseif($_GET['act'] === "additional") {
$maxAddFields = ovkGetQuirk("users.max-fields");
$items = [];

for($i = 0; $i < $maxAddFields; $i++) {
if(!$this->postParam("name_".$i)) {
continue;
}

$items[] = [
"name" => $this->postParam("name_".$i),
"text" => $this->postParam("text_".$i),
"place" => $this->postParam("place_".$i),
];
}

\openvk\Web\Models\Entities\UserInfoEntities\AdditionalField::resetByOwner($this->user->id);
foreach($items as $new_field_info) {
$name = ovk_proc_strtr($new_field_info["name"], 50);
$text = ovk_proc_strtr($new_field_info["text"], 1000);
if(ctype_space($name) || ctype_space($text)) {
continue;
}

$place = (int)($new_field_info["place"]);

$new_field = new \openvk\Web\Models\Entities\UserInfoEntities\AdditionalField;
$new_field->setOwner($this->user->id);
$new_field->setName($name);
$new_field->setText($text);
$new_field->setPlace([0, 1][$place] ? $place : 0);

$new_field->save();
}
}

try {
$user->save();
if($_GET['act'] !== "additional") {
$user->save();
}
} catch(\PDOException $ex) {
if($ex->getCode() == 23000)
$this->flashFail("err", tr("error"), tr("error_shorturl"));
Expand All @@ -310,7 +348,7 @@ function renderEdit(): void
}

$this->template->mode = in_array($this->queryParam("act"), [
"main", "contacts", "interests", "avatar", "backdrop"
"main", "contacts", "interests", "avatar", "backdrop", "additional"
]) ? $this->queryParam("act")
: "main";

Expand Down
1 change: 1 addition & 0 deletions Web/Presenters/templates/@layout.xml
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,7 @@
"max_filesize_mb": 5,
"current_id": {$thisUser ? $thisUser->getId() : 0},
"disable_ajax": {$disable_ajax ? $disable_ajax : 0},
"max_add_fields": {ovkGetQuirk("users.max-fields")},
}
</script>

Expand Down
Loading

0 comments on commit 0aecc29

Please sign in to comment.