forked from magento/magento2
/
Container.php
310 lines (289 loc) · 8.37 KB
/
Container.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
<?php
/**
* Magento
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@magentocommerce.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade Magento to newer
* versions in the future. If you wish to customize Magento for your
* needs please refer to http://www.magentocommerce.com for more information.
*
* @category Mage
* @package Mage_Adminhtml
* @copyright Copyright (c) 2011 Magento Inc. (http://www.magentocommerce.com)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/
/**
* Adminhtml container block
*
* @category Mage
* @package Mage_Adminhtml
* @author Magento Core Team <core@magentocommerce.com>
*/
class Mage_Adminhtml_Block_Widget_Container extends Mage_Adminhtml_Block_Template
{
/**
* So called "container controller" to specify group of blocks participating in some action
*
* @var string
*/
protected $_controller = 'empty';
/**
* Array of buttons
*
*
* @var array
*/
protected $_buttons = array(
-1 => array(),
0 => array(),
1 => array(),
);
/**
* Header text
*
* @var string
*/
protected $_headerText = 'Container Widget Header';
/**
* Add a button
*
* @param string $id
* @param array $data
* @param integer $level
* @param integer $sortOrder
* @param string|null $placement area, that button should be displayed in ('header', 'footer', null)
* @return Mage_Adminhtml_Block_Widget_Container
*/
protected function _addButton($id, $data, $level = 0, $sortOrder = 0, $area = 'header')
{
if (!isset($this->_buttons[$level])) {
$this->_buttons[$level] = array();
}
$this->_buttons[$level][$id] = $data;
$this->_buttons[$level][$id]['area'] = $area;
if ($sortOrder) {
$this->_buttons[$level][$id]['sort_order'] = $sortOrder;
} else {
$this->_buttons[$level][$id]['sort_order'] = count($this->_buttons[$level]) * 10;
}
return $this;
}
/**
* Public wrapper for protected _addButton method
*
* @param string $id
* @param array $data
* @param integer $level
* @param integer $sortOrder
* @param string|null $placement area, that button should be displayed in ('header', 'footer', null)
* @return Mage_Adminhtml_Block_Widget_Container
*/
public function addButton($id, $data, $level = 0, $sortOrder = 0, $area = 'header')
{
return $this->_addButton($id, $data, $level, $sortOrder, $area);
}
/**
* Remove existing button
*
* @param string $id
* @return Mage_Adminhtml_Block_Widget_Container
*/
protected function _removeButton($id)
{
foreach ($this->_buttons as $level => $buttons) {
if (isset($buttons[$id])) {
unset($this->_buttons[$level][$id]);
}
}
return $this;
}
/**
* Public wrapper for the _removeButton() method
*
* @param string $id
* @return Mage_Adminhtml_Block_Widget_Container
*/
public function removeButton($id)
{
return $this->_removeButton($id);
}
/**
* Update specified button property
*
* @param string $id
* @param string|null $key
* @param mixed $data
* @return Mage_Adminhtml_Block_Widget_Container
*/
protected function _updateButton($id, $key=null, $data)
{
foreach ($this->_buttons as $level => $buttons) {
if (isset($buttons[$id])) {
if (!empty($key)) {
if ($child = $this->getChild($id . '_button')) {
$child->setData($key, $data);
}
if ('level' == $key) {
$this->_buttons[$data][$id] = $this->_buttons[$level][$id];
unset($this->_buttons[$level][$id]);
} else {
$this->_buttons[$level][$id][$key] = $data;
}
} else {
$this->_buttons[$level][$id] = $data;
}
break;
}
}
return $this;
}
/**
* Public wrapper for protected _updateButton method
*
* @param string $id
* @param string|null $key
* @param mixed $data
* @return Mage_Adminhtml_Block_Widget_Container
*/
public function updateButton($id, $key=null, $data)
{
return $this->_updateButton($id, $key, $data);
}
/**
* Preparing child blocks for each added button
*
* @return Mage_Core_Block_Abstract
*/
protected function _prepareLayout()
{
foreach ($this->_buttons as $level => $buttons) {
foreach ($buttons as $id => $data) {
$childId = $this->_prepareButtonBlockId($id);
$this->_addButtonChildBlock($childId);
}
}
return parent::_prepareLayout();
}
/**
* Prepare block id for button's id
*
* @param string $id
* @return string
*/
protected function _prepareButtonBlockId($id)
{
return $id . '_button';
}
/**
* Adding child block with specified child's id.
*
* @param string $childId
* @return Mage_Adminhtml_Block_Widget_Button
*/
protected function _addButtonChildBlock($childId)
{
$block = $this->getLayout()->createBlock('Mage_Adminhtml_Block_Widget_Button');
$this->setChild($childId, $block);
return $block;
}
/**
* Produce buttons HTML
*
* @param string $area
* @return string
*/
public function getButtonsHtml($area = null)
{
$out = '';
foreach ($this->_buttons as $level => $buttons) {
$_buttons = array();
foreach ($buttons as $id => $data) {
$_buttons[$data['sort_order']]['id'] = $id;
$_buttons[$data['sort_order']]['data'] = $data;
}
ksort($_buttons);
foreach ($_buttons as $button) {
$id = $button['id'];
$data = $button['data'];
if ($area && isset($data['area']) && ($area != $data['area'])) {
continue;
}
$childId = $this->_prepareButtonBlockId($id);
$child = $this->getChild($childId);
if (!$child) {
$child = $this->_addButtonChildBlock($childId);
}
if (isset($data['name'])) {
$data['element_name'] = $data['name'];
}
$child->setData($data);
$out .= $this->getChildHtml($childId);
}
}
return $out;
}
/**
* Get header text
*
* @return string
*/
public function getHeaderText()
{
return $this->_headerText;
}
/**
* Get header CSS class
*
* @return string
*/
public function getHeaderCssClass()
{
return 'head-' . strtr($this->_controller, '_', '-');
}
/**
* Get header HTML
*
* @return string
*/
public function getHeaderHtml()
{
return '<h3 class="' . $this->getHeaderCssClass() . '">' . $this->getHeaderText() . '</h3>';
}
/**
* Check if there's anything to display in footer
*
* @return boolean
*/
public function hasFooterButtons()
{
foreach ($this->_buttons as $level => $buttons) {
foreach ($buttons as $id => $data) {
if (isset($data['area']) && ('footer' == $data['area'])) {
return true;
}
}
}
return false;
}
/**
* Prepare html output
*
* @return string
*/
protected function _toHtml()
{
Mage::dispatchEvent('adminhtml_widget_container_html_before', array('block' => $this));
return parent::_toHtml();
}
}