Save::removeButton()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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