PageRenderTime 24ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/app/code/community/TBT/Rewards/controllers/Manage/Promo/QuoteController.php

https://bitbucket.org/acidel/buykoala
PHP | 240 lines | 170 code | 34 blank | 36 comment | 23 complexity | f9965d812c13b4b91c31109ba9d81f88 MD5 | raw file
  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) 2008 Irubin Consulting Inc. DBA Varien (http://www.varien.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. include_once('app'.DS.'code'.DS.'community'.DS.'TBT'.DS.'Rewards'.DS.'controllers'.DS.'Admin'.DS.'AbstractController.php');
  27. class TBT_Rewards_Manage_Promo_QuoteController extends TBT_Rewards_Admin_AbstractController {
  28. protected function _construct() {
  29. $this->_usedModuleName = 'rewards';
  30. parent::_construct ();
  31. }
  32. protected function _initRule() {
  33. Mage::register ( 'current_promo_quote_rule', Mage::getModel ( 'rewards/salesrule_rule' ) );
  34. if ($id = ( int ) $this->getRequest ()->getParam ( 'id' )) {
  35. Mage::registry ( 'current_promo_quote_rule' )->load ( $id );
  36. }
  37. }
  38. protected function _initAction() {
  39. $this->loadLayout ()->_setActiveMenu ( 'rewards/rules/quote' )->_addBreadcrumb ( Mage::helper ( 'rewards' )->__ ( 'Promotions' ), Mage::helper ( 'rewards' )->__ ( 'Promotions' ) );
  40. return $this;
  41. }
  42. public function indexAction() {
  43. $type = $this->getRequest ()->getParam ( 'type' ); // redemption type or distribution type
  44. $this->_initAction ()->_addBreadcrumb ( Mage::helper ( 'rewards' )->__ ( 'Catalog' ), Mage::helper ( 'rewards' )->__ ( 'Catalog' ) );
  45. if (Mage::helper ( 'rewards/rule_type' )->isRedemption ( $type )) {
  46. $this->_addContent ( $this->getLayout ()->createBlock ( 'rewards/manage_promo_quote_redemptions' ) );
  47. } else {
  48. $this->_addContent ( $this->getLayout ()->createBlock ( 'rewards/manage_promo_quote_distributions' ) );
  49. }
  50. $this->renderLayout ();
  51. }
  52. public function newAction() {
  53. $this->editAction ();
  54. }
  55. public function editAction() {
  56. $id = $this->getRequest ()->getParam ( 'id' );
  57. $type = $this->getRequest ()->getParam ( 'type' ); // redemption type or distribution type
  58. $model = Mage::getModel ( 'rewards/salesrule_rule' );
  59. if ($id) {
  60. $model->load ( $id );
  61. if (! $model->getRuleId ()) {
  62. //print_r($model->getData());
  63. Mage::getSingleton ( 'adminhtml/session' )->addError ( Mage::helper ( 'rewards' )->__ ( 'This rule no longer exists' ) );
  64. $this->_redirect ( '*/*', array ('type' => $type ) );
  65. return;
  66. }
  67. $type = $model->getRuleTypeId (); // redemption type or distribution type
  68. }
  69. // set entered data if was error when we do save
  70. $data = Mage::getSingleton ( 'adminhtml/session' )->getPageData ( true );
  71. if (! empty ( $data )) {
  72. $model->addData ( $data );
  73. }
  74. $model->getConditions ()->setJsFormObject ( 'rule_conditions_fieldset' );
  75. $model->getActions ()->setJsFormObject ( 'rule_actions_fieldset' );
  76. Mage::register ( 'current_promo_quote_rule', $model );
  77. $block = $this->getLayout ()->createBlock ( 'rewards/manage_promo_quote_edit' )->setData ( 'action', $this->getUrl ( '*/*/save', array ('type' => $type ) ) );
  78. $this->_initAction ();
  79. $this->getLayout ()->getBlock ( 'head' )->setCanLoadExtJs ( true )->setCanLoadRulesJs ( true );
  80. $this->_addBreadcrumb ( $id ? Mage::helper ( 'rewards' )->__ ( 'Edit Rule' ) : Mage::helper ( 'rewards' )->__ ( 'New Rule' ), $id ? Mage::helper ( 'rewards' )->__ ( 'Edit Rule' ) : Mage::helper ( 'rewards' )->__ ( 'New Rule' ) )->_addContent ( $block )->_addLeft ( $this->getLayout ()->createBlock ( 'rewards/manage_promo_quote_edit_tabs' ) )->renderLayout ();
  81. }
  82. public function saveAction() {
  83. if ($data = $this->getRequest ()->getPost ()) {
  84. $model = Mage::getModel ( 'rewards/salesrule_rule' );
  85. if (Mage::helper ( 'rewards' )->isBaseMageVersionAtLeast ( '1.4.0.0' )) {
  86. $data = $this->_filterDates ( $data, array ('from_date', 'to_date' ) );
  87. $id = $model->getId ();
  88. $session = Mage::getSingleton ( 'adminhtml/session' );
  89. $validateResult = $model->validateData ( new Varien_Object ( $data ) );
  90. if ($validateResult !== true) {
  91. foreach ( $validateResult as $errorMessage ) {
  92. $session->addError ( $errorMessage );
  93. }
  94. $session->setPageData ( $data );
  95. $this->_redirect ( '*/*/', array ('type' => $model->getRuleTypeId (), 'id' => $model->getId () ) );
  96. return;
  97. }
  98. }
  99. if (isset ( $data ['simple_action'] ) && $data ['simple_action'] == 'by_percent' && isset ( $data ['discount_amount'] )) {
  100. $data ['discount_amount'] = min ( 100, $data ['discount_amount'] );
  101. }
  102. if (isset ( $data ['rule'] ['conditions'] )) {
  103. $data ['conditions'] = $data ['rule'] ['conditions'];
  104. }
  105. if (isset ( $data ['rule'] ['actions'] )) {
  106. $data ['actions'] = $data ['rule'] ['actions'];
  107. }
  108. unset ( $data ['rule'] );
  109. try {
  110. $model->loadPost ( $data );
  111. Mage::getSingleton ( 'adminhtml/session' )->setPageData ( $model->getData () );
  112. $type = $model->getRuleTypeId (); // redemption type or distribution type
  113. $model->save ();
  114. $type = $model->getRuleTypeId (); // redemption type or distribution type (again in case it changed)
  115. Mage::getSingleton ( 'adminhtml/session' )->addSuccess ( Mage::helper ( 'rewards' )->__ ( 'Rule was successfully saved' ) );
  116. Mage::getSingleton ( 'adminhtml/session' )->setPageData ( false );
  117. $this->_redirect ( '*/*/', array ('type' => $type, 'id' => $model->getId () ) );
  118. return;
  119. } catch ( Exception $e ) {
  120. Mage::getSingleton ( 'adminhtml/session' )->addError ( $e->getMessage () );
  121. Mage::getSingleton ( 'adminhtml/session' )->setPageData ( $data );
  122. $this->_redirect ( '*/*/edit', array ('id' => $this->getRequest ()->getParam ( 'rule_id' ), 'type' => $type ) );
  123. return;
  124. }
  125. }
  126. $this->_redirect ( '*/*/' );
  127. }
  128. public function deleteAction() {
  129. $type = $this->getRequest ()->getParam ( 'type' ); // redemption type or distribution type
  130. $id = $this->getRequest ()->getParam ( 'id' );
  131. try {
  132. $model = Mage::getModel ( 'rewards/salesrule_rule' );
  133. if (! $id) {
  134. throw new Exception ( 'No rule specified, so the rule could not be deleted.' );
  135. }
  136. $model->load ( $id );
  137. if (! $model->getId ()) {
  138. throw new Exception ( Mage::helper ( 'rewards' )->__ ( 'The rule you are trying to delete no longer exists' ) );
  139. }
  140. $id = $model->getId ();
  141. $type = $model->getRuleTypeId (); // redemption type or distribution type
  142. $model->delete ();
  143. Mage::getSingleton ( 'adminhtml/session' )->addSuccess ( Mage::helper ( 'rewards' )->__ ( 'Rule was successfully deleted' ) );
  144. $this->_redirect ( '*/*/', array ('type' => $type ) );
  145. return;
  146. } catch ( Exception $e ) {
  147. Mage::getSingleton ( 'adminhtml/session' )->addError ( $e->getMessage () );
  148. Mage::getSingleton ( 'adminhtml/session' )->setPageData ( $this->getRequest ()->getPost () );
  149. $this->_redirect ( '*/*/edit', array ('id' => $id, 'type' => $type ) );
  150. return;
  151. }
  152. }
  153. public function newConditionHtmlAction() {
  154. $id = $this->getRequest ()->getParam ( 'id' );
  155. $typeArr = explode ( '|', str_replace ( '-', '/', (string)$this->getRequest ()->getParam ( 'type' ) ) );
  156. $type = $typeArr [0];
  157. $model = Mage::getModel ( $type )->setId ( $id )->setType ( $type )->setRule ( Mage::getModel ( 'salesrule/rule' ) )->setPrefix ( 'conditions' );
  158. if (! empty ( $typeArr [1] )) {
  159. $model->setAttribute ( $typeArr [1] );
  160. }
  161. if ($model instanceof Mage_Rule_Model_Condition_Abstract) {
  162. $model->setJsFormObject ( $this->getRequest ()->getParam ( 'form' ) );
  163. $html = $model->asHtmlRecursive ();
  164. } else {
  165. $html = '';
  166. }
  167. $this->getResponse ()->setBody ( $html );
  168. }
  169. public function newActionHtmlAction() {
  170. $id = $this->getRequest ()->getParam ( 'id' );
  171. $typeArr = explode ( '|', str_replace ( '-', '/', (string)$this->getRequest ()->getParam ( 'type' ) ) );
  172. $type = $typeArr [0];
  173. $model = Mage::getModel ( $type )->setId ( $id )->setType ( $type )->setRule ( Mage::getModel ( 'salesrule/rule' ) )->setPrefix ( 'actions' );
  174. if (! empty ( $typeArr [1] )) {
  175. $model->setAttribute ( $typeArr [1] );
  176. }
  177. if ($model instanceof Mage_Rule_Model_Condition_Abstract) {
  178. $model->setJsFormObject ( $this->getRequest ()->getParam ( 'form' ) );
  179. $html = $model->asHtmlRecursive ();
  180. } else {
  181. $html = '';
  182. }
  183. $this->getResponse ()->setBody ( $html );
  184. }
  185. public function applyRulesAction() {
  186. $this->_initAction ();
  187. $this->renderLayout ();
  188. }
  189. public function gridAction() {
  190. $this->_initRule ();
  191. $this->getResponse ()->setBody ( $this->getLayout ()->createBlock ( 'rewards/manage_promo_quote_edit_tab_product' )->toHtml () );
  192. }
  193. protected function _isAllowed() {
  194. return Mage::getSingleton ( 'admin/session' )->isAllowed ( 'rewards/rules' );
  195. }
  196. /* added in ... 1.4.1.**
  197. /**
  198. * Chooser source action
  199. public function chooserAction()
  200. {
  201. $uniqId = $this->getRequest()->getParam('uniq_id');
  202. $chooserBlock = $this->getLayout()->createBlock('adminhtml/promo_widget_chooser', '', array('id' => $uniqId ));
  203. $this->getResponse()->setBody($chooserBlock->toHtml());
  204. }
  205. */
  206. }