Feefo.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <?php
  2. namespace Dotdigitalgroup\Email\Block;
  3. use DOMDocument;
  4. use Magento\Quote\Model\ResourceModel\Quote;
  5. use XSLTProcessor;
  6. const FEEFO_URL = 'http://www.feefo.com/feefo/xmlfeed.jsp?';
  7. /**
  8. * Feefo block
  9. *
  10. * @api
  11. */
  12. class Feefo extends \Magento\Framework\View\Element\Template
  13. {
  14. /**
  15. * @var \Dotdigitalgroup\Email\Helper\Data
  16. */
  17. public $helper;
  18. /**
  19. * @var \Magento\Framework\Pricing\Helper\Data
  20. */
  21. public $priceHelper;
  22. /**
  23. * @var DOMDocument
  24. */
  25. public $domDocument;
  26. /**
  27. * @var XSLTProcessor
  28. */
  29. public $processor;
  30. /**
  31. * @var Quote
  32. */
  33. private $quoteResource;
  34. /**
  35. * @var \Dotdigitalgroup\Email\Model\ResourceModel\Review
  36. */
  37. private $review;
  38. /**
  39. * @var \Magento\Framework\View\Asset\Repository
  40. */
  41. private $assetRepository;
  42. /**
  43. * @var \Magento\Quote\Model\QuoteFactory
  44. */
  45. private $quoteFactory;
  46. /**
  47. * Feefo constructor.
  48. *
  49. * @param \Magento\Framework\View\Element\Template\Context $context
  50. * @param XSLTProcessor $processor
  51. * @param DOMDocument $document
  52. * @param \Dotdigitalgroup\Email\Helper\Data $helper
  53. * @param \Magento\Framework\Pricing\Helper\Data $priceHelper
  54. * @param \Dotdigitalgroup\Email\Model\ResourceModel\Review $review
  55. * @param Quote $quoteResource
  56. * @param \Magento\Quote\Model\QuoteFactory $quoteFactory
  57. * @param array $data
  58. */
  59. public function __construct(
  60. \Magento\Framework\View\Element\Template\Context $context,
  61. XSLTProcessor $processor,
  62. DOMDocument $document,
  63. \Dotdigitalgroup\Email\Helper\Data $helper,
  64. \Magento\Framework\Pricing\Helper\Data $priceHelper,
  65. \Dotdigitalgroup\Email\Model\ResourceModel\Review $review,
  66. \Magento\Quote\Model\ResourceModel\Quote $quoteResource,
  67. \Magento\Quote\Model\QuoteFactory $quoteFactory,
  68. array $data = []
  69. ) {
  70. $this->helper = $helper;
  71. $this->domDocument = $document;
  72. $this->processor = $processor;
  73. $this->priceHelper = $priceHelper;
  74. $this->review = $review;
  75. $this->assetRepository = $context->getAssetRepository();
  76. $this->quoteFactory = $quoteFactory;
  77. $this->quoteResource = $quoteResource;
  78. parent::__construct($context, $data);
  79. }
  80. /**
  81. * Get customer's service score logo and output it.
  82. *
  83. * @return array
  84. */
  85. public function getServiceScoreLogo()
  86. {
  87. $params = $this->getRequest()->getParams();
  88. if (! isset($params['code']) || ! $this->helper->isCodeValid($params['code'])) {
  89. $this->helper->log('Feefo no valid code is set');
  90. return [];
  91. }
  92. $url = 'http://www.feefo.com/feefo/feefologo.jsp?logon=';
  93. $logon = $this->helper->getFeefoLogon();
  94. $template = '';
  95. if ($this->helper->getFeefoLogoTemplate()) {
  96. $template = '&template=' . $this->helper->getFeefoLogoTemplate();
  97. }
  98. $fullUrl = $url . $logon . $template;
  99. $vendorUrl = 'http://www.feefo.com/feefo/viewvendor.jsp?logon='
  100. . $logon;
  101. return ['vendorUrl' => $vendorUrl, 'fullUrl' => $fullUrl];
  102. }
  103. /**
  104. * Get quote products to show feefo reviews.
  105. *
  106. * @return array
  107. */
  108. private function getQuoteProducts()
  109. {
  110. $products = [];
  111. $params = $this->_request->getParams();
  112. if (! isset($params['quote_id']) || ! isset($params['code']) || ! $this->helper->isCodeValid($params['code'])) {
  113. $this->helper->log('Feefo no quote id or code is set');
  114. return $products;
  115. }
  116. $quoteId = (int) $params['quote_id'];
  117. $quoteModel = $this->quoteFactory->create();
  118. $this->quoteResource->load($quoteModel, $quoteId);
  119. if (! $quoteModel->getId()) {
  120. return $products;
  121. }
  122. $productCollection = $this->review->getProductCollection($quoteModel);
  123. foreach ($productCollection as $product) {
  124. $products[$product->getSku()] = $product->getName();
  125. }
  126. return $products;
  127. }
  128. /**
  129. * Get product reviews from feefo.
  130. *
  131. * @param bool $check
  132. *
  133. * @return array
  134. */
  135. public function getProductsReview($check = true)
  136. {
  137. $reviews = [];
  138. $logon = $this->helper->getFeefoLogon();
  139. $limit = $this->helper->getFeefoReviewsPerProduct();
  140. $products = $this->getQuoteProducts();
  141. foreach ($products as $sku => $name) {
  142. $url = 'http://www.feefo.com/feefo/xmlfeed.jsp?logon=' . $logon
  143. . '&limit=' . $limit . '&vendorref=' . $sku
  144. . '&mode=productonly';
  145. $doc = $this->domDocument;
  146. $xsl = $this->processor;
  147. if ($check) {
  148. $pathToTemplate = $this->getFeefoTemplate('feedback.xsl');
  149. $doc->load($pathToTemplate);
  150. } else {
  151. $pathToTemplate = $this->getFeefoTemplate('feedback-no-th.xsl');
  152. $doc->load($pathToTemplate);
  153. }
  154. $xsl->importStyleSheet($doc);
  155. $doc->loadXML(file_get_contents($url));
  156. $productReview = $xsl->transformToXML($doc);
  157. if (strpos($productReview, '<td') !== false) {
  158. $reviews[$name] = $xsl->transformToXML($doc);
  159. }
  160. $check = false;
  161. }
  162. return $reviews;
  163. }
  164. /**
  165. * @param string $template
  166. *
  167. * @return string
  168. */
  169. private function getFeefoTemplate($template)
  170. {
  171. return $this->assetRepository
  172. ->createAsset('Dotdigitalgroup_Email::feefo/' . $template)
  173. ->getUrl();
  174. }
  175. /**
  176. * @return string
  177. */
  178. public function getCssUrl()
  179. {
  180. return $this->assetRepository
  181. ->createAsset('Dotdigitalgroup_Email::css/feefo.css')
  182. ->getUrl();
  183. }
  184. }