Overview

Packages

  • currencysymbol
  • MAbout
  • Mage
    • Admin
    • Adminhtml
    • AdminNotification
    • Api
    • Api2
    • Authorizenet
    • Backup
    • Bundle
    • Captcha
    • Catalog
    • CatalogIndex
    • CatalogInventory
    • CatalogRule
    • CatalogSearch
    • Centinel
    • Checkout
    • Cms
    • Compiler
    • Connect
    • Contacts
    • Core
    • Cron
    • CurrencySymbol
    • Customer
    • Dataflow
    • Directory
    • DirtectPost
    • Downloadable
    • Eav
    • GiftMessage
    • GoogleAnalytics
    • GoogleBase
    • GoogleCheckout
    • ImportExport
    • Index
    • Install
    • Log
    • Media
    • Newsletter
    • Oauth
    • Page
    • PageCache
    • Paygate
    • Payment
    • Paypal
    • PaypalUk
    • Persistent
    • Poll
    • ProductAlert
    • Rating
    • Reports
    • Review
    • Rss
    • Rule
    • Sales
    • SalesRule
    • Sedfriend
    • Sendfriend
    • Shipping
    • Sitemap
    • Tag
    • Tax
    • Usa
    • Weee
    • Widget
    • Wishlist
    • XmlConnect
  • None
  • Phoenix
    • Moneybookers
  • PHP
  • Zend
    • Date
    • Mime
    • XmlRpc

Classes

  • Mage_Rule_Block_Actions
  • Mage_Rule_Block_Conditions
  • Mage_Rule_Block_Editable
  • Mage_Rule_Block_Newchild
  • Mage_Rule_Block_Rule
  • Mage_Rule_Helper_Data
  • Mage_Rule_Model_Abstract
  • Mage_Rule_Model_Action_Abstract
  • Mage_Rule_Model_Action_Collection
  • Mage_Rule_Model_Condition_Abstract
  • Mage_Rule_Model_Condition_Combine
  • Mage_Rule_Model_Condition_Product_Abstract
  • Mage_Rule_Model_Environment
  • Mage_Rule_Model_Mysql4_Rule
  • Mage_Rule_Model_Mysql4_Rule_Collection
  • Mage_Rule_Model_Renderer_Actions
  • Mage_Rule_Model_Renderer_Conditions
  • Mage_Rule_Model_Resource_Abstract
  • Mage_Rule_Model_Resource_Rule_Collection_Abstract

Interfaces

  • Mage_Rule_Model_Action_Interface
  • Mage_Rule_Model_Condition_Interface
  • Overview
  • Package
  • Class
  • Tree
  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_Rule
 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: 
 27: /**
 28:  * Abstract Rule entity data model
 29:  *
 30:  * @category Mage
 31:  * @package Mage_Rule
 32:  * @author Magento Core Team <core@magentocommerce.com>
 33:  */
 34: abstract class Mage_Rule_Model_Abstract extends Mage_Core_Model_Abstract
 35: {
 36:     /**
 37:      * Store rule combine conditions model
 38:      *
 39:      * @var Mage_Rule_Model_Condition_Combine
 40:      */
 41:     protected $_conditions;
 42: 
 43:     /**
 44:      * Store rule actions model
 45:      *
 46:      * @var Mage_Rule_Model_Action_Collection
 47:      */
 48:     protected $_actions;
 49: 
 50:     /**
 51:      * Store rule form instance
 52:      *
 53:      * @var Varien_Data_Form
 54:      */
 55:     protected $_form;
 56: 
 57:     /**
 58:      * Is model can be deleted flag
 59:      *
 60:      * @var bool
 61:      */
 62:     protected $_isDeleteable = true;
 63: 
 64:     /**
 65:      * Is model readonly
 66:      *
 67:      * @var bool
 68:      */
 69:     protected $_isReadonly = false;
 70: 
 71:     /**
 72:      * Getter for rule combine conditions instance
 73:      *
 74:      * @return Mage_Rule_Model_Condition_Combine
 75:      */
 76:     abstract public function getConditionsInstance();
 77: 
 78:     /**
 79:      * Getter for rule actions collection instance
 80:      *
 81:      * @return Mage_Rule_Model_Action_Collection
 82:      */
 83:     abstract public function getActionsInstance();
 84: 
 85:     /**
 86:      * Prepare data before saving
 87:      *
 88:      * @return Mage_Rule_Model_Abstract
 89:      */
 90:     protected function _beforeSave()
 91:     {
 92:         // Check if discount amount not negative
 93:         if ($this->hasDiscountAmount()) {
 94:             if ((int)$this->getDiscountAmount() < 0) {
 95:                 Mage::throwException(Mage::helper('rule')->__('Invalid discount amount.'));
 96:             }
 97:         }
 98: 
 99:         // Serialize conditions
100:         if ($this->getConditions()) {
101:             $this->setConditionsSerialized(serialize($this->getConditions()->asArray()));
102:             $this->unsConditions();
103:         }
104: 
105:         // Serialize actions
106:         if ($this->getActions()) {
107:             $this->setActionsSerialized(serialize($this->getActions()->asArray()));
108:             $this->unsActions();
109:         }
110: 
111:         /**
112:          * Prepare website Ids if applicable and if they were set as string in comma separated format.
113:          * Backwards compatibility.
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:          * Prepare customer group Ids if applicable and if they were set as string in comma separated format.
124:          * Backwards compatibility.
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:      * Set rule combine conditions model
139:      *
140:      * @param Mage_Rule_Model_Condition_Combine $conditions
141:      *
142:      * @return Mage_Rule_Model_Abstract
143:      */
144:     public function setConditions($conditions)
145:     {
146:         $this->_conditions = $conditions;
147:         return $this;
148:     }
149: 
150:     /**
151:      * Retrieve rule combine conditions model
152:      *
153:      * @return Mage_Rule_Model_Condition_Combine
154:      */
155:     public function getConditions()
156:     {
157:         if (empty($this->_conditions)) {
158:             $this->_resetConditions();
159:         }
160: 
161:         // Load rule conditions if it is applicable
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:      * Set rule actions model
178:      *
179:      * @param Mage_Rule_Model_Action_Collection $actions
180:      *
181:      * @return Mage_Rule_Model_Abstract
182:      */
183:     public function setActions($actions)
184:     {
185:         $this->_actions = $actions;
186:         return $this;
187:     }
188: 
189:     /**
190:      * Retrieve rule actions model
191:      *
192:      * @return Mage_Rule_Model_Action_Collection
193:      */
194:     public function getActions()
195:     {
196:         if (!$this->_actions) {
197:             $this->_resetActions();
198:         }
199: 
200:         // Load rule actions if it is applicable
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:      * Reset rule combine conditions
217:      *
218:      * @param null|Mage_Rule_Model_Condition_Combine $conditions
219:      *
220:      * @return Mage_Rule_Model_Abstract
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:      * Reset rule actions
235:      *
236:      * @param null|Mage_Rule_Model_Action_Collection $actions
237:      *
238:      * @return Mage_Rule_Model_Abstract
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:      * Rule form getter
253:      *
254:      * @return Varien_Data_Form
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:      * Initialize rule model data from array
266:      *
267:      * @param array $data
268:      *
269:      * @return Mage_Rule_Model_Abstract
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:      * Set specified data to current rule.
286:      * Set conditions and actions recursively.
287:      * Convert dates into Zend_Date.
288:      *
289:      * @param array $data
290:      *
291:      * @return array
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:                  * Convert dates into Zend_Date
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:      * Validate rule conditions to determine if rule can run
332:      *
333:      * @param Varien_Object $object
334:      *
335:      * @return bool
336:      */
337:     public function validate(Varien_Object $object)
338:     {
339:         return $this->getConditions()->validate($object);
340:     }
341: 
342:     /**
343:      * Validate rule data
344:      *
345:      * @param Varien_Object $object
346:      *
347:      * @return bool|array - return true if validation passed successfully. Array with errors description otherwise
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:      * Check availability to delete rule
386:      *
387:      * @return bool
388:      */
389:     public function isDeleteable()
390:     {
391:         return $this->_isDeleteable;
392:     }
393: 
394:     /**
395:      * Set is rule can be deleted flag
396:      *
397:      * @param bool $value
398:      *
399:      * @return Mage_Rule_Model_Abstract
400:      */
401:     public function setIsDeleteable($value)
402:     {
403:         $this->_isDeleteable = (bool) $value;
404:         return $this;
405:     }
406: 
407:     /**
408:      * Check if rule is readonly
409:      *
410:      * @return bool
411:      */
412:     public function isReadonly()
413:     {
414:         return $this->_isReadonly;
415:     }
416: 
417:     /**
418:      * Set is readonly flag to rule
419:      *
420:      * @param bool $value
421:      *
422:      * @return Mage_Rule_Model_Abstract
423:      */
424:     public function setIsReadonly($value)
425:     {
426:         $this->_isReadonly = (bool) $value;
427:         return $this;
428:     }
429: 
430:     /**
431:      * Get rule associated website Ids
432:      *
433:      * @return array
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:      * @deprecated since 1.7.0.0
449:      *
450:      * @param string $format
451:      *
452:      * @return string
453:      */
454:     public function asString($format='')
455:     {
456:         return '';
457:     }
458: 
459:     /**
460:      * @deprecated since 1.7.0.0
461:      *
462:      * @return string
463:      */
464:     public function asHtml()
465:     {
466:         return '';
467:     }
468: 
469:     /**
470:      * Returns rule as an array for admin interface
471:      *
472:      * @deprecated since 1.7.0.0
473:      *
474:      * @param array $arrAttributes
475:      *
476:      * @return array
477:      */
478:     public function asArray(array $arrAttributes = array())
479:     {
480:         return array();
481:     }
482: 
483:     /**
484:      * Combine website ids to string
485:      *
486:      * @deprecated since 1.7.0.0
487:      *
488:      * @return Mage_Rule_Model_Abstract
489:      */
490:     protected function _prepareWebsiteIds()
491:     {
492:         return $this;
493:     }
494: }
495: 
Magento 1.7.0.2 API documentation generated by ApiGen 2.8.0