Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Tax.php
Go to the documentation of this file.
1 <?php
8 
10 
14 class Tax extends \Magento\Backend\Block\Widget implements
15  \Magento\Framework\Data\Form\Element\Renderer\RendererInterface
16 {
20  protected $_element = null;
21 
25  protected $_countries = null;
26 
30  protected $_websites = null;
31 
35  protected $_template = 'Magento_Weee::renderer/tax.phtml';
36 
42  protected $_coreRegistry;
43 
47  protected $_sourceCountry;
48 
52  protected $_directoryHelper;
53 
61  public function __construct(
62  \Magento\Backend\Block\Template\Context $context,
63  \Magento\Directory\Model\Config\Source\Country $sourceCountry,
64  \Magento\Directory\Helper\Data $directoryHelper,
65  \Magento\Framework\Registry $registry,
66  array $data = []
67  ) {
68  $this->_sourceCountry = $sourceCountry;
69  $this->_directoryHelper = $directoryHelper;
70  $this->_coreRegistry = $registry;
71  parent::__construct($context, $data);
72  }
73 
77  public function getProduct()
78  {
79  return $this->_coreRegistry->registry('product');
80  }
81 
86  public function render(AbstractElement $element)
87  {
88  $this->setElement($element);
89  return $this->toHtml();
90  }
91 
95  protected function _prepareLayout()
96  {
97  $this->addChild(
98  'add_button',
99  \Magento\Backend\Block\Widget\Button::class,
100  ['label' => __('Add Tax'), 'data_attribute' => ['action' => 'add-fpt-item'], 'class' => 'add']
101  );
102  $this->addChild(
103  'delete_button',
104  \Magento\Backend\Block\Widget\Button::class,
105  [
106  'label' => __('Delete Tax'),
107  'data_attribute' => ['action' => 'delete-fpt-item'],
108  'class' => 'delete'
109  ]
110  );
111  return parent::_prepareLayout();
112  }
113 
119  {
120  $this->_element = $element;
121  return $this;
122  }
123 
127  public function getElement()
128  {
129  return $this->_element;
130  }
131 
135  public function getValues()
136  {
137  $values = [];
138  $data = $this->getElement()->getEscapedValue();
139 
140  if (is_array($data) && count($data)) {
141  usort($data, [$this, '_sortWeeeTaxes']);
142  $values = $data;
143  }
144  return $values;
145  }
146 
152  protected function _sortWeeeTaxes($firstItem, $secondItem)
153  {
154  if ($firstItem['website_id'] != $secondItem['website_id']) {
155  return $firstItem['website_id'] < $secondItem['website_id'] ? -1 : 1;
156  }
157  if ($firstItem['country'] != $secondItem['country']) {
158  return $firstItem['country'] < $secondItem['country'] ? -1 : 1;
159  }
160  return 0;
161  }
162 
166  public function getWebsiteCount()
167  {
168  return count($this->getWebsites());
169  }
170 
174  public function isMultiWebsites()
175  {
176  return !$this->_storeManager->hasSingleStore();
177  }
178 
182  public function getCountries()
183  {
184  if (null === $this->_countries) {
185  $this->_countries = $this->_sourceCountry->toOptionArray();
186  }
187 
188  return $this->_countries;
189  }
190 
194  public function getWebsites()
195  {
196  if (null !== $this->_websites) {
197  return $this->_websites;
198  }
199  $websites = [];
200  $websites[0] = [
201  'name' => __('All Websites'),
202  'currency' => $this->_directoryHelper->getBaseCurrencyCode(),
203  ];
204 
205  if (!$this->_storeManager->hasSingleStore() && !$this->getElement()->getEntityAttribute()->isScopeGlobal()) {
206  if ($storeId = $this->getProduct()->getStoreId()) {
207  $website = $this->_storeManager->getStore($storeId)->getWebsite();
208  $websites[$website->getId()] = [
209  'name' => $website->getName(),
210  'currency' => $website->getConfig(\Magento\Directory\Model\Currency::XML_PATH_CURRENCY_BASE),
211  ];
212  } else {
213  foreach ($this->_storeManager->getWebsites() as $website) {
214  if (!in_array($website->getId(), $this->getProduct()->getWebsiteIds())) {
215  continue;
216  }
217  $websites[$website->getId()] = [
218  'name' => $website->getName(),
219  'currency' => $website->getConfig(\Magento\Directory\Model\Currency::XML_PATH_CURRENCY_BASE),
220  ];
221  }
222  }
223  }
224  $this->_websites = $websites;
225  return $this->_websites;
226  }
227 
231  public function getAddButtonHtml()
232  {
233  return $this->getChildHtml('add_button');
234  }
235 }
setElement(AbstractElement $element)
Definition: Tax.php:118
$values
Definition: options.phtml:88
__()
Definition: __.php:13
render(AbstractElement $element)
Definition: Tax.php:86
_sortWeeeTaxes($firstItem, $secondItem)
Definition: Tax.php:152
__construct(\Magento\Backend\Block\Template\Context $context, \Magento\Directory\Model\Config\Source\Country $sourceCountry, \Magento\Directory\Helper\Data $directoryHelper, \Magento\Framework\Registry $registry, array $data=[])
Definition: Tax.php:61
$element
Definition: element.phtml:12