|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | + |
| 7 | +namespace Magento\Catalog\Ui\DataProvider\Product\Form\Modifier; |
| 8 | + |
| 9 | +use Magento\Catalog\Block\Adminhtml\Product\Edit\Tab\Alerts\Price; |
| 10 | +use Magento\Catalog\Block\Adminhtml\Product\Edit\Tab\Alerts\Stock; |
| 11 | +use Magento\Framework\App\Config\ScopeConfigInterface; |
| 12 | +use Magento\Framework\View\LayoutFactory; |
| 13 | +use Magento\Store\Model\ScopeInterface; |
| 14 | +use Magento\Ui\Component\Form\Fieldset; |
| 15 | + |
| 16 | +class Alerts extends AbstractModifier |
| 17 | +{ |
| 18 | + const DATA_SCOPE = 'data'; |
| 19 | + const DATA_SCOPE_STOCK = 'stock'; |
| 20 | + const DATA_SCOPE_PRICE = 'price'; |
| 21 | + |
| 22 | + /** |
| 23 | + * @var string |
| 24 | + */ |
| 25 | + private static $previousGroup = 'related'; |
| 26 | + |
| 27 | + /** |
| 28 | + * @var int |
| 29 | + */ |
| 30 | + private static $sortOrder = 110; |
| 31 | + |
| 32 | + /** |
| 33 | + * @var ScopeConfigInterface |
| 34 | + */ |
| 35 | + private $scopeConfig; |
| 36 | + |
| 37 | + /** |
| 38 | + * @var LayoutFactory |
| 39 | + */ |
| 40 | + private $layoutFactory; |
| 41 | + |
| 42 | + /** |
| 43 | + * Alerts constructor. |
| 44 | + * @param ScopeConfigInterface $scopeConfig |
| 45 | + * @param LayoutFactory $layoutFactory |
| 46 | + */ |
| 47 | + public function __construct( |
| 48 | + ScopeConfigInterface $scopeConfig, |
| 49 | + LayoutFactory $layoutFactory |
| 50 | + ) { |
| 51 | + $this->scopeConfig = $scopeConfig; |
| 52 | + $this->layoutFactory = $layoutFactory; |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * {@inheritdoc} |
| 57 | + * @since 101.0.0 |
| 58 | + */ |
| 59 | + public function modifyData(array $data) |
| 60 | + { |
| 61 | + return $data; |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * {@inheritdoc} |
| 66 | + * @since 101.0.0 |
| 67 | + */ |
| 68 | + public function modifyMeta(array $meta) |
| 69 | + { |
| 70 | + if (!$this->canShowTab()) { |
| 71 | + return $meta; |
| 72 | + } |
| 73 | + |
| 74 | + $meta = array_replace_recursive( |
| 75 | + $meta, |
| 76 | + [ |
| 77 | + 'alerts' => [ |
| 78 | + 'arguments' => [ |
| 79 | + 'data' => [ |
| 80 | + 'config' => [ |
| 81 | + 'additionalClasses' => 'admin__fieldset-section', |
| 82 | + 'label' => __('Product Alerts'), |
| 83 | + 'collapsible' => true, |
| 84 | + 'componentType' => Fieldset::NAME, |
| 85 | + 'dataScope' => static::DATA_SCOPE, |
| 86 | + 'sortOrder' => |
| 87 | + $this->getNextGroupSortOrder( |
| 88 | + $meta, |
| 89 | + self::$previousGroup, |
| 90 | + self::$sortOrder |
| 91 | + ), |
| 92 | + ], |
| 93 | + ], |
| 94 | + ], |
| 95 | + 'children' => [ |
| 96 | + static::DATA_SCOPE_STOCK => $this->getAlertStockFieldset(), |
| 97 | + static::DATA_SCOPE_PRICE => $this->getAlertPriceFieldset() |
| 98 | + ], |
| 99 | + ], |
| 100 | + ] |
| 101 | + ); |
| 102 | + |
| 103 | + return $meta; |
| 104 | + } |
| 105 | + |
| 106 | + /** |
| 107 | + * @return bool |
| 108 | + */ |
| 109 | + private function canShowTab() |
| 110 | + { |
| 111 | + $alertPriceAllow = $this->scopeConfig->getValue( |
| 112 | + 'catalog/productalert/allow_price', |
| 113 | + ScopeInterface::SCOPE_STORE |
| 114 | + ); |
| 115 | + $alertStockAllow = $this->scopeConfig->getValue( |
| 116 | + 'catalog/productalert/allow_stock', |
| 117 | + ScopeInterface::SCOPE_STORE |
| 118 | + ); |
| 119 | + |
| 120 | + return ($alertPriceAllow || $alertStockAllow); |
| 121 | + } |
| 122 | + |
| 123 | + /** |
| 124 | + * Prepares config for the alert stock products fieldset |
| 125 | + * @return array |
| 126 | + */ |
| 127 | + private function getAlertStockFieldset() |
| 128 | + { |
| 129 | + return [ |
| 130 | + 'arguments' => [ |
| 131 | + 'data' => [ |
| 132 | + 'config' => [ |
| 133 | + 'label' => __('Alert stock'), |
| 134 | + 'componentType' => 'container', |
| 135 | + 'component' => 'Magento_Ui/js/form/components/html', |
| 136 | + 'additionalClasses' => 'admin__fieldset-note', |
| 137 | + 'content' => |
| 138 | + '<h4>' . __('Alert Stock') . '</h4>' . |
| 139 | + $this->layoutFactory->create()->createBlock( |
| 140 | + Stock::class |
| 141 | + )->toHtml(), |
| 142 | + ] |
| 143 | + ] |
| 144 | + ] |
| 145 | + ]; |
| 146 | + } |
| 147 | + |
| 148 | + /** |
| 149 | + * Prepares config for the alert price products fieldset |
| 150 | + * @return array |
| 151 | + */ |
| 152 | + private function getAlertPriceFieldset() |
| 153 | + { |
| 154 | + return [ |
| 155 | + 'arguments' => [ |
| 156 | + 'data' => [ |
| 157 | + 'config' => [ |
| 158 | + 'label' => __('Alert price'), |
| 159 | + 'componentType' => 'container', |
| 160 | + 'component' => 'Magento_Ui/js/form/components/html', |
| 161 | + 'additionalClasses' => 'admin__fieldset-note', |
| 162 | + 'content' => |
| 163 | + '<h4>' . __('Alert Price') . '</h4>' . |
| 164 | + $this->layoutFactory->create()->createBlock( |
| 165 | + Price::class |
| 166 | + )->toHtml(), |
| 167 | + ] |
| 168 | + ] |
| 169 | + ] |
| 170 | + ]; |
| 171 | + } |
| 172 | +} |
0 commit comments