|
1 | | -<?php |
2 | | -/** |
3 | | - * Copyright © Magefan (support@magefan.com). All rights reserved. |
4 | | - * Please visit Magefan.com for license details (https://magefan.com/end-user-license-agreement). |
5 | | - */ |
6 | | -declare(strict_types=1); |
7 | | - |
8 | | -namespace Magefan\ProductLabel\Model; |
9 | | - |
10 | | -use Magefan\ProductLabel\Model\Config; |
11 | | -use Magento\Store\Model\StoreManagerInterface; |
12 | | -use Magefan\ProductLabel\Model\GetProductIdsToRuleIdsMap; |
13 | | -use Magefan\ProductLabel\Model\ResourceModel\Rule\CollectionFactory as RuleCollectionFactory; |
14 | | -use Magefan\ProductLabel\Api\LabelProcessorInterface; |
15 | | -use Magento\Framework\View\LayoutInterface; |
16 | | - |
17 | | -class GetLabels |
18 | | -{ |
19 | | - /** |
20 | | - * @var Config |
21 | | - */ |
22 | | - protected $config; |
23 | | - |
24 | | - /** |
25 | | - * @var StoreManagerInterface |
26 | | - */ |
27 | | - protected $storeManager; |
28 | | - |
29 | | - /** |
30 | | - * @var GetProductIdsToRuleIdsMap |
31 | | - */ |
32 | | - protected $getProductIdsToRuleIdsMap; |
33 | | - |
34 | | - /** |
35 | | - * @var RuleCollectionFactory |
36 | | - */ |
37 | | - protected $ruleCollectionFactory; |
38 | | - |
39 | | - /** |
40 | | - * @var LabelProcessorInterface|null |
41 | | - */ |
42 | | - protected $labelProcessor; |
43 | | - |
44 | | - /** |
45 | | - * @var LayoutInterface |
46 | | - */ |
47 | | - protected $layout; |
48 | | - |
49 | | - /** |
50 | | - * GetLabels constructor. |
51 | | - * @param StoreManagerInterface $storeManager |
52 | | - * @param \Magefan\ProductLabel\Model\GetProductIdsToRuleIdsMap $getProductIdsToRuleIdsMap |
53 | | - * @param RuleCollectionFactory $ruleCollectionFactory |
54 | | - * @param LayoutInterface $layout |
55 | | - * @param LabelProcessorInterface|null $labelProcessor |
56 | | - */ |
57 | | - public function __construct( |
58 | | - Config $config, |
59 | | - StoreManagerInterface $storeManager, |
60 | | - GetProductIdsToRuleIdsMap $getProductIdsToRuleIdsMap, |
61 | | - RuleCollectionFactory $ruleCollectionFactory, |
62 | | - LayoutInterface $layout, |
63 | | - ?LabelProcessorInterface $labelProcessor = null |
64 | | - ) { |
65 | | - $this->config = $config; |
66 | | - $this->storeManager = $storeManager; |
67 | | - $this->getProductIdsToRuleIdsMap = $getProductIdsToRuleIdsMap; |
68 | | - $this->ruleCollectionFactory = $ruleCollectionFactory; |
69 | | - $this->labelProcessor = $labelProcessor; |
70 | | - $this->layout = $layout; |
71 | | - } |
72 | | - |
73 | | - /** |
74 | | - * @param array $productIds |
75 | | - * @param $idsUseForProductPage |
76 | | - * @return array |
77 | | - */ |
78 | | - public function execute(array $productIds, $productIdsForProductPage = []): array |
79 | | - { |
80 | | - [$ruleIds, $productIdRuleIds] = $this->getProductIdsToRuleIdsMap->execute($productIds); |
81 | | - |
82 | | - $rules = $this->ruleCollectionFactory->create() |
83 | | - ->addActiveFilter() |
84 | | - ->addFieldToFilter('id', ['in' => $ruleIds]) |
85 | | - ->addStoreFilter($this->storeManager->getStore()->getId()) |
86 | | - ->setOrder('priority', 'asc'); |
87 | | - |
88 | | - $replaceMap = []; |
89 | | - |
90 | | - foreach ($productIdRuleIds as $productId => $productRuleIds) { |
91 | | - $forProductPage = in_array($productId, $productIdsForProductPage); |
92 | | - |
93 | | - $productLabels = $this->getAvailableProductLabels($rules, $productRuleIds, $forProductPage); |
94 | | - |
95 | | - $htmlToReplace = $this->layout |
96 | | - ->createBlock(\Magefan\ProductLabel\Block\Label::class) |
97 | | - ->setProductLabels($productLabels) |
98 | | - ->setAdditionalCssClass($forProductPage ? 'mfpl-product-page' : '') |
99 | | - ->toHtml(); |
100 | | - |
101 | | - if ($htmlToReplace) { |
102 | | - $replaceMap[$productId] = $htmlToReplace; |
103 | | - } |
104 | | - } |
105 | | - |
106 | | - if (null !== $this->labelProcessor) { |
107 | | - $replaceMap = $this->labelProcessor->execute($replaceMap, $productIds); |
108 | | - } |
109 | | - |
110 | | - return $replaceMap; |
111 | | - } |
112 | | - |
113 | | - /** |
114 | | - * @param $rules |
115 | | - * @param $productRuleIds |
116 | | - * @param bool $forProductPage |
117 | | - * @return array |
118 | | - */ |
119 | | - public function getAvailableProductLabels($rules, $productRuleIds, $forProductPage = false): array |
120 | | - { |
121 | | - $productLabelsCount = 0; |
122 | | - $productLabels = []; |
123 | | - |
124 | | - $discardRulePerPosition = []; |
125 | | - |
126 | | - foreach ($rules as $rule) { |
127 | | - if (in_array($rule->getId(), $productRuleIds)) { |
128 | | - $productLabelsCount++; |
129 | | - |
130 | | - if ($forProductPage) { |
131 | | - $position = $rule->getPpPosition() ?: $rule->getPosition(); |
132 | | - } else { |
133 | | - $position = $rule->getPosition() ?: 'top-left'; |
134 | | - } |
135 | | - |
136 | | - if (!isset($discardRulePerPosition[$position])) { |
137 | | - $productLabels[$position][] = $rule->getLabelData($forProductPage); |
138 | | - } |
139 | | - |
140 | | - if ($rule->getDiscardSubsequentRules()) { |
141 | | - $discardRulePerPosition[$position] = true; |
142 | | - } |
143 | | - } |
144 | | - |
145 | | - if ($productLabelsCount >= $this->config->getLabelsPerProduct()) { |
146 | | - break; |
147 | | - } |
148 | | - } |
149 | | - |
150 | | - return $productLabels; |
151 | | - } |
152 | | -} |
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magefan (support@magefan.com). All rights reserved. |
| 4 | + * Please visit Magefan.com for license details (https://magefan.com/end-user-license-agreement). |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magefan\ProductLabel\Model; |
| 9 | + |
| 10 | +use Magefan\ProductLabel\Api\GetLabelsInterface; |
| 11 | +use Magefan\ProductLabel\Model\Config; |
| 12 | +use Magento\Store\Model\StoreManagerInterface; |
| 13 | +use Magefan\ProductLabel\Model\GetProductIdsToRuleIdsMap; |
| 14 | +use Magefan\ProductLabel\Model\ResourceModel\Rule\CollectionFactory as RuleCollectionFactory; |
| 15 | +use Magefan\ProductLabel\Api\LabelProcessorInterface; |
| 16 | +use Magento\Framework\View\LayoutInterface; |
| 17 | + |
| 18 | +class GetLabels implements GetLabelsInterface |
| 19 | +{ |
| 20 | + /** |
| 21 | + * @var Config |
| 22 | + */ |
| 23 | + protected $config; |
| 24 | + |
| 25 | + /** |
| 26 | + * @var StoreManagerInterface |
| 27 | + */ |
| 28 | + protected $storeManager; |
| 29 | + |
| 30 | + /** |
| 31 | + * @var GetProductIdsToRuleIdsMap |
| 32 | + */ |
| 33 | + protected $getProductIdsToRuleIdsMap; |
| 34 | + |
| 35 | + /** |
| 36 | + * @var RuleCollectionFactory |
| 37 | + */ |
| 38 | + protected $ruleCollectionFactory; |
| 39 | + |
| 40 | + /** |
| 41 | + * @var LabelProcessorInterface|null |
| 42 | + */ |
| 43 | + protected $labelProcessor; |
| 44 | + |
| 45 | + /** |
| 46 | + * @var LayoutInterface |
| 47 | + */ |
| 48 | + protected $layout; |
| 49 | + |
| 50 | + /** |
| 51 | + * GetLabels constructor. |
| 52 | + * @param \Magefan\ProductLabel\Model\Config $config |
| 53 | + * @param StoreManagerInterface $storeManager |
| 54 | + * @param \Magefan\ProductLabel\Model\GetProductIdsToRuleIdsMap $getProductIdsToRuleIdsMap |
| 55 | + * @param RuleCollectionFactory $ruleCollectionFactory |
| 56 | + * @param LayoutInterface $layout |
| 57 | + * @param LabelProcessorInterface|null $labelProcessor |
| 58 | + */ |
| 59 | + public function __construct( |
| 60 | + Config $config, |
| 61 | + StoreManagerInterface $storeManager, |
| 62 | + GetProductIdsToRuleIdsMap $getProductIdsToRuleIdsMap, |
| 63 | + RuleCollectionFactory $ruleCollectionFactory, |
| 64 | + LayoutInterface $layout, |
| 65 | + ?LabelProcessorInterface $labelProcessor = null |
| 66 | + ) { |
| 67 | + $this->config = $config; |
| 68 | + $this->storeManager = $storeManager; |
| 69 | + $this->getProductIdsToRuleIdsMap = $getProductIdsToRuleIdsMap; |
| 70 | + $this->ruleCollectionFactory = $ruleCollectionFactory; |
| 71 | + $this->labelProcessor = $labelProcessor; |
| 72 | + $this->layout = $layout; |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * @param array $productIds |
| 77 | + * @param array $productIdsForProductPage |
| 78 | + * @return array |
| 79 | + * @throws \Magento\Framework\Exception\NoSuchEntityException |
| 80 | + */ |
| 81 | + public function execute(array $productIds, array $productIdsForProductPage = []): array |
| 82 | + { |
| 83 | + [$ruleIds, $productIdRuleIds] = $this->getProductIdsToRuleIdsMap->execute($productIds); |
| 84 | + |
| 85 | + $rules = $this->ruleCollectionFactory->create() |
| 86 | + ->addActiveFilter() |
| 87 | + ->addFieldToFilter('id', ['in' => $ruleIds]) |
| 88 | + ->addStoreFilter($this->storeManager->getStore()->getId()) |
| 89 | + ->setOrder('priority', 'asc'); |
| 90 | + |
| 91 | + $replaceMap = []; |
| 92 | + |
| 93 | + foreach ($productIdRuleIds as $productId => $productRuleIds) { |
| 94 | + $forProductPage = in_array($productId, $productIdsForProductPage); |
| 95 | + |
| 96 | + $productLabels = $this->getAvailableProductLabels($rules, $productRuleIds, $forProductPage); |
| 97 | + |
| 98 | + $htmlToReplace = $this->layout |
| 99 | + ->createBlock(\Magefan\ProductLabel\Block\Label::class) |
| 100 | + ->setProductLabels($productLabels) |
| 101 | + ->setAdditionalCssClass($forProductPage ? 'mfpl-product-page' : '') |
| 102 | + ->toHtml(); |
| 103 | + |
| 104 | + if ($htmlToReplace) { |
| 105 | + $replaceMap[$productId] = $htmlToReplace; |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + if (null !== $this->labelProcessor) { |
| 110 | + $replaceMap = $this->labelProcessor->execute($replaceMap, $productIds); |
| 111 | + } |
| 112 | + |
| 113 | + return $replaceMap; |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * @param $rules |
| 118 | + * @param $productRuleIds |
| 119 | + * @param bool $forProductPage |
| 120 | + * @return array |
| 121 | + */ |
| 122 | + public function getAvailableProductLabels($rules, $productRuleIds, $forProductPage = false): array |
| 123 | + { |
| 124 | + $productLabelsCount = 0; |
| 125 | + $productLabels = []; |
| 126 | + |
| 127 | + $discardRulePerPosition = []; |
| 128 | + |
| 129 | + foreach ($rules as $rule) { |
| 130 | + if (in_array($rule->getId(), $productRuleIds)) { |
| 131 | + $productLabelsCount++; |
| 132 | + |
| 133 | + if ($forProductPage) { |
| 134 | + $position = $rule->getPpPosition() ?: $rule->getPosition(); |
| 135 | + } else { |
| 136 | + $position = $rule->getPosition() ?: 'top-left'; |
| 137 | + } |
| 138 | + |
| 139 | + if (!isset($discardRulePerPosition[$position])) { |
| 140 | + $productLabels[$position][] = $rule->getLabelData($forProductPage); |
| 141 | + } |
| 142 | + |
| 143 | + if ($rule->getDiscardSubsequentRules()) { |
| 144 | + $discardRulePerPosition[$position] = true; |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + if ($productLabelsCount >= $this->config->getLabelsPerProduct()) { |
| 149 | + break; |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + return $productLabels; |
| 154 | + } |
| 155 | +} |
0 commit comments