1: <?php
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: class Mage_Widget_Block_Adminhtml_Widget_Instance_Edit_Chooser_Block
35: extends Mage_Adminhtml_Block_Widget
36: {
37: protected $_layoutHandlesXml = null;
38:
39: protected $_layoutHandleUpdates = array();
40:
41: protected $_layoutHandleUpdatesXml = null;
42:
43: protected $_layoutHandle = array();
44:
45: protected $_blocks = array();
46:
47: protected $_allowedBlocks = array();
48:
49: 50: 51: 52: 53: 54:
55: public function setAllowedBlocks($allowedBlocks)
56: {
57: $this->_allowedBlocks = $allowedBlocks;
58: return $this;
59: }
60:
61: 62: 63: 64: 65: 66:
67: public function addAllowedBlock($block)
68: {
69: $this->_allowedBlocks[] = $block;
70: return $this;
71: }
72:
73: 74: 75: 76: 77:
78: public function getAllowedBlocks()
79: {
80: return $this->_allowedBlocks;
81: }
82:
83: 84: 85: 86: 87: 88: 89:
90: public function setLayoutHandle($layoutHandle)
91: {
92: if (is_string($layoutHandle)) {
93: $layoutHandle = explode(',', $layoutHandle);
94: }
95: $this->_layoutHandle = array_merge(array('default'), (array)$layoutHandle);
96: return $this;
97: }
98:
99: 100: 101: 102: 103:
104: public function getLayoutHandle()
105: {
106: return $this->_layoutHandle;
107: }
108:
109: 110: 111: 112: 113:
114: public function getArea()
115: {
116: if (!$this->_getData('area')) {
117: return Mage_Core_Model_Design_Package::DEFAULT_AREA;
118: }
119: return $this->_getData('area');
120: }
121:
122: 123: 124: 125: 126:
127: public function getPackage()
128: {
129: if (!$this->_getData('package')) {
130: return Mage_Core_Model_Design_Package::DEFAULT_PACKAGE;
131: }
132: return $this->_getData('package');
133: }
134:
135: 136: 137: 138: 139:
140: public function getTheme()
141: {
142: if (!$this->_getData('theme')) {
143: return Mage_Core_Model_Design_Package::DEFAULT_THEME;
144: }
145: return $this->_getData('theme');
146: }
147:
148: 149: 150: 151: 152:
153: protected function _toHtml()
154: {
155: $selectBlock = $this->getLayout()->createBlock('core/html_select')
156: ->setName('block')
157: ->setClass('required-entry select')
158: ->setExtraParams('onchange="WidgetInstance.loadSelectBoxByType(\'block_template\','
159: .' this.up(\'div.group_container\'), this.value)"')
160: ->setOptions($this->getBlocks())
161: ->setValue($this->getSelected());
162: return parent::_toHtml().$selectBlock->toHtml();
163: }
164:
165: 166: 167: 168: 169:
170: public function getBlocks()
171: {
172: if (empty($this->_blocks)) {
173:
174: $update = Mage::getModel('core/layout')->getUpdate();
175:
176: $this->_layoutHandlesXml = $update->getFileLayoutUpdatesXml(
177: $this->getArea(),
178: $this->getPackage(),
179: $this->getTheme());
180: $this->_collectLayoutHandles();
181: $this->_collectBlocks();
182: array_unshift($this->_blocks, array(
183: 'value' => '',
184: 'label' => Mage::helper('widget')->__('-- Please Select --')
185: ));
186: }
187: return $this->_blocks;
188: }
189:
190: 191: 192: 193:
194: protected function _collectLayoutHandles()
195: {
196: foreach ($this->getLayoutHandle() as $handle) {
197: $this->_mergeLayoutHandles($handle);
198: }
199: $updatesStr = '<'.'?xml version="1.0"?'.'><layout>'.implode('', $this->_layoutHandleUpdates).'</layout>';
200: $this->_layoutHandleUpdatesXml = simplexml_load_string($updatesStr, 'Varien_Simplexml_Element');
201: }
202:
203: 204: 205: 206: 207:
208: public function _mergeLayoutHandles($handle)
209: {
210: foreach ($this->_layoutHandlesXml->{$handle} as $updateXml) {
211: foreach ($updateXml->children() as $child) {
212: if (strtolower($child->getName()) == 'update' && isset($child['handle'])) {
213: $this->_mergeLayoutHandles((string)$child['handle']);
214: }
215: }
216: $this->_layoutHandleUpdates[] = $updateXml->asNiceXml();
217: }
218: }
219:
220:
221: 222: 223:
224: protected function _collectBlocks()
225: {
226: if ($blocks = $this->_layoutHandleUpdatesXml->xpath('//block/label/..')) {
227:
228: foreach ($blocks as $block) {
229: if ((string)$block->getAttribute('name') && $this->_filterBlock($block)) {
230: $helper = Mage::helper(Mage_Core_Model_Layout::findTranslationModuleName($block));
231: $this->_blocks[(string)$block->getAttribute('name')] = $helper->__((string)$block->label);
232: }
233: }
234: }
235: asort($this->_blocks, SORT_STRING);
236: }
237:
238: 239: 240: 241: 242: 243:
244: protected function _filterBlock($block)
245: {
246: if (!$this->getAllowedBlocks()) {
247: return true;
248: }
249: if (in_array((string)$block->getAttribute('name'), $this->getAllowedBlocks())) {
250: return true;
251: }
252: return false;
253: }
254: }
255: