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.

148 satır
5.2KB

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