PageRenderTime 50ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/app/code/core/Mage/Adminhtml/controllers/Promo/QuoteController.php

https://github.com/gryzz/crystal_magento
PHP | 267 lines | 201 code | 34 blank | 32 comment | 23 complexity | 13b25e058c62829bec4f577615280115 MD5 | raw file
  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) 2010 Magento Inc. (http://www.magentocommerce.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. class Mage_Adminhtml_Promo_QuoteController extends Mage_Adminhtml_Controller_Action
  27. {
  28. protected function _initRule()
  29. {
  30. $this->_title($this->__('Promotions'))->_title($this->__('Shopping Cart Price Rules'));
  31. Mage::register('current_promo_quote_rule', Mage::getModel('salesrule/rule'));
  32. if ($id = (int) $this->getRequest()->getParam('id')) {
  33. Mage::registry('current_promo_quote_rule')
  34. ->load($id);
  35. }
  36. }
  37. protected function _initAction()
  38. {
  39. $this->loadLayout()
  40. ->_setActiveMenu('promo/quote')
  41. ->_addBreadcrumb(Mage::helper('salesrule')->__('Promotions'), Mage::helper('salesrule')->__('Promotions'))
  42. ;
  43. return $this;
  44. }
  45. public function indexAction()
  46. {
  47. $this->_title($this->__('Promotions'))->_title($this->__('Shopping Cart Price Rules'));
  48. $this->_initAction()
  49. ->_addBreadcrumb(Mage::helper('salesrule')->__('Catalog'), Mage::helper('salesrule')->__('Catalog'))
  50. ->renderLayout();
  51. }
  52. public function newAction()
  53. {
  54. $this->_forward('edit');
  55. }
  56. public function editAction()
  57. {
  58. $id = $this->getRequest()->getParam('id');
  59. $model = Mage::getModel('salesrule/rule');
  60. if ($id) {
  61. $model->load($id);
  62. if (! $model->getRuleId()) {
  63. Mage::getSingleton('adminhtml/session')->addError(Mage::helper('salesrule')->__('This rule no longer exists.'));
  64. $this->_redirect('*/*');
  65. return;
  66. }
  67. }
  68. $this->_title($model->getRuleId() ? $model->getName() : $this->__('New Rule'));
  69. // set entered data if was error when we do save
  70. $data = Mage::getSingleton('adminhtml/session')->getPageData(true);
  71. if (!empty($data)) {
  72. $model->addData($data);
  73. }
  74. $model->getConditions()->setJsFormObject('rule_conditions_fieldset');
  75. $model->getActions()->setJsFormObject('rule_actions_fieldset');
  76. Mage::register('current_promo_quote_rule', $model);
  77. $this->_initAction()->getLayout()->getBlock('promo_quote_edit')
  78. ->setData('action', $this->getUrl('*/*/save'));
  79. $this
  80. ->_addBreadcrumb($id ? Mage::helper('salesrule')->__('Edit Rule') : Mage::helper('salesrule')->__('New Rule'), $id ? Mage::helper('salesrule')->__('Edit Rule') : Mage::helper('salesrule')->__('New Rule'))
  81. ->renderLayout();
  82. }
  83. /**
  84. * Promo quote save action
  85. *
  86. */
  87. public function saveAction()
  88. {
  89. if ($this->getRequest()->getPost()) {
  90. try {
  91. $model = Mage::getModel('salesrule/rule');
  92. Mage::dispatchEvent('adminhtml_controller_salesrule_prepare_save', array('request' => $this->getRequest()));
  93. $data = $this->getRequest()->getPost();
  94. $data = $this->_filterDates($data, array('from_date', 'to_date'));
  95. $id = $this->getRequest()->getParam('rule_id');
  96. if ($id) {
  97. $model->load($id);
  98. if ($id != $model->getId()) {
  99. Mage::throwException(Mage::helper('salesrule')->__('Wrong rule specified.'));
  100. }
  101. }
  102. $session = Mage::getSingleton('adminhtml/session');
  103. $validateResult = $model->validateData(new Varien_Object($data));
  104. if ($validateResult !== true) {
  105. foreach($validateResult as $errorMessage) {
  106. $session->addError($errorMessage);
  107. }
  108. $session->setPageData($data);
  109. $this->_redirect('*/*/edit', array('id'=>$model->getId()));
  110. return;
  111. }
  112. if (isset($data['simple_action']) && $data['simple_action'] == 'by_percent' && isset($data['discount_amount'])) {
  113. $data['discount_amount'] = min(100,$data['discount_amount']);
  114. }
  115. if (isset($data['rule']['conditions'])) {
  116. $data['conditions'] = $data['rule']['conditions'];
  117. }
  118. if (isset($data['rule']['actions'])) {
  119. $data['actions'] = $data['rule']['actions'];
  120. }
  121. unset($data['rule']);
  122. $model->loadPost($data);
  123. $session->setPageData($model->getData());
  124. $model->save();
  125. $session->addSuccess(Mage::helper('salesrule')->__('The rule has been saved.'));
  126. $session->setPageData(false);
  127. if ($this->getRequest()->getParam('back')) {
  128. $this->_redirect('*/*/edit', array('id' => $model->getId()));
  129. return;
  130. }
  131. $this->_redirect('*/*/');
  132. return;
  133. } catch (Mage_Core_Exception $e) {
  134. $this->_getSession()->addError($e->getMessage());
  135. } catch (Exception $e) {
  136. $this->_getSession()->addError(Mage::helper('catalogrule')->__('An error occurred while saving the rule data. Please review the log and try again.'));
  137. Mage::logException($e);
  138. Mage::getSingleton('adminhtml/session')->setPageData($data);
  139. $this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('rule_id')));
  140. return;
  141. }
  142. }
  143. $this->_redirect('*/*/');
  144. }
  145. public function deleteAction()
  146. {
  147. if ($id = $this->getRequest()->getParam('id')) {
  148. try {
  149. $model = Mage::getModel('salesrule/rule');
  150. $model->load($id);
  151. $model->delete();
  152. Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('salesrule')->__('The rule has been deleted.'));
  153. $this->_redirect('*/*/');
  154. return;
  155. } catch (Mage_Core_Exception $e) {
  156. $this->_getSession()->addError($e->getMessage());
  157. } catch (Exception $e) {
  158. $this->_getSession()->addError(Mage::helper('catalogrule')->__('An error occurred while deleting the rule. Please review the log and try again.'));
  159. Mage::logException($e);
  160. $this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('id')));
  161. return;
  162. }
  163. }
  164. Mage::getSingleton('adminhtml/session')->addError(Mage::helper('salesrule')->__('Unable to find a rule to delete.'));
  165. $this->_redirect('*/*/');
  166. }
  167. public function newConditionHtmlAction()
  168. {
  169. $id = $this->getRequest()->getParam('id');
  170. $typeArr = explode('|', str_replace('-', '/', $this->getRequest()->getParam('type')));
  171. $type = $typeArr[0];
  172. $model = Mage::getModel($type)
  173. ->setId($id)
  174. ->setType($type)
  175. ->setRule(Mage::getModel('salesrule/rule'))
  176. ->setPrefix('conditions');
  177. if (!empty($typeArr[1])) {
  178. $model->setAttribute($typeArr[1]);
  179. }
  180. if ($model instanceof Mage_Rule_Model_Condition_Abstract) {
  181. $model->setJsFormObject($this->getRequest()->getParam('form'));
  182. $html = $model->asHtmlRecursive();
  183. } else {
  184. $html = '';
  185. }
  186. $this->getResponse()->setBody($html);
  187. }
  188. public function newActionHtmlAction()
  189. {
  190. $id = $this->getRequest()->getParam('id');
  191. $typeArr = explode('|', str_replace('-', '/', $this->getRequest()->getParam('type')));
  192. $type = $typeArr[0];
  193. $model = Mage::getModel($type)
  194. ->setId($id)
  195. ->setType($type)
  196. ->setRule(Mage::getModel('salesrule/rule'))
  197. ->setPrefix('actions');
  198. if (!empty($typeArr[1])) {
  199. $model->setAttribute($typeArr[1]);
  200. }
  201. if ($model instanceof Mage_Rule_Model_Condition_Abstract) {
  202. $model->setJsFormObject($this->getRequest()->getParam('form'));
  203. $html = $model->asHtmlRecursive();
  204. } else {
  205. $html = '';
  206. }
  207. $this->getResponse()->setBody($html);
  208. }
  209. public function applyRulesAction()
  210. {
  211. $this->_initAction();
  212. $this->renderLayout();
  213. }
  214. public function gridAction()
  215. {
  216. $this->_initRule()->loadLayout()->renderLayout();
  217. }
  218. /**
  219. * Chooser source action
  220. */
  221. public function chooserAction()
  222. {
  223. $uniqId = $this->getRequest()->getParam('uniq_id');
  224. $chooserBlock = $this->getLayout()->createBlock('adminhtml/promo_widget_chooser', '', array(
  225. 'id' => $uniqId
  226. ));
  227. $this->getResponse()->setBody($chooserBlock->toHtml());
  228. }
  229. protected function _isAllowed()
  230. {
  231. return Mage::getSingleton('admin/session')->isAllowed('promo/quote');
  232. }
  233. }