From 2795e1e1a47eb2e6c460e4f2da533a6e3f490075 Mon Sep 17 00:00:00 2001 From: mrilyew <99399973+mrilyew@users.noreply.github.com> Date: Mon, 14 Oct 2024 13:53:56 +0300 Subject: [PATCH] feat(profiles): add ability to crop avatar (#1089) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * feat(profiles): add ability to crop avatar Closes #1068 and maybe slash 106 * add ability to take avatar photo from camera почему бы и нет --------- Co-authored-by: lalka2018 <99399973+lalka2016@users.noreply.github.com> Co-authored-by: celestora --- 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 | 262 +++++++++++++++++++++++ Web/static/js/openvk.cls.js | 85 -------- Web/static/js/package.json | 1 + Web/static/js/yarn.lock | 24 ++- locales/en.strings | 12 +- locales/ru.strings | 12 +- locales/uk.strings | 2 +- themepacks/midnight/stylesheet.css | 4 + 19 files changed, 624 insertions(+), 259 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 5e8919b4..88844fbe 100644 --- a/Web/Presenters/GroupPresenter.php +++ b/Web/Presenters/GroupPresenter.php @@ -282,48 +282,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 1c2fa02e..baf8f046 100644 --- a/Web/Presenters/UserPresenter.php +++ b/Web/Presenters/UserPresenter.php @@ -337,8 +337,7 @@ final class UserPresenter extends OpenVKPresenter $this->redirect($_SERVER['HTTP_REFERER']); } - function renderSetAvatar() - { + function renderSetAvatar() { $this->assertUserLoggedIn(); $this->willExecuteWriteAction(); @@ -349,8 +348,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); @@ -361,23 +360,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 aa2db799..803ebdd0 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} + - +