|
| 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 | +} |
0 commit comments