Browse Source

Emails automatiques

feature/rotating_product
Guillaume Bourgeois 5 months ago
parent
commit
a41d8580d4
4 changed files with 73 additions and 25 deletions
  1. +11
    -1
      backend/views/user/view.php
  2. +15
    -19
      domain/User/User/NewsletterManager.php
  3. +13
    -0
      domain/User/UserProducer/UserProducerBuilder.php
  4. +34
    -5
      producer/views/newsletter/index.php

+ 11
- 1
backend/views/user/view.php View File

@@ -38,6 +38,7 @@ termes.

use common\helpers\GlobalParam;
use common\helpers\Price;
use domain\Communication\Email\Email;
use domain\Order\Order\OrderModule;
use domain\Subscription\Subscription\SubscriptionModule;
use domain\User\User\UserModule;
@@ -122,12 +123,21 @@ $this->addBreadcrumb('Récapitulatif') ;
<li class="list-group-item">
<strong>Infolettre</strong>
<span class="pull-right">
<?php $isUserSubscribedNewsletter = $userModule->getNewsletterManager()->isUserSubscribedNewsletter($model); ?>
<?php $isUserSubscribedNewsletter = $userModule->getNewsletterManager()->isUserSubscribedNewsletter($model, Email::TYPE_NEWSLETTER); ?>
<span class="label label-<?= $isUserSubscribedNewsletter ? 'success' : 'default' ?>">
<?= $isUserSubscribedNewsletter ? 'Oui' : 'Non' ?>
</span>
</span>
</li>
<li class="list-group-item">
<strong>Emails de prise de commande</strong>
<span class="pull-right">
<?php $isUserSubscribedNewsletterOrderTaking = $userModule->getNewsletterManager()->isUserSubscribedNewsletter($model, Email::TYPE_ORDER_TAKING); ?>
<span class="label label-<?= $isUserSubscribedNewsletterOrderTaking ? 'success' : 'default' ?>">
<?= $isUserSubscribedNewsletterOrderTaking ? 'Oui' : 'Non' ?>
</span>
</span>
</li>
<?php if($userModule->getAuthorizationChecker()->isGrantedAsAdministrator($userCurrent)): ?>
<li class="list-group-item">
<strong>Infolettre Souke</strong>

+ 15
- 19
domain/User/User/NewsletterManager.php View File

@@ -19,11 +19,12 @@ class NewsletterManager extends AbstractService implements ManagerInterface
$this->userProducerBuilder = $this->loadService(UserProducerBuilder::class);
}

public function isUserSubscribedNewsletter(User $user = null): bool
public function isUserSubscribedNewsletter(User $user = null, string $type = Email::TYPE_NEWSLETTER): bool
{
if($user) {
$userProducer = $this->userProducerRepository->findOneUserProducer($user);
if($userProducer && $userProducer->newsletter) {
$fieldNewsletter = ($type == Email::TYPE_NEWSLETTER) ? 'newsletter' : 'newsletter_order_taking';
if($userProducer && $userProducer->$fieldNewsletter) {
return true;
}
}
@@ -41,37 +42,32 @@ class NewsletterManager extends AbstractService implements ManagerInterface
}

if($newsletterOrderTaking) {
$this->subscribeUserNewsletter($user, Email::TYPE_ORDER_TAKING);
}
else {
$this->unsubscribeUserNewsletter($user, Email::TYPE_ORDER_TAKING);
}
}

public function subscribeUserNewsletter(User $user, string $type): bool
{
$userProducer = $this->userProducerBuilder->createUserProducerIfNotExist($user, $this->getProducerContext());

if($type == Email::TYPE_NEWSLETTER) {
return $this->userProducerBuilder->updateUserProducerNewsletter($userProducer, true);
}
elseif($type == Email::TYPE_ORDER_TAKING) {
return $this->userProducerBuilder->updateUserProducerNewsletterOrderTaking($userProducer, true);
}

return false;
return $this->userProducerBuilder->updateUserProducerNewsletterByType(
$userProducer,
true,
$type
);
}

