diff --git a/Web/Models/Entities/Photo.php b/Web/Models/Entities/Photo.php index 10b941d0..3e546724 100644 --- a/Web/Models/Entities/Photo.php +++ b/Web/Models/Entities/Photo.php @@ -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; + } } diff --git a/Web/Presenters/CommentPresenter.php b/Web/Presenters/CommentPresenter.php index 96425e16..979a258e 100644 --- a/Web/Presenters/CommentPresenter.php +++ b/Web/Presenters/CommentPresenter.php @@ -1,6 +1,6 @@ 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", "Не удалось опубликовать комментарий", "Нельзя опубликовать пустой комментарий."); } diff --git a/Web/Presenters/WallPresenter.php b/Web/Presenters/WallPresenter.php index 01c2ca0f..49470646 100644 --- a/Web/Presenters/WallPresenter.php +++ b/Web/Presenters/WallPresenter.php @@ -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", "Не удалось опубликовать пост", "Файл изображения повреждён, слишком велик или одна сторона изображения в разы больше другой."); } diff --git a/Web/Presenters/templates/components/comment.xml b/Web/Presenters/templates/components/comment.xml index b32ebb59..3a427d67 100644 --- a/Web/Presenters/templates/components/comment.xml +++ b/Web/Presenters/templates/components/comment.xml @@ -19,6 +19,12 @@