|
| 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\AutoRelatedProduct\Block\Adminhtml; |
| 9 | + |
| 10 | +use Magento\Backend\Block\Template\Context; |
| 11 | +use Magefan\AutoRelatedProduct\Api\ConfigInterface; |
| 12 | +use Magefan\Community\Api\GetModuleVersionInterface; |
| 13 | +use Magefan\AutoRelatedProduct\Api\RelatedCollectionInterfaceFactory; |
| 14 | +use Magento\Framework\Session\SessionManagerInterface; |
| 15 | +use Magefan\AutoRelatedProduct\Model\Config\Source\SortBy; |
| 16 | + |
| 17 | +class TmpInfo extends \Magento\Backend\Block\Template |
| 18 | +{ |
| 19 | + /** |
| 20 | + * @var \Magefan\AutoRelatedProduct\Model\Config |
| 21 | + */ |
| 22 | + protected $config; |
| 23 | + |
| 24 | + /** |
| 25 | + * @var GetModuleVersionInterface |
| 26 | + */ |
| 27 | + protected $getModuleVersion; |
| 28 | + |
| 29 | + /** |
| 30 | + * @var RelatedCollectionInterfaceFactory |
| 31 | + */ |
| 32 | + protected $ruleCollection; |
| 33 | + |
| 34 | + /** |
| 35 | + * @param Context $context |
| 36 | + * @param ConfigInterface $config |
| 37 | + * @param GetModuleVersionInterface $getModuleVersion |
| 38 | + * @param RelatedCollectionInterfaceFactory $ruleCollection |
| 39 | + * @param array $data |
| 40 | + */ |
| 41 | + public function __construct( |
| 42 | + Context $context, |
| 43 | + ConfigInterface $config, |
| 44 | + GetModuleVersionInterface $getModuleVersion, |
| 45 | + RelatedCollectionInterfaceFactory $ruleCollection, |
| 46 | + SessionManagerInterface $session, |
| 47 | + array $data = [] |
| 48 | + ) { |
| 49 | + $this->config = $config; |
| 50 | + $this->getModuleVersion = $getModuleVersion; |
| 51 | + $this->ruleCollection = $ruleCollection; |
| 52 | + $this->session = $session; |
| 53 | + parent::__construct($context, $data); |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * @return bool |
| 58 | + */ |
| 59 | + public function isSomeFeaturesRestricted(): bool |
| 60 | + { |
| 61 | + if ($this->getModuleVersion->execute('Magefan_AutoRelatedProductExtra') || $this->getModuleVersion->execute('Magefan_AutoRelatedProductPlus')) { |
| 62 | + return true; |
| 63 | + } |
| 64 | + |
| 65 | + return false; |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * @return string |
| 70 | + */ |
| 71 | + public function getAffectedRulesByDisplayModes(): string |
| 72 | + { |
| 73 | + $rules = $this->ruleCollection->create() |
| 74 | + ->addFieldToFilter('status', 1); |
| 75 | + |
| 76 | + $connection = $rules->getConnection(); |
| 77 | + $tableName = $rules->getMainTable(); |
| 78 | + |
| 79 | + $conditions = []; |
| 80 | + |
| 81 | + if ($connection->tableColumnExists($tableName, 'from_one_category_only')) { |
| 82 | + $conditions[] = 'from_one_category_only = 1'; |
| 83 | + } |
| 84 | + |
| 85 | + if ($connection->tableColumnExists($tableName, 'only_with_higher_price')) { |
| 86 | + $conditions[] = 'only_with_higher_price = 1'; |
| 87 | + } |
| 88 | + |
| 89 | + if ($connection->tableColumnExists($tableName, 'only_with_lower_price')) { |
| 90 | + $conditions[] = 'only_with_lower_price = 1'; |
| 91 | + } |
| 92 | + |
| 93 | + if (!empty($conditions)) { |
| 94 | + $rules->getSelect()->where(implode(' OR ', $conditions)); |
| 95 | + } |
| 96 | + |
| 97 | + return implode(',', $rules->getAllIds()); |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * @return string |
| 102 | + */ |
| 103 | + public function getAffectedRulesBySortBy(): string |
| 104 | + { |
| 105 | + $restrictedSortByOptionsIds = [ |
| 106 | + SortBy::NAME, |
| 107 | + SortBy::NEWEST, |
| 108 | + SortBy::PRICE_DESC, |
| 109 | + SortBy::PRICE_ASC |
| 110 | + ]; |
| 111 | + |
| 112 | + $rules = $this->ruleCollection->create() |
| 113 | + ->addFieldToFilter('status', 1) |
| 114 | + ->addFieldToFilter('sort_by', ['in' => $restrictedSortByOptionsIds]); |
| 115 | + |
| 116 | + return implode(',', $rules->getAllIds()); |
| 117 | + } |
| 118 | + |
| 119 | + /** |
| 120 | + * @return string |
| 121 | + */ |
| 122 | + public function toHtml() |
| 123 | + { |
| 124 | + if (!$this->config->isEnabled() || $this->session->getIsNeedToShowAlert() === false) { |
| 125 | + return ''; |
| 126 | + } |
| 127 | + |
| 128 | + $this->session->setIsNeedToShowAlert( |
| 129 | + !$this->isSomeFeaturesRestricted() && ($this->getAffectedRulesByDisplayModes() || $this->getAffectedRulesBySortBy()) |
| 130 | + ); |
| 131 | + |
| 132 | + return parent::_toHtml(); |
| 133 | + } |
| 134 | +} |
0 commit comments