Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AbstractGroup.php
Go to the documentation of this file.
1 <?php
7 
12 
18 abstract class AbstractGroup extends Widget implements RendererInterface
19 {
25  protected $_element;
26 
32  protected $_customerGroups;
33 
39  protected $_websites;
40 
46  protected $moduleManager;
47 
53  protected $_coreRegistry = null;
54 
58  protected $_directoryHelper;
59 
63  protected $_groupRepository;
64 
68  protected $_groupManagement;
69 
74 
78  protected $_localeCurrency;
79 
91  public function __construct(
92  \Magento\Backend\Block\Template\Context $context,
94  \Magento\Directory\Helper\Data $directoryHelper,
95  \Magento\Framework\Module\Manager $moduleManager,
96  \Magento\Framework\Registry $registry,
97  GroupManagementInterface $groupManagement,
98  \Magento\Framework\Api\SearchCriteriaBuilder $searchCriteriaBuilder,
99  \Magento\Framework\Locale\CurrencyInterface $localeCurrency,
100  array $data = []
101  ) {
102  $this->_groupRepository = $groupRepository;
103  $this->_directoryHelper = $directoryHelper;
104  $this->moduleManager = $moduleManager;
105  $this->_coreRegistry = $registry;
106  $this->_groupManagement = $groupManagement;
107  $this->_searchCriteriaBuilder = $searchCriteriaBuilder;
108  $this->_localeCurrency = $localeCurrency;
109  parent::__construct($context, $data);
110  }
111 
117  public function getProduct()
118  {
119  return $this->_coreRegistry->registry('product');
120  }
121 
128  public function render(\Magento\Framework\Data\Form\Element\AbstractElement $element)
129  {
130  $this->setElement($element);
131  return $this->toHtml();
132  }
133 
140  public function setElement(\Magento\Framework\Data\Form\Element\AbstractElement $element)
141  {
142  $this->_element = $element;
143  return $this;
144  }
145 
151  public function getElement()
152  {
153  return $this->_element;
154  }
155 
161  public function getValues()
162  {
163  $values = [];
164  $data = $this->getElement()->getValue();
165 
166  if (is_array($data)) {
167  $values = $this->_sortValues($data);
168  }
169 
170  $currency = $this->_localeCurrency->getCurrency($this->_directoryHelper->getBaseCurrencyCode());
171 
172  foreach ($values as &$value) {
173  $value['readonly'] = $value['website_id'] == 0 &&
174  $this->isShowWebsiteColumn() &&
175  !$this->isAllowChangeWebsite();
176  $value['price'] =
177  $currency->toCurrency($value['price'], ['display' => \Magento\Framework\Currency::NO_SYMBOL]);
178  }
179 
180  return $values;
181  }
182 
189  protected function _sortValues($data)
190  {
191  return $data;
192  }
193 
200  public function getCustomerGroups($groupId = null)
201  {
202  if ($this->_customerGroups === null) {
203  if (!$this->moduleManager->isEnabled('Magento_Customer')) {
204  return [];
205  }
206  $this->_customerGroups = $this->_getInitialCustomerGroups();
208  $groups = $this->_groupRepository->getList($this->_searchCriteriaBuilder->create());
209  foreach ($groups->getItems() as $group) {
210  $this->_customerGroups[$group->getId()] = $group->getCode();
211  }
212  }
213 
214  if ($groupId !== null) {
215  return isset($this->_customerGroups[$groupId]) ? $this->_customerGroups[$groupId] : [];
216  }
217 
218  return $this->_customerGroups;
219  }
220 
226  protected function _getInitialCustomerGroups()
227  {
228  return [];
229  }
230 
236  public function getWebsiteCount()
237  {
238  return count($this->getWebsites());
239  }
240 
246  public function isMultiWebsites()
247  {
248  return !$this->_storeManager->isSingleStoreMode();
249  }
250 
256  public function getWebsites()
257  {
258  if ($this->_websites !== null) {
259  return $this->_websites;
260  }
261 
262  $this->_websites = [
263  0 => ['name' => __('All Websites'), 'currency' => $this->_directoryHelper->getBaseCurrencyCode()]
264  ];
265 
266  if (!$this->isScopeGlobal() && $this->getProduct()->getStoreId()) {
268  $website = $this->_storeManager->getStore($this->getProduct()->getStoreId())->getWebsite();
269 
270  $this->_websites[$website->getId()] = [
271  'name' => $website->getName(),
272  'currency' => $website->getBaseCurrencyCode()
273  ];
274  } elseif (!$this->isScopeGlobal()) {
275  $websites = $this->_storeManager->getWebsites();
276  $productWebsiteIds = $this->getProduct()->getWebsiteIds();
277  foreach ($websites as $website) {
279  if (!in_array($website->getId(), $productWebsiteIds)) {
280  continue;
281  }
282  $this->_websites[$website->getId()] = [
283  'name' => $website->getName(),
284  'currency' => $website->getBaseCurrencyCode()
285  ];
286  }
287  }
288 
289  return $this->_websites;
290  }
291 
297  public function getDefaultCustomerGroup()
298  {
299  return $this->_groupManagement->getAllCustomersGroup()->getId();
300  }
301 
307  public function getDefaultWebsite()
308  {
309  if ($this->isShowWebsiteColumn() && !$this->isAllowChangeWebsite()) {
310  return $this->_storeManager->getStore($this->getProduct()->getStoreId())->getWebsiteId();
311  }
312  return 0;
313  }
314 
320  public function getAddButtonHtml()
321  {
322  return $this->getChildHtml('add_button');
323  }
324 
331  public function getPriceColumnHeader($default)
332  {
333  if ($this->hasData('price_column_header')) {
334  return $this->getData('price_column_header');
335  } else {
336  return $default;
337  }
338  }
339 
346  public function getPriceValidation($default)
347  {
348  if ($this->hasData('price_validation')) {
349  return $this->getData('price_validation');
350  } else {
351  return $default;
352  }
353  }
354 
360  public function getAttribute()
361  {
362  return $this->getElement()->getEntityAttribute();
363  }
364 
370  public function isScopeGlobal()
371  {
372  return $this->getAttribute()->isScopeGlobal();
373  }
374 
380  public function isShowWebsiteColumn()
381  {
382  if ($this->isScopeGlobal() || $this->_storeManager->isSingleStoreMode()) {
383  return false;
384  }
385  return true;
386  }
387 
393  public function isAllowChangeWebsite()
394  {
395  if (!$this->isShowWebsiteColumn() || $this->getProduct()->getStoreId()) {
396  return false;
397  }
398  return true;
399  }
400 }
$groupRepository
getData($key='', $index=null)
Definition: DataObject.php:119
elseif(isset( $params[ 'redirect_parent']))
Definition: iframe.phtml:17
render(\Magento\Framework\Data\Form\Element\AbstractElement $element)
__construct(\Magento\Backend\Block\Template\Context $context, GroupRepositoryInterface $groupRepository, \Magento\Directory\Helper\Data $directoryHelper, \Magento\Framework\Module\Manager $moduleManager, \Magento\Framework\Registry $registry, GroupManagementInterface $groupManagement, \Magento\Framework\Api\SearchCriteriaBuilder $searchCriteriaBuilder, \Magento\Framework\Locale\CurrencyInterface $localeCurrency, array $data=[])
setElement(\Magento\Framework\Data\Form\Element\AbstractElement $element)
$group
Definition: sections.phtml:16
$values
Definition: options.phtml:88
__()
Definition: __.php:13
$value
Definition: gender.phtml:16
$searchCriteriaBuilder
$element
Definition: element.phtml:12