public function unsubscribeUserNewsletter(User $user, string $type): bool
{
$userProducer = $this->userProducerRepository->findOneUserProducer($user);
if($userProducer) {
if($type == Email::TYPE_NEWSLETTER) {
return $this->userProducerBuilder->updateUserProducerNewsletter($userProducer, false);
}
elseif($type == Email::TYPE_ORDER_TAKING) {
return $this->userProducerBuilder->updateUserProducerNewsletterOrderTaking($userProducer, false);
}
return $this->userProducerBuilder->updateUserProducerNewsletterByType(
$userProducer,
false,
$type
);
}

return false;

+ 13
- 0
domain/User/UserProducer/UserProducerBuilder.php View File

@@ -3,6 +3,7 @@
namespace domain\User\UserProducer;

use common\helpers\MeanPayment;
use domain\Communication\Email\Email;
use domain\Order\Order\Order;
use domain\Order\Order\OrderRepository;
use domain\Order\Order\OrderSolver;
@@ -189,6 +190,18 @@ class UserProducerBuilder extends AbstractBuilder
return $this->update($userProducer);
}

public function updateUserProducerNewsletterByType(UserProducer $userProducer, bool $newsletter, string $type)
{
if($type == Email::TYPE_NEWSLETTER) {
return $this->updateUserProducerNewsletter($userProducer, $newsletter);
}
elseif($type == Email::TYPE_ORDER_TAKING) {
return $this->updateUserProducerNewsletterOrderTaking($userProducer, $newsletter);
}

return false;
}

public function updateUserProducerNewsletter(UserProducer $userProducer, bool $newsletter)
{
$userProducer->newsletter = $newsletter;

+ 34
- 5
producer/views/newsletter/index.php View File

@@ -37,13 +37,15 @@ termes.
*/

use domain\User\User\UserModule;
use lo\widgets\Toggle;
use yii\bootstrap\ActiveForm;
use yii\helpers\Html;

$userModule = UserModule::getInstance();
$producer = $this->context->getProducerCurrent();
$this->setTitle("Infolettre");
$this->setTitle("Infolettres");

if($userModule->isUserSubscribedNewsletter($user)) {
/*if($userModule->isUserSubscribedNewsletter($user)) {
$this->addButton(
[
'label' => '<i class="bi bi-x-circle"></i> Me désinscrire',
@@ -60,8 +62,7 @@ else {
'class' => 'btn btn-primary'
]
);
}

}*/

?>
<div class="newsletter-index">
@@ -73,9 +74,10 @@ else {
<?php if($producer->option_newsletter_description): ?>
<?= nl2br(Html::encode($producer->option_newsletter_description)); ?>
<?php else: ?>
L'infolettre vous permet de recevoir les emails de prise de commande et les actualités de ce producteur.
Les infolettres vous permettent de recevoir les actualités et les emails de prise de commande de ce producteur.
<?php endif; ?>
</div>
<!--
<?php if($userModule->isUserSubscribedNewsletter($user)): ?>
<div class="alert alert-success">
<i class="bi bi-megaphone"></i>
@@ -87,6 +89,33 @@ else {
Vous n'êtes pas inscrit à l'infolettre <strong><?= Html::encode($producer->name) ?></strong>.
</div>
<?php endif; ?>
-->

<?php $form = ActiveForm::begin(['id' => 'newsletter-form', 'enableClientValidation' => false,]); ?>
<?= $form->field($user, 'newsletter')->widget(Toggle::class,
[
'options' => [
'data-id' => $user->id,
'data-on' => 'Activé',
'data-off' => 'Désactivé'
],
])
->label('Actualités');
?>
<?= $form->field($user, 'newsletter_order_taking')->widget(Toggle::class,
[
'options' => [
'data-id' => $user->id,
'data-on' => 'Activé',
'data-off' => 'Désactivé'
],
])
->label('Emails de prise de commande');
?>
<div class="form-group form-buttons">
<?= Html::submitButton('Enregistrer', ['class' => 'btn btn-primary', 'name' => 'newsletter-button']) ?>
</div>
<?php ActiveForm::end(); ?>
</div>
<div class="col-md-4">
<div class="alert alert-dark">

Loading…
Cancel
Save