Skip to content

Commit

Permalink
Merge pull request #27 from magefan/10790-release-2.4.0
Browse files Browse the repository at this point in the history
10790 moved ui to basic
  • Loading branch information
magefan authored Jul 17, 2024
2 parents 50420da + 6cedd9f commit a59f663
Show file tree
Hide file tree
Showing 19 changed files with 1,006 additions and 584 deletions.
45 changes: 45 additions & 0 deletions Block/Adminhtml/Rule/DuplicateButton.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php
/**
* Copyright © Magefan ([email protected]). All rights reserved.
* Please visit Magefan.com for license details (https://magefan.com/end-user-license-agreement).
*/

declare(strict_types=1);

namespace Magefan\AutoRelatedProduct\Block\Adminhtml\Rule;

use Magefan\Community\Block\Adminhtml\Edit\GenericButton;
use Magento\Framework\View\Element\UiComponent\Control\ButtonProviderInterface;

class DuplicateButton extends GenericButton implements ButtonProviderInterface
{
/**
* @return array|string
*/
public function getButtonData()
{
$data = [];

if (!$this->authorization->isAllowed("Magefan_AutoRelatedProduct::rule")) {
return $data;
}

if ($this->getObjectId()) {
$data = [
'label' => __('Duplicate (Plus)'),
'class' => 'duplicate',
'on_click' => '(typeof versionsManager !== "undefined" && versionsManager._currentPlan == "Basic") ? versionsManager.showAlert("Plus or Extra") : window.location=\'' . $this->getDuplicateUrl() . '\'',
'sort_order' => 40,
];
}
return $data;
}

/**
* @return string
*/
public function getDuplicateUrl()
{
return $this->getUrl('mfautorp/*/duplicate', ['id' => $this->getObjectId()]);
}
}
134 changes: 134 additions & 0 deletions Block/Adminhtml/TmpInfo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
<?php
/**
* Copyright © Magefan ([email protected]). All rights reserved.
* Please visit Magefan.com for license details (https://magefan.com/end-user-license-agreement).
*/
declare(strict_types=1);

namespace Magefan\AutoRelatedProduct\Block\Adminhtml;

use Magento\Backend\Block\Template\Context;
use Magefan\AutoRelatedProduct\Api\ConfigInterface;
use Magefan\Community\Api\GetModuleVersionInterface;
use Magefan\AutoRelatedProduct\Api\RelatedCollectionInterfaceFactory;
use Magento\Framework\Session\SessionManagerInterface;
use Magefan\AutoRelatedProduct\Model\Config\Source\SortBy;

