|
- <?php
-
-
-
- namespace backend\models;
-
- use common\helpers\GlobalParam;
- use common\helpers\Mailjet;
- use common\helpers\Price;
- use domain\Config\Unit\UnitDefinition;
- use domain\Distribution\Distribution\Distribution;
- use domain\Distribution\Distribution\DistributionModule;
- use domain\Producer\Producer\ProducerModule;
- use domain\Product\Product\Product;
- use domain\Product\Product\ProductModule;
- use GuzzleHttp\Client;
- use GuzzleHttp\Psr7\Response;
- use Psr\Http\Message\ResponseInterface;
- use Yii;
- use yii\base\Model;
- use yii\helpers\Html;
-
-
- class MailForm extends Model
- {
- public $id_distribution ;
- public $subject;
- public $message;
- public $integrate_product_list = false;
-
-
-
- public function rules()
- {
- return [
- [['subject', 'message'], 'required', 'message' => 'Champs obligatoire'],
- [['integrate_product_list'], 'boolean'],
- [['id_distribution'],'integer']
- ];
- }
-
-
-
- public function attributeLabels()
- {
- return [
- 'subject' => 'Sujet',
- 'message' => 'Message',
- 'id_distribution' => 'Distribution',
- 'integrate_product_list' => 'Intégrer la liste des produits au message'
- ];
- }
-
- public function sendEmail($contactsArray, $fromProducer = true)
- {
- $productModule = ProductModule::getInstance();
- $producerModule = ProducerModule::getInstance();
- $distributionModule = DistributionModule::getInstance();
-
- $messageAutoText = '' ;
- $messageAutoHtml = '' ;
-
- $messageAutoHtml .= ' <style type="text/css">
- h1, h2, h3, h4, h5, h6 {
- padding: 0px;
- margin: 0px;
- margin-bottom: 10px;
- }
-
- p {
- margin: 0px;
- padding: 0px;
- margin-bottom: 5px;
- }
- </style>';
-
- if($this->id_distribution) {
-
- $messageAutoText = '
-
- ' ;
- $messageAutoHtml .= '<br /><br />' ;
-
- $distribution = Distribution::searchOne(['id' => $this->id_distribution]) ;
-
- if($distribution) {
-
- $linkOrder = $distributionModule->getLinkOrder($distribution);
- $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 ;
-
- if($this->integrate_product_list) {
- $productsArray = Product::find()
- ->where([
- 'id_producer' => GlobalParam::getCurrentProducerId(),
- ])
- ->andWhere('status >= :status')
- ->addParams(['status' => Product::STATUS_OFFLINE])
- ->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($productModule->getPriceWithTax($product)) ;
- $productDescription .= ' ('. $productModule->getSolver()->strUnit($product, UnitDefinition::WORDING_UNIT).')' ;
- }
-
- $messageAutoText .= '- '.$productDescription.'
- ' ;
- $messageAutoHtml .= '<li>'.Html::encode($productDescription).'</li>' ;
- }
- $messageAutoHtml .= '</ul>' ;
- }
- }
- }
- }
-
- if($fromProducer) {
- $producer = GlobalParam::getCurrentProducer() ;
- $fromEmail = $producerModule->getProducerEmailPlatform($producer) ;
- $fromName = $producer->name ;
-
- $linkProducer = 'https://'.$producer->slug.'.souke.fr';
- $linkUnsubscribe = Yii::$app->urlManagerProducer->createAbsoluteUrl(['newsletter/unsubscribe', 'slug_producer' => $producer->slug]);
-
-
- $messageAutoText .= "
-
- --
- Boutique : ".$linkProducer."
- Me désinscrire : ".$linkUnsubscribe;
-
- $messageAutoHtml .= "<br /><br />--<br>";
- $messageAutoHtml .= "Boutique : <a href=\"".$linkProducer."\">".$linkProducer."</a><br>";
- $messageAutoHtml .= "Me désinscrire : <a href=\"".$linkUnsubscribe."\">".$linkUnsubscribe."</a>";
- }
- else {
- $fromEmail = 'contact@souke.fr' ;
- $fromName = 'Souke' ;
- }
-
- $subject = $this->subject;
- $htmlContent = nl2br($this->message).$messageAutoHtml;
- $textContent = $this->message.$messageAutoText;
-
- Yii::$app->bulkMailer->sendEmails($contactsArray, $fromName, $fromEmail, $subject, $htmlContent, $textContent);
- }
-
-
-
- }
|