Add::updateButton()   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 3
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
/**
10
 * Admin sub seller class toolbar.
11
 *
12
 * @author      Magento Core Team <[email protected]>
13
 */
14
15
namespace Getnet\SubSellerMagento\Block\Adminhtml\SubSeller\Toolbar;
16
17
/**
18
 * @api
19
 *
20
 * @since 100.0.2
21
 */
22
class Add extends \Magento\Backend\Block\Template implements \Magento\Backend\Block\Widget\ContainerInterface
23
{
24
    /**
25
     * @var string
26
     */
27
    protected $_template = 'Getnet_SubSellerMagento::toolbar/sub_seller/add.phtml';
28
29
    /**
30
     * @var \Magento\Backend\Block\Widget\Button\ButtonList
31
     */
32
    protected $buttonList;
33
34
    /**
35
     * @var \Magento\Backend\Block\Widget\Button\SplitButton
36
     */
37
    protected $splitButton;
38
39
    /**
40
     * @var \Magento\Backend\Block\Widget\Button\ToolbarInterface
41
     */
42
    protected $toolbar;
43
44
    /**
45
     * @param \Magento\Backend\Block\Template\Context               $context
46
     * @param \Magento\Backend\Block\Widget\Button\ButtonList       $buttonList
47
     * @param \Magento\Backend\Block\Widget\Button\SplitButton      $splitButton
48
     * @param \Magento\Backend\Block\Widget\Button\ToolbarInterface $toolbar
49
     * @param array                                                 $data
50
     */
51
    public function __construct(
52
        \Magento\Backend\Block\Template\Context $context,
53
        \Magento\Backend\Block\Widget\Button\ButtonList $buttonList,
54
        \Magento\Backend\Block\Widget\Button\SplitButton $splitButton,
55
        \Magento\Backend\Block\Widget\Button\ToolbarInterface $toolbar,
56
        array $data = []
57
    ) {
58
        $this->buttonList = $buttonList;
59
        $this->splitButton = $splitButton;
60
        $this->toolbar = $toolbar;
61
        parent::__construct($context, $data);
62
    }
63
64
    /**
65
     * @inheritdoc
66
     */
67
    public function addButton($buttonId, $data, $level = 0, $sortOrder = 0, $region = 'toolbar')
68
    {
69
        $this->buttonList->add($buttonId, $data, $level, $sortOrder, $region);
70
71
        return $this;
72
    }
73
74
    /**
75
     * @inheritdoc
76
     */
77
    public function removeButton($buttonId)
78
    {
79
        $this->buttonList->remove($buttonId);
80
81
        return $this;
82
    }
83
84
    /**
85
     * Prepare Layout.
86
     *
87
     * @return $this
88
     */
89
    protected function _prepareLayout()
90
    {
91
        $this->buttonList->add(
92
            'add',
93
            [
94
                'class_name' => \Magento\Backend\Block\Widget\Button\SplitButton::class,
95
                'label'      => __('Add New Sub Seller'),
96
                'onclick'    => 'window.location.href=\''.$this->getUrl('subseller/subseller/add').'\'',
97
                'class'      => 'add primary add-sub-seller',
98
                'options'    => $this->newSellerType(),
99
            ]
100
        );
101
102
        $this->toolbar->pushButtons($this, $this->buttonList);
103
104
        return parent::_prepareLayout();
105
    }
106
107
    /**
108
     * @inheritdoc
109
     */
110
    public function updateButton($buttonId, $key, $data)
111
    {
112
        $this->buttonList->update($buttonId, $key, $data);
113
114
        return $this;
115
    }
116
117
    /**
118
     * @inheritdoc
119
     */
120
    public function canRender(\Magento\Backend\Block\Widget\Button\Item $item)
121
    {
122
        return !$item->isDeleted();
123
    }
124
125
    /**
126
     * Retrieve options for newSellerType split button.
127
     *
128
     * @return array
129
     */
130
    protected function newSellerType()
131
    {
132
        $splitButtonOptions = [
133
            'pf' => [
134
                'label'   => __('Pessoa Física'),
135
                'onclick' => "setLocation('".$this->getUrl('subseller/subseller/add/type/0')."')",
136
                'default' => false,
137
            ],
138
            'pj' => [
139
                'label'   => __('Pessoa Jurídica'),
140
                'onclick' => "setLocation('".$this->getUrl('subseller/subseller/add/type/1')."')",
141
                'default' => false,
142
            ],
143
        ];
144
145
        return $splitButtonOptions;
146
    }
147
}
148