PageRenderTime 80ms CodeModel.GetById 32ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/claudiu_marginean/magento-hg-mirror
PHP | 285 lines | 221 code | 36 blank | 28 comment | 24 complexity | 9f2e56ec610a54a77e7c12b8ef0719bb MD5 | raw file
Possible License(s): CC-BY-SA-3.0, LGPL-2.1, GPL-2.0, WTFPL
  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_CatalogController extends Mage_Adminhtml_Controller_Action
  27. {
  28. protected function _initAction()
  29. {
  30. $this->loadLayout()
  31. ->_setActiveMenu('promo/catalog')
  32. ->_addBreadcrumb(Mage::helper('catalogrule')->__('Promotions'), Mage::helper('catalogrule')->__('Promotions'));
  33. return $this;
  34. }
  35. public function indexAction()
  36. {
  37. $this->_title($this->__('Promotions'))->_title($this->__('Catalog Price Rules'));
  38. if (Mage::app()->loadCache('catalog_rules_dirty')) {
  39. Mage::getSingleton('adminhtml/session')->addNotice(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.'));
  40. }
  41. $this->_initAction()
  42. ->_addBreadcrumb(Mage::helper('catalogrule')->__('Catalog'), Mage::helper('catalogrule')->__('Catalog'))
  43. ->renderLayout();
  44. }
  45. public function newAction()
  46. {
  47. $this->_forward('edit');
  48. }
  49. public function editAction()
  50. {
  51. $this->_title($this->__('Promotions'))->_title($this->__('Catalog Price Rules'));
  52. $id = $this->getRequest()->getParam('id');
  53. $model = Mage::getModel('catalogrule/rule');
  54. if ($id) {
  55. $model->load($id);
  56. if (! $model->getRuleId()) {
  57. Mage::getSingleton('adminhtml/session')->addError(Mage::helper('catalogrule')->__('This rule no longer exists.'));
  58. $this->_redirect('*/*');
  59. return;
  60. }
  61. }
  62. $this->_title($model->getRuleId() ? $model->getName() : $this->__('New Rule'));
  63. // set entered data if was error when we do save
  64. $data = Mage::getSingleton('adminhtml/session')->getPageData(true);
  65. if (!empty($data)) {
  66. $model->addData($data);
  67. }
  68. $model->getConditions()->setJsFormObject('rule_conditions_fieldset');
  69. Mage::register('current_promo_catalog_rule', $model);
  70. $this->_initAction()->getLayout()->getBlock('promo_catalog_edit')
  71. ->setData('action', $this->getUrl('*/promo_catalog/save'));
  72. $this
  73. ->_addBreadcrumb($id ? Mage::helper('catalogrule')->__('Edit Rule') : Mage::helper('catalogrule')->__('New Rule'), $id ? Mage::helper('catalogrule')->__('Edit Rule') : Mage::helper('catalogrule')->__('New Rule'))
  74. ->renderLayout();
  75. }
  76. public function saveAction()
  77. {
  78. if ($this->getRequest()->getPost()) {
  79. try {
  80. $model = Mage::getModel('catalogrule/rule');
  81. Mage::dispatchEvent('adminhtml_controller_catalogrule_prepare_save', array('request' => $this->getRequest()));
  82. $data = $this->getRequest()->getPost();
  83. $data = $this->_filterDates($data, array('from_date', 'to_date'));
  84. if ($id = $this->getRequest()->getParam('rule_id')) {
  85. $model->load($id);
  86. if ($id != $model->getId()) {
  87. Mage::throwException(Mage::helper('catalogrule')->__('Wrong rule specified.'));
  88. }
  89. }
  90. $validateResult = $model->validateData(new Varien_Object($data));
  91. if ($validateResult !== true) {
  92. foreach($validateResult as $errorMessage) {
  93. $this->_getSession()->addError($errorMessage);
  94. }
  95. $this->_getSession()->setPageData($data);
  96. $this->_redirect('*/*/edit', array('id'=>$model->getId()));
  97. return;
  98. }
  99. $data['conditions'] = $data['rule']['conditions'];
  100. unset($data['rule']);
  101. if (!empty($data['auto_apply'])) {
  102. $autoApply = true;
  103. unset($data['auto_apply']);
  104. } else {
  105. $autoApply = false;
  106. }
  107. $model->loadPost($data);
  108. Mage::getSingleton('adminhtml/session')->setPageData($model->getData());
  109. $model->save();
  110. Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('catalogrule')->__('The rule has been saved.'));
  111. Mage::getSingleton('adminhtml/session')->setPageData(false);
  112. if ($autoApply) {
  113. $this->_forward('applyRules');
  114. } else {
  115. Mage::app()->saveCache(1, 'catalog_rules_dirty');
  116. if ($this->getRequest()->getParam('back')) {
  117. $this->_redirect('*/*/edit', array('id' => $model->getId()));
  118. return;
  119. }
  120. $this->_redirect('*/*/');
  121. }
  122. return;
  123. } catch (Mage_Core_Exception $e) {
  124. $this->_getSession()->addError($e->getMessage());
  125. } catch (Exception $e) {
  126. $this->_getSession()->addError(Mage::helper('catalogrule')->__('An error occurred while saving the rule data. Please review the log and try again.'));
  127. Mage::logException($e);
  128. Mage::getSingleton('adminhtml/session')->setPageData($data);
  129. $this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('rule_id')));
  130. return;
  131. }
  132. }
  133. $this->_redirect('*/*/');
  134. }
  135. public function deleteAction()
  136. {
  137. if ($id = $this->getRequest()->getParam('id')) {
  138. try {
  139. $model = Mage::getModel('catalogrule/rule');
  140. $model->load($id);
  141. $model->delete();
  142. Mage::app()->saveCache(1, 'catalog_rules_dirty');
  143. Mage::getSingleton('adminhtml/session')->addSuccess(Mage::helper('catalogrule')->__('The rule has been deleted.'));
  144. $this->_redirect('*/*/');
  145. return;
  146. } catch (Mage_Core_Exception $e) {
  147. $this->_getSession()->addError($e->getMessage());
  148. } catch (Exception $e) {
  149. $this->_getSession()->addError(Mage::helper('catalogrule')->__('An error occurred while deleting the rule. Please review the log and try again.'));
  150. Mage::logException($e);
  151. $this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('id')));
  152. return;
  153. }
  154. }
  155. Mage::getSingleton('adminhtml/session')->addError(Mage::helper('catalogrule')->__('Unable to find a rule to delete.'));
  156. $this->_redirect('*/*/');
  157. }
  158. public function newConditionHtmlAction()
  159. {
  160. $id = $this->getRequest()->getParam('id');
  161. $typeArr = explode('|', str_replace('-', '/', $this->getRequest()->getParam('type')));
  162. $type = $typeArr[0];
  163. $model = Mage::getModel($type)
  164. ->setId($id)
  165. ->setType($type)
  166. ->setRule(Mage::getModel('catalogrule/rule'))
  167. ->setPrefix('conditions');
  168. if (!empty($typeArr[1])) {
  169. $model->setAttribute($typeArr[1]);
  170. }
  171. if ($model instanceof Mage_Rule_Model_Condition_Abstract) {
  172. $model->setJsFormObject($this->getRequest()->getParam('form'));
  173. $html = $model->asHtmlRecursive();
  174. } else {
  175. $html = '';
  176. }
  177. $this->getResponse()->setBody($html);
  178. }
  179. public function chooserAction()
  180. {
  181. switch ($this->getRequest()->getParam('attribute')) {
  182. case 'sku':
  183. $type = 'adminhtml/promo_widget_chooser_sku';
  184. break;
  185. case 'categories':
  186. $type = 'adminhtml/promo_widget_chooser_categories';
  187. break;
  188. }
  189. if (!empty($type)) {
  190. $block = $this->getLayout()->createBlock($type);
  191. if ($block) {
  192. $this->getResponse()->setBody($block->toHtml());
  193. }
  194. }
  195. }
  196. public function newActionHtmlAction()
  197. {
  198. $id = $this->getRequest()->getParam('id');
  199. $typeArr = explode('|', str_replace('-', '/', $this->getRequest()->getParam('type')));
  200. $type = $typeArr[0];
  201. $model = Mage::getModel($type)
  202. ->setId($id)
  203. ->setType($type)
  204. ->setRule(Mage::getModel('catalogrule/rule'))
  205. ->setPrefix('actions');
  206. if (!empty($typeArr[1])) {
  207. $model->setAttribute($typeArr[1]);
  208. }
  209. if ($model instanceof Mage_Rule_Model_Action_Abstract) {
  210. $model->setJsFormObject($this->getRequest()->getParam('form'));
  211. $html = $model->asHtmlRecursive();
  212. } else {
  213. $html = '';
  214. }
  215. $this->getResponse()->setBody($html);
  216. }
  217. /**
  218. * Apply all active catalog price rules
  219. */
  220. public function applyRulesAction()
  221. {
  222. try {
  223. Mage::getModel('catalogrule/rule')->applyAll();
  224. Mage::app()->removeCache('catalog_rules_dirty');
  225. Mage::getSingleton('adminhtml/session')->addSuccess(
  226. Mage::helper('catalogrule')->__('The rules have been applied.')
  227. );
  228. } catch (Exception $e) {
  229. Mage::getSingleton('adminhtml/session')->addError(
  230. Mage::helper('catalogrule')->__('Unable to apply rules.')
  231. );
  232. throw $e;
  233. }
  234. $this->_redirect('*/*');
  235. }
  236. public function addToAlersAction()
  237. {
  238. $alerts = Mage::getResourceModel('customeralert/type')->getAlertsForCronChecking();
  239. foreach ($alerts as $val) {
  240. Mage::getSingleton('customeralert/config')->getAlertByType('price_is_changed')
  241. ->setParamValues($val)
  242. ->updateForPriceRule();
  243. }
  244. }
  245. protected function _isAllowed()
  246. {
  247. return Mage::getSingleton('admin/session')->isAllowed('promo/catalog');
  248. }
  249. }