Skip to content

Commit a59f663

Browse files
authored
Merge pull request #27 from magefan/10790-release-2.4.0
10790 moved ui to basic
2 parents 50420da + 6cedd9f commit a59f663

19 files changed

Lines changed: 1006 additions & 584 deletions

File tree

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
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+
7+
declare(strict_types=1);
8+
9+
namespace Magefan\AutoRelatedProduct\Block\Adminhtml\Rule;
10+
11+
use Magefan\Community\Block\Adminhtml\Edit\GenericButton;
12+
use Magento\Framework\View\Element\UiComponent\Control\ButtonProviderInterface;
13+
14+
class DuplicateButton extends GenericButton implements ButtonProviderInterface
15+
{
16+
/**
17+
* @return array|string
18+
*/
19+
public function getButtonData()
20+
{
21+
$data = [];
22+
23+
if (!$this->authorization->isAllowed("Magefan_AutoRelatedProduct::rule")) {
24+
return $data;
25+
}
26+
27+
if ($this->getObjectId()) {
28+
$data = [
29+
'label' => __('Duplicate (Plus)'),
30+
'class' => 'duplicate',
31+
'on_click' => '(typeof versionsManager !== "undefined" && versionsManager._currentPlan == "Basic") ? versionsManager.showAlert("Plus or Extra") : window.location=\'' . $this->getDuplicateUrl() . '\'',
32+
'sort_order' => 40,
33+
];
34+
}
35+
return $data;
36+
}
37+
38+
/**
39+
* @return string
40+
*/
41+
public function getDuplicateUrl()
42+
{
43+
return $this->getUrl('mfautorp/*/duplicate', ['id' => $this->getObjectId()]);
44+
}
45+
}

Block/Adminhtml/TmpInfo.php

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
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+
}

Block/RelatedProductList.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,11 +217,11 @@ public function hasItems(): bool
217217
public function getIdentities()
218218
{
219219
$identities = [];
220-
220+
221221
if ($this->getProduct()) {
222222
$identities = [Product::CACHE_TAG . '_' . $this->getProduct()->getId()];
223223
}
224-
224+
225225
if (count($this->getItems())) {
226226
foreach ($this->getItems() as $item) {
227227
foreach ($item->getIdentities() as $identity) {
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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+
7+
declare(strict_types=1);
8+
9+
namespace Magefan\AutoRelatedProduct\Model\Config\Source;
10+
11+
class RelatedTemplate implements \Magento\Framework\Option\ArrayInterface
12+
{
13+
/**
14+
* @const string
15+
*/
16+
const DEFAULT = 'Magento_Catalog::product/list/items.phtml';
17+
18+
/**
19+
* @const string
20+
*/
21+
const COMPARE = 'Magefan_AutoRelatedProductExtra::product/list/compare.phtml';
22+
23+
/**
24+
* @const string
25+
*/
26+
const FBT = 'Magefan_AutoRelatedProductExtra::product/list/frequently-bought-together.phtml';
27+
28+
const CUSTOM = 'custom';
29+
30+
const DEFAULT_TEMPLATES = [
31+
self::DEFAULT,
32+
self::COMPARE,
33+
self::FBT
34+
];
35+
36+
/**
37+
* Options
38+
*
39+
* @return array
40+
*/
41+
public function toOptionArray()
42+
{
43+
return [
44+
['value' => self::DEFAULT, 'label' => __('Default Related Template')],
45+
['value' => self::COMPARE, 'label' => __('Compare Template (Extra)')],
46+
['value' => self::FBT, 'label' => __('Frequently Bought Together Template (Extra)')],
47+
['value' => self::CUSTOM, 'label' => __(' - Set Custom Template (Plus) - ')],
48+
];
49+
}
50+
51+
/**
52+
* Get options in "key-value" format
53+
*
54+
* @return array
55+
*/
56+
public function toArray()
57+
{
58+
$array = [];
59+
foreach ($this->toOptionArray() as $item) {
60+
$array[$item['value']] = $item['label'];
61+
}
62+
return $array;
63+
}
64+
}

Model/Config/Source/SortBy.php

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ class SortBy implements \Magento\Framework\Data\OptionSourceInterface
2020
const PRICE_DESC = 4;
2121
const PRICE_ASC = 5;
2222

23+
const BEST_PR_WEEK = 100;
24+
const BEST_PR_MONTH = 101;
25+
const BEST_PR_QUARTER = 102;
26+
const BEST_PR_YEAR = 103;
27+
2328
/**
2429
* @return array[]
2530
*/
@@ -28,10 +33,15 @@ public function toOptionArray():array
2833
return [
2934
['value' => self::NONE, 'label' => __('None')],
3035
['value' => self::RANDOM, 'label' => __('Random')],
31-
['value' => self::NAME, 'label' => __('Name')],
32-
['value' => self::NEWEST, 'label' => __('Newest')],
33-
['value' => self::PRICE_DESC, 'label' => __('Price (high to low)')],
34-
['value' => self::PRICE_ASC, 'label' => __('Price (low to high)')],
36+
['value' => self::NAME, 'label' => __('Name (Plus)')],
37+
['value' => self::NEWEST, 'label' => __('Newest (Plus)')],
38+
['value' => self::PRICE_DESC, 'label' => __('Price (high to low) (Plus)')],
39+
['value' => self::PRICE_ASC, 'label' => __('Price (low to high) (Plus)')],
40+
['value' => self::BEST_PR_WEEK, 'label' => __('Best Sellers (QTY) Per Week (Plus)')],
41+
['value' => self::BEST_PR_MONTH, 'label' => __('Best Sellers (QTY) Per Month (Plus)')],
42+
['value' => self::BEST_PR_QUARTER, 'label' => __('Best Sellers (QTY) Per Three Months (Plus)')],
43+
['value' => self::BEST_PR_YEAR, 'label' => __(' Best Sellers (QTY) Per Year (Plus)')],
3544
];
3645
}
46+
3747
}

Model/ResourceModel/Rule.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,20 @@ public function updateLinks(AbstractModel $object, array $newRelatedIds, array $
213213

214214
protected function _beforeSave(AbstractModel $object)
215215
{
216+
if (is_array($object->getData('customer_group_ids'))) {
217+
$object->setData('customer_group_ids', implode(',', $object->getData('customer_group_ids')));
218+
}
219+
220+
if (is_array($object->getData('category_ids'))) {
221+
$arr = $object->getData('category_ids');
222+
223+
if ($arr[0] == '') {
224+
unset($arr[0]);
225+
}
226+
227+
$object->setData('category_ids', implode(',', $arr));
228+
}
229+
216230
/* Conditions */
217231
if ($object->getRule('conditions')) {
218232
$catalogRule = $this->catalogRuleFactory->create();

Model/Rule.php

Lines changed: 5 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -126,29 +126,7 @@ public function getStatus()
126126
return $this->getData('status');
127127
}
128128

129-
/**
130-
* @return int
131-
*/
132-
public function getIsFromOneCategory() : int
133-
{
134-
return (int)$this->getData('from_one_category_only');
135-
}
136129

137-
/**
138-
* @return int
139-
*/
140-
public function getIsOnlyWithHigherPrice() : int
141-
{
142-
return (int)$this->getData('only_with_higher_price');
143-
}
144-
145-
/**
146-
* @return int
147-
*/
148-
public function getIsOnlyWithLowerPrice() : int
149-
{
150-
return (int)$this->getData('only_with_lower_price');
151-
}
152130

153131
/**
154132
* @return false|string|string[]|null
@@ -230,17 +208,17 @@ public function isVisibleOnStore($storeId): bool
230208
*/
231209
public function getRuleBlockIdentifier(): string
232210
{
233-
$indentifire = $this->getBlockPosition();
211+
$identifier = $this->getBlockPosition();
234212

235-
if ((0 !== $this->getIsFromOneCategory() || 0 !== $this->getIsOnlyWithHigherPrice()) && 'custom' != $this->getBlockPosition()) {
236-
$indentifire .= '_' . '1';
213+
if ((0 !== $this->getData('from_one_category_only') || 0 !== $this->getData('only_with_higher_price')) && 'custom' != $this->getBlockPosition()) {
214+
$identifier .= '_' . '1';
237215

238216
}
239217
if ($this->getId()) {
240-
$indentifire .= '_' . $this->getId();
218+
$identifier .= '_' . $this->getId();
241219
}
242220

243-
return $indentifire;
221+
return $identifier;
244222
}
245223

246224
/**

0 commit comments

Comments
 (0)