Browse Source

[backend] Envoi de mail aux utilisateurs (via points de vente) : possibilité de choisir un jour de distribution.

Ajout automatique d'un lien pour passer commande ainsi que les produits disponibles pour cette distribution.
dev
Guillaume Bourgeois 5 years ago
parent
commit
ae29b11ec6
4 changed files with 77 additions and 5 deletions
  1. +10
    -2
      backend/controllers/UserController.php
  2. +63
    -2
      backend/models/MailForm.php
  3. +2
    -0
      backend/views/user/emails.php
  4. +2
    -1
      common/models/Distribution.php

+ 10
- 2
backend/controllers/UserController.php View File

@@ -40,6 +40,7 @@ namespace backend\controllers;

use common\models\User ;
use common\models\Producer ;
use common\models\Distribution ;
use backend\models\MailForm ;

/**
@@ -249,13 +250,20 @@ class UserController extends BackendController
return $this->redirect(['mail','idPointSale' => $idPointSale]);
}

$incomingDistributions = Distribution::getIncomingDistributions() ;
$incomingDistributionsArray = ['0' => '--'] ;
foreach($incomingDistributions as $distribution) {
$incomingDistributionsArray[$distribution->id] = strftime('%A %d %B %Y', strtotime($distribution->date)) ;
}
return $this->render('emails', [
'usersArray' => $usersArray,
'pointsSaleArray' => $pointsSaleArray,
'pointSale' => $pointSale,
'mailForm' => $mailForm,
'idPointSaleActive' => $idPointSale
'idPointSaleActive' => $idPointSale,
'incomingDistributionsArray' => $incomingDistributionsArray,
]);
}


+ 63
- 2
backend/models/MailForm.php View File

@@ -40,6 +40,7 @@ namespace backend\models;

use Yii;
use yii\base\Model;
use common\helpers\Price ;

/**
* ContactForm is the model behind the contact form.
@@ -47,6 +48,7 @@ use yii\base\Model;
class MailForm extends Model
{

public $id_distribution ;
public $subject;
public $message;

@@ -57,6 +59,7 @@ class MailForm extends Model
{
return [
[['subject', 'message'], 'required', 'message' => 'Champs obligatoire'],
[['id_distribution'],'integer']
];
}

@@ -68,6 +71,7 @@ class MailForm extends Model
return [
'subject' => 'Sujet',
'message' => 'Message',
'id_distribution' => 'Distribution'
];
}

@@ -90,6 +94,63 @@ class MailForm extends Model

$body = ['Messages' => []] ;
$messageAutoText = '' ;
$messageAutoHtml = '' ;
if($this->id_distribution) {
$messageAutoText = '

' ;
$messageAutoHtml = '<br /><br />' ;
$distribution = Distribution::searchOne(['id' => $this->id_distribution]) ;
if($distribution) {
$linkOrder = Yii::$app->urlManagerProducer->createAbsoluteUrl(['order/order','slug_producer' => Producer::getCurrent()->slug, 'date' => $distribution->date]) ;
$dateOrder = strftime('%A %d %B %Y', strtotime($distribution->date)) ;
$messageAutoHtml .= '<a href="'.$linkOrder.'">Passer ma commande du '.$dateOrder.'</a>' ;
$messageAutoText .= 'Suivez ce lien pour passer votre commande du '.$dateOrder.' :
'.$linkOrder ;
$productsArray = Product::find()
->where([
'id_producer' => Producer::getId(),
])
->innerJoinWith(['productDistribution' => function($query) use($distribution) {
$query->andOnCondition([
'product_distribution.id_distribution' => $distribution->id,
'product_distribution.active' => 1
]);
}])
->orderBy('product.name ASC')
->all();

if(count($productsArray) > 1) {
$messageAutoHtml .= '<br /><br />Produits disponibles : <br /><ul>' ;
$messageAutoText .= '

Produits disponibles :
' ;
foreach($productsArray as $product) {
$productDescription = $product->name ;
if(strlen($product->description)) {
$productDescription .= ' / '.$product->description ;
}
if($product->price) {
$productDescription .= ' / '.Price::format($product->price) ;
}
$messageAutoText .= '- '.$productDescription.'
' ;
$messageAutoHtml .= '<li>'.Html::encode($productDescription).'</li>' ;
}
$messageAutoHtml .= '</ul>' ;
}
}
}
foreach($usersArray as $user) {
$body['Messages'][] = [
'From' => [
@@ -103,8 +164,8 @@ class MailForm extends Model
]
],
'Subject' => $this->subject,
'TextPart' => $this->message,
'HTMLPart' => nl2br($this->message)
'TextPart' => $this->message.$messageAutoText,
'HTMLPart' => nl2br($this->message).$messageAutoHtml
] ;
}

+ 2
- 0
backend/views/user/emails.php View File

@@ -64,6 +64,8 @@ $this->render('_menu',[
</div>
<div class="panel-body">
<?php $form = ActiveForm::begin(); ?>
<?= $form->field($mailForm, 'id_distribution')->dropDownList($incomingDistributionsArray)
->hint("Sélectionnez une distribution pour ajouter automatiquement au message :<br /><ul><li>un lien vers la prise de commande de cette distribution</li><li>la liste des produits disponibles pour cette distribution</li></ul>"); ?>
<?= $form->field($mailForm, 'subject')->textInput() ; ?>
<?= $form->field($mailForm, 'message')->textarea(['rows' => '15']) ; ?>
<div class="form-group">

+ 2
- 1
common/models/Distribution.php View File

@@ -203,8 +203,9 @@ class Distribution extends ActiveRecordCommon
public static function getIncomingDistributions()
{
$distributionsArray = Distribution::find()
->where('date > ' . date('Y-m-d'))
->where('date > \'' . date('Y-m-d').'\'')
->andWhere(['id_producer' => Producer::getId()])
->orderBy('date ASC')
->all();
return $distributionsArray ;

Loading…
Cancel
Save