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_Adminhtml_Promo_CatalogController extends Mage_Adminhtml_Controller_Action
35: {
36: 37: 38: 39: 40:
41: protected $_dirtyRulesNoticeMessage;
42:
43: protected function _initAction()
44: {
45: $this->loadLayout()
46: ->_setActiveMenu('promo/catalog')
47: ->_addBreadcrumb(
48: Mage::helper('catalogrule')->__('Promotions'),
49: Mage::helper('catalogrule')->__('Promotions')
50: );
51: return $this;
52: }
53:
54: public function indexAction()
55: {
56: $this->_title($this->__('Promotions'))->_title($this->__('Catalog Price Rules'));
57:
58: $dirtyRules = Mage::getModel('catalogrule/flag')->loadSelf();
59: if ($dirtyRules->getState()) {
60: Mage::getSingleton('adminhtml/session')->addNotice($this->getDirtyRulesNoticeMessage());
61: }
62:
63: $this->_initAction()
64: ->_addBreadcrumb(
65: Mage::helper('catalogrule')->__('Catalog'),
66: Mage::helper('catalogrule')->__('Catalog')
67: )
68: ->renderLayout();
69: }
70:
71: public function newAction()
72: {
73: $this->_forward('edit');
74: }
75:
76: public function editAction()
77: {
78: $this->_title($this->__('Promotions'))->_title($this->__('Catalog Price Rules'));
79:
80: $id = $this->getRequest()->getParam('id');
81: $model = Mage::getModel('catalogrule/rule');
82:
83: if ($id) {
84: $model->load($id);
85: if (! $model->getRuleId()) {
86: Mage::getSingleton('adminhtml/session')->addError(
87: Mage::helper('catalogrule')->__('This rule no longer exists.')
88: );
89: $this->_redirect('*/*');
90: return;
91: }
92: }
93:
94: $this->_title($model->getRuleId() ? $model->getName() : $this->__('New Rule'));
95:
96:
97: $data = Mage::getSingleton('adminhtml/session')->getPageData(true);
98: if (!empty($data)) {
99: $model->addData($data);
100: }
101: $model->getConditions()->setJsFormObject('rule_conditions_fieldset');
102:
103: Mage::register('current_promo_catalog_rule', $model);
104:
105: $this->_initAction()->getLayout()->getBlock('promo_catalog_edit')
106: ->setData('action', $this->getUrl('*/promo_catalog/save'));
107:
108: $breadcrumb = $id
109: ? Mage::helper('catalogrule')->__('Edit Rule')
110: : Mage::helper('catalogrule')->__('New Rule');
111: $this->_addBreadcrumb($breadcrumb, $breadcrumb)->renderLayout();
112:
113: }
114:
115: public function saveAction()
116: {
117: if ($this->getRequest()->getPost()) {
118: try {
119: $model = Mage::getModel('catalogrule/rule');
120: Mage::dispatchEvent(
121: 'adminhtml_controller_catalogrule_prepare_save',
122: array('request' => $this->getRequest())
123: );
124: $data = $this->getRequest()->getPost();
125: $data = $this->_filterDates($data, array('from_date', 'to_date'));
126: if ($id = $this->getRequest()->getParam('rule_id')) {
127: $model->load($id);
128: if ($id != $model->getId()) {
129: Mage::throwException(Mage::helper('catalogrule')->__('Wrong rule specified.'));
130: }
131: }
132:
133: $validateResult = $model->validateData(new Varien_Object($data));
134: if ($validateResult !== true) {
135: foreach($validateResult as $errorMessage) {
136: $this->_getSession()->addError($errorMessage);
137: }
138: $this->_getSession()->setPageData($data);
139: $this->_redirect('*/*/edit', array('id'=>$model->getId()));
140: return;
141: }
142:
143: $data['conditions'] = $data['rule']['conditions'];
144: unset($data['rule']);
145:
146: $autoApply = false;
147: if (!empty($data['auto_apply'])) {
148: $autoApply = true;
149: unset($data['auto_apply']);
150: }
151:
152: $model->loadPost($data);
153:
154: Mage::getSingleton('adminhtml/session')->setPageData($model->getData());
155:
156: $model->save();
157:
158: Mage::getSingleton('adminhtml/session')->addSuccess(
159: Mage::helper('catalogrule')->__('The rule has been saved.')
160: );
161: Mage::getSingleton('adminhtml/session')->setPageData(false);
162: if ($autoApply) {
163: $this->getRequest()->setParam('rule_id', $model->getId());
164: $this->_forward('applyRules');
165: } else {
166: Mage::getModel('catalogrule/flag')->loadSelf()
167: ->setState(1)
168: ->save();
169: if ($this->getRequest()->getParam('back')) {
170: $this->_redirect('*/*/edit', array('id' => $model->getId()));
171: return;
172: }
173: $this->_redirect('*/*/');
174: }
175: return;
176: } catch (Mage_Core_Exception $e) {
177: $this->_getSession()->addError($e->getMessage());
178: } catch (Exception $e) {
179: $this->_getSession()->addError(
180: Mage::helper('catalogrule')->__('An error occurred while saving the rule data. Please review the log and try again.')
181: );
182: Mage::logException($e);
183: Mage::getSingleton('adminhtml/session')->setPageData($data);
184: $this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('rule_id')));
185: return;
186: }
187: }
188: $this->_redirect('*/*/');
189: }
190:
191: public function deleteAction()
192: {
193: if ($id = $this->getRequest()->getParam('id')) {
194: try {
195: $model = Mage::getModel('catalogrule/rule');
196: $model->load($id);
197: $model->delete();
198: Mage::getModel('catalogrule/flag')->loadSelf()
199: ->setState(1)
200: ->save();
201: Mage::getSingleton('adminhtml/session')->addSuccess(
202: Mage::helper('catalogrule')->__('The rule has been deleted.')
203: );
204: $this->_redirect('*/*/');
205: return;
206: } catch (Mage_Core_Exception $e) {
207: $this->_getSession()->addError($e->getMessage());
208: } catch (Exception $e) {
209: $this->_getSession()->addError(
210: Mage::helper('catalogrule')->__('An error occurred while deleting the rule. Please review the log and try again.')
211: );
212: Mage::logException($e);
213: $this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('id')));
214: return;
215: }
216: }
217: Mage::getSingleton('adminhtml/session')->addError(
218: Mage::helper('catalogrule')->__('Unable to find a rule to delete.')
219: );
220: $this->_redirect('*/*/');
221: }
222:
223: public function newConditionHtmlAction()
224: {
225: $id = $this->getRequest()->getParam('id');
226: $typeArr = explode('|', str_replace('-', '/', $this->getRequest()->getParam('type')));
227: $type = $typeArr[0];
228:
229: $model = Mage::getModel($type)
230: ->setId($id)
231: ->setType($type)
232: ->setRule(Mage::getModel('catalogrule/rule'))
233: ->setPrefix('conditions');
234: if (!empty($typeArr[1])) {
235: $model->setAttribute($typeArr[1]);
236: }
237:
238: if ($model instanceof Mage_Rule_Model_Condition_Abstract) {
239: $model->setJsFormObject($this->getRequest()->getParam('form'));
240: $html = $model->asHtmlRecursive();
241: } else {
242: $html = '';
243: }
244: $this->getResponse()->setBody($html);
245: }
246:
247: public function chooserAction()
248: {
249: switch ($this->getRequest()->getParam('attribute')) {
250: case 'sku':
251: $type = 'adminhtml/promo_widget_chooser_sku';
252: break;
253:
254: case 'categories':
255: $type = 'adminhtml/promo_widget_chooser_categories';
256: break;
257: }
258: if (!empty($type)) {
259: $block = $this->getLayout()->createBlock($type);
260: if ($block) {
261: $this->getResponse()->setBody($block->toHtml());
262: }
263: }
264: }
265:
266: public function newActionHtmlAction()
267: {
268: $id = $this->getRequest()->getParam('id');
269: $typeArr = explode('|', str_replace('-', '/', $this->getRequest()->getParam('type')));
270: $type = $typeArr[0];
271:
272: $model = Mage::getModel($type)
273: ->setId($id)
274: ->setType($type)
275: ->setRule(Mage::getModel('catalogrule/rule'))
276: ->setPrefix('actions');
277: if (!empty($typeArr[1])) {
278: $model->setAttribute($typeArr[1]);
279: }
280:
281: if ($model instanceof Mage_Rule_Model_Action_Abstract) {
282: $model->setJsFormObject($this->getRequest()->getParam('form'));
283: $html = $model->asHtmlRecursive();
284: } else {
285: $html = '';
286: }
287: $this->getResponse()->setBody($html);
288: }
289:
290: 291: 292:
293: public function applyRulesAction()
294: {
295: $errorMessage = Mage::helper('catalogrule')->__('Unable to apply rules.');
296: try {
297: Mage::getModel('catalogrule/rule')->applyAll();
298: Mage::getModel('catalogrule/flag')->loadSelf()
299: ->setState(0)
300: ->save();
301: $this->_getSession()->addSuccess(Mage::helper('catalogrule')->__('The rules have been applied.'));
302: } catch (Mage_Core_Exception $e) {
303: $this->_getSession()->addError($errorMessage . ' ' . $e->getMessage());
304: } catch (Exception $e) {
305: $this->_getSession()->addError($errorMessage);
306: }
307: $this->_redirect('*/*');
308: }
309:
310: 311: 312:
313: public function addToAlersAction()
314: {
315: }
316:
317: protected function _isAllowed()
318: {
319: return Mage::getSingleton('admin/session')->isAllowed('promo/catalog');
320: }
321:
322: 323: 324: 325: 326:
327: public function setDirtyRulesNoticeMessage($dirtyRulesNoticeMessage)
328: {
329: $this->_dirtyRulesNoticeMessage = $dirtyRulesNoticeMessage;
330: }
331:
332: 333: 334: 335: 336:
337: public function getDirtyRulesNoticeMessage()
338: {
339: $defaultMessage = Mage::helper('catalogrule')->__('There are rules that have been changed but were not applied. Please, click Apply Rules in order to see immediate effect in the catalog.');
340: return $this->_dirtyRulesNoticeMessage ? $this->_dirtyRulesNoticeMessage : $defaultMessage;
341: }
342: }
343: