You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

158 line
5.4KB

  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. $linkProducer = '';
  36. if($producer) {
  37. $linkProducer = 'https://'.$producer->slug.'.souke.fr';
  38. }
  39. $messageAutoText = '' ;
  40. $messageAutoHtml = '' ;
  41. $messageAutoHtml .= ' <style type="text/css">
  42. h1, h2, h3, h4, h5, h6 {
  43. padding: 0px;
  44. margin: 0px;
  45. margin-bottom: 10px;
  46. }
  47. p {
  48. margin: 0px;
  49. padding: 0px;
  50. margin-bottom: 5px;
  51. }
  52. </style>';
  53. if($distribution) {
  54. $messageAutoText = '
  55. ' ;
  56. $messageAutoHtml .= '<br /><br />' ;
  57. $linkOrder = $linkProducer.'/order/order?date='.$distribution->date;
  58. $dateOrder = strftime('%A %d %B %Y', strtotime($distribution->date)) ;
  59. $messageAutoHtml .= '<a href="'.$linkOrder.'">Passer ma commande du '.$dateOrder.'</a>' ;
  60. $messageAutoText .= 'Suivez ce lien pour passer votre commande du '.$dateOrder.' :
  61. '.$linkOrder ;
  62. if($integrateProductList) {
  63. $productsArray = Product::find()
  64. ->where([
  65. 'id_producer' => $producer->id,
  66. ])
  67. ->andWhere('status >= :status')
  68. ->addParams(['status' => Product::STATUS_OFFLINE])
  69. ->innerJoinWith(['productDistribution' => function($query) use($distribution) {
  70. $query->andOnCondition([
  71. 'product_distribution.id_distribution' => $distribution->id,
  72. 'product_distribution.active' => 1
  73. ]);
  74. }])
  75. ->orderBy('product.name ASC')
  76. ->all();
  77. if(count($productsArray) > 0) {
  78. $messageAutoHtml .= '<br /><br />Produits disponibles : <br /><ul>' ;
  79. $messageAutoText .= '
  80. Produits disponibles :
  81. ' ;
  82. foreach($productsArray as $product) {
  83. $productDescription = $product->name ;
  84. if(strlen($product->description)) {
  85. $productDescription .= ' / '.$product->description ;
  86. }
  87. if($product->price) {
  88. $productDescription .= ' / '.Price::format($this->productSolver->getPriceWithTax($product)) ;
  89. $productDescription .= ' ('. $this->productSolver->strUnit($product, UnitDefinition::WORDING_UNIT).')' ;
  90. }
  91. $messageAutoText .= '- '.$productDescription.'
  92. ' ;
  93. $messageAutoHtml .= '<li>'.Html::encode($productDescription).'</li>' ;
  94. }
  95. $messageAutoHtml .= '</ul>' ;
  96. }
  97. }
  98. }
  99. else {
  100. if($addLinkOrderWhenNoDistribution) {
  101. $linkOrder = $linkProducer.'/order/order';
  102. $messageAutoHtml .= '<a href="'.$linkOrder.'">Passer ma commande</a>' ;
  103. $messageAutoText .= 'Suivez ce lien pour passer votre commande :
  104. '.$linkOrder ;
  105. }
  106. }
  107. if($producer) {
  108. $fromEmail = $this->producerSolver->getProducerEmailPlatform($producer) ;
  109. $fromName = $producer->name ;
  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. }