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 class Mage_Rule_Model_Rule extends Mage_Core_Model_Abstract
00028 {
00029 protected $_conditions;
00030 protected $_actions;
00031 protected $_form;
00032
00033
00034
00035
00036
00037
00038 protected $_isDeleteable = true;
00039
00040
00041
00042
00043
00044
00045 protected $_isReadonly = false;
00046
00047
00048 protected function _construct()
00049 {
00050 $this->_init('rule/rule');
00051 parent::_construct();
00052 }
00053
00054 public function getConditionsInstance()
00055 {
00056 return Mage::getModel('rule/condition_combine');
00057 }
00058
00059 public function _resetConditions($conditions=null)
00060 {
00061 if (is_null($conditions)) {
00062 $conditions = $this->getConditionsInstance();
00063 }
00064 $conditions->setRule($this)->setId('1')->setPrefix('conditions');
00065 $this->setConditions($conditions);
00066
00067 return $this;
00068 }
00069
00070 public function setConditions($conditions)
00071 {
00072 $this->_conditions = $conditions;
00073 return $this;
00074 }
00075
00076
00077
00078
00079
00080
00081 public function getConditions()
00082 {
00083 if (empty($this->_conditions)) {
00084 $this->_resetConditions();
00085 }
00086 return $this->_conditions;
00087 }
00088
00089 public function getActionsInstance()
00090 {
00091 return Mage::getModel('rule/action_collection');
00092 }
00093
00094 public function _resetActions($actions=null)
00095 {
00096 if (is_null($actions)) {
00097 $actions = $this->getActionsInstance();
00098 }
00099 $actions->setRule($this)->setId('1')->setPrefix('actions');
00100 $this->setActions($actions);
00101
00102 return $this;
00103 }
00104
00105 public function setActions($actions)
00106 {
00107 $this->_actions = $actions;
00108 return $this;
00109 }
00110
00111 public function getActions()
00112 {
00113 if (!$this->_actions) {
00114 $this->_resetActions();
00115 }
00116 return $this->_actions;
00117 }
00118
00119 public function getForm()
00120 {
00121 if (!$this->_form) {
00122 $this->_form = new Varien_Data_Form();
00123 }
00124 return $this->_form;
00125 }
00126
00127 public function asString($format='')
00128 {
00129 $str = Mage::helper('rule')->__("Name: %s", $this->getName()) ."\n"
00130 . Mage::helper('rule')->__("Start at: %s", $this->getStartAt()) ."\n"
00131 . Mage::helper('rule')->__("Expire at: %s", $this->getExpireAt()) ."\n"
00132 . Mage::helper('rule')->__("Description: %s", $this->getDescription()) ."\n\n"
00133 . $this->getConditions()->asStringRecursive() ."\n\n"
00134 . $this->getActions()->asStringRecursive() ."\n\n";
00135 return $str;
00136 }
00137
00138 public function asHtml()
00139 {
00140 $str = Mage::helper('rule')->__("Name: %s", $this->getName()) ."<br/>"
00141 . Mage::helper('rule')->__("Start at: %s", $this->getStartAt()) ."<br/>"
00142 . Mage::helper('rule')->__("Expire at: %s", $this->getExpireAt()) ."<br/>"
00143 . Mage::helper('rule')->__("Description: %s", $this->getDescription()) .'<br/>'
00144 . '<ul class="rule-conditions">'.$this->getConditions()->asHtmlRecursive().'</ul>'
00145 . '<ul class="rule-actions">'.$this->getActions()->asHtmlRecursive()."</ul>";
00146 return $str;
00147 }
00148
00149 public function loadPost(array $rule)
00150 {
00151 $arr = $this->_convertFlatToRecursive($rule);
00152 if (isset($arr['conditions'])) {
00153 $this->getConditions()->loadArray($arr['conditions'][1]);
00154 }
00155 if (isset($arr['actions'])) {
00156 $this->getActions()->loadArray($arr['actions'][1]);
00157 }
00158
00159 return $this;
00160 }
00161
00162 protected function _convertFlatToRecursive(array $rule)
00163 {
00164 $arr = array();
00165 foreach ($rule as $key=>$value) {
00166 if (($key==='conditions' || $key==='actions') && is_array($value)) {
00167 foreach ($value as $id=>$data) {
00168 $path = explode('--', $id);
00169 $node =& $arr;
00170 for ($i=0, $l=sizeof($path); $i<$l; $i++) {
00171 if (!isset($node[$key][$path[$i]])) {
00172 $node[$key][$path[$i]] = array();
00173 }
00174 $node =& $node[$key][$path[$i]];
00175 }
00176 foreach ($data as $k=>$v) {
00177 $node[$k] = $v;
00178 }
00179 }
00180 } else {
00181
00182
00183
00184 if (in_array($key, array('from_date', 'to_date')) && $value) {
00185 $value = Mage::app()->getLocale()->date(
00186 $value,
00187 Mage::app()->getLocale()->getDateFormat(Mage_Core_Model_Locale::FORMAT_TYPE_SHORT),
00188 null,
00189 false
00190 );
00191 }
00192 $this->setData($key, $value);
00193 }
00194 }
00195 return $arr;
00196 }
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210 public function asArray(array $arrAttributes = array())
00211 {
00212 $out = array(
00213 'name'=>$this->getName(),
00214 'start_at'=>$this->getStartAt(),
00215 'expire_at'=>$this->getExpireAt(),
00216 'description'=>$this->getDescription(),
00217 'conditions'=>$this->getConditions()->asArray(),
00218 'actions'=>$this->getActions()->asArray(),
00219 );
00220
00221 return $out;
00222 }
00223
00224 public function validate(Varien_Object $object)
00225 {
00226 return $this->getConditions()->validate($object);
00227 }
00228
00229 public function afterLoad()
00230 {
00231 $this->_afterLoad();
00232 }
00233
00234 protected function _afterLoad()
00235 {
00236 parent::_afterLoad();
00237 $conditionsArr = unserialize($this->getConditionsSerialized());
00238 if (!empty($conditionsArr) && is_array($conditionsArr)) {
00239 $this->getConditions()->loadArray($conditionsArr);
00240 }
00241
00242 $actionsArr = unserialize($this->getActionsSerialized());
00243 if (!empty($actionsArr) && is_array($actionsArr)) {
00244 $this->getActions()->loadArray($actionsArr);
00245 }
00246
00247 $this->setWebsiteIds(explode(',',$this->getWebsiteIds()));
00248 $groupIds = $this->getCustomerGroupIds();
00249 if (is_string($groupIds)) {
00250 $this->setCustomerGroupIds(explode(',',$groupIds));
00251 }
00252 }
00253
00254 protected function _beforeSave()
00255 {
00256
00257 if ((int)$this->getDiscountAmount() < 0) {
00258 Mage::throwException(Mage::helper('rule')->__('Invalid discount amount.'));
00259 }
00260
00261
00262 if ($this->getConditions()) {
00263 $this->setConditionsSerialized(serialize($this->getConditions()->asArray()));
00264 $this->unsConditions();
00265 }
00266 if ($this->getActions()) {
00267 $this->setActionsSerialized(serialize($this->getActions()->asArray()));
00268 $this->unsActions();
00269 }
00270 if (is_array($this->getWebsiteIds())) {
00271 $this->setWebsiteIds(join(',', $this->getWebsiteIds()));
00272 }
00273 if (is_array($this->getCustomerGroupIds())) {
00274 $this->setCustomerGroupIds(join(',', $this->getCustomerGroupIds()));
00275 }
00276 parent::_beforeSave();
00277 }
00278
00279
00280
00281
00282
00283
00284 public function isDeleteable()
00285 {
00286 return $this->_isDeleteable;
00287 }
00288
00289
00290
00291
00292
00293
00294
00295 public function setIsDeleteable($flag)
00296 {
00297 $this->_isDeleteable = (bool) $flag;
00298 return $this;
00299 }
00300
00301
00302
00303
00304
00305
00306
00307 public function isReadonly()
00308 {
00309 return $this->_isReadonly;
00310 }
00311
00312
00313
00314
00315
00316
00317
00318 public function setIsReadonly($value)
00319 {
00320 $this->_isReadonly = (boolean) $value;
00321 return $this;
00322 }
00323 }