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: class Mage_Adminhtml_Promo_QuoteController extends Mage_Adminhtml_Controller_Action
29: {
30: protected function _initRule()
31: {
32: $this->_title($this->__('Promotions'))->_title($this->__('Shopping Cart Price Rules'));
33:
34: Mage::register('current_promo_quote_rule', Mage::getModel('salesrule/rule'));
35: $id = (int)$this->getRequest()->getParam('id');
36:
37: if (!$id && $this->getRequest()->getParam('rule_id')) {
38: $id = (int)$this->getRequest()->getParam('rule_id');
39: }
40:
41: if ($id) {
42: Mage::registry('current_promo_quote_rule')->load($id);
43: }
44: }
45:
46: protected function _initAction()
47: {
48: $this->loadLayout()
49: ->_setActiveMenu('promo/quote')
50: ->_addBreadcrumb(Mage::helper('salesrule')->__('Promotions'), Mage::helper('salesrule')->__('Promotions'))
51: ;
52: return $this;
53: }
54:
55: public function indexAction()
56: {
57: $this->_title($this->__('Promotions'))->_title($this->__('Shopping Cart Price Rules'));
58:
59: $this->_initAction()
60: ->_addBreadcrumb(Mage::helper('salesrule')->__('Catalog'), Mage::helper('salesrule')->__('Catalog'))
61: ->renderLayout();
62: }
63:
64: public function newAction()
65: {
66: $this->_forward('edit');
67: }
68:
69: public function editAction()
70: {
71: $id = $this->getRequest()->getParam('id');
72: $model = Mage::getModel('salesrule/rule');
73:
74: if ($id) {
75: $model->load($id);
76: if (! $model->getRuleId()) {
77: Mage::getSingleton('adminhtml/session')->addError(
78: Mage::helper('salesrule')->__('This rule no longer exists.'));
79: $this->_redirect('*/*');
80: return;
81: }
82: }
83:
84: $this->_title($model->getRuleId() ? $model->getName() : $this->__('New Rule'));
85:
86:
87: $data = Mage::getSingleton('adminhtml/session')->getPageData(true);
88: if (!empty($data)) {
89: $model->addData($data);
90: }
91:
92: $model->getConditions()->setJsFormObject('rule_conditions_fieldset');
93: $model->getActions()->setJsFormObject('rule_actions_fieldset');
94:
95: Mage::register('current_promo_quote_rule', $model);
96:
97: $this->_initAction()->getLayout()->getBlock('promo_quote_edit')
98: ->setData('action', $this->getUrl('*/*/save'));
99:
100: $this
101: ->_addBreadcrumb(
102: $id ? Mage::helper('salesrule')->__('Edit Rule')
103: : Mage::helper('salesrule')->__('New Rule'),
104: $id ? Mage::helper('salesrule')->__('Edit Rule')
105: : Mage::helper('salesrule')->__('New Rule'))
106: ->renderLayout();
107:
108: }
109:
110: 111: 112: 113:
114: public function saveAction()
115: {
116: if ($this->getRequest()->getPost()) {
117: try {
118:
119: $model = Mage::getModel('salesrule/rule');
120: Mage::dispatchEvent(
121: 'adminhtml_controller_salesrule_prepare_save',
122: array('request' => $this->getRequest()));
123: $data = $this->getRequest()->getPost();
124: $data = $this->_filterDates($data, array('from_date', 'to_date'));
125: $id = $this->getRequest()->getParam('rule_id');
126: if ($id) {
127: $model->load($id);
128: if ($id != $model->getId()) {
129: Mage::throwException(Mage::helper('salesrule')->__('Wrong rule specified.'));
130: }
131: }
132:
133: $session = Mage::getSingleton('adminhtml/session');
134:
135: $validateResult = $model->validateData(new Varien_Object($data));
136: if ($validateResult !== true) {
137: foreach($validateResult as $errorMessage) {
138: $session->addError($errorMessage);
139: }
140: $session->setPageData($data);
141: $this->_redirect('*/*/edit', array('id'=>$model->getId()));
142: return;
143: }
144:
145: if (isset($data['simple_action']) && $data['simple_action'] == 'by_percent'
146: && isset($data['discount_amount'])) {
147: $data['discount_amount'] = min(100,$data['discount_amount']);
148: }
149: if (isset($data['rule']['conditions'])) {
150: $data['conditions'] = $data['rule']['conditions'];
151: }
152: if (isset($data['rule']['actions'])) {
153: $data['actions'] = $data['rule']['actions'];
154: }
155: unset($data['rule']);
156: $model->loadPost($data);
157:
158: $useAutoGeneration = (int)!empty($data['use_auto_generation']);
159: $model->setUseAutoGeneration($useAutoGeneration);
160:
161: $session->setPageData($model->getData());
162:
163: $model->save();
164: $session->addSuccess(Mage::helper('salesrule')->__('The rule has been saved.'));
165: $session->setPageData(false);
166: if ($this->getRequest()->getParam('back')) {
167: $this->_redirect('*/*/edit', array('id' => $model->getId()));
168: return;
169: }
170: $this->_redirect('*/*/');
171: return;
172: } catch (Mage_Core_Exception $e) {
173: $this->_getSession()->addError($e->getMessage());
174: $id = (int)$this->getRequest()->getParam('rule_id');
175: if (!empty($id)) {
176: $this->_redirect('*/*/edit', array('id' => $id));
177: } else {
178: $this->_redirect('*/*/new');
179: }
180: return;
181:
182: } catch (Exception $e) {
183: $this->_getSession()->addError(
184: Mage::helper('catalogrule')->__('An error occurred while saving the rule data. Please review the log and try again.'));
185: Mage::logException($e);
186: Mage::getSingleton('adminhtml/session')->setPageData($data);
187: $this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('rule_id')));
188: return;
189: }
190: }
191: $this->_redirect('*/*/');
192: }
193:
194: public function deleteAction()
195: {
196: if ($id = $this->getRequest()->getParam('id')) {
197: try {
198: $model = Mage::getModel('salesrule/rule');
199: $model->load($id);
200: $model->delete();
201: Mage::getSingleton('adminhtml/session')->addSuccess(
202: Mage::helper('salesrule')->__('The rule has been deleted.'));
203: $this->_redirect('*/*/');
204: return;
205: } catch (Mage_Core_Exception $e) {
206: $this->_getSession()->addError($e->getMessage());
207: } catch (Exception $e) {
208: $this->_getSession()->addError(
209: Mage::helper('catalogrule')->__('An error occurred while deleting the rule. Please review the log and try again.'));
210: Mage::logException($e);
211: $this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('id')));
212: return;
213: }
214: }
215: Mage::getSingleton('adminhtml/session')->addError(
216: Mage::helper('salesrule')->__('Unable to find a rule to delete.'));
217: $this->_redirect('*/*/');
218: }
219:
220: public function newConditionHtmlAction()
221: {
222: $id = $this->getRequest()->getParam('id');
223: $typeArr = explode('|', str_replace('-', '/', $this->getRequest()->getParam('type')));
224: $type = $typeArr[0];
225:
226: $model = Mage::getModel($type)
227: ->setId($id)
228: ->setType($type)
229: ->setRule(Mage::getModel('salesrule/rule'))
230: ->setPrefix('conditions');
231: if (!empty($typeArr[1])) {
232: $model->setAttribute($typeArr[1]);
233: }
234:
235: if ($model instanceof Mage_Rule_Model_Condition_Abstract) {
236: $model->setJsFormObject($this->getRequest()->getParam('form'));
237: $html = $model->asHtmlRecursive();
238: } else {
239: $html = '';
240: }
241: $this->getResponse()->setBody($html);
242: }
243:
244: public function newActionHtmlAction()
245: {
246: $id = $this->getRequest()->getParam('id');
247: $typeArr = explode('|', str_replace('-', '/', $this->getRequest()->getParam('type')));
248: $type = $typeArr[0];
249:
250: $model = Mage::getModel($type)
251: ->setId($id)
252: ->setType($type)
253: ->setRule(Mage::getModel('salesrule/rule'))
254: ->setPrefix('actions');
255: if (!empty($typeArr[1])) {
256: $model->setAttribute($typeArr[1]);
257: }
258:
259: if ($model instanceof Mage_Rule_Model_Condition_Abstract) {
260: $model->setJsFormObject($this->getRequest()->getParam('form'));
261: $html = $model->asHtmlRecursive();
262: } else {
263: $html = '';
264: }
265: $this->getResponse()->setBody($html);
266: }
267:
268: public function applyRulesAction()
269: {
270: $this->_initAction();
271: $this->renderLayout();
272: }
273:
274: public function gridAction()
275: {
276: $this->_initRule()->loadLayout()->renderLayout();
277: }
278:
279: 280: 281:
282: public function couponsGridAction()
283: {
284: $this->_initRule();
285: $this->loadLayout()->renderLayout();
286: }
287:
288: 289: 290: 291: 292:
293: public function exportCouponsXmlAction()
294: {
295: $this->_initRule();
296: $rule = Mage::registry('current_promo_quote_rule');
297: if ($rule->getId()) {
298: $fileName = 'coupon_codes.xml';
299: $content = $this->getLayout()
300: ->createBlock('adminhtml/promo_quote_edit_tab_coupons_grid')
301: ->getExcelFile($fileName);
302: $this->_prepareDownloadResponse($fileName, $content);
303: } else {
304: $this->_redirect('*/*/detail', array('_current' => true));
305: return;
306: }
307: }
308:
309: 310: 311: 312: 313:
314: public function exportCouponsCsvAction()
315: {
316: $this->_initRule();
317: $rule = Mage::registry('current_promo_quote_rule');
318: if ($rule->getId()) {
319: $fileName = 'coupon_codes.csv';
320: $content = $this->getLayout()
321: ->createBlock('adminhtml/promo_quote_edit_tab_coupons_grid')
322: ->getCsvFile();
323: $this->_prepareDownloadResponse($fileName, $content);
324: } else {
325: $this->_redirect('*/*/detail', array('_current' => true));
326: return;
327: }
328: }
329:
330: 331: 332:
333: public function couponsMassDeleteAction()
334: {
335: $this->_initRule();
336: $rule = Mage::registry('current_promo_quote_rule');
337:
338: if (!$rule->getId()) {
339: $this->_forward('noRoute');
340: }
341:
342: $codesIds = $this->getRequest()->getParam('ids');
343:
344: if (is_array($codesIds)) {
345:
346: $couponsCollection = Mage::getResourceModel('salesrule/coupon_collection')
347: ->addFieldToFilter('coupon_id', array('in' => $codesIds));
348:
349: foreach ($couponsCollection as $coupon) {
350: $coupon->delete();
351: }
352: }
353: }
354:
355: 356: 357:
358: public function generateAction()
359: {
360: if (!$this->getRequest()->isAjax()) {
361: $this->_forward('noRoute');
362: return;
363: }
364: $result = array();
365: $this->_initRule();
366:
367:
368: $rule = Mage::registry('current_promo_quote_rule');
369:
370: if (!$rule->getId()) {
371: $result['error'] = Mage::helper('salesrule')->__('Rule is not defined');
372: } else {
373: try {
374: $data = $this->getRequest()->getParams();
375: if (!empty($data['to_date'])) {
376: $data = array_merge($data, $this->_filterDates($data, array('to_date')));
377: }
378:
379:
380: $generator = $rule->getCouponMassGenerator();
381: if (!$generator->validateData($data)) {
382: $result['error'] = Mage::helper('salesrule')->__('Not valid data provided');
383: } else {
384: $generator->setData($data);
385: $generator->generatePool();
386: $generated = $generator->getGeneratedCount();
387: $this->_getSession()->addSuccess(Mage::helper('salesrule')->__('%s Coupon(s) have been generated', $generated));
388: $this->_initLayoutMessages('adminhtml/session');
389: $result['messages'] = $this->getLayout()->getMessagesBlock()->getGroupedHtml();
390: }
391: } catch (Mage_Core_Exception $e) {
392: $result['error'] = $e->getMessage();
393: } catch (Exception $e) {
394: $result['error'] = Mage::helper('salesrule')->__('An error occurred while generating coupons. Please review the log and try again.');
395: Mage::logException($e);
396: }
397: }
398: $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($result));
399: }
400:
401: 402: 403:
404: public function chooserAction()
405: {
406: $uniqId = $this->getRequest()->getParam('uniq_id');
407: $chooserBlock = $this->getLayout()->createBlock('adminhtml/promo_widget_chooser', '', array(
408: 'id' => $uniqId
409: ));
410: $this->getResponse()->setBody($chooserBlock->toHtml());
411: }
412:
413: 414: 415: 416:
417: protected function _isAllowed()
418: {
419: return Mage::getSingleton('admin/session')->isAllowed('promo/quote');
420: }
421: }
422: