Skip to content

Commit

Permalink
Fix improbable redirects pointing to non-existing presenter and butto…
Browse files Browse the repository at this point in the history
…n redraw

remp/crm#2115
  • Loading branch information
rootpd committed Nov 12, 2021
1 parent 6d9f95e commit 0025b07
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 127 deletions.
66 changes: 36 additions & 30 deletions src/Components/MailSettings/MailSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Crm\RempMailerModule\Repositories\MailUserSubscriptionsRepository;
use Crm\UsersModule\Auth\UserManager;
use Nette\Application\UI\Control;
use Nette\Localization\ITranslator;

/**
* @property FrontendPresenter $presenter
Expand All @@ -26,29 +27,38 @@ class MailSettings extends Control
private $mailTypesRepository;

private $userManager;

private $translator;

public function __construct(
EmailSettingsFormFactory $emailSettingsFormFactory,
MailUserSubscriptionsRepository $mailUserSubscriptionsRepository,
MailTypeCategoriesRepository $mailTypeCategoriesRepository,
MailTypesRepository $mailTypesRepository,
UserManager $userManager
UserManager $userManager,
ITranslator $translator
) {
parent::__construct();
$this->emailSettingsFormFactory = $emailSettingsFormFactory;
$this->mailUserSubscriptionsRepository = $mailUserSubscriptionsRepository;
$this->mailTypesRepository = $mailTypesRepository;
$this->mailTypeCategoriesRepository = $mailTypeCategoriesRepository;
$this->userManager = $userManager;
$this->translator = $translator;
}

public function render()
{
$categories = $this->mailTypeCategoriesRepository->all();
$this->template->categories = $categories;

$types = $this->mailTypesRepository->all(true);
$this->template->types = $types;
$mailTypes = $this->mailTypesRepository->all(true);
$this->template->types = $mailTypes;

$mappedMailTypes = [];
foreach ($mailTypes as $mailType) {
$mappedMailTypes[$mailType->id] = $mailType;
}

$userSubscriptions = [];
if ($this->presenter->getUser()->isLoggedIn()) {
Expand All @@ -57,41 +67,36 @@ public function render()
$this->template->notLogged = true;
}

$showUnsubsribeAll = false;
$showUnsubscribeAll = false;
$showSubscribeAll = false;

foreach ($userSubscriptions as $typeId => $userSubscription) {
foreach ($types as $type) {
if ($type->id == $typeId && $type->locked) {
continue 2;
}
}
if (!$userSubscription['is_subscribed']) {
$showSubscribeAll = true;
foreach ($userSubscriptions as $mailTypeId => $userSubscription) {
if (isset($mappedMailTypes[$mailTypeId]) && $mappedMailTypes[$mailTypeId]->locked) {
continue;
}

if ($userSubscription['is_subscribed']) {
$showUnsubsribeAll = true;
$showUnsubscribeAll = true;
} else {
$showSubscribeAll = true;
}
}

$this->template->showUnsubsribeAll = $showUnsubsribeAll;
$this->template->showUnsubscribeAll = $showUnsubscribeAll;
$this->template->showSubscribeAll = $showSubscribeAll;

$this->template->userSubscriptions = $userSubscriptions;


$this->template->setFile(__DIR__ . '/' . $this->view);
$this->template->render();
}


public function handleSubscribe($id, $variantId = null)
{
if (!$this->presenter->getUser()->isLoggedIn()) {
$this->presenter->redirect(':Mailer:Register:email', $id, $variantId);
}
$this->getPresenter()->onlyLoggedIn();

$this->mailUserSubscriptionsRepository->subscribeUser($this->presenter->getUser()->getIdentity(), $id, $variantId, $this->presenter->rtmParams());
$this->flashMessage($this->presenter->translator->translate('remp_mailer.frontend.mail_settings.subscribe_success'));
$this->flashMessage($this->translator->translate('remp_mailer.frontend.mail_settings.subscribe_success'));
$this->template->changedId = (int)$id;

if ($this->presenter->isAjax()) {
Expand All @@ -106,8 +111,9 @@ public function handleSubscribe($id, $variantId = null)
public function handleUnSubscribe($id)
{
$this->getPresenter()->onlyLoggedIn();

$this->mailUserSubscriptionsRepository->unSubscribeUser($this->presenter->getUser()->getIdentity(), $id, $this->presenter->rtmParams());
$this->flashMessage($this->presenter->translator->translate('remp_mailer.frontend.mail_settings.unsubscribe_success'));
$this->flashMessage($this->translator->translate('remp_mailer.frontend.mail_settings.unsubscribe_success'));
$this->template->changedId = (int)$id;

if ($this->presenter->isAjax()) {
Expand All @@ -121,18 +127,17 @@ public function handleUnSubscribe($id)

public function handleAllSubscribe()
{
if (!$this->presenter->getUser()->isLoggedIn()) {
$this->presenter->redirect(':Mailer:Register:email');
}
$this->getPresenter()->onlyLoggedIn();

$user = $this->userManager->loadUser($this->presenter->user);
$this->mailUserSubscriptionsRepository->subscribeUserAll($user);

$this->flashMessage($this->presenter->translator->translate('remp_mailer.frontend.mail_settings.subscribe_success'));
$this->flashMessage($this->translator->translate('remp_mailer.frontend.mail_settings.subscribe_success'));
if ($this->presenter->isAjax()) {
$this->redrawControl('data-wrapper');
$this->redrawControl('buttons');
} else {
$this->presenter->redirect(':Mailer:MailSettings:mailSettings');
$this->presenter->redirect(':RempMailer:MailSettings:mailSettings');
}
}

Expand All @@ -143,11 +148,12 @@ public function handleAllUnSubscribe()

$this->mailUserSubscriptionsRepository->unsubscribeUserAll($user);

$this->flashMessage($this->presenter->translator->translate('remp_mailer.frontend.mail_settings.unsubscribe_success'));
$this->flashMessage($this->translator->translate('remp_mailer.frontend.mail_settings.unsubscribe_success'));
if ($this->presenter->isAjax()) {
$this->redrawControl('data-wrapper');
$this->redrawControl('buttons');
} else {
$this->presenter->redirect(':Mailer:MailSettings:mailSettings');
$this->presenter->redirect(':RempMailer:MailSettings:mailSettings');
}
}

Expand All @@ -156,8 +162,8 @@ public function createComponentEmailSettingsForm()
$form = $this->emailSettingsFormFactory->create($this->presenter->getUser()->getId());

$this->emailSettingsFormFactory->onUpdate = function () {
$this->flashMessage('Nastavenia boli aktualizované');
$this->presenter->redirect(':Mailer:MailSettings:mailSettings');
$this->flashMessage($this->translator->translate('remp_mailer.frontend.mail_settings.actualized_message'));
$this->presenter->redirect(':RempMailer:MailSettings:mailSettings');
};

return $form;
Expand Down
Loading

0 comments on commit 0025b07

Please sign in to comment.