|
- <?php
-
-
-
- namespace producer\controllers;
-
- use domain\Communication\Email\Email;
- use yii\filters\AccessControl;
-
- class NewsletterController extends ProducerBaseController
- {
- public function behaviors()
- {
- return [];
- }
-
- public function actionIndex()
- {
- $userModule = $this->getUserModule();
- $user = $this->getUserCurrent();
-
- if(!$user) {
- return $this->redirectProducerLoginFrontend('index');
- }
-
- $user->newsletter = $userModule->getNewsletterManager()->isUserSubscribedNewsletter($user, Email::TYPE_NEWSLETTER);
- $user->newsletter_order_taking = $userModule->getNewsletterManager()->isUserSubscribedNewsletter($user, Email::TYPE_ORDER_TAKING);
-
- if($user->load(\Yii::$app->request->post()) && $user->validate()) {
- $userModule->getNewsletterManager()->manageUserNewsletter(
- $user,
- $user->newsletter,
- $user->newsletter_order_taking
- );
- $this->setFlash('success', 'Vos changements ont bien été pris en compte.');
- }
-
- return $this->render('index', [
- 'user' => $user,
- 'producer' => $this->getProducerCurrent()
- ]);
- }
-
- public function actionSubscribe()
- {
- if(!$this->getUserCurrent()) {
- return $this->redirectProducerLoginFrontend('subscribe');
- }
-
- $userModule = $this->getUserModule();
- $userCurrent = $this->getUserCurrent();
- $userModule->subscribeUserNewsletter($userCurrent);
-
- return $this->redirect('index');
- }
-
- public function actionUnsubscribe()
- {
- if(!$this->getUserCurrent()) {
- return $this->redirectProducerLoginFrontend('unsubscribe');
- }
-
- $newsletterManager = $this->getUserModule()->getNewsletterManager();
- $userCurrent = $this->getUserCurrent();
- $newsletterManager->unsubscribeUserNewsletter($userCurrent, Email::TYPE_NEWSLETTER);
- $newsletterManager->unsubscribeUserNewsletter($userCurrent, Email::TYPE_ORDER_TAKING);
- $this->setFlash('success', "<i class=\"bi bi-check2-square\"></i> Votre désinscription a bien été prise en compte, vous ne recevrez plus d'emails de la part de ce producteur.");
-
- return $this->redirect('index');
- }
-
- public function actionReportProblemReceivingEmails()
- {
- if(!$this->getUserCurrent()) {
- return $this->redirectProducerLoginFrontend('index');
- }
-
- if(!$this->getUserModule()->getManager()->reportProblemReceivingEmails($this->getUserCurrent())) {
- $this->setFlash('error', "Une erreur est survenue.");
- }
-
- return $this->redirect('index');
- }
-
- public function redirectProducerLoginFrontend(string $actionNewsletter)
- {
- return $this->redirect($this->getUrlManagerFrontend()->createAbsoluteUrl(['site/producer', 'id' => $this->getProducerCurrent()->id, 'return_url' => \Yii::$app->urlManagerProducer->createAbsoluteUrl(['newsletter/'.$actionNewsletter, 'slug_producer' => $this->getProducerCurrent()->slug])]));
- }
- }
-
- ?>
|