1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25:
26:
27: 28: 29: 30: 31: 32: 33:
34: abstract class Mage_Rule_Model_Abstract extends Mage_Core_Model_Abstract
35: {
36: 37: 38: 39: 40:
41: protected $_conditions;
42:
43: 44: 45: 46: 47:
48: protected $_actions;
49:
50: 51: 52: 53: 54:
55: protected $_form;
56:
57: 58: 59: 60: 61:
62: protected $_isDeleteable = true;
63:
64: 65: 66: 67: 68:
69: protected $_isReadonly = false;
70:
71: 72: 73: 74: 75:
76: abstract public function getConditionsInstance();
77:
78: 79: 80: 81: 82:
83: abstract public function getActionsInstance();
84:
85: 86: 87: 88: 89:
90: protected function _beforeSave()
91: {
92:
93: if ($this->hasDiscountAmount()) {
94: if ((int)$this->getDiscountAmount() < 0) {
95: Mage::throwException(Mage::helper('rule')->__('Invalid discount amount.'));
96: }
97: }
98:
99:
100: if ($this->getConditions()) {
101: $this->setConditionsSerialized(serialize($this->getConditions()->asArray()));
102: $this->unsConditions();
103: }
104:
105:
106: if ($this->getActions()) {
107: $this->setActionsSerialized(serialize($this->getActions()->asArray()));
108: $this->unsActions();
109: }
110:
111: 112: 113: 114:
115: if ($this->hasWebsiteIds()) {
116: $websiteIds = $this->getWebsiteIds();
117: if (is_string($websiteIds) && !empty($websiteIds)) {
118: $this->setWebsiteIds(explode(',', $websiteIds));
119: }
120: }
121:
122: 123: 124: 125:
126: if ($this->hasCustomerGroupIds()) {
127: $groupIds = $this->getCustomerGroupIds();
128: if (is_string($groupIds) && !empty($groupIds)) {
129: $this->setCustomerGroupIds(explode(',', $groupIds));
130: }
131: }
132:
133: parent::_beforeSave();
134: return $this;
135: }
136:
137: 138: 139: 140: 141: 142: 143:
144: public function setConditions($conditions)
145: {
146: $this->_conditions = $conditions;
147: return $this;
148: }
149:
150: 151: 152: 153: 154:
155: public function getConditions()
156: {
157: if (empty($this->_conditions)) {
158: $this->_resetConditions();
159: }
160:
161:
162: if ($this->hasConditionsSerialized()) {
163: $conditions = $this->getConditionsSerialized();
164: if (!empty($conditions)) {
165: $conditions = unserialize($conditions);
166: if (is_array($conditions) && !empty($conditions)) {
167: $this->_conditions->loadArray($conditions);
168: }
169: }
170: $this->unsConditionsSerialized();
171: }
172:
173: return $this->_conditions;
174: }
175:
176: 177: 178: 179: 180: 181: 182:
183: public function setActions($actions)
184: {
185: $this->_actions = $actions;
186: return $this;
187: }
188:
189: 190: 191: 192: 193:
194: public function getActions()
195: {
196: if (!$this->_actions) {
197: $this->_resetActions();
198: }
199:
200:
201: if ($this->hasActionsSerialized()) {
202: $actions = $this->getActionsSerialized();
203: if (!empty($actions)) {
204: $actions = unserialize($actions);
205: if (is_array($actions) && !empty($actions)) {
206: $this->_actions->loadArray($actions);
207: }
208: }
209: $this->unsActionsSerialized();
210: }
211:
212: return $this->_actions;
213: }
214:
215: 216: 217: 218: 219: 220: 221:
222: protected function _resetConditions($conditions = null)
223: {
224: if (is_null($conditions)) {
225: $conditions = $this->getConditionsInstance();
226: }
227: $conditions->setRule($this)->setId('1')->setPrefix('conditions');
228: $this->setConditions($conditions);
229:
230: return $this;
231: }
232:
233: 234: 235: 236: 237: 238: 239:
240: protected function _resetActions($actions = null)
241: {
242: if (is_null($actions)) {
243: $actions = $this->getActionsInstance();
244: }
245: $actions->setRule($this)->setId('1')->setPrefix('actions');
246: $this->setActions($actions);
247:
248: return $this;
249: }
250:
251: 252: 253: 254: 255:
256: public function getForm()
257: {
258: if (!$this->_form) {
259: $this->_form = new Varien_Data_Form();
260: }
261: return $this->_form;
262: }
263:
264: 265: 266: 267: 268: 269: 270:
271: public function loadPost(array $data)
272: {
273: $arr = $this->_convertFlatToRecursive($data);
274: if (isset($arr['conditions'])) {
275: $this->getConditions()->setConditions(array())->loadArray($arr['conditions'][1]);
276: }
277: if (isset($arr['actions'])) {
278: $this->getActions()->setActions(array())->loadArray($arr['actions'][1], 'actions');
279: }
280:
281: return $this;
282: }
283:
284: 285: 286: 287: 288: 289: 290: 291: 292:
293: protected function _convertFlatToRecursive(array $data)
294: {
295: $arr = array();
296: foreach ($data as $key => $value) {
297: if (($key === 'conditions' || $key === 'actions') && is_array($value)) {
298: foreach ($value as $id=>$data) {
299: $path = explode('--', $id);
300: $node =& $arr;
301: for ($i=0, $l=sizeof($path); $i<$l; $i++) {
302: if (!isset($node[$key][$path[$i]])) {
303: $node[$key][$path[$i]] = array();
304: }
305: $node =& $node[$key][$path[$i]];
306: }
307: foreach ($data as $k => $v) {
308: $node[$k] = $v;
309: }
310: }
311: } else {
312: 313: 314:
315: if (in_array($key, array('from_date', 'to_date')) && $value) {
316: $value = Mage::app()->getLocale()->date(
317: $value,
318: Varien_Date::DATE_INTERNAL_FORMAT,
319: null,
320: false
321: );
322: }
323: $this->setData($key, $value);
324: }
325: }
326:
327: return $arr;
328: }
329:
330: 331: 332: 333: 334: 335: 336:
337: public function validate(Varien_Object $object)
338: {
339: return $this->getConditions()->validate($object);
340: }
341:
342: 343: 344: 345: 346: 347: 348:
349: public function validateData(Varien_Object $object)
350: {
351: $result = array();
352: $fromDate = $toDate = null;
353:
354: if ($object->hasFromDate() && $object->hasToDate()) {
355: $fromDate = $object->getFromDate();
356: $toDate = $object->getToDate();
357: }
358:
359: if ($fromDate && $toDate) {
360: $fromDate = new Zend_Date($fromDate, Varien_Date::DATE_INTERNAL_FORMAT);
361: $toDate = new Zend_Date($toDate, Varien_Date::DATE_INTERNAL_FORMAT);
362:
363: if ($fromDate->compare($toDate) === 1) {
364: $result[] = Mage::helper('rule')->__('End Date must be greater than Start Date.');
365: }
366: }
367:
368: if ($object->hasWebsiteIds()) {
369: $websiteIds = $object->getWebsiteIds();
370: if (empty($websiteIds)) {
371: $result[] = Mage::helper('rule')->__('Websites must be specified.');
372: }
373: }
374: if ($object->hasCustomerGroupIds()) {
375: $customerGroupIds = $object->getCustomerGroupIds();
376: if (empty($customerGroupIds)) {
377: $result[] = Mage::helper('rule')->__('Customer Groups must be specified.');
378: }
379: }
380:
381: return !empty($result) ? $result : true;
382: }
383:
384: 385: 386: 387: 388:
389: public function isDeleteable()
390: {
391: return $this->_isDeleteable;
392: }
393:
394: 395: 396: 397: 398: 399: 400:
401: public function setIsDeleteable($value)
402: {
403: $this->_isDeleteable = (bool) $value;
404: return $this;
405: }
406:
407: 408: 409: 410: 411:
412: public function isReadonly()
413: {
414: return $this->_isReadonly;
415: }
416:
417: 418: 419: 420: 421: 422: 423:
424: public function setIsReadonly($value)
425: {
426: $this->_isReadonly = (bool) $value;
427: return $this;
428: }
429:
430: 431: 432: 433: 434:
435: public function getWebsiteIds()
436: {
437: if (!$this->hasWebsiteIds()) {
438: $websiteIds = $this->_getResource()->getWebsiteIds($this->getId());
439: $this->setData('website_ids', (array)$websiteIds);
440: }
441: return $this->_getData('website_ids');
442: }
443:
444:
445:
446:
447: 448: 449: 450: 451: 452: 453:
454: public function asString($format='')
455: {
456: return '';
457: }
458:
459: 460: 461: 462: 463:
464: public function asHtml()
465: {
466: return '';
467: }
468:
469: 470: 471: 472: 473: 474: 475: 476: 477:
478: public function asArray(array $arrAttributes = array())
479: {
480: return array();
481: }
482:
483: 484: 485: 486: 487: 488: 489:
490: protected function _prepareWebsiteIds()
491: {
492: return $this;
493: }
494: }
495: