PageRenderTime 38ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/jit_bec/shopifine
PHP | 421 lines | 313 code | 50 blank | 58 comment | 39 complexity | 8c51fc4e9dcb2bd174db70cb90277b37 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. 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. $id = (int)$this->getRequest()->getParam('id');
  33. if (!$id && $this->getRequest()->getParam('rule_id')) {
  34. $id = (int)$this->getRequest()->getParam('rule_id');
  35. }
  36. if ($id) {
  37. Mage::registry('current_promo_quote_rule')->load($id);
  38. }
  39. }
  40. protected function _initAction()
  41. {
  42. $this->loadLayout()
  43. ->_setActiveMenu('promo/quote')
  44. ->_addBreadcrumb(Mage::helper('salesrule')->__('Promotions'), Mage::helper('salesrule')->__('Promotions'))
  45. ;
  46. return $this;
  47. }
  48. public function indexAction()
  49. {
  50. $this->_title($this->__('Promotions'))->_title($this->__('Shopping Cart Price Rules'));
  51. $this->_initAction()
  52. ->_addBreadcrumb(Mage::helper('salesrule')->__('Catalog'), Mage::helper('salesrule')->__('Catalog'))
  53. ->renderLayout();
  54. }
  55. public function newAction()
  56. {
  57. $this->_forward('edit');
  58. }
  59. public function editAction()
  60. {
  61. $id = $this->getRequest()->getParam('id');
  62. $model = Mage::getModel('salesrule/rule');
  63. if ($id) {
  64. $model->load($id);
  65. if (! $model->getRuleId()) {
  66. Mage::getSingleton('adminhtml/session')->addError(
  67. Mage::helper('salesrule')->__('This rule no longer exists.'));
  68. $this->_redirect('*/*');
  69. return;
  70. }
  71. }
  72. $this->_title($model->getRuleId() ? $model->getName() : $this->__('New Rule'));
  73. // set entered data if was error when we do save
  74. $data = Mage::getSingleton('adminhtml/session')->getPageData(true);
  75. if (!empty($data)) {
  76. $model->addData($data);
  77. }
  78. $model->getConditions()->setJsFormObject('rule_conditions_fieldset');
  79. $model->getActions()->setJsFormObject('rule_actions_fieldset');
  80. Mage::register('current_promo_quote_rule', $model);
  81. $this->_initAction()->getLayout()->getBlock('promo_quote_edit')
  82. ->setData('action', $this->getUrl('*/*/save'));
  83. $this
  84. ->_addBreadcrumb(
  85. $id ? Mage::helper('salesrule')->__('Edit Rule')
  86. : Mage::helper('salesrule')->__('New Rule'),
  87. $id ? Mage::helper('salesrule')->__('Edit Rule')
  88. : Mage::helper('salesrule')->__('New Rule'))
  89. ->renderLayout();
  90. }
  91. /**
  92. * Promo quote save action
  93. *
  94. */
  95. public function saveAction()
  96. {
  97. if ($this->getRequest()->getPost()) {
  98. try {
  99. /** @var $model Mage_SalesRule_Model_Rule */
  100. $model = Mage::getModel('salesrule/rule');
  101. Mage::dispatchEvent(
  102. 'adminhtml_controller_salesrule_prepare_save',
  103. array('request' => $this->getRequest()));
  104. $data = $this->getRequest()->getPost();
  105. $data = $this->_filterDates($data, array('from_date', 'to_date'));
  106. $id = $this->getRequest()->getParam('rule_id');
  107. if ($id) {
  108. $model->load($id);
  109. if ($id != $model->getId()) {
  110. Mage::throwException(Mage::helper('salesrule')->__('Wrong rule specified.'));
  111. }
  112. }
  113. $session = Mage::getSingleton('adminhtml/session');
  114. $validateResult = $model->validateData(new Varien_Object($data));
  115. if ($validateResult !== true) {
  116. foreach($validateResult as $errorMessage) {
  117. $session->addError($errorMessage);
  118. }
  119. $session->setPageData($data);
  120. $this->_redirect('*/*/edit', array('id'=>$model->getId()));
  121. return;
  122. }
  123. if (isset($data['simple_action']) && $data['simple_action'] == 'by_percent'
  124. && isset($data['discount_amount'])) {
  125. $data['discount_amount'] = min(100,$data['discount_amount']);
  126. }
  127. if (isset($data['rule']['conditions'])) {
  128. $data['conditions'] = $data['rule']['conditions'];
  129. }
  130. if (isset($data['rule']['actions'])) {
  131. $data['actions'] = $data['rule']['actions'];
  132. }
  133. unset($data['rule']);
  134. $model->loadPost($data);
  135. $useAutoGeneration = (int)!empty($data['use_auto_generation']);
  136. $model->setUseAutoGeneration($useAutoGeneration);
  137. $session->setPageData($model->getData());
  138. $model->save();
  139. $session->addSuccess(Mage::helper('salesrule')->__('The rule has been saved.'));
  140. $session->setPageData(false);
  141. if ($this->getRequest()->getParam('back')) {
  142. $this->_redirect('*/*/edit', array('id' => $model->getId()));
  143. return;
  144. }
  145. $this->_redirect('*/*/');
  146. return;
  147. } catch (Mage_Core_Exception $e) {
  148. $this->_getSession()->addError($e->getMessage());
  149. $id = (int)$this->getRequest()->getParam('rule_id');
  150. if (!empty($id)) {
  151. $this->_redirect('*/*/edit', array('id' => $id));
  152. } else {
  153. $this->_redirect('*/*/new');
  154. }
  155. return;
  156. } catch (Exception $e) {
  157. $this->_getSession()->addError(
  158. Mage::helper('catalogrule')->__('An error occurred while saving the rule data. Please review the log and try again.'));
  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('salesrule/rule');
  172. $model->load($id);
  173. $model->delete();
  174. Mage::getSingleton('adminhtml/session')->addSuccess(
  175. Mage::helper('salesrule')->__('The rule has been deleted.'));
  176. $this->_redirect('*/*/');
  177. return;
  178. } catch (Mage_Core_Exception $e) {
  179. $this->_getSession()->addError($e->getMessage());
  180. } catch (Exception $e) {
  181. $this->_getSession()->addError(
  182. Mage::helper('catalogrule')->__('An error occurred while deleting the rule. Please review the log and try again.'));
  183. Mage::logException($e);
  184. $this->_redirect('*/*/edit', array('id' => $this->getRequest()->getParam('id')));
  185. return;
  186. }
  187. }
  188. Mage::getSingleton('adminhtml/session')->addError(
  189. Mage::helper('salesrule')->__('Unable to find a rule to delete.'));
  190. $this->_redirect('*/*/');
  191. }
  192. public function newConditionHtmlAction()
  193. {
  194. $id = $this->getRequest()->getParam('id');
  195. $typeArr = explode('|', str_replace('-', '/', $this->getRequest()->getParam('type')));
  196. $type = $typeArr[0];
  197. $model = Mage::getModel($type)
  198. ->setId($id)
  199. ->setType($type)
  200. ->setRule(Mage::getModel('salesrule/rule'))
  201. ->setPrefix('conditions');
  202. if (!empty($typeArr[1])) {
  203. $model->setAttribute($typeArr[1]);
  204. }
  205. if ($model instanceof Mage_Rule_Model_Condition_Abstract) {
  206. $model->setJsFormObject($this->getRequest()->getParam('form'));
  207. $html = $model->asHtmlRecursive();
  208. } else {
  209. $html = '';
  210. }
  211. $this->getResponse()->setBody($html);
  212. }
  213. public function newActionHtmlAction()
  214. {
  215. $id = $this->getRequest()->getParam('id');
  216. $typeArr = explode('|', str_replace('-', '/', $this->getRequest()->getParam('type')));
  217. $type = $typeArr[0];
  218. $model = Mage::getModel($type)
  219. ->setId($id)
  220. ->setType($type)
  221. ->setRule(Mage::getModel('salesrule/rule'))
  222. ->setPrefix('actions');
  223. if (!empty($typeArr[1])) {
  224. $model->setAttribute($typeArr[1]);
  225. }
  226. if ($model instanceof Mage_Rule_Model_Condition_Abstract) {
  227. $model->setJsFormObject($this->getRequest()->getParam('form'));
  228. $html = $model->asHtmlRecursive();
  229. } else {
  230. $html = '';
  231. }
  232. $this->getResponse()->setBody($html);
  233. }
  234. public function applyRulesAction()
  235. {
  236. $this->_initAction();
  237. $this->renderLayout();
  238. }
  239. public function gridAction()
  240. {
  241. $this->_initRule()->loadLayout()->renderLayout();
  242. }
  243. /**
  244. * Coupon codes grid
  245. */
  246. public function couponsGridAction()
  247. {
  248. $this->_initRule();
  249. $this->loadLayout()->renderLayout();
  250. }
  251. /**
  252. * Export coupon codes as excel xml file
  253. *
  254. * @return void
  255. */
  256. public function exportCouponsXmlAction()
  257. {
  258. $this->_initRule();
  259. $rule = Mage::registry('current_promo_quote_rule');
  260. if ($rule->getId()) {
  261. $fileName = 'coupon_codes.xml';
  262. $content = $this->getLayout()
  263. ->createBlock('adminhtml/promo_quote_edit_tab_coupons_grid')
  264. ->getExcelFile($fileName);
  265. $this->_prepareDownloadResponse($fileName, $content);
  266. } else {
  267. $this->_redirect('*/*/detail', array('_current' => true));
  268. return;
  269. }
  270. }
  271. /**
  272. * Export coupon codes as CSV file
  273. *
  274. * @return void
  275. */
  276. public function exportCouponsCsvAction()
  277. {
  278. $this->_initRule();
  279. $rule = Mage::registry('current_promo_quote_rule');
  280. if ($rule->getId()) {
  281. $fileName = 'coupon_codes.csv';
  282. $content = $this->getLayout()
  283. ->createBlock('adminhtml/promo_quote_edit_tab_coupons_grid')
  284. ->getCsvFile();
  285. $this->_prepareDownloadResponse($fileName, $content);
  286. } else {
  287. $this->_redirect('*/*/detail', array('_current' => true));
  288. return;
  289. }
  290. }
  291. /**
  292. * Coupons mass delete action
  293. */
  294. public function couponsMassDeleteAction()
  295. {
  296. $this->_initRule();
  297. $rule = Mage::registry('current_promo_quote_rule');
  298. if (!$rule->getId()) {
  299. $this->_forward('noRoute');
  300. }
  301. $codesIds = $this->getRequest()->getParam('ids');
  302. if (is_array($codesIds)) {
  303. $couponsCollection = Mage::getResourceModel('salesrule/coupon_collection')
  304. ->addFieldToFilter('coupon_id', array('in' => $codesIds));
  305. foreach ($couponsCollection as $coupon) {
  306. $coupon->delete();
  307. }
  308. }
  309. }
  310. /**
  311. * Generate Coupons action
  312. */
  313. public function generateAction()
  314. {
  315. if (!$this->getRequest()->isAjax()) {
  316. $this->_forward('noRoute');
  317. return;
  318. }
  319. $result = array();
  320. $this->_initRule();
  321. /** @var $rule Mage_SalesRule_Model_Rule */
  322. $rule = Mage::registry('current_promo_quote_rule');
  323. if (!$rule->getId()) {
  324. $result['error'] = Mage::helper('salesrule')->__('Rule is not defined');
  325. } else {
  326. try {
  327. $data = $this->getRequest()->getParams();
  328. if (!empty($data['to_date'])) {
  329. $data = array_merge($data, $this->_filterDates($data, array('to_date')));
  330. }
  331. /** @var $generator Mage_SalesRule_Model_Coupon_Massgenerator */
  332. $generator = $rule->getCouponMassGenerator();
  333. if (!$generator->validateData($data)) {
  334. $result['error'] = Mage::helper('salesrule')->__('Not valid data provided');
  335. } else {
  336. $generator->setData($data);
  337. $generator->generatePool();
  338. $generated = $generator->getGeneratedCount();
  339. $this->_getSession()->addSuccess(Mage::helper('salesrule')->__('%s Coupon(s) have been generated', $generated));
  340. $this->_initLayoutMessages('adminhtml/session');
  341. $result['messages'] = $this->getLayout()->getMessagesBlock()->getGroupedHtml();
  342. }
  343. } catch (Mage_Core_Exception $e) {
  344. $result['error'] = $e->getMessage();
  345. } catch (Exception $e) {
  346. $result['error'] = Mage::helper('salesrule')->__('An error occurred while generating coupons. Please review the log and try again.');
  347. Mage::logException($e);
  348. }
  349. }
  350. $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($result));
  351. }
  352. /**
  353. * Chooser source action
  354. */
  355. public function chooserAction()
  356. {
  357. $uniqId = $this->getRequest()->getParam('uniq_id');
  358. $chooserBlock = $this->getLayout()->createBlock('adminhtml/promo_widget_chooser', '', array(
  359. 'id' => $uniqId
  360. ));
  361. $this->getResponse()->setBody($chooserBlock->toHtml());
  362. }
  363. /**
  364. * Returns result of current user permission check on resource and privilege
  365. * @return boolean
  366. */
  367. protected function _isAllowed()
  368. {
  369. return Mage::getSingleton('admin/session')->isAllowed('promo/quote');
  370. }
  371. }