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.

145 line
5.0KB

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