Review.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. <?php
  2. namespace Dotdigitalgroup\Email\Model\ResourceModel;
  3. use Dotdigitalgroup\Email\Setup\Schema;
  4. use Magento\Review\Model\ResourceModel\Rating\Option;
  5. /**
  6. * Class Review
  7. *
  8. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  9. * @SuppressWarnings(PHPMD.ExcessiveParameterList)
  10. */
  11. class Review extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb
  12. {
  13. /**
  14. * @var \Dotdigitalgroup\Email\Helper\Data
  15. */
  16. public $helper;
  17. /**
  18. * @var \Magento\Review\Model\ResourceModel\Review\CollectionFactory
  19. */
  20. public $mageReviewCollection;
  21. /**
  22. * @var \Magento\Catalog\Model\ProductFactory
  23. */
  24. public $productFactory;
  25. /**
  26. * @var \Magento\Review\Model\Rating\Option\Vote
  27. */
  28. public $vote;
  29. /**
  30. * @var Option\Vote\CollectionFactory
  31. */
  32. private $voteCollection;
  33. /**
  34. * @var \Magento\Quote\Model\QuoteFactory
  35. */
  36. private $quoteFactory;
  37. /**
  38. * @var \Magento\Review\Model\ReviewFactory
  39. */
  40. private $reviewFactory;
  41. /**
  42. * @var \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory
  43. */
  44. private $productCollection;
  45. /**
  46. * Initialize resource.
  47. *
  48. * @return null
  49. */
  50. public function _construct()
  51. {
  52. $this->_init(Schema::EMAIL_REVIEW_TABLE, 'id');
  53. }
  54. /**
  55. * Review constructor.
  56. *
  57. * @param \Magento\Framework\Model\ResourceModel\Db\Context $context
  58. * @param \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory
  59. * @param \Magento\Quote\Model\QuoteFactory $quoteFactory
  60. * @param \Magento\Review\Model\ReviewFactory $reviewFactory
  61. * @param \Dotdigitalgroup\Email\Helper\Data $data
  62. * @param \Magento\Review\Model\ResourceModel\Review\CollectionFactory $mageReviewCollection
  63. * @param \Magento\Catalog\Model\ProductFactory $productFactory
  64. * @param Option\Vote\CollectionFactory $voteCollection
  65. * @param \Magento\Review\Model\Rating\Option\Vote $vote
  66. * @param null $connectionName
  67. */
  68. public function __construct(
  69. \Magento\Framework\Model\ResourceModel\Db\Context $context,
  70. \Magento\Catalog\Model\ResourceModel\Product\CollectionFactory $productCollectionFactory,
  71. \Magento\Quote\Model\QuoteFactory $quoteFactory,
  72. \Magento\Review\Model\ReviewFactory $reviewFactory,
  73. \Dotdigitalgroup\Email\Helper\Data $data,
  74. \Magento\Review\Model\ResourceModel\Review\CollectionFactory $mageReviewCollection,
  75. \Magento\Catalog\Model\ProductFactory $productFactory,
  76. \Magento\Review\Model\ResourceModel\Rating\Option\Vote\CollectionFactory $voteCollection,
  77. \Magento\Review\Model\Rating\Option\Vote $vote,
  78. $connectionName = null
  79. ) {
  80. $this->helper = $data;
  81. $this->mageReviewCollection = $mageReviewCollection;
  82. $this->productFactory = $productFactory;
  83. $this->vote = $vote;
  84. $this->quoteFactory = $quoteFactory;
  85. $this->reviewFactory = $reviewFactory;
  86. $this->voteCollection = $voteCollection;
  87. $this->productCollection = $productCollectionFactory;
  88. parent::__construct(
  89. $context,
  90. $connectionName
  91. );
  92. }
  93. /**
  94. * Reset the email reviews for re-import.
  95. *
  96. * @param string $from
  97. * @param string $to
  98. *
  99. * @return int
  100. */
  101. public function resetReviews($from = null, $to = null)
  102. {
  103. $conn = $this->getConnection();
  104. if ($from && $to) {
  105. $where = [
  106. 'created_at >= ?' => $from . ' 00:00:00',
  107. 'created_at <= ?' => $to . ' 23:59:59',
  108. 'review_imported is ?' => new \Zend_Db_Expr('not null')
  109. ];
  110. } else {
  111. $where = $conn->quoteInto(
  112. 'review_imported is ?',
  113. new \Zend_Db_Expr('not null')
  114. );
  115. }
  116. $num = $conn->update(
  117. $this->getTable(Schema::EMAIL_REVIEW_TABLE),
  118. ['review_imported' => new \Zend_Db_Expr('null')],
  119. $where
  120. );
  121. return $num;
  122. }
  123. /**
  124. * Filter items for review.
  125. *
  126. * @param array $items
  127. * @param int $customerId
  128. * @param \Magento\Sales\Model\Order $order
  129. *
  130. * @return mixed
  131. */
  132. public function filterItemsForReview($items, $customerId, $order)
  133. {
  134. foreach ($items as $key => $item) {
  135. $productId = $item->getProduct()->getId();
  136. $collection = $this->reviewFactory->create()->getCollection()
  137. ->addCustomerFilter($customerId)
  138. ->addStoreFilter($order->getStoreId())
  139. ->addFieldToFilter('main_table.entity_pk_value', $productId);
  140. //remove item if customer has already placed review on this item
  141. if ($collection->getSize()) {
  142. unset($items[$key]);
  143. }
  144. }
  145. return $items;
  146. }
  147. /**
  148. * Get product collection from order.
  149. *
  150. * @param \Magento\Quote\Model\Quote $quote
  151. *
  152. * @return array|\Magento\Framework\Data\Collection\AbstractDb
  153. */
  154. public function getProductCollection($quote)
  155. {
  156. $productIds = [];
  157. $products = [];
  158. $items = $quote->getAllVisibleItems();
  159. //get the product ids for the collection
  160. foreach ($items as $item) {
  161. $productIds[] = $item->getProductId();
  162. }
  163. if (! empty($productIds)) {
  164. $products = $this->productCollection->create()
  165. ->addAttributeToSelect('*')
  166. ->addFieldToFilter('entity_id', ['in' => $productIds]);
  167. }
  168. return $products;
  169. }
  170. /**
  171. * Set imported in bulk query.
  172. *
  173. * @param array $ids
  174. * @param string $nowDate
  175. *
  176. * @return null
  177. */
  178. public function setImported($ids, $nowDate)
  179. {
  180. try {
  181. $coreResource = $this->getConnection();
  182. $tableName = $this->getTable(Schema::EMAIL_REVIEW_TABLE);
  183. $coreResource->update(
  184. $tableName,
  185. ['review_imported' => 1, 'updated_at' => $nowDate],
  186. ["review_id IN (?)" => $ids]
  187. );
  188. } catch (\Exception $e) {
  189. $this->helper->debug((string)$e, []);
  190. }
  191. }
  192. /**
  193. * Get Mage reviews by ids.
  194. *
  195. * @param array $ids
  196. *
  197. * @return \Magento\Review\Model\ResourceModel\Review\Collection
  198. */
  199. public function getMageReviewsByIds($ids)
  200. {
  201. $reviews = $this->mageReviewCollection->create()
  202. ->addFieldToFilter(
  203. 'main_table.review_id',
  204. ['in' => $ids]
  205. )
  206. ->addFieldToFilter('customer_id', ['notnull' => 'true']);
  207. $reviews->getSelect()
  208. ->joinLeft(
  209. ['c' => $this->getTable('customer_entity')],
  210. 'c.entity_id = customer_id',
  211. ['email', 'store_id']
  212. );
  213. return $reviews;
  214. }
  215. /**
  216. * Get product by id and store.
  217. *
  218. * @param int $id
  219. * @param int $storeId
  220. *
  221. * @return mixed
  222. */
  223. public function getProductByIdAndStore($id, $storeId)
  224. {
  225. $product = $this->productFactory->create()
  226. ->getCollection()
  227. ->addIdFilter($id)
  228. ->setStoreId($storeId)
  229. ->addAttributeToSelect(
  230. ['product_url', 'name', 'store_id', 'small_image']
  231. )
  232. ->setPage(1, 1);
  233. return $product->getFirstItem();
  234. }
  235. /**
  236. * Get vote collection by review.
  237. *
  238. * @param int $reviewId
  239. *
  240. * @return mixed
  241. */
  242. public function getVoteCollectionByReview($reviewId)
  243. {
  244. $votesCollection = $this->voteCollection->create()
  245. ->setReviewFilter($reviewId);
  246. $votesCollection->getSelect()->join(
  247. ['rating' => $this->getTable('rating')],
  248. 'rating.rating_id = main_table.rating_id',
  249. ['rating_code' => 'rating.rating_code']
  250. );
  251. return $votesCollection;
  252. }
  253. }