[WIP] Comments: Allow photo uploads in comments

This commit is contained in:
Celestora 2021-10-12 14:17:21 +03:00
parent 2e15de74fc
commit 4ab9d67718
4 changed files with 35 additions and 10 deletions

View file

@ -41,4 +41,19 @@ class Photo extends Media
DB::i()->getContext()->table("album_relations")->where("media", $this->getRecord()->id)->delete();
}
static function fastMake(int $owner, string $description = "", array $file, ?Album $album = NULL): Photo
{
$photo = new static;
$photo->setOwner($owner);
$photo->setDescription(iconv_substr($description, 0, 36) . "...");
$photo->setCreated(time());
$photo->setFile($file);
$photo->save();
if(!is_null($album))
$album->addPhoto($photo);
return $photo;
}
}

View file

@ -1,6 +1,6 @@
<?php declare(strict_types=1);
namespace openvk\Web\Presenters;
use openvk\Web\Models\Entities\{Comment, User};
use openvk\Web\Models\Entities\{Comment, Photo, User};
use openvk\Web\Models\Entities\Notifications\CommentNotification;
use openvk\Web\Models\Repositories\Comments;
@ -46,6 +46,15 @@ final class CommentPresenter extends OpenVKPresenter
$comment->setContent($this->postParam("text"));
$comment->setCreated(time());
$comment->save();
if($_FILES["_pic_attachment"]["error"] === UPLOAD_ERR_OK) {
try {
$photo = Photo::fastMake($this->user->id, $this->postParam("text"), $_FILES["_pic_attachment"]);
$comment->attach($photo);
} catch(ISE $ex) {
$this->flashFail("err", "Не удалось опубликовать пост", "Файл изображения повреждён, слишком велик или одна сторона изображения в разы больше другой.");
}
}
} catch(\LogicException $ex) {
$this->flashFail("err", "Не удалось опубликовать комментарий", "Нельзя опубликовать пустой комментарий.");
}

View file

@ -189,16 +189,11 @@ final class WallPresenter extends OpenVKPresenter
if($_FILES["_pic_attachment"]["error"] === UPLOAD_ERR_OK) {
try {
$photo = new Photo;
$photo->setOwner($this->user->id);
$photo->setDescription(iconv_substr($this->postParam("text"), 0, 36) . "...");
$photo->setCreated(time());
$photo->setFile($_FILES["_pic_attachment"]);
$photo->save();
$album = NULL;
if($wall > 0 && $wall === $this->user->id)
$album = (new Albums)->getUserWallAlbum($wallOwner);
if($wall > 0 && $wall === $this->user->id) {
(new Albums)->getUserWallAlbum($wallOwner)->addPhoto($photo);
}
$photo = Photo::fastMake($this->user->id, $this->postParam("text"), $_FILES["_pic_attachment"], $album);
} catch(ISE $ex) {
$this->flashFail("err", "Не удалось опубликовать пост", "Файл изображения повреждён, слишком велик или одна сторона изображения в разы больше другой.");
}

View file

@ -19,6 +19,12 @@
<div class="post-content" id="{$comment->getId()}">
<div class="text" id="text{$comment->getId()}">
{$comment->getText()|noescape}
<div n:ifcontent class="attachments_b">
<div class="attachment" n:foreach="$comment->getChildren() as $attachment" data-localized-nsfw-text="{_nsfw_warning}">
{include "attachment.xml", attachment => $attachment}
</div>
</div>
</div>
<div n:if="isset($thisUser) &&! ($compact ?? false)" class="post-menu">
{var canDelete = $comment->getOwner()->getId() == $thisUser->getId()}