nativegallery-weryskok/app/Controllers/Api/Profile/Update.php

57 lines
1.5 KiB
PHP
Raw Normal View History

2024-07-05 15:03:35 +03:00
<?php
namespace App\Controllers\Api\Profile;
use App\Services\{Auth, Router, GenerateRandomStr, DB, Json, EXIF};
use App\Models\{User, Vote};
use App\Services\Upload as UploadPhoto;
class Update
{
private $photourl;
public function __construct()
{
$postData = $_POST;
$dataArray = [];
foreach ($postData as $key => $value) {
if (strpos($key, 'about') === 0) {
$paramKey = $key;
if (!isset($dataArray[$paramKey])) {
$dataArray[$paramKey] = [];
}
if (strpos($key, 'OnMain') === false) {
$dataArray[$paramKey]['value'] = $value;
}
}
}
$user = new \App\Models\User(Auth::userid());
$content = json_decode($user->i('content'), true);
$existingArray = array_replace_recursive($content, $dataArray);
$newJson = json_encode($existingArray, JSON_PRETTY_PRINT);
if (isset($_FILES['userphoto'])) {
$upload = new UploadPhoto($_FILES['userphoto'], 'cdn/img/');
if ($upload->getType() !== null) {
$this->photourl = $upload->getSrc();
} else {
$this->photourl = $user->i('photourl');
}
} else {
$this->photourl = $user->i('photourl');
}
DB::query('UPDATE users SET content=:c, photourl=:ph WHERE id=:id', [':id' => Auth::userid(), ':c' => $newJson, ':ph'=>$this->photourl]);
}
}