class TmpInfo extends \Magento\Backend\Block\Template
{
/**
* @var \Magefan\AutoRelatedProduct\Model\Config
*/
protected $config;

/**
* @var GetModuleVersionInterface
*/
protected $getModuleVersion;

/**
* @var RelatedCollectionInterfaceFactory
*/
protected $ruleCollection;

/**
* @param Context $context
* @param ConfigInterface $config
* @param GetModuleVersionInterface $getModuleVersion
* @param RelatedCollectionInterfaceFactory $ruleCollection
* @param array $data
*/
public function __construct(
Context $context,
ConfigInterface $config,
GetModuleVersionInterface $getModuleVersion,
RelatedCollectionInterfaceFactory $ruleCollection,
SessionManagerInterface $session,
array $data = []
) {
$this->config = $config;
$this->getModuleVersion = $getModuleVersion;
$this->ruleCollection = $ruleCollection;
$this->session = $session;
parent::__construct($context, $data);
}

/**
* @return bool
*/
public function isSomeFeaturesRestricted(): bool
{
if ($this->getModuleVersion->execute('Magefan_AutoRelatedProductExtra') || $this->getModuleVersion->execute('Magefan_AutoRelatedProductPlus')) {
return true;
}

return false;
}

/**
* @return string
*/
public function getAffectedRulesByDisplayModes(): string
{
$rules = $this->ruleCollection->create()
->addFieldToFilter('status', 1);

$connection = $rules->getConnection();
$tableName = $rules->getMainTable();

$conditions = [];

if ($connection->tableColumnExists($tableName, 'from_one_category_only')) {
$conditions[] = 'from_one_category_only = 1';
}

if ($connection->tableColumnExists($tableName, 'only_with_higher_price')) {
$conditions[] = 'only_with_higher_price = 1';
}

if ($connection->tableColumnExists($tableName, 'only_with_lower_price')) {
$conditions[] = 'only_with_lower_price = 1';
}

if (!empty($conditions)) {
$rules->getSelect()->where(implode(' OR ', $conditions));
}

return implode(',', $rules->getAllIds());
}

/**
* @return string
*/
public function getAffectedRulesBySortBy(): string
{
$restrictedSortByOptionsIds = [
SortBy::NAME,
SortBy::NEWEST,
SortBy::PRICE_DESC,
SortBy::PRICE_ASC
];

$rules = $this->ruleCollection->create()
->addFieldToFilter('status', 1)
->addFieldToFilter('sort_by', ['in' => $restrictedSortByOptionsIds]);

return implode(',', $rules->getAllIds());
}

/**
* @return string
*/
public function toHtml()
{
if (!$this->config->isEnabled() || $this->session->getIsNeedToShowAlert() === false) {
return '';
}

$this->session->setIsNeedToShowAlert(
!$this->isSomeFeaturesRestricted() && ($this->getAffectedRulesByDisplayModes() || $this->getAffectedRulesBySortBy())
);

return parent::_toHtml();
}
}
4 changes: 2 additions & 2 deletions Block/RelatedProductList.php
Original file line number Diff line number Diff line change
Expand Up @@ -217,11 +217,11 @@ public function hasItems(): bool
public function getIdentities()
{
$identities = [];

if ($this->getProduct()) {
$identities = [Product::CACHE_TAG . '_' . $this->getProduct()->getId()];
}

if (count($this->getItems())) {
foreach ($this->getItems() as $item) {
foreach ($item->getIdentities() as $identity) {
Expand Down
64 changes: 64 additions & 0 deletions Model/Config/Source/RelatedTemplate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php
/**
* Copyright © Magefan ([email protected]). All rights reserved.
* Please visit Magefan.com for license details (https://magefan.com/end-user-license-agreement).
*/

declare(strict_types=1);

namespace Magefan\AutoRelatedProduct\Model\Config\Source;

class RelatedTemplate implements \Magento\Framework\Option\ArrayInterface
{
/**
* @const string
*/
const DEFAULT = 'Magento_Catalog::product/list/items.phtml';

/**
* @const string
*/
const COMPARE = 'Magefan_AutoRelatedProductExtra::product/list/compare.phtml';

/**
* @const string
*/
const FBT = 'Magefan_AutoRelatedProductExtra::product/list/frequently-bought-together.phtml';

const CUSTOM = 'custom';

const DEFAULT_TEMPLATES = [
self::DEFAULT,
self::COMPARE,
self::FBT
];

/**
* Options
*
* @return array
*/
public function toOptionArray()
{
return [
['value' => self::DEFAULT, 'label' => __('Default Related Template')],
['value' => self::COMPARE, 'label' => __('Compare Template (Extra)')],
['value' => self::FBT, 'label' => __('Frequently Bought Together Template (Extra)')],
['value' => self::CUSTOM, 'label' => __(' - Set Custom Template (Plus) - ')],
];
}

/**
* Get options in "key-value" format
*
* @return array
*/
public function toArray()
{
$array = [];
foreach ($this->toOptionArray() as $item) {
$array[$item['value']] = $item['label'];
}
return $array;
}
}
18 changes: 14 additions & 4 deletions Model/Config/Source/SortBy.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ class SortBy implements \Magento\Framework\Data\OptionSourceInterface
const PRICE_DESC = 4;
const PRICE_ASC = 5;

const BEST_PR_WEEK = 100;
const BEST_PR_MONTH = 101;
const BEST_PR_QUARTER = 102;
const BEST_PR_YEAR = 103;

/**
* @return array[]
*/
Expand All @@ -28,10 +33,15 @@ public function toOptionArray():array
return [
['value' => self::NONE, 'label' => __('None')],
['value' => self::RANDOM, 'label' => __('Random')],
['value' => self::NAME, 'label' => __('Name')],
['value' => self::NEWEST, 'label' => __('Newest')],
['value' => self::PRICE_DESC, 'label' => __('Price (high to low)')],
['value' => self::PRICE_ASC, 'label' => __('Price (low to high)')],
['value' => self::NAME, 'label' => __('Name (Plus)')],
['value' => self::NEWEST, 'label' => __('Newest (Plus)')],
['value' => self::PRICE_DESC, 'label' => __('Price (high to low) (Plus)')],
['value' => self::PRICE_ASC, 'label' => __('Price (low to high) (Plus)')],
['value' => self::BEST_PR_WEEK, 'label' => __('Best Sellers (QTY) Per Week (Plus)')],
['value' => self::BEST_PR_MONTH, 'label' => __('Best Sellers (QTY) Per Month (Plus)')],
['value' => self::BEST_PR_QUARTER, 'label' => __('Best Sellers (QTY) Per Three Months (Plus)')],
['value' => self::BEST_PR_YEAR, 'label' => __(' Best Sellers (QTY) Per Year (Plus)')],
];
}

}
14 changes: 14 additions & 0 deletions Model/ResourceModel/Rule.php
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,20 @@ public function updateLinks(AbstractModel $object, array $newRelatedIds, array $

protected function _beforeSave(AbstractModel $object)
{
if (is_array($object->getData('customer_group_ids'))) {
$object->setData('customer_group_ids', implode(',', $object->getData('customer_group_ids')));
}

if (is_array($object->getData('category_ids'))) {
$arr = $object->getData('category_ids');

if ($arr[0] == '') {
unset($arr[0]);
}

$object->setData('category_ids', implode(',', $arr));
}

/* Conditions */
if ($object->getRule('conditions')) {
$catalogRule = $this->catalogRuleFactory->create();
Expand Down
32 changes: 5 additions & 27 deletions Model/Rule.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,29 +126,7 @@ public function getStatus()
return $this->getData('status');
}

/**
* @return int
*/
public function getIsFromOneCategory() : int
{
return (int)$this->getData('from_one_category_only');
}

/**
* @return int
*/
public function getIsOnlyWithHigherPrice() : int
{
return (int)$this->getData('only_with_higher_price');
}

/**
* @return int
*/
public function getIsOnlyWithLowerPrice() : int
{
return (int)$this->getData('only_with_lower_price');
}

/**
* @return false|string|string[]|null
Expand Down Expand Up @@ -230,17 +208,17 @@ public function isVisibleOnStore($storeId): bool
*/
public function getRuleBlockIdentifier(): string
{
$indentifire = $this->getBlockPosition();
$identifier = $this->getBlockPosition();

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

}
if ($this->getId()) {
$indentifire .= '_' . $this->getId();
$identifier .= '_' . $this->getId();
}

return $indentifire;
return $identifier;
}

/**
Expand Down
Loading

0 comments on commit a59f663

Please sign in to comment.