From 77bd3932157618c29a9e345247af65300d8b98f5 Mon Sep 17 00:00:00 2001 From: lalka2018 <99399973+lalka2016@users.noreply.github.com> Date: Sun, 4 Feb 2024 18:59:34 +0300 Subject: [PATCH] feat(profiles): add ability to crop avatar Closes #1068 and maybe slash 106 --- Web/Presenters/GroupPresenter.php | 95 +++++++++--- Web/Presenters/UserPresenter.php | 63 ++++++-- Web/Presenters/templates/@layout.xml | 2 + Web/Presenters/templates/Group/View.xml | 22 +-- Web/Presenters/templates/User/View.xml | 21 +-- Web/Presenters/templates/_includeCSS.xml | 4 +- Web/routes.yml | 4 + Web/static/css/avatar-edit.css | 182 ++++++++++++++++++++++ Web/static/css/avataredit.css | 88 ----------- Web/static/img/delete.png | Bin 263 -> 0 bytes Web/static/img/upload.png | Bin 165 -> 923 bytes Web/static/js/al_wall.js | 187 +++++++++++++++++++++++ Web/static/js/openvk.cls.js | 85 ----------- Web/static/js/package.json | 1 + Web/static/js/yarn.lock | 24 +-- locales/en.strings | 8 +- locales/ru.strings | 8 +- themepacks/midnight/stylesheet.css | 4 + 18 files changed, 542 insertions(+), 256 deletions(-) create mode 100644 Web/static/css/avatar-edit.css delete mode 100644 Web/static/css/avataredit.css delete mode 100644 Web/static/img/delete.png diff --git a/Web/Presenters/GroupPresenter.php b/Web/Presenters/GroupPresenter.php index beeede13..a0d1ce47 100644 --- a/Web/Presenters/GroupPresenter.php +++ b/Web/Presenters/GroupPresenter.php @@ -279,48 +279,99 @@ final class GroupPresenter extends OpenVKPresenter function renderSetAvatar(int $id) { - $photo = new Photo; + $this->assertUserLoggedIn(); + $this->willExecuteWriteAction(); + $club = $this->clubs->get($id); - if ($club->isBanned()) $this->flashFail("err", tr("error"), tr("forbidden")); - if($_SERVER["REQUEST_METHOD"] === "POST" && $_FILES["ava"]["error"] === UPLOAD_ERR_OK) { + + if(!$club || $club->isBanned() || !$club->canBeModifiedBy($this->user->identity)) + $this->flashFail("err", tr("error"), tr("forbidden"), NULL, true); + + if($_SERVER["REQUEST_METHOD"] === "POST" && $_FILES["blob"]["error"] === UPLOAD_ERR_OK) { try { + $photo = new Photo; + $anon = OPENVK_ROOT_CONF["openvk"]["preferences"]["wall"]["anonymousPosting"]["enable"]; if($anon && $this->user->id === $club->getOwner()->getId()) $anon = $club->isOwnerHidden(); else if($anon) $anon = $club->getManager($this->user->identity)->isHidden(); + $photo->setOwner($this->user->id); $photo->setDescription("Club image"); - $photo->setFile($_FILES["ava"]); + $photo->setFile($_FILES["blob"]); $photo->setCreated(time()); $photo->setAnonymous($anon); $photo->save(); (new Albums)->getClubAvatarAlbum($club)->addPhoto($photo); - $flags = 0; - $flags |= 0b00010000; - $flags |= 0b10000000; + if($this->postParam("on_wall") == 1) { + $post = new Post; + + $post->setOwner($this->user->id); + $post->setWall($club->getId() * -1); + $post->setCreated(time()); + $post->setContent(""); - $post = new Post; - $post->setOwner($this->user->id); - $post->setWall($club->getId()*-1); - $post->setCreated(time()); - $post->setContent(""); - $post->setFlags($flags); - $post->save(); - $post->attach($photo); + $flags = 0; + $flags |= 0b00010000; + $flags |= 0b10000000; - } catch(ISE $ex) { - $name = $album->getName(); - $this->flashFail("err", tr("error"), tr("error_when_uploading_photo")); + $post->setFlags($flags); + $post->save(); + + $post->attach($photo); + } + + } catch(\Throwable $ex) { + $this->flashFail("err", tr("error"), tr("error_when_uploading_photo"), NULL, true); } + + $this->returnJson([ + "success" => true, + "new_photo" => $photo->getPrettyId(), + "url" => $photo->getURL(), + ]); + } else { + return " "; } - $this->returnJson([ - "url" => $photo->getURL(), - "id" => $photo->getPrettyId() - ]); } + + function renderDeleteAvatar(int $id) { + $this->assertUserLoggedIn(); + $this->willExecuteWriteAction(); + + $club = $this->clubs->get($id); + + if(!$club || $club->isBanned() || !$club->canBeModifiedBy($this->user->identity)) + $this->flashFail("err", tr("error"), tr("forbidden"), NULL, true); + + $avatar = $club->getAvatarPhoto(); + + if(!$avatar) + $this->flashFail("succ", tr("error"), "no avatar bro", NULL, true); + + $avatar->isolate(); + + $newAvatar = $club->getAvatarPhoto(); + + if(!$newAvatar) + $this->returnJson([ + "success" => true, + "has_new_photo" => false, + "new_photo" => NULL, + "url" => "/assets/packages/static/openvk/img/camera_200.png", + ]); + else + $this->returnJson([ + "success" => true, + "has_new_photo" => true, + "new_photo" => $newAvatar->getPrettyId(), + "url" => $newAvatar->getURL(), + ]); + } + function renderEditBackdrop(int $id): void { $this->assertUserLoggedIn(); diff --git a/Web/Presenters/UserPresenter.php b/Web/Presenters/UserPresenter.php index 3785e47b..2b8c3087 100644 --- a/Web/Presenters/UserPresenter.php +++ b/Web/Presenters/UserPresenter.php @@ -334,8 +334,7 @@ final class UserPresenter extends OpenVKPresenter $this->redirect($user->getURL()); } - function renderSetAvatar() - { + function renderSetAvatar() { $this->assertUserLoggedIn(); $this->willExecuteWriteAction(); @@ -346,8 +345,8 @@ final class UserPresenter extends OpenVKPresenter $photo->setFile($_FILES["blob"]); $photo->setCreated(time()); $photo->save(); - } catch(ISE $ex) { - $this->flashFail("err", tr("error"), tr("error_upload_failed")); + } catch(\Throwable $ex) { + $this->flashFail("err", tr("error"), tr("error_upload_failed"), NULL, (int)$this->postParam("ajax", true) == 1); } $album = (new Albums)->getUserAvatarAlbum($this->user->identity); @@ -358,23 +357,57 @@ final class UserPresenter extends OpenVKPresenter $flags = 0; $flags |= 0b00010000; - $post = new Post; - $post->setOwner($this->user->id); - $post->setWall($this->user->id); - $post->setCreated(time()); - $post->setContent(""); - $post->setFlags($flags); - $post->save(); - $post->attach($photo); - if($this->postParam("ava", true) == (int)1) { + if($this->postParam("on_wall") == 1) { + $post = new Post; + $post->setOwner($this->user->id); + $post->setWall($this->user->id); + $post->setCreated(time()); + $post->setContent(""); + $post->setFlags($flags); + $post->save(); + + $post->attach($photo); + } + + if((int)$this->postParam("ajax", true) == 1) { $this->returnJson([ - "url" => $photo->getURL(), - "id" => $photo->getPrettyId() + "success" => true, + "new_photo" => $photo->getPrettyId(), + "url" => $photo->getURL(), ]); } else { $this->flashFail("succ", tr("photo_saved"), tr("photo_saved_comment")); } } + + function renderDeleteAvatar() { + $this->assertUserLoggedIn(); + $this->willExecuteWriteAction(); + + $avatar = $this->user->identity->getAvatarPhoto(); + + if(!$avatar) + $this->flashFail("succ", tr("error"), "no avatar bro", NULL, true); + + $avatar->isolate(); + + $newAvatar = $this->user->identity->getAvatarPhoto(); + + if(!$newAvatar) + $this->returnJson([ + "success" => true, + "has_new_photo" => false, + "new_photo" => NULL, + "url" => "/assets/packages/static/openvk/img/camera_200.png", + ]); + else + $this->returnJson([ + "success" => true, + "has_new_photo" => true, + "new_photo" => $newAvatar->getPrettyId(), + "url" => $newAvatar->getURL(), + ]); + } function renderSettings(): void { diff --git a/Web/Presenters/templates/@layout.xml b/Web/Presenters/templates/@layout.xml index d636ad42..a90fdc36 100644 --- a/Web/Presenters/templates/@layout.xml +++ b/Web/Presenters/templates/@layout.xml @@ -18,9 +18,11 @@ {script "js/l10n.js"} {script "js/openvk.cls.js"} {script "js/node_modules/dashjs/dist/dash.all.min.js"} + {script "js/al_music.js"} {css "js/node_modules/tippy.js/dist/backdrop.css"} + {css "js/node_modules/cropperjs/dist/cropper.css"} {css "js/node_modules/tippy.js/dist/border.css"} {css "js/node_modules/tippy.js/dist/svg-arrow.css"} {css "js/node_modules/tippy.js/themes/light.css"} diff --git a/Web/Presenters/templates/Group/View.xml b/Web/Presenters/templates/Group/View.xml index 91a96119..79ef3704 100644 --- a/Web/Presenters/templates/Group/View.xml +++ b/Web/Presenters/templates/Group/View.xml @@ -127,25 +127,19 @@
{var $avatarPhoto = $club->getAvatarPhoto()} {var $avatarLink = ((is_null($avatarPhoto) ? FALSE : $avatarPhoto->isAnonymous()) ? "/photo" . ("s/" . base_convert((string) $avatarPhoto->getId(), 10, 32)) : $club->getAvatarLink())} -
- {var $hasAvatar = !str_contains($club->getAvatarUrl('miniscule'), "/assets/packages/static/openvk/img/camera_200.png")} - {if !is_null($thisUser) && $hasAvatar == false && $club->canBeModifiedBy($thisUser)} - {_add_image_group} - {elseif !is_null($thisUser) && $hasAvatar == true && $club->canBeModifiedBy($thisUser)} -
-
- -
+
+ {if $thisUser && $club->canBeModifiedBy($thisUser)} + {_add_image} + {/if} + - +