Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

157 lines
5.5KB

  1. <?php
  2. namespace domain\Communication\Email;
  3. use common\helpers\Price;
  4. use domain\_\AbstractResolver;
  5. use domain\Config\Unit\UnitDefinition;
  6. use domain\Distribution\Distribution\Distribution;
  7. use domain\Distribution\Distribution\DistributionSolver;
  8. use domain\Producer\Producer\Producer;
  9. use domain\Producer\Producer\ProducerSolver;
  10. use domain\Product\Product\Product;
  11. use domain\Product\Product\ProductSolver;
  12. use yii\helpers\Html;
  13. class EmailGenerator extends AbstractResolver
  14. {
  15. protected EmailBuilder $emailBuilder;
  16. protected ProductSolver $productSolver;
  17. protected DistributionSolver $distributionSolver;
  18. protected ProducerSolver $producerSolver;
  19. public function loadDependencies(): void
  20. {
  21. $this->emailBuilder = $this->loadService(EmailBuilder::class);
  22. $this->productSolver = $this->loadService(ProductSolver::class);
  23. $this->distributionSolver = $this->loadService(DistributionSolver::class);
  24. $this->producerSolver = $this->loadService(ProducerSolver::class);
  25. }
  26. public function createEmail(
  27. string $subject,
  28. string $message,
  29. bool $integrateProductList,
  30. Producer $producer = null,
  31. Distribution $distribution = null,
  32. bool $addLinkOrderWhenNoDistribution = false
  33. ): Email
  34. {
  35. $messageAutoText = '' ;
  36. $messageAutoHtml = '' ;
  37. $messageAutoHtml .= ' <style type="text/css">
  38. h1, h2, h3, h4, h5, h6 {
  39. padding: 0px;
  40. margin: 0px;
  41. margin-bottom: 10px;
  42. }
  43. p {
  44. margin: 0px;
  45. padding: 0px;
  46. margin-bottom: 5px;
  47. }
  48. </style>';
  49. if($distribution) {
  50. $messageAutoText = '
  51. ' ;
  52. $messageAutoHtml .= '<br /><br />' ;
  53. $linkOrder = $this->distributionSolver->getLinkOrder($distribution);
  54. $dateOrder = strftime('%A %d %B %Y', strtotime($distribution->date)) ;
  55. $messageAutoHtml .= '<a href="'.$linkOrder.'">Passer ma commande du '.$dateOrder.'</a>' ;
  56. $messageAutoText .= 'Suivez ce lien pour passer votre commande du '.$dateOrder.' :
  57. '.$linkOrder ;
  58. if($integrateProductList) {
  59. $productsArray = Product::find()
  60. ->where([
  61. 'id_producer' => $producer->id,
  62. ])
  63. ->andWhere('status >= :status')
  64. ->addParams(['status' => Product::STATUS_OFFLINE])
  65. ->innerJoinWith(['productDistribution' => function($query) use($distribution) {
  66. $query->andOnCondition([
  67. 'product_distribution.id_distribution' => $distribution->id,
  68. 'product_distribution.active' => 1
  69. ]);
  70. }])
  71. ->orderBy('product.name ASC')
  72. ->all();
  73. if(count($productsArray) > 0) {
  74. $messageAutoHtml .= '<br /><br />Produits disponibles : <br /><ul>' ;
  75. $messageAutoText .= '
  76. Produits disponibles :
  77. ' ;
  78. foreach($productsArray as $product) {
  79. $productDescription = $product->name ;
  80. if(strlen($product->description)) {
  81. $productDescription .= ' / '.$product->description ;
  82. }
  83. if($product->price) {
  84. $productDescription .= ' / '.Price::format($this->productSolver->getPriceWithTax($product)) ;
  85. $productDescription .= ' ('. $this->productSolver->strUnit($product, UnitDefinition::WORDING_UNIT).')' ;
  86. }
  87. $messageAutoText .= '- '.$productDescription.'
  88. ' ;
  89. $messageAutoHtml .= '<li>'.Html::encode($productDescription).'</li>' ;
  90. }
  91. $messageAutoHtml .= '</ul>' ;
  92. }
  93. }
  94. }
  95. else {
  96. if($addLinkOrderWhenNoDistribution) {
  97. $linkOrder = \Yii::$app->urlManagerProducer->createAbsoluteUrl([
  98. 'order/order',
  99. 'slug_producer' => $this->getProducerContext()->slug
  100. ]) ;
  101. $messageAutoHtml .= '<a href="'.$linkOrder.'">Passer ma commande</a>' ;
  102. $messageAutoText .= 'Suivez ce lien pour passer votre commande :
  103. '.$linkOrder ;
  104. }
  105. }
  106. if($producer) {
  107. $fromEmail = $this->producerSolver->getProducerEmailPlatform($producer) ;
  108. $fromName = $producer->name ;
  109. $linkProducer = 'https://'.$producer->slug.'.souke.fr';
  110. $linkUnsubscribe = $linkProducer.'/newsletter/unsubscribe';
  111. // Message inscription newsletter
  112. $messageAutoText .= "
  113. --
  114. Boutique : ".$linkProducer."
  115. Me désinscrire : ".$linkUnsubscribe;
  116. $messageAutoHtml .= "<br /><br />--<br>";
  117. $messageAutoHtml .= "Boutique : <a href=\"".$linkProducer."\">".$linkProducer."</a><br>";
  118. $messageAutoHtml .= "Me désinscrire : <a href=\"".$linkUnsubscribe."\">".$linkUnsubscribe."</a>";
  119. }
  120. else {
  121. $fromEmail = 'contact@souke.fr' ;
  122. $fromName = 'Souke' ;
  123. }
  124. $htmlContent = $message.$messageAutoHtml;
  125. $textContent = strip_tags($message).$messageAutoText;
  126. return $this->emailBuilder->instanciateEmail(
  127. $fromName,
  128. $fromEmail,
  129. $subject,
  130. $htmlContent,
  131. $textContent
  132. );
  133. }
  134. }