Save.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. /**
  7. * Admin tax rate save toolbar
  8. *
  9. * @author Magento Core Team <core@magentocommerce.com>
  10. */
  11. namespace Magento\Tax\Block\Adminhtml\Rate\Toolbar;
  12. /**
  13. * Rate toolbar block
  14. */
  15. class Save extends \Magento\Backend\Block\Template implements \Magento\Backend\Block\Widget\ContainerInterface
  16. {
  17. /**
  18. * @var string
  19. */
  20. protected $_template = 'Magento_Tax::toolbar/rate/save.phtml';
  21. /**
  22. * @var \Magento\Backend\Block\Widget\Button\ButtonList
  23. */
  24. protected $buttonList;
  25. /**
  26. * @var \Magento\Backend\Block\Widget\Button\ToolbarInterface
  27. */
  28. protected $toolbar;
  29. /**
  30. * @param \Magento\Backend\Block\Template\Context $context
  31. * @param \Magento\Backend\Block\Widget\Button\ButtonList $buttonList
  32. * @param \Magento\Backend\Block\Widget\Button\ToolbarInterface $toolbar
  33. * @param array $data
  34. */
  35. public function __construct(
  36. \Magento\Backend\Block\Template\Context $context,
  37. \Magento\Backend\Block\Widget\Button\ButtonList $buttonList,
  38. \Magento\Backend\Block\Widget\Button\ToolbarInterface $toolbar,
  39. array $data = []
  40. ) {
  41. $this->buttonList = $buttonList;
  42. $this->toolbar = $toolbar;
  43. parent::__construct($context, $data);
  44. }
  45. /**
  46. * Init model
  47. *
  48. * @return void
  49. */
  50. protected function _construct()
  51. {
  52. parent::_construct();
  53. $this->assign('createUrl', $this->getUrl('tax/rate/save'));
  54. }
  55. /**
  56. * Public wrapper for the button list
  57. *
  58. * @param string $buttonId
  59. * @param array $data
  60. * @param integer $level
  61. * @param integer $sortOrder
  62. * @param string|null $region That button should be displayed in ('toolbar', 'header', 'footer', null)
  63. * @return $this
  64. */
  65. public function addButton($buttonId, $data, $level = 0, $sortOrder = 0, $region = 'toolbar')
  66. {
  67. $this->buttonList->add($buttonId, $data, $level, $sortOrder, $region);
  68. return $this;
  69. }
  70. /**
  71. * Public wrapper for the button list
  72. *
  73. * @param string $buttonId
  74. * @return $this
  75. */
  76. public function removeButton($buttonId)
  77. {
  78. $this->buttonList->remove($buttonId);
  79. return $this;
  80. }
  81. /**
  82. * Public wrapper for protected _updateButton method
  83. *
  84. * @param string $buttonId
  85. * @param string|null $key
  86. * @param string $data
  87. * @return $this
  88. */
  89. public function updateButton($buttonId, $key, $data)
  90. {
  91. $this->buttonList->update($buttonId, $key, $data);
  92. return $this;
  93. }
  94. /**
  95. * Prepare layout
  96. *
  97. * @return $this
  98. */
  99. protected function _prepareLayout()
  100. {
  101. $this->buttonList->add(
  102. 'back',
  103. [
  104. 'label' => __('Back'),
  105. 'onclick' => 'window.location.href=\'' . $this->getUrl('tax/*/') . '\'',
  106. 'class' => 'back'
  107. ]
  108. );
  109. $this->buttonList->add(
  110. 'reset',
  111. ['label' => __('Reset'), 'onclick' => 'window.location.reload()', 'class' => 'reset']
  112. );
  113. $rate = (int)$this->getRequest()->getParam('rate');
  114. if ($rate) {
  115. $this->buttonList->add(
  116. 'delete',
  117. [
  118. 'label' => __('Delete Rate'),
  119. 'onclick' => 'deleteConfirm(\'' . __(
  120. 'Are you sure you want to do this?'
  121. ) . '\', \'' . $this->getUrl(
  122. 'tax/*/delete',
  123. ['rate' => $rate]
  124. ) . '\')',
  125. 'class' => 'delete'
  126. ]
  127. );
  128. }
  129. $this->buttonList->add(
  130. 'save',
  131. [
  132. 'label' => __('Save Rate'),
  133. 'class' => 'save primary save-rate',
  134. 'data_attribute' => [
  135. 'mage-init' => ['button' => ['event' => 'save', 'target' => '#rate-form']],
  136. ]
  137. ]
  138. );
  139. $this->toolbar->pushButtons($this, $this->buttonList);
  140. return parent::_prepareLayout();
  141. }
  142. /**
  143. * Check whether button rendering is allowed in current context
  144. *
  145. * @param \Magento\Backend\Block\Widget\Button\Item $item
  146. * @return bool
  147. */
  148. public function canRender(\Magento\Backend\Block\Widget\Button\Item $item)
  149. {
  150. return !$item->isDeleted();
  151. }
  152. }