Browse Source

Emails automatiques

feature/rotating_product
Guillaume Bourgeois 5 months ago
parent
commit
cc25240ecb
18 changed files with 181 additions and 76 deletions
  1. +7
    -3
      backend/controllers/CommunicateController.php
  2. +49
    -32
      backend/views/communicate/email.php
  3. +1
    -1
      backend/views/user/index.php
  4. +10
    -0
      backend/web/css/screen.css
  5. +13
    -0
      backend/web/sass/communication/_email.scss
  6. +1
    -0
      backend/web/sass/screen.scss
  7. +2
    -1
      console/commands/AutomaticEmailController.php
  8. +3
    -0
      domain/Communication/Email/ContactListResolver.php
  9. +6
    -0
      domain/User/User/NewsletterManager.php
  10. +3
    -1
      domain/User/User/UserRepository.php
  11. +6
    -3
      domain/User/User/UserSearch.php
  12. +18
    -2
      producer/controllers/NewsletterController.php
  13. +1
    -1
      producer/views/layouts/main.php
  14. +1
    -32
      producer/views/newsletter/index.php
  15. +24
    -0
      producer/web/css/screen.css
  16. +7
    -0
      producer/web/js/producer.js
  17. +28
    -0
      producer/web/sass/newsletter/_index.scss
  18. +1
    -0
      producer/web/sass/screen.scss

+ 7
- 3
backend/controllers/CommunicateController.php View File

@@ -41,6 +41,7 @@ namespace backend\controllers;
use backend\models\MailForm;
use common\helpers\GlobalParam;
use domain\Communication\Email\ContactListResolver;
use domain\Communication\Email\Email;
use domain\Communication\Email\EmailGenerator;
use domain\PointSale\PointSale\PointSale;
use kartik\mpdf\Pdf;
@@ -84,8 +85,9 @@ class CommunicateController extends BackendController
$sectionInactiveUsers = 0,
$usersPointSaleLink = 0,
$usersPointSaleHasOrder = 0,
$idDistribution = 0)
{
$idDistribution = 0,
$newsletterType = null
) {
$emailModule = $this->getEmailModule();

$mailForm = new MailForm();
@@ -101,6 +103,7 @@ class CommunicateController extends BackendController

$usersArray = $emailModule->getContactListResolver()->search(
$this->getProducerCurrent(),
$newsletterType,
$distribution,
$pointSale,
$usersPointSaleLink,
@@ -121,7 +124,7 @@ class CommunicateController extends BackendController
$email = $emailModule->getGenerator()->createEmail(
$mailForm->subject,
$mailForm->message,
$mailForm->integrate_product_list,
(bool) $mailForm->integrate_product_list,
$this->getProducerCurrent(),
$distribution
);
@@ -154,6 +157,7 @@ class CommunicateController extends BackendController
'sectionInactiveUsers' => $sectionInactiveUsers,
'usersPointSaleLink' => $usersPointSaleLink,
'usersPointSaleHasOrder' => $usersPointSaleHasOrder,
'newsletterType' => $newsletterType,
]);
}


+ 49
- 32
backend/views/communicate/email.php View File

@@ -36,6 +36,7 @@ pris connaissance de la licence CeCILL, et que vous en avez accepté les
termes.
*/

use domain\Communication\Email\Email;
use yii\helpers\Html ;
use yii\widgets\ActiveForm;

