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_Adminhtml_Widget_InstanceController extends Mage_Adminhtml_Controller_Action
35: {
36: 37: 38: 39: 40:
41: protected function _getSession()
42: {
43: return Mage::getSingleton('adminhtml/session');
44: }
45:
46: 47: 48: 49: 50:
51: protected function _initAction()
52: {
53: $this->loadLayout()
54: ->_setActiveMenu('cms/widgets')
55: ->_addBreadcrumb(Mage::helper('widget')->__('CMS'),
56: Mage::helper('widget')->__('CMS'))
57: ->_addBreadcrumb(Mage::helper('widget')->__('Manage Widget Instances'),
58: Mage::helper('widget')->__('Manage Widget Instances'));
59: return $this;
60: }
61:
62: 63: 64: 65: 66:
67: protected function _initWidgetInstance()
68: {
69: $this->_title($this->__('CMS'))->_title($this->__('Widgets'));
70:
71:
72: $widgetInstance = Mage::getModel('widget/widget_instance');
73:
74: $instanceId = $this->getRequest()->getParam('instance_id', null);
75: $type = $this->getRequest()->getParam('type', null);
76: $package = $this->getRequest()->getParam('package', null);
77: $theme = $this->getRequest()->getParam('theme', null);
78:
79: if ($instanceId) {
80: $widgetInstance->load($instanceId);
81: if (!$widgetInstance->getId()) {
82: $this->_getSession()->addError(Mage::helper('widget')->__('Wrong widget instance specified.'));
83: return false;
84: }
85: } else {
86: $packageTheme = $package . '/' . $theme == '/' ? null : $package . '/' . $theme;
87: $widgetInstance->setType($type)
88: ->setPackageTheme($packageTheme);
89: }
90: Mage::register('current_widget_instance', $widgetInstance);
91: return $widgetInstance;
92: }
93:
94: 95: 96: 97:
98: public function indexAction()
99: {
100: $this->_title($this->__('CMS'))->_title($this->__('Widgets'));
101:
102: $this->_initAction()
103: ->renderLayout();
104: }
105:
106: 107: 108: 109:
110: public function newAction()
111: {
112: $this->_forward('edit');
113: }
114:
115: 116: 117: 118:
119: public function editAction()
120: {
121: $widgetInstance = $this->_initWidgetInstance();
122: if (!$widgetInstance) {
123: $this->_redirect('*/*/');
124: return;
125: }
126:
127: $this->_title($widgetInstance->getId() ? $widgetInstance->getTitle() : $this->__('New Instance'));
128:
129: $this->_initAction();
130: $this->renderLayout();
131: }
132:
133: 134: 135: 136: 137:
138: private function setBody($body)
139: {
140: Mage::getSingleton('core/translate_inline')->processResponseBody($body);
141: $this->getResponse()->setBody($body);
142: }
143:
144: 145: 146: 147:
148: public function validateAction()
149: {
150: $response = new Varien_Object();
151: $response->setError(false);
152: $widgetInstance = $this->_initWidgetInstance();
153: $result = $widgetInstance->validate();
154: if ($result !== true && is_string($result)) {
155: $this->_getSession()->addError($result);
156: $this->_initLayoutMessages('adminhtml/session');
157: $response->setError(true);
158: $response->setMessage($this->getLayout()->getMessagesBlock()->getGroupedHtml());
159: }
160: $this->setBody($response->toJson());
161: }
162:
163: 164: 165: 166:
167: public function saveAction()
168: {
169: $widgetInstance = $this->_initWidgetInstance();
170: if (!$widgetInstance) {
171: $this->_redirect('*/*/');
172: return;
173: }
174: $widgetInstance->setTitle($this->getRequest()->getPost('title'))
175: ->setStoreIds($this->getRequest()->getPost('store_ids', array(0)))
176: ->setSortOrder($this->getRequest()->getPost('sort_order', 0))
177: ->setPageGroups($this->getRequest()->getPost('widget_instance'))
178: ->setWidgetParameters($this->getRequest()->getPost('parameters'));
179: try {
180: $widgetInstance->save();
181: $this->_getSession()->addSuccess(
182: Mage::helper('widget')->__('The widget instance has been saved.')
183: );
184: if ($this->getRequest()->getParam('back', false)) {
185: $this->_redirect('*/*/edit', array(
186: 'instance_id' => $widgetInstance->getId(),
187: '_current' => true
188: ));
189: } else {
190: $this->_redirect('*/*/');
191: }
192: return;
193: } catch (Exception $e) {
194: $this->_getSession()->addError($e->getMessage());
195: $this->_redirect('*/*/edit', array('_current' => true));
196: return;
197: }
198: $this->_redirect('*/*/');
199: return;
200: }
201:
202: 203: 204: 205:
206: public function deleteAction()
207: {
208: $widgetInstance = $this->_initWidgetInstance();
209: if ($widgetInstance) {
210: try {
211: $widgetInstance->delete();
212: $this->_getSession()->addSuccess(
213: Mage::helper('widget')->__('The widget instance has been deleted.')
214: );
215: } catch (Exception $e) {
216: $this->_getSession()->addError($e->getMessage());
217: }
218: }
219: $this->_redirect('*/*/');
220: return;
221: }
222:
223: 224: 225: 226:
227: public function categoriesAction()
228: {
229: $selected = $this->getRequest()->getParam('selected', '');
230: $isAnchorOnly = $this->getRequest()->getParam('is_anchor_only', 0);
231: $chooser = $this->getLayout()
232: ->createBlock('adminhtml/catalog_category_widget_chooser')
233: ->setUseMassaction(true)
234: ->setId(Mage::helper('core')->uniqHash('categories'))
235: ->setIsAnchorOnly($isAnchorOnly)
236: ->setSelectedCategories(explode(',', $selected));
237: $this->setBody($chooser->toHtml());
238: }
239:
240: 241: 242: 243:
244: public function productsAction()
245: {
246: $selected = $this->getRequest()->getParam('selected', '');
247: $productTypeId = $this->getRequest()->getParam('product_type_id', '');
248: $chooser = $this->getLayout()
249: ->createBlock('adminhtml/catalog_product_widget_chooser')
250: ->setName(Mage::helper('core')->uniqHash('products_grid_'))
251: ->setUseMassaction(true)
252: ->setProductTypeId($productTypeId)
253: ->setSelectedProducts(explode(',', $selected));
254:
255: $serializer = $this->getLayout()->createBlock('adminhtml/widget_grid_serializer');
256: $serializer->initSerializerBlock($chooser, 'getSelectedProducts', 'selected_products', 'selected_products');
257: $this->setBody($chooser->toHtml().$serializer->toHtml());
258: }
259:
260: 261: 262: 263:
264: public function blocksAction()
265: {
266:
267: $widgetInstance = $this->_initWidgetInstance();
268: $layout = $this->getRequest()->getParam('layout');
269: $selected = $this->getRequest()->getParam('selected', null);
270: $blocksChooser = $this->getLayout()
271: ->createBlock('widget/adminhtml_widget_instance_edit_chooser_block')
272: ->setArea($widgetInstance->getArea())
273: ->setPackage($widgetInstance->getPackage())
274: ->setTheme($widgetInstance->getTheme())
275: ->setLayoutHandle($layout)
276: ->setSelected($selected)
277: ->setAllowedBlocks($widgetInstance->getWidgetSupportedBlocks());
278: $this->setBody($blocksChooser->toHtml());
279: }
280:
281: 282: 283: 284:
285: public function templateAction()
286: {
287:
288: $widgetInstance = $this->_initWidgetInstance();
289: $block = $this->getRequest()->getParam('block');
290: $selected = $this->getRequest()->getParam('selected', null);
291: $templateChooser = $this->getLayout()
292: ->createBlock('widget/adminhtml_widget_instance_edit_chooser_template')
293: ->setSelected($selected)
294: ->setWidgetTemplates($widgetInstance->getWidgetSupportedTemplatesByBlock($block));
295: $this->setBody($templateChooser->toHtml());
296: }
297:
298: 299: 300: 301: 302:
303: protected function _isAllowed()
304: {
305: return Mage::getSingleton('admin/session')->isAllowed('cms/widget_instance');
306: }
307: }
308: