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