@@ -60,51 +61,67 @@ $this->render('@backend/views/user/_menu_filter.php',[

?>

<div id="">
<div class="communication-email row">
<div class="col-md-8">
<div class="panel panel-default">
<div id="panel-mail-form" class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">Envoyer un email</h3>
</div>
<div class="panel-body">
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($mailForm, 'id_distribution')->dropDownList($incomingDistributionsDatesArray)
->hint("Sélectionnez une distribution pour ajouter automatiquement au message un lien vers la prise de commande de cette distribution"); ?>
<?= $form->field($mailForm, 'integrate_product_list')->checkbox() ; ?>
<?= $form->field($mailForm, 'subject')->textInput() ; ?>
<?php //$form->field($mailForm, 'message')->textarea(['rows' => '15']) ; ?>
<?= $form->field($mailForm, 'message')->widget(letyii\tinymce\Tinymce::class, [
'configs' => [
'plugins' => Yii::$app->parameterBag->get('tinyMcePlugins')
],
]); ?>
<div class="form-group form-buttons">
<?= Html::submitButton( 'Envoyer', ['class' => 'btn btn-primary']) ?>
<div class="newsletter-type">
<strong>Type d'email</strong>
<div>
<?php $linkParamsArray = ['idPointSale' => $idPointSaleActive, 'idDistribution' => $idDistributionActive, 'usersPointSaleLink' => $usersPointSaleLink, 'usersPointSaleHasOrder' => $usersPointSaleHasOrder, 'sectionSubscribers' => $sectionSubscribers, 'sectionInactiveUsers' => $sectionInactiveUsers]; ?>
<a class="btn <?= ($newsletterType == Email::TYPE_ORDER_TAKING) ? 'btn-primary' : 'btn-default'; ?>" href="<?= Yii::$app->urlManager->createUrl(['communicate/email', 'newsletterType' => Email::TYPE_ORDER_TAKING] + $linkParamsArray) ?>">Prise de commande</a>
<a class="btn <?= ($newsletterType == Email::TYPE_NEWSLETTER) ? 'btn-primary' : 'btn-default'; ?>" href="<?= Yii::$app->urlManager->createUrl(['communicate/email', 'newsletterType' => Email::TYPE_NEWSLETTER] + $linkParamsArray) ?>">Actualité</a>
</div>
</div>
<?php ActiveForm::end(); ?>

<?php if($newsletterType): ?>
<?php $form = ActiveForm::begin(['enableClientValidation' => false]); ?>
<?php if($newsletterType == Email::TYPE_ORDER_TAKING): ?>
<?= $form->field($mailForm, 'id_distribution')->dropDownList($incomingDistributionsDatesArray)
->hint("Sélectionnez une distribution pour ajouter automatiquement au message un lien vers la prise de commande de cette distribution"); ?>
<?php endif; ?>
<?= $form->field($mailForm, 'subject')->textInput() ; ?>
<?= $form->field($mailForm, 'message')->widget(letyii\tinymce\Tinymce::class, [
'configs' => [
'plugins' => Yii::$app->parameterBag->get('tinyMcePlugins')
],
]); ?>
<?php if($newsletterType == Email::TYPE_ORDER_TAKING): ?>
<?= $form->field($mailForm, 'integrate_product_list')->checkbox() ; ?>
<?php endif; ?>
<div class="form-group form-buttons">
<?= Html::submitButton( 'Envoyer', ['class' => 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>
<?php endif; ?>
</div>
</div>
</div>
<div class="col-md-4">
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">
Liste des emails <span class="label label-default"><?= count($usersArray); ?></span>
</h3>
</div>
<div class="panel-body">
<?php if($newsletterType): ?>
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title">
Liste des emails <span class="label label-default"><?= count($usersArray); ?></span>
</h3>
</div>
<div class="panel-body">

<?php if($idPointSaleActive): ?>
<a class="btn btn-xs <?php if($usersPointSaleLink): ?>btn-primary<?php else: ?>btn-default<?php endif; ?>" href="<?= Yii::$app->urlManager->createUrl(['communicate/email','idPointSale' => $idPointSaleActive, 'idDistribution' => $idDistributionActive, 'usersPointSaleLink' => 1]); ?>">Liés au point de vente</a>
<a class="btn btn-xs <?php if($usersPointSaleHasOrder): ?>btn-primary<?php else: ?>btn-default<?php endif; ?>" href="<?= Yii::$app->urlManager->createUrl(['communicate/email','idPointSale' => $idPointSaleActive, 'idDistribution' => $idDistributionActive, 'usersPointSaleHasOrder' => 1]); ?>">Déjà commandés dans ce point de vente</a>
<br /><br />
<?php endif; ?>
<?php if($idPointSaleActive): ?>
<a class="btn btn-xs <?php if($usersPointSaleLink): ?>btn-primary<?php else: ?>btn-default<?php endif; ?>" href="<?= Yii::$app->urlManager->createUrl(['communicate/email','idPointSale' => $idPointSaleActive, 'idDistribution' => $idDistributionActive, 'usersPointSaleLink' => 1, 'newsletterType' => $newsletterType]); ?>">Liés au point de vente</a>
<a class="btn btn-xs <?php if($usersPointSaleHasOrder): ?>btn-primary<?php else: ?>btn-default<?php endif; ?>" href="<?= Yii::$app->urlManager->createUrl(['communicate/email','idPointSale' => $idPointSaleActive, 'idDistribution' => $idDistributionActive, 'usersPointSaleHasOrder' => 1, 'newsletterType' => $newsletterType]); ?>">Déjà commandés dans ce point de vente</a>
<br /><br />
<?php endif; ?>

<?= implode(', ', array_map(function($user) {
return $user['email'];
}, $usersArray)); ?>
<?= implode(', ', array_map(function($user) {
return $user['email'];
}, $usersArray)); ?>
</div>
</div>
</div>
<?php endif; ?>
</div>
<div class="clr"></div>
</div>

+ 1
- 1
backend/views/user/index.php View File

@@ -188,7 +188,7 @@ $this->render('_menu_filter', [
1 => 'Oui'
],
'value' => function ($user) use ($userModule) {
if($userModule->isUserSubscribedNewsletter($user)) {
if($userModule->getNewsletterManager()->isUserSubscribedOneNewsletter($user)) {
return '<span class="label label-success">Oui</span>';
}
return '<span class="label label-danger">Non</span>';

+ 10
- 0
backend/web/css/screen.css View File

@@ -3340,6 +3340,16 @@ termes.
display: block;
}

/* line 5, ../sass/communication/_email.scss */
.communication-email #panel-mail-form .newsletter-type {
margin-bottom: 20px;
}
/* line 7, ../sass/communication/_email.scss */
.communication-email #panel-mail-form .newsletter-type strong {
display: block;
margin-bottom: 5px;
}

/**
Copyright Souke (2018)


+ 13
- 0
backend/web/sass/communication/_email.scss View File

@@ -0,0 +1,13 @@


.communication-email {
#panel-mail-form {
.newsletter-type {
margin-bottom: 20px;
strong {
display: block;
margin-bottom: 5px;
}
}
}
}

+ 1
- 0
backend/web/sass/screen.scss View File

@@ -1556,4 +1556,5 @@ a.btn, button.btn {
@import "setting/_form.scss";
@import "dashboard-admin/_index.scss" ;
@import "automatic-email/_form.scss" ;
@import "communication/_email.scss" ;
@import "_responsive.scss" ;

+ 2
- 1
console/commands/AutomaticEmailController.php View File

@@ -3,6 +3,7 @@
namespace console\commands;

use domain\Communication\AutomaticEmail\AutomaticEmailModule;
use domain\Communication\Email\Email;
use domain\Communication\Email\EmailModule;
use domain\Producer\Producer\ProducerModule;
use yii\console\Controller;
@@ -24,7 +25,7 @@ class AutomaticEmailController extends Controller
$distribution = $automaticEmailModule->getResolver()->getMatchedDistribution($automaticEmail);
if($distribution) {
$email = $automaticEmailModule->getManager()->createEmailFromAutomaticEmail($automaticEmail, $distribution);
$usersArray = $emailModule->getContactListResolver()->search($producer, $distribution);
$usersArray = $emailModule->getContactListResolver()->search($producer, Email::TYPE_ORDER_TAKING, $distribution);
$emailModule->getBulkMailer()->sendEmail($email, $usersArray);
echo 'Email automatique "'.$automaticEmail->getSubject().'" envoyé à '.count($usersArray)." utilisateur(s)\n";
}

+ 3
- 0
domain/Communication/Email/ContactListResolver.php View File

@@ -20,6 +20,7 @@ class ContactListResolver extends AbstractResolver

public function search(
Producer $producer,
string $newsletterType = null,
Distribution $distribution = null,
PointSale $pointSale = null,
bool $usersPointSaleLink = false,
@@ -45,6 +46,7 @@ class ContactListResolver extends AbstractResolver
'id_point_sale' => $pointSaleDistribution->id_point_sale,
'users_point_sale_link' => $usersPointSaleLink,
'users_point_sale_has_order' => $usersPointSaleHasOrder,
'newsletter_type' => $newsletterType,
'newsletter' => true
])->all();

@@ -62,6 +64,7 @@ class ContactListResolver extends AbstractResolver
'users_point_sale_has_order' => $usersPointSaleHasOrder,
'subscribers' => $isSubscriber,
'inactive' => $isInactive,
'newsletter_type' => $newsletterType,
'newsletter' => true
])->all();
}

+ 6
- 0
domain/User/User/NewsletterManager.php View File

@@ -32,6 +32,12 @@ class NewsletterManager extends AbstractService implements ManagerInterface
return false;
}

public function isUserSubscribedOneNewsletter(User $user): bool
{
return $this->isUserSubscribedNewsletter($user, Email::TYPE_NEWSLETTER)
|| $this->isUserSubscribedNewsletter($user, Email::TYPE_ORDER_TAKING);
}

public function manageUserNewsletter(User $user, ?bool $newsletter, ?bool $newsletterOrderTaking)
{
if($newsletter) {

+ 3
- 1
domain/User/User/UserRepository.php View File

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

use common\helpers\GlobalParam;
use domain\Communication\Email\Email;
use domain\PointSale\PointSale\PointSale;
use domain\Producer\Producer\Producer;
use domain\User\UserProducer\UserProducerRepository;
@@ -102,7 +103,8 @@ class UserRepository extends AbstractRepository
->from('user');

$active = (isset($params['inactive']) && $params['inactive']) ? 0 : 1;
$filterNewsletter = (isset($params['newsletter']) && $params['newsletter']) ? 'AND user_producer.newsletter = 1' : '';
$fieldNewsletter = (isset($params['newsletter_type']) && $params['newsletter_type'] == Email::TYPE_ORDER_TAKING) ? 'newsletter_order_taking' : 'newsletter';;
$filterNewsletter = (isset($params['newsletter']) && $params['newsletter']) ? 'AND user_producer.'.$fieldNewsletter.' = 1' : '';
$query->innerJoin('user_producer', 'user.id = user_producer.id_user AND user_producer.active = ' . $active . ' '.$filterNewsletter.' AND user_producer.id_producer = :id_producer', [':id_producer' => $params['id_producer']]);

if (isset($params['id_point_sale']) && $params['id_point_sale']) {

+ 6
- 3
domain/User/User/UserSearch.php View File

@@ -151,9 +151,12 @@ class UserSearch extends User
}

if (isset($this->newsletter) && is_numeric($this->newsletter)) {
$query->andWhere([
'user_producer.newsletter' => $this->newsletter
]);
if($this->newsletter) {
$query->andWhere('user_producer.newsletter = 1 OR user_producer.newsletter_order_taking = 1');
}
else {
$query->andWhere('(user_producer.newsletter = 0 OR user_producer.newsletter IS NULL) AND (user_producer.newsletter_order_taking = 0 OR user_producer.newsletter_order_taking IS NULL)');
}
}

if (isset($this->id_user_user_group) && is_numeric($this->id_user_user_group)) {

+ 18
- 2
producer/controllers/NewsletterController.php View File

@@ -38,6 +38,7 @@

namespace producer\controllers;

use domain\Communication\Email\Email;
use yii\filters\AccessControl;

class NewsletterController extends ProducerBaseController
@@ -49,12 +50,27 @@ class NewsletterController extends ProducerBaseController

public function actionIndex()
{
if(!$this->getUserCurrent()) {
$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' => $this->getUserCurrent(),
'user' => $user,
'producer' => $this->getProducerCurrent()
]);
}

+ 1
- 1
producer/views/layouts/main.php View File

@@ -220,7 +220,7 @@ $mainColor = $producer->option_main_color ?: '#ee6f42' ;
->count();
$labelOrders = $countOrders > 0 ? 'success' : 'default';

$isUserSubscribedNewsletter = $userModule->isUserSubscribedNewsletter($userCurrent);
$isUserSubscribedNewsletter = $userModule->getNewsletterManager()->isUserSubscribedOneNewsletter($userCurrent);
$isHome = in_array($this->getControllerAction(), ['site/index', 'site/products', 'site/points-sale']);

echo Nav::widget([

+ 1
- 32
producer/views/newsletter/index.php View File

@@ -45,25 +45,6 @@ $userModule = UserModule::getInstance();
$producer = $this->context->getProducerCurrent();
$this->setTitle("Infolettres");

/*if($userModule->isUserSubscribedNewsletter($user)) {
$this->addButton(
[
'label' => '<i class="bi bi-x-circle"></i> Me désinscrire',
'url' => 'newsletter/unsubscribe',
'class' => 'btn btn-secondary'
]
);
}
else {
$this->addButton(
[
'label' => '<i class="bi bi-megaphone"></i> M\'inscrire',
'url' => 'newsletter/subscribe',
'class' => 'btn btn-primary'
]
);
}*/

?>
<div class="newsletter-index">

@@ -77,19 +58,6 @@ else {
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>
Vous êtes inscrit à l'infolettre <strong><?= Html::encode($producer->name) ?></strong>.
</div>
<?php else: ?>
<div class="alert alert-danger">
<i class="bi bi-x-circle"></i>
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,
@@ -102,6 +70,7 @@ else {
])
->label('Actualités');
?>
<div class="clr"></div>
<?= $form->field($user, 'newsletter_order_taking')->widget(Toggle::class,
[
'options' => [

+ 24
- 0
producer/web/css/screen.css View File

@@ -1952,6 +1952,30 @@ termes.
display: inline-block;
}

/* line 5, ../sass/newsletter/_index.scss */
.newsletter-index #newsletter-form .form-group {
margin-bottom: 25px;
}
/* line 8, ../sass/newsletter/_index.scss */
.newsletter-index #newsletter-form .form-group .toggle {
float: left;
width: 150px !important;
}
/* line 13, ../sass/newsletter/_index.scss */
.newsletter-index #newsletter-form .form-group label {
display: inline-block;
font-weight: bold;
margin-left: 20px;
}
/* line 18, ../sass/newsletter/_index.scss */
.newsletter-index #newsletter-form .form-group label.control-label {
margin-top: 10px;
}
/* line 23, ../sass/newsletter/_index.scss */
.newsletter-index #newsletter-form .form-group.form-buttons {
display: none;
}

/**
Copyright Souke (2018)


+ 7
- 0
producer/web/js/producer.js View File

@@ -78,8 +78,15 @@ $(document).ready(function () {
opendistrib_datepicker();
opendistrib_fix_width_sidebar();
opendistrib_products();
opendistrib_newsletter();
});

function opendistrib_newsletter() {
$('#newsletter-form input').change(function() {
$('#newsletter-form').submit();
});
}

function opendistrib_products() {
$('.product-name-description-block .show-more-link').unbind('click').click(function() {
$(this).hide();

+ 28
- 0
producer/web/sass/newsletter/_index.scss View File

@@ -0,0 +1,28 @@


.newsletter-index {
#newsletter-form {
.form-group {
margin-bottom: 25px;

.toggle {
float: left;
width: 150px !important;
}

label {
display: inline-block;
font-weight: bold;
margin-left: 20px;

&.control-label {
margin-top: 10px;
}
}

&.form-buttons {
display: none;
}
}
}
}

+ 1
- 0
producer/web/sass/screen.scss View File

@@ -56,4 +56,5 @@ $color-text: black;
@import "credit/_history.scss";
@import "subscription/_index.scss";
@import "subscription/_form.scss";
@import "newsletter/_index.scss";
@import "_responsive.scss";

Loading…
Cancel
Save