00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 class Mage_Adminhtml_Promo_QuoteController extends Mage_Adminhtml_Controller_Action
00029 {
00030 protected function _initRule()
00031 {
00032 Mage::register('current_promo_quote_rule', Mage::getModel('salesrule/rule'));
00033 if ($id = (int) $this->getRequest()->getParam('id')) {
00034 Mage::registry('current_promo_quote_rule')
00035 ->load($id);
00036 }
00037 }
00038
00039 protected function _initAction()
00040 {
00041 $this->loadLayout()
00042 ->_setActiveMenu('promo/quote')
00043 ->_addBreadcrumb(Mage::helper('salesrule')->__('Promotions'), Mage::helper('salesrule')->__('Promotions'))
00044 ;
00045 return $this;
00046 }
00047
00048 public function indexAction()
00049 {
00050 $this->_initAction()
00051 ->_addBreadcrumb(Mage::helper('salesrule')->__('Catalog'), Mage::helper('salesrule')->__('Catalog'))
00052 ->_addContent($this->getLayout()->createBlock('adminhtml/promo_quote'))
00053 ->renderLayout();
00054 }
00055
00056 public function newAction()
00057 {
00058 $this->_forward('edit');
00059 }
00060
00061 public function editAction()
00062 {
00063 $id = $this->getRequest()->getParam('id');
00064 $model = Mage::getModel('salesrule/rule');
00065
00066 if ($id) {
00067 $model->load($id);
00068 if (! $model->getRuleId()) {
00069 Mage::getSingleton('adminhtml/session')->addError(Mage::helper('salesrule')->__('This rule no longer exists'));
00070 $this->_redirect('*/*');
00071 return;
00072 }
00073 }
00074
00075
00076 $data = Mage::getSingleton('adminhtml/session')->getPageData(true);
00077 if (!empty($data)) {
00078 $model->addData($data);
00079 }
00080
00081 $model->getConditions()->setJsFormObject('rule_conditions_fieldset');
00082 $model->getActions()->setJsFormObject('rule_actions_fieldset');
00083
00084 Mage::register('current_promo_quote_rule', $model);
00085
00086 $block = $this->getLayout()->createBlock('adminhtml/promo_quote_edit')
00087 ->setData('action', $this->getUrl('*/*/save'));
00088
00089 $this->_initAction();
00090 $this->getLayout()->getBlock('head')
00091 ->setCanLoadExtJs(true)
00092 ->setCanLoadRulesJs(true);
00093
00094 $this
00095 ->_addBreadcrumb($id ? Mage::helper('salesrule')->__('Edit Rule') : Mage::helper('salesrule')->__('New Rule'), $id ? Mage::helper('salesrule')->__('Edit Rule') : Mage::helper('salesrule')->__('New Rule'))
00096 ->_addContent($block)
00097 ->_addLeft($this->getLayout()->createBlock('adminhtml/promo_quote_edit_tabs'))
00098 ->renderLayout();
00099
00100 }
00101
00102 public function saveAction()
00103 {
00104 if ($data = $this->getRequest()->getPost()) {
00105 try {
00106 $model = Mage::getModel('salesrule/rule');
00107
00108 if ($id = $this->getRequest()->getParam('rule_id')) {
00109 $model->load($id);
00110 if ($id != $model->getId()) {
00111 Mage::throwException(Mage::helper('salesrule')->__('Wrong rule specified.'));
00112 }
00113 }
00114 if (isset($data['simple_action']) && $data['simple_action'] == 'by_percent' && isset($data['discount_amount'])) {
00115 $data['discount_amount'] = min(100,$data['discount_amount']);
00116 }
00117 if (isset($data['rule']['conditions'])) {
00118 $data['conditions'] = $data['rule']['conditions'];
00119 }
00120 if (isset($data['rule']['actions'])) {
00121 $data['actions'] = $data['rule']['actions'];
00122 }
00123 unset($data['rule']);
00124
00125 $model->loadPost($data);
00126
00127 Mage::getSingleton('adminhtml/session')->setPageData($model->getData());
00128
00129 $model->save();
00130 Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('salesrule')->__('Rule was successfully saved'));
00131 Mage::getSingleton('adminhtml/session')->setPageData(false);
00132 $this->_redirect('*/*/');
00133 return;
00134 } catch (Exception $e) {
00135 Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
00136 Mage::getSingleton('adminhtml/session')->setPageData($data);
00137 $this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('rule_id')));
00138 return;
00139 }
00140 }
00141 $this->_redirect('*/*/');
00142 }
00143
00144 public function deleteAction()
00145 {
00146 if ($id = $this->getRequest()->getParam('id')) {
00147 try {
00148 $model = Mage::getModel('salesrule/rule');
00149 $model->load($id);
00150 $model->delete();
00151 Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('salesrule')->__('Rule was successfully deleted'));
00152 $this->_redirect('*/*/');
00153 return;
00154 }
00155 catch (Exception $e) {
00156 Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
00157 $this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('id')));
00158 return;
00159 }
00160 }
00161 Mage::getSingleton('adminhtml/session')->addError(Mage::helper('salesrule')->__('Unable to find a page to delete'));
00162 $this->_redirect('*/*/');
00163 }
00164
00165 public function newConditionHtmlAction()
00166 {
00167 $id = $this->getRequest()->getParam('id');
00168 $typeArr = explode('|', str_replace('-', '/', $this->getRequest()->getParam('type')));
00169 $type = $typeArr[0];
00170
00171 $model = Mage::getModel($type)
00172 ->setId($id)
00173 ->setType($type)
00174 ->setRule(Mage::getModel('salesrule/rule'))
00175 ->setPrefix('conditions');
00176 if (!empty($typeArr[1])) {
00177 $model->setAttribute($typeArr[1]);
00178 }
00179
00180 if ($model instanceof Mage_Rule_Model_Condition_Abstract) {
00181 $model->setJsFormObject($this->getRequest()->getParam('form'));
00182 $html = $model->asHtmlRecursive();
00183 } else {
00184 $html = '';
00185 }
00186 $this->getResponse()->setBody($html);
00187 }
00188
00189 public function newActionHtmlAction()
00190 {
00191 $id = $this->getRequest()->getParam('id');
00192 $typeArr = explode('|', str_replace('-', '/', $this->getRequest()->getParam('type')));
00193 $type = $typeArr[0];
00194
00195 $model = Mage::getModel($type)
00196 ->setId($id)
00197 ->setType($type)
00198 ->setRule(Mage::getModel('salesrule/rule'))
00199 ->setPrefix('actions');
00200 if (!empty($typeArr[1])) {
00201 $model->setAttribute($typeArr[1]);
00202 }
00203
00204 if ($model instanceof Mage_Rule_Model_Condition_Abstract) {
00205 $model->setJsFormObject($this->getRequest()->getParam('form'));
00206 $html = $model->asHtmlRecursive();
00207 } else {
00208 $html = '';
00209 }
00210 $this->getResponse()->setBody($html);
00211 }
00212
00213 public function applyRulesAction()
00214 {
00215 $this->_initAction();
00216
00217 $this->renderLayout();
00218 }
00219
00220 public function gridAction()
00221 {
00222 $this->_initRule();
00223 $this->getResponse()->setBody(
00224 $this->getLayout()->createBlock('adminhtml/promo_quote_edit_tab_product')->toHtml()
00225 );
00226 }
00227
00228 protected function _isAllowed()
00229 {
00230 return Mage::getSingleton('admin/session')->isAllowed('promo/quote');
00231 }
00232
00233 }