PageRenderTime 45ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/jit_bec/shopifine
PHP | 342 lines | 252 code | 37 blank | 53 comment | 23 complexity | 22dce2b5b6b3b064c6c9231a6a2f9833 MD5 | raw file
Possible License(s): LGPL-3.0
  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) 2012 Magento Inc. (http://www.magentocommerce.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. /**
  27. * Backend Catalog Price Rules controller
  28. *
  29. * @category Mage
  30. * @package Mage_Adminhtml
  31. * @author Magento Core Team <core@magentocommerce.com>
  32. */
  33. class Mage_Adminhtml_Promo_CatalogController extends Mage_Adminhtml_Controller_Action
  34. {
  35. /**
  36. * Dirty rules notice message
  37. *
  38. * @var string
  39. */
  40. protected $_dirtyRulesNoticeMessage;
  41. protected function _initAction()
  42. {
  43. $this->loadLayout()
  44. ->_setActiveMenu('promo/catalog')
  45. ->_addBreadcrumb(
  46. Mage::helper('catalogrule')->__('Promotions'),
  47. Mage::helper('catalogrule')->__('Promotions')
  48. );
  49. return $this;
  50. }
  51. public function indexAction()
  52. {
  53. $this->_title($this->__('Promotions'))->_title($this->__('Catalog Price Rules'));
  54. $dirtyRules = Mage::getModel('catalogrule/flag')->loadSelf();
  55. if ($dirtyRules->getState()) {
  56. Mage::getSingleton('adminhtml/session')->addNotice($this->getDirtyRulesNoticeMessage());
  57. }
  58. $this->_initAction()
  59. ->_addBreadcrumb(
  60. Mage::helper('catalogrule')->__('Catalog'),
  61. Mage::helper('catalogrule')->__('Catalog')
  62. )
  63. ->renderLayout();
  64. }
  65. public function newAction()
  66. {
  67. $this->_forward('edit');
  68. }
  69. public function editAction()
  70. {
  71. $this->_title($this->__('Promotions'))->_title($this->__('Catalog Price Rules'));
  72. $id = $this->getRequest()->getParam('id');
  73. $model = Mage::getModel('catalogrule/rule');
  74. if ($id) {
  75. $model->load($id);
  76. if (! $model->getRuleId()) {
  77. Mage::getSingleton('adminhtml/session')->addError(
  78. Mage::helper('catalogrule')->__('This rule no longer exists.')
  79. );
  80. $this->_redirect('*/*');
  81. return;
  82. }
  83. }
  84. $this->_title($model->getRuleId() ? $model->getName() : $this->__('New Rule'));
  85. // set entered data if was error when we do save
  86. $data = Mage::getSingleton('adminhtml/session')->getPageData(true);
  87. if (!empty($data)) {
  88. $model->addData($data);
  89. }
  90. $model->getConditions()->setJsFormObject('rule_conditions_fieldset');
  91. Mage::register('current_promo_catalog_rule', $model);
  92. $this->_initAction()->getLayout()->getBlock('promo_catalog_edit')
  93. ->setData('action', $this->getUrl('*/promo_catalog/save'));
  94. $breadcrumb = $id
  95. ? Mage::helper('catalogrule')->__('Edit Rule')
  96. : Mage::helper('catalogrule')->__('New Rule');
  97. $this->_addBreadcrumb($breadcrumb, $breadcrumb)->renderLayout();
  98. }
  99. public function saveAction()
  100. {
  101. if ($this->getRequest()->getPost()) {
  102. try {
  103. $model = Mage::getModel('catalogrule/rule');
  104. Mage::dispatchEvent(
  105. 'adminhtml_controller_catalogrule_prepare_save',
  106. array('request' => $this->getRequest())
  107. );
  108. $data = $this->getRequest()->getPost();
  109. $data = $this->_filterDates($data, array('from_date', 'to_date'));
  110. if ($id = $this->getRequest()->getParam('rule_id')) {
  111. $model->load($id);
  112. if ($id != $model->getId()) {
  113. Mage::throwException(Mage::helper('catalogrule')->__('Wrong rule specified.'));
  114. }
  115. }
  116. $validateResult = $model->validateData(new Varien_Object($data));
  117. if ($validateResult !== true) {
  118. foreach($validateResult as $errorMessage) {
  119. $this->_getSession()->addError($errorMessage);
  120. }
  121. $this->_getSession()->setPageData($data);
  122. $this->_redirect('*/*/edit', array('id'=>$model->getId()));
  123. return;
  124. }
  125. $data['conditions'] = $data['rule']['conditions'];
  126. unset($data['rule']);
  127. $autoApply = false;
  128. if (!empty($data['auto_apply'])) {
  129. $autoApply = true;
  130. unset($data['auto_apply']);
  131. }
  132. $model->loadPost($data);
  133. Mage::getSingleton('adminhtml/session')->setPageData($model->getData());
  134. $model->save();
  135. Mage::getSingleton('adminhtml/session')->addSuccess(
  136. Mage::helper('catalogrule')->__('The rule has been saved.')
  137. );
  138. Mage::getSingleton('adminhtml/session')->setPageData(false);
  139. if ($autoApply) {
  140. $this->getRequest()->setParam('rule_id', $model->getId());
  141. $this->_forward('applyRules');
  142. } else {
  143. Mage::getModel('catalogrule/flag')->loadSelf()
  144. ->setState(1)
  145. ->save();
  146. if ($this->getRequest()->getParam('back')) {
  147. $this->_redirect('*/*/edit', array('id' => $model->getId()));
  148. return;
  149. }
  150. $this->_redirect('*/*/');
  151. }
  152. return;
  153. } catch (Mage_Core_Exception $e) {
  154. $this->_getSession()->addError($e->getMessage());
  155. } catch (Exception $e) {
  156. $this->_getSession()->addError(
  157. Mage::helper('catalogrule')->__('An error occurred while saving the rule data. Please review the log and try again.')
  158. );
  159. Mage::logException($e);
  160. Mage::getSingleton('adminhtml/session')->setPageData($data);
  161. $this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('rule_id')));
  162. return;
  163. }
  164. }
  165. $this->_redirect('*/*/');
  166. }
  167. public function deleteAction()
  168. {
  169. if ($id = $this->getRequest()->getParam('id')) {
  170. try {
  171. $model = Mage::getModel('catalogrule/rule');
  172. $model->load($id);
  173. $model->delete();
  174. Mage::getModel('catalogrule/flag')->loadSelf()
  175. ->setState(1)
  176. ->save();
  177. Mage::getSingleton('adminhtml/session')->addSuccess(
  178. Mage::helper('catalogrule')->__('The rule has been deleted.')
  179. );
  180. $this->_redirect('*/*/');
  181. return;
  182. } catch (Mage_Core_Exception $e) {
  183. $this->_getSession()->addError($e->getMessage());
  184. } catch (Exception $e) {
  185. $this->_getSession()->addError(
  186. Mage::helper('catalogrule')->__('An error occurred while deleting the rule. Please review the log and try again.')
  187. );
  188. Mage::logException($e);
  189. $this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('id')));
  190. return;
  191. }
  192. }
  193. Mage::getSingleton('adminhtml/session')->addError(
  194. Mage::helper('catalogrule')->__('Unable to find a rule to delete.')
  195. );
  196. $this->_redirect('*/*/');
  197. }
  198. public function newConditionHtmlAction()
  199. {
  200. $id = $this->getRequest()->getParam('id');
  201. $typeArr = explode('|', str_replace('-', '/', $this->getRequest()->getParam('type')));
  202. $type = $typeArr[0];
  203. $model = Mage::getModel($type)
  204. ->setId($id)
  205. ->setType($type)
  206. ->setRule(Mage::getModel('catalogrule/rule'))
  207. ->setPrefix('conditions');
  208. if (!empty($typeArr[1])) {
  209. $model->setAttribute($typeArr[1]);
  210. }
  211. if ($model instanceof Mage_Rule_Model_Condition_Abstract) {
  212. $model->setJsFormObject($this->getRequest()->getParam('form'));
  213. $html = $model->asHtmlRecursive();
  214. } else {
  215. $html = '';
  216. }
  217. $this->getResponse()->setBody($html);
  218. }
  219. public function chooserAction()
  220. {
  221. switch ($this->getRequest()->getParam('attribute')) {
  222. case 'sku':
  223. $type = 'adminhtml/promo_widget_chooser_sku';
  224. break;
  225. case 'categories':
  226. $type = 'adminhtml/promo_widget_chooser_categories';
  227. break;
  228. }
  229. if (!empty($type)) {
  230. $block = $this->getLayout()->createBlock($type);
  231. if ($block) {
  232. $this->getResponse()->setBody($block->toHtml());
  233. }
  234. }
  235. }
  236. public function newActionHtmlAction()
  237. {
  238. $id = $this->getRequest()->getParam('id');
  239. $typeArr = explode('|', str_replace('-', '/', $this->getRequest()->getParam('type')));
  240. $type = $typeArr[0];
  241. $model = Mage::getModel($type)
  242. ->setId($id)
  243. ->setType($type)
  244. ->setRule(Mage::getModel('catalogrule/rule'))
  245. ->setPrefix('actions');
  246. if (!empty($typeArr[1])) {
  247. $model->setAttribute($typeArr[1]);
  248. }
  249. if ($model instanceof Mage_Rule_Model_Action_Abstract) {
  250. $model->setJsFormObject($this->getRequest()->getParam('form'));
  251. $html = $model->asHtmlRecursive();
  252. } else {
  253. $html = '';
  254. }
  255. $this->getResponse()->setBody($html);
  256. }
  257. /**
  258. * Apply all active catalog price rules
  259. */
  260. public function applyRulesAction()
  261. {
  262. $errorMessage = Mage::helper('catalogrule')->__('Unable to apply rules.');
  263. try {
  264. Mage::getModel('catalogrule/rule')->applyAll();
  265. Mage::getModel('catalogrule/flag')->loadSelf()
  266. ->setState(0)
  267. ->save();
  268. $this->_getSession()->addSuccess(Mage::helper('catalogrule')->__('The rules have been applied.'));
  269. } catch (Mage_Core_Exception $e) {
  270. $this->_getSession()->addError($errorMessage . ' ' . $e->getMessage());
  271. } catch (Exception $e) {
  272. $this->_getSession()->addError($errorMessage);
  273. }
  274. $this->_redirect('*/*');
  275. }
  276. /**
  277. * @deprecated since 1.5.0.0
  278. */
  279. public function addToAlersAction()
  280. {
  281. }
  282. protected function _isAllowed()
  283. {
  284. return Mage::getSingleton('admin/session')->isAllowed('promo/catalog');
  285. }
  286. /**
  287. * Set dirty rules notice message
  288. *
  289. * @param string $dirtyRulesNoticeMessage
  290. */
  291. public function setDirtyRulesNoticeMessage($dirtyRulesNoticeMessage)
  292. {
  293. $this->_dirtyRulesNoticeMessage = $dirtyRulesNoticeMessage;
  294. }
  295. /**
  296. * Get dirty rules notice message
  297. *
  298. * @return string
  299. */
  300. public function getDirtyRulesNoticeMessage()
  301. {
  302. $defaultMessage = 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.');
  303. return $this->_dirtyRulesNoticeMessage ? $this->_dirtyRulesNoticeMessage : $defaultMessage;
  304. }
  305. }