1 | 1 |
new file mode 100644 |
... | ... |
@@ -0,0 +1,94 @@ |
1 |
+<?php |
|
2 |
+ |
|
3 |
+declare(strict_types=1); |
|
4 |
+ |
|
5 |
+/* |
|
6 |
+ * This file is part of Oveleon ContaoMemberExtension Bundle. |
|
7 |
+ * |
|
8 |
+ * @package contao-member-extension-bundle |
|
9 |
+ * @license MIT |
|
10 |
+ * @author Sebastian Zoglowek <https://github.com/zoglo> |
|
11 |
+ * @author Daniele Sciannimanica <https://github.com/doishub> |
|
12 |
+ * @author Fabian Ekert <https://github.com/eki89> |
|
13 |
+ * @copyright Oveleon <https://www.oveleon.de/> |
|
14 |
+ */ |
|
15 |
+ |
|
16 |
+namespace Oveleon\ContaoMemberExtensionBundle\Controller\FrontendModule; |
|
17 |
+ |
|
18 |
+use Contao\CoreBundle\Controller\FrontendModule\AbstractFrontendModuleController; |
|
19 |
+use Contao\CoreBundle\DependencyInjection\Attribute\AsFrontendModule; |
|
20 |
+use Contao\CoreBundle\Exception\RedirectResponseException; |
|
21 |
+use Contao\FrontendUser; |
|
22 |
+use Contao\Input; |
|
23 |
+use Contao\MemberModel; |
|
24 |
+use Contao\ModuleModel; |
|
25 |
+use Contao\StringUtil; |
|
26 |
+use Contao\System; |
|
27 |
+use Contao\Template; |
|
28 |
+use Exception; |
|
29 |
+use Oveleon\ContaoMemberExtensionBundle\Member; |
|
30 |
+use Symfony\Component\HttpFoundation\Request; |
|
31 |
+use Symfony\Component\HttpFoundation\Response; |
|
32 |
+ |
|
33 |
+#[AsFrontendModule(DeleteAvatarController::TYPE, category:'user', template:'memberExtension_deleteAvatar')] |
|
34 |
+class DeleteAvatarController extends AbstractFrontendModuleController |
|
35 |
+{ |
|
36 |
+ const TYPE = 'deleteAvatar'; |
|
37 |
+ |
|
38 |
+ /** |
|
39 |
+ * @throws Exception |
|
40 |
+ */ |
|
41 |
+ protected function getResponse(Template $template, ModuleModel $model, Request $request): Response |
|
42 |
+ { |
|
43 |
+ $container = System::getContainer(); |
|
44 |
+ |
|
45 |
+ // Return if there is no logged-in user |
|
46 |
+ if ( |
|
47 |
+ !$container->get('contao.security.token_checker')->hasFrontendUser() || |
|
48 |
+ null === ($member = MemberModel::findByPk(FrontendUser::getInstance()->id)) |
|
49 |
+ ) { |
|
50 |
+ return new Response(); |
|
51 |
+ } |
|
52 |
+ |
|
53 |
+ // Confirmation message |
|
54 |
+ $session = $container->get('request_stack')->getSession(); |
|
55 |
+ $flashBag = $session->getFlashBag(); |
|
56 |
+ |
|
57 |
+ if (!($session->isStarted() && $flashBag->has('mod_avatar_deleted')) && !$member->avatar) |
|
58 |
+ { |
|
59 |
+ return new Response(); |
|
60 |
+ } |
|
61 |
+ |
|
62 |
+ $strFormId = 'deleteAvatar_' . $model->id; |
|
63 |
+ |
|
64 |
+ // Get form submit |
|
65 |
+ if (Input::post('FORM_SUBMIT') == $strFormId) |
|
66 |
+ { |
|
67 |
+ // Delete avatar if it exists |
|
68 |
+ if (!!$member->avatar) |
|
69 |
+ { |
|
70 |
+ Member::deleteAvatar($member); |
|
71 |
+ // Unset avatar |
|
72 |
+ $member->avatar = null; |
|
73 |
+ $member->save(); |
|
74 |
+ |
|
75 |
+ // Set message for deletion feedback |
|
76 |
+ $flashBag->set('mod_avatar_deleted', $GLOBALS['TL_LANG']['MSC']['avatarDeleted'] ?? ''); |
|
77 |
+ |
|
78 |
+ throw new RedirectResponseException($request->getRequestUri()); |
|
79 |
+ } |
|
80 |
+ } |
|
81 |
+ |
|
82 |
+ // Confirmation message |
|
83 |
+ if ($session->isStarted() && $flashBag->has('mod_avatar_deleted')) { |
|
84 |
+ $arrMessages = $flashBag->get('mod_avatar_deleted'); |
|
85 |
+ $template->message = $arrMessages[0]; |
|
86 |
+ } |
|
87 |
+ |
|
88 |
+ $template->formId = $strFormId; |
|
89 |
+ $template->slabel = StringUtil::specialchars($GLOBALS['TL_LANG']['MSC']['deleteAvatar'] ?? ''); |
|
90 |
+ $template->requestToken = System::getContainer()->get('contao.csrf.token_manager')->getDefaultTokenValue(); |
|
91 |
+ |
|
92 |
+ return $template->getResponse(); |
|
93 |
+ } |
|
94 |
+} |