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_CatalogController extends Mage_Adminhtml_Controller_Action
00029 {
00030     protected function _initAction()
00031     {
00032         $this->loadLayout()
00033             ->_setActiveMenu('promo/catalog')
00034             ->_addBreadcrumb(Mage::helper('catalogrule')->__('Promotions'), Mage::helper('catalogrule')->__('Promotions'))
00035         ;
00036         return $this;
00037     }
00038 
00039     public function indexAction()
00040     {
00041         if (Mage::app()->loadCache('catalog_rules_dirty')) {
00042             Mage::getSingleton('adminhtml/session')->addNotice(Mage::helper('catalogrule')->__('There are rules that have been changed but not applied. Please, click Apply Rules in order to see immediate effect in catalog.'));
00043         }
00044 
00045         $this->_initAction()
00046             ->_addBreadcrumb(Mage::helper('catalogrule')->__('Catalog'), Mage::helper('catalogrule')->__('Catalog'))
00047             ->_addContent($this->getLayout()->createBlock('adminhtml/promo_catalog'))
00048             ->renderLayout();
00049     }
00050 
00051     public function newAction()
00052     {
00053         $this->_forward('edit');
00054     }
00055 
00056     public function editAction()
00057     {
00058         $id = $this->getRequest()->getParam('id');
00059         $model = Mage::getModel('catalogrule/rule');
00060 
00061         if ($id) {
00062             $model->load($id);
00063             if (! $model->getRuleId()) {
00064                 Mage::getSingleton('adminhtml/session')->addError(Mage::helper('catalogrule')->__('This rule no longer exists'));
00065                 $this->_redirect('*/*');
00066                 return;
00067             }
00068         }
00069 
00070         
00071         $data = Mage::getSingleton('adminhtml/session')->getPageData(true);
00072         if (!empty($data)) {
00073             $model->addData($data);
00074         }
00075         $model->getConditions()->setJsFormObject('rule_conditions_fieldset');
00076 
00077         Mage::register('current_promo_catalog_rule', $model);
00078 
00079         $block = $this->getLayout()->createBlock('adminhtml/promo_catalog_edit')
00080             ->setData('action', $this->getUrl('*/promo_catalog/save'));
00081 
00082         $this->_initAction();
00083 
00084         $this->getLayout()->getBlock('head')
00085             ->setCanLoadExtJs(true)
00086             ->setCanLoadRulesJs(true);
00087 
00088         $this
00089             ->_addBreadcrumb($id ? Mage::helper('catalogrule')->__('Edit Rule') : Mage::helper('catalogrule')->__('New Rule'), $id ? Mage::helper('catalogrule')->__('Edit Rule') : Mage::helper('catalogrule')->__('New Rule'))
00090             ->_addContent($block)
00091             ->_addLeft($this->getLayout()->createBlock('adminhtml/promo_catalog_edit_tabs'))
00092             ->renderLayout();
00093 
00094     }
00095 
00096     public function saveAction()
00097     {
00098         if ($data = $this->getRequest()->getPost()) {
00099             try {
00100                 $model = Mage::getModel('catalogrule/rule');
00101                 if ($id = $this->getRequest()->getParam('rule_id')) {
00102                     $model->load($id);
00103                     if ($id != $model->getId()) {
00104                         Mage::throwException(Mage::helper('catalogrule')->__('Wrong rule specified.'));
00105                     }
00106                 }
00107                 $data['conditions'] = $data['rule']['conditions'];
00108                 unset($data['rule']);
00109 
00110                 if (!empty($data['auto_apply'])) {
00111                     $autoApply = true;
00112                     unset($data['auto_apply']);
00113                 } else {
00114                     $autoApply = false;
00115                 }
00116 
00117                 $model->loadPost($data);
00118                 Mage::getSingleton('adminhtml/session')->setPageData($model->getData());
00119                 $model->save();
00120 
00121                 Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('catalogrule')->__('Rule was successfully saved'));
00122                 Mage::getSingleton('adminhtml/session')->setPageData(false);
00123                 if ($autoApply) {
00124                     $this->_forward('applyRules');
00125                 } else {
00126                     Mage::app()->saveCache(1, 'catalog_rules_dirty');
00127                     $this->_redirect('*/*/');
00128                 }
00129                 return;
00130             } catch (Exception $e) {
00131                 Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
00132                 Mage::getSingleton('adminhtml/session')->setPageData($data);
00133                 $this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('rule_id')));
00134                 return;
00135             }
00136         }
00137         $this->_redirect('*/*/');
00138     }
00139 
00140     public function deleteAction()
00141     {
00142         if ($id = $this->getRequest()->getParam('id')) {
00143             try {
00144                 $model = Mage::getModel('catalogrule/rule');
00145                 $model->load($id);
00146                 $model->delete();
00147                 Mage::app()->saveCache(1, 'catalog_rules_dirty');
00148                 Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('catalogrule')->__('Rule was successfully deleted'));
00149                 $this->_redirect('*/*/');
00150                 return;
00151             }
00152             catch (Exception $e) {
00153                 Mage::getSingleton('adminhtml/session')->addError($e->getMessage());
00154                 $this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('id')));
00155                 return;
00156             }
00157         }
00158         Mage::getSingleton('adminhtml/session')->addError(Mage::helper('catalogrule')->__('Unable to find a page to delete'));
00159         $this->_redirect('*/*/');
00160     }
00161 
00162     public function newConditionHtmlAction()
00163     {
00164         $id = $this->getRequest()->getParam('id');
00165         $typeArr = explode('|', str_replace('-', '/', $this->getRequest()->getParam('type')));
00166         $type = $typeArr[0];
00167 
00168         $model = Mage::getModel($type)
00169             ->setId($id)
00170             ->setType($type)
00171             ->setRule(Mage::getModel('catalogrule/rule'))
00172             ->setPrefix('conditions');
00173         if (!empty($typeArr[1])) {
00174             $model->setAttribute($typeArr[1]);
00175         }
00176 
00177         if ($model instanceof Mage_Rule_Model_Condition_Abstract) {
00178             $model->setJsFormObject($this->getRequest()->getParam('form'));
00179             $html = $model->asHtmlRecursive();
00180         } else {
00181             $html = '';
00182         }
00183         $this->getResponse()->setBody($html);
00184     }
00185 
00186     public function chooserAction()
00187     {
00188         switch ($this->getRequest()->getParam('attribute')) {
00189             case 'sku':
00190                 $type = 'adminhtml/promo_widget_chooser_sku';
00191                 break;
00192 
00193             case 'categories':
00194                 $type = 'adminhtml/promo_widget_chooser_categories';
00195                 break;
00196         }
00197         if (!empty($type)) {
00198             $block = $this->getLayout()->createBlock($type);
00199             if ($block) {
00200                 $this->getResponse()->setBody($block->toHtml());
00201             }
00202         }
00203     }
00204 
00205     public function newActionHtmlAction()
00206     {
00207         $id = $this->getRequest()->getParam('id');
00208         $typeArr = explode('|', str_replace('-', '/', $this->getRequest()->getParam('type')));
00209         $type = $typeArr[0];
00210 
00211         $model = Mage::getModel($type)
00212             ->setId($id)
00213             ->setType($type)
00214             ->setRule(Mage::getModel('catalogrule/rule'))
00215             ->setPrefix('actions');
00216         if (!empty($typeArr[1])) {
00217             $model->setAttribute($typeArr[1]);
00218         }
00219 
00220         if ($model instanceof Mage_Rule_Model_Action_Abstract) {
00221             $model->setJsFormObject($this->getRequest()->getParam('form'));
00222             $html = $model->asHtmlRecursive();
00223         } else {
00224             $html = '';
00225         }
00226         $this->getResponse()->setBody($html);
00227     }
00228 
00229 
00230 
00231 
00232     public function applyRulesAction()
00233     {
00234         try {
00235             $resource = Mage::getResourceSingleton('catalogrule/rule');
00236             $resource->applyAllRulesForDateRange();
00237             Mage::app()->removeCache('catalog_rules_dirty');
00238             Mage::getSingleton('adminhtml/session')->addSuccess(
00239                 Mage::helper('catalogrule')->__('Rules were successfully applied')
00240             );
00241         } catch (Exception $e) {
00242             Mage::getSingleton('adminhtml/session')->addError(
00243                 Mage::helper('catalogrule')->__('Unable to apply rules')
00244             );
00245             throw $e;
00246         }
00247 
00248         $this->_redirect('*/*');
00249     }
00250 
00251     public function addToAlersAction()
00252     {
00253         $alerts = Mage::getResourceModel('customeralert/type')->getAlertsForCronChecking();
00254         foreach ($alerts as $val) {
00255             Mage::getSingleton('customeralert/config')->getAlertByType('price_is_changed')
00256                 ->setParamValues($val)
00257                 ->updateForPriceRule();
00258         }
00259     }
00260 
00261     protected function _isAllowed()
00262     {
00263         return Mage::getSingleton('admin/session')->isAllowed('promo/catalog');
00264     }
00265 }