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_SalesRule_Model_Rule extends Mage_Rule_Model_Rule
00029 {
00030 const FREE_SHIPPING_ITEM = 1;
00031 const FREE_SHIPPING_ADDRESS = 2;
00032
00033 protected function _construct()
00034 {
00035 parent::_construct();
00036 $this->_init('salesrule/rule');
00037 $this->setIdFieldName('rule_id');
00038 }
00039
00040 protected function _beforeSave()
00041 {
00042 if($coupon = $this->getCouponCode()) {
00043 $this->getResource()->addUniqueField(array(
00044 'field' => 'coupon_code',
00045 'title' => Mage::helper('salesRule')->__('Coupon with the same code')
00046 ));
00047 Mage::app()->cleanCache('salesrule_coupon_'.md5($coupon));
00048 } else {
00049 $this->getResource()->resetUniqueField();
00050 }
00051 return parent::_beforeSave();
00052 }
00053
00054
00055 protected function _beforeDelete()
00056 {
00057 if ($coupon = $this->getCouponCode()) {
00058 Mage::app()->cleanCache('salesrule_coupon_'.$coupon);
00059 }
00060 parent::_beforeDelete();
00061 }
00062
00063 public function getConditionsInstance()
00064 {
00065 return Mage::getModel('salesrule/rule_condition_combine');
00066 }
00067
00068 public function getActionsInstance()
00069 {
00070 return Mage::getModel('salesrule/rule_condition_product_combine');
00071 }
00072
00073 public function toString($format='')
00074 {
00075 $str = Mage::helper('salesrule')->__("Name: %s", $this->getName()) ."\n"
00076 . Mage::helper('salesrule')->__("Start at: %s", $this->getStartAt()) ."\n"
00077 . Mage::helper('salesrule')->__("Expire at: %s", $this->getExpireAt()) ."\n"
00078 . Mage::helper('salesrule')->__("Customer registered: %s", $this->getCustomerRegistered()) ."\n"
00079 . Mage::helper('salesrule')->__("Customer is new buyer: %s", $this->getCustomerNewBuyer()) ."\n"
00080 . Mage::helper('salesrule')->__("Description: %s", $this->getDescription()) ."\n\n"
00081 . $this->getConditions()->toStringRecursive() ."\n\n"
00082 . $this->getActions()->toStringRecursive() ."\n\n";
00083 return $str;
00084 }
00085
00086 public function loadPost(array $rule)
00087 {
00088 $arr = $this->_convertFlatToRecursive($rule);
00089 if (isset($arr['conditions'])) {
00090 $this->getConditions()->loadArray($arr['conditions'][1]);
00091 }
00092 if (isset($arr['actions'])) {
00093 $this->getActions()->loadArray($arr['actions'][1], 'actions');
00094 }
00095
00096 return $this;
00097 }
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111 public function toArray(array $arrAttributes = array())
00112 {
00113 $out = parent::toArray($arrAttributes);
00114 $out['customer_registered'] = $this->getCustomerRegistered();
00115 $out['customer_new_buyer'] = $this->getCustomerNewBuyer();
00116
00117 return $out;
00118 }
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149 public function getResourceCollection()
00150 {
00151 return Mage::getResourceModel('salesrule/rule_collection');
00152 }
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171 }