|
- <?php
-
- namespace domain\Communication\Email;
-
- use common\helpers\Price;
- use domain\_\AbstractResolver;
- use domain\Config\Unit\UnitDefinition;
- use domain\Distribution\Distribution\Distribution;
- use domain\Distribution\Distribution\DistributionSolver;
- use domain\Producer\Producer\Producer;
- use domain\Producer\Producer\ProducerSolver;
- use domain\Product\Product\Product;
- use domain\Product\Product\ProductSolver;
- use yii\helpers\Html;
-
- class EmailGenerator extends AbstractResolver
- {
- protected EmailBuilder $emailBuilder;
- protected ProductSolver $productSolver;
- protected DistributionSolver $distributionSolver;
- protected ProducerSolver $producerSolver;
-
- public function loadDependencies(): void
- {
- $this->emailBuilder = $this->loadService(EmailBuilder::class);
- $this->productSolver = $this->loadService(ProductSolver::class);
- $this->distributionSolver = $this->loadService(DistributionSolver::class);
- $this->producerSolver = $this->loadService(ProducerSolver::class);
- }
-
- public function createEmail(
- string $subject,
- string $message,
- bool $integrateProductList,
- Producer $producer = null,
- Distribution $distribution = null
- ): Email
- {
- $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($distribution) {
- $messageAutoText = '
-
- ' ;
- $messageAutoHtml .= '<br /><br />' ;
-
- $linkOrder = $this->distributionSolver->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($integrateProductList) {
- $productsArray = Product::find()
- ->where([
- 'id_producer' => $producer->id,
- ])
- ->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) > 0) {
-
- $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($this->productSolver->getPriceWithTax($product)) ;
- $productDescription .= ' ('. $this->productSolver->strUnit($product, UnitDefinition::WORDING_UNIT).')' ;
- }
-
- $messageAutoText .= '- '.$productDescription.'
- ' ;
- $messageAutoHtml .= '<li>'.Html::encode($productDescription).'</li>' ;
- }
- $messageAutoHtml .= '</ul>' ;
- }
- }
- }
-
- if($producer) {
- $fromEmail = $this->producerSolver->getProducerEmailPlatform($producer) ;
- $fromName = $producer->name ;
-
- $linkProducer = 'https://'.$producer->slug.'.souke.fr';
- $linkUnsubscribe = $linkProducer.'/newsletter/unsubscribe';
-
- // Message inscription newsletter
- $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' ;
- }
-
- $htmlContent = $message.$messageAutoHtml;
- $textContent = strip_tags($message).$messageAutoText;
-
- return $this->emailBuilder->instanciateEmail(
- $fromName,
- $fromEmail,
- $subject,
- $htmlContent,
- $textContent
- );
- }
- }
|