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: class Mage_Rule_Model_Condition_Combine extends Mage_Rule_Model_Condition_Abstract
 29: {
 30:     /**
 31:      * Store all used condition models
 32:      *
 33:      * @var array
 34:      */
 35:     static protected $_conditionModels = array();
 36: 
 37: 
 38: 
 39: 
 40: 
 41:     /**
 42:      * Retrieve new object for each requested model.
 43:      * If model is requested first time, store it at static array.
 44:      *
 45:      * It's made by performance reasons to avoid initialization of same models each time when rules are being processed.
 46:      *
 47:      * @param  string $modelClass
 48:      * @return Mage_Rule_Model_Condition_Abstract|bool
 49:      */
 50:     protected function _getNewConditionModelInstance($modelClass)
 51:     {
 52:         if (empty($modelClass)) {
 53:             return false;
 54:         }
 55: 
 56:         if (!array_key_exists($modelClass, self::$_conditionModels)) {
 57:             $model = Mage::getModel($modelClass);
 58:             self::$_conditionModels[$modelClass] = $model;
 59:         } else {
 60:             $model = self::$_conditionModels[$modelClass];
 61:         }
 62: 
 63:         if (!$model) {
 64:             return false;
 65:         }
 66: 
 67:         $newModel = clone $model;
 68:         return $newModel;
 69:     }
 70: 
 71:     public function __construct()
 72:     {
 73:         parent::__construct();
 74:         $this->setType('rule/condition_combine')
 75:             ->setAggregator('all')
 76:             ->setValue(true)
 77:             ->setConditions(array())
 78:             ->setActions(array());
 79: 
 80: 
 81:         $this->loadAggregatorOptions();
 82:         if ($options = $this->getAggregatorOptions()) {
 83:             foreach ($options as $aggregator=>$dummy) { $this->setAggregator($aggregator); break; }
 84:         }
 85:     }
 86: /* start aggregator methods */
 87:     public function loadAggregatorOptions()
 88:     {
 89:         $this->setAggregatorOption(array(
 90:             'all' => Mage::helper('rule')->__('ALL'),
 91:             'any' => Mage::helper('rule')->__('ANY'),
 92:         ));
 93:         return $this;
 94:     }
 95: 
 96:     public function getAggregatorSelectOptions()
 97:     {
 98:         $opt = array();
 99:         foreach ($this->getAggregatorOption() as $k=>$v) {
100:             $opt[] = array('value'=>$k, 'label'=>$v);
101:         }
102:         return $opt;
103:     }
104: 
105:     public function getAggregatorName()
106:     {
107:         return $this->getAggregatorOption($this->getAggregator());
108:     }
109: 
110:     public function getAggregatorElement()
111:     {
112:         if (is_null($this->getAggregator())) {
113:             foreach ($this->getAggregatorOption() as $k=>$v) {
114:                 $this->setAggregator($k);
115:                 break;
116:             }
117:         }
118:         return $this->getForm()->addField($this->getPrefix().'__'.$this->getId().'__aggregator', 'select', array(
119:             'name'=>'rule['.$this->getPrefix().']['.$this->getId().'][aggregator]',
120:             'values'=>$this->getAggregatorSelectOptions(),
121:             'value'=>$this->getAggregator(),
122:             'value_name'=>$this->getAggregatorName(),
123:         ))->setRenderer(Mage::getBlockSingleton('rule/editable'));
124:     }
125: /* end aggregator methods */
126: 
127:     public function loadValueOptions()
128:     {
129:         $this->setValueOption(array(
130:             1 => Mage::helper('rule')->__('TRUE'),
131:             0 => Mage::helper('rule')->__('FALSE'),
132:         ));
133:         return $this;
134:     }
135: 
136:     public function addCondition($condition)
137:     {
138:         $condition->setRule($this->getRule());
139:         $condition->setObject($this->getObject());
140:         $condition->setPrefix($this->getPrefix());
141: 
142:         $conditions = $this->getConditions();
143:         $conditions[] = $condition;
144: 
145:         if (!$condition->getId()) {
146:             $condition->setId($this->getId().'--'.sizeof($conditions));
147:         }
148: 
149:         $this->setData($this->getPrefix(), $conditions);
150:         return $this;
151:     }
152: 
153:     public function getValueElementType()
154:     {
155:         return 'select';
156:     }
157: 
158:     /**
159:      * Returns array containing conditions in the collection
160:      *
161:      * Output example:
162:      * array(
163:      *   'type'=>'combine',
164:      *   'operator'=>'ALL',
165:      *   'value'=>'TRUE',
166:      *   'conditions'=>array(
167:      *     {condition::asArray},
168:      *     {combine::asArray},
169:      *     {quote_item_combine::asArray}
170:      *   )
171:      * )
172:      *
173:      * @return array
174:      */
175:     public function asArray(array $arrAttributes = array())
176:     {
177:         $out = parent::asArray();
178:         $out['aggregator'] = $this->getAggregator();
179: 
180:         foreach ($this->getConditions() as $condition) {
181:             $out['conditions'][] = $condition->asArray();
182:         }
183: 
184:         return $out;
185:     }
186: 
187:     public function asXml($containerKey='conditions', $itemKey='condition')
188:     {
189:         $xml = "<aggregator>".$this->getAggregator()."</aggregator>"
190:             ."<value>".$this->getValue()."</value>"
191:             ."<$containerKey>";
192:         foreach ($this->getConditions() as $condition) {
193:             $xml .= "<$itemKey>".$condition->asXml()."</$itemKey>";
194:         }
195:         $xml .= "</$containerKey>";
196:         return $xml;
197:     }
198: 
199:     public function loadArray($arr, $key='conditions')
200:     {
201:         $this->setAggregator(isset($arr['aggregator']) ? $arr['aggregator']
202:                 : (isset($arr['attribute']) ? $arr['attribute'] : null))
203:             ->setValue(isset($arr['value']) ? $arr['value']
204:                 : (isset($arr['operator']) ? $arr['operator'] : null));
205: 
206:         if (!empty($arr[$key]) && is_array($arr[$key])) {
207:             foreach ($arr[$key] as $condArr) {
208:                 try {
209:                     $cond = $this->_getNewConditionModelInstance($condArr['type']);
210:                     if ($cond) {
211:                         $this->addCondition($cond);
212:                         $cond->loadArray($condArr, $key);
213:                     }
214:                 } catch (Exception $e) {
215:                     Mage::logException($e);
216:                 }
217:             }
218:         }
219:         return $this;
220:     }
221: 
222:     public function loadXml($xml)
223:     {
224:         if (is_string($xml)) {
225:             $xml = simplexml_load_string($xml);
226:         }
227:         $arr = parent::loadXml($xml);
228:         foreach ($xml->conditions->children() as $condition) {
229:             $arr['conditions'] = parent::loadXml($condition);
230:         }
231:         $this->loadArray($arr);
232:         return $this;
233:     }
234: 
235:     public function asHtml()
236:     {
237:            $html = $this->getTypeElement()->getHtml().
238:                Mage::helper('rule')->__('If %s of these conditions are %s:', $this->getAggregatorElement()->getHtml(), $this->getValueElement()->getHtml());
239:            if ($this->getId() != '1') {
240:                $html.= $this->getRemoveLinkHtml();
241:            }
242:         return $html;
243:     }
244: 
245:     public function getNewChildElement()
246:     {
247:         return $this->getForm()->addField($this->getPrefix().'__'.$this->getId().'__new_child', 'select', array(
248:             'name'=>'rule['.$this->getPrefix().']['.$this->getId().'][new_child]',
249:             'values'=>$this->getNewChildSelectOptions(),
250:             'value_name'=>$this->getNewChildName(),
251:         ))->setRenderer(Mage::getBlockSingleton('rule/newchild'));
252:     }
253: 
254:     public function asHtmlRecursive()
255:     {
256:         $html = $this->asHtml().'<ul id="'.$this->getPrefix().'__'.$this->getId().'__children" class="rule-param-children">';
257:         foreach ($this->getConditions() as $cond) {
258:             $html .= '<li>'.$cond->asHtmlRecursive().'</li>';
259:         }
260:         $html .= '<li>'.$this->getNewChildElement()->getHtml().'</li></ul>';
261:         return $html;
262:     }
263: 
264:     public function asString($format='')
265:     {
266:         $str = Mage::helper('rule')->__("If %s of these conditions are %s:", $this->getAggregatorName(), $this->getValueName());
267:         return $str;
268:     }
269: 
270:     public function asStringRecursive($level=0)
271:     {
272:         $str = parent::asStringRecursive($level);
273:         foreach ($this->getConditions() as $cond) {
274:             $str .= "\n".$cond->asStringRecursive($level+1);
275:         }
276:         return $str;
277:     }
278: 
279:     public function validate(Varien_Object $object)
280:     {
281:         if (!$this->getConditions()) {
282:             return true;
283:         }
284: 
285:         $all    = $this->getAggregator() === 'all';
286:         $true   = (bool)$this->getValue();
287: 
288:         foreach ($this->getConditions() as $cond) {
289:             $validated = $cond->validate($object);
290: 
291:             if ($all && $validated !== $true) {
292:                 return false;
293:             } elseif (!$all && $validated === $true) {
294:                 return true;
295:             }
296:         }
297:         return $all ? true : false;
298:     }
299: 
300:     public function setJsFormObject($form)
301:     {
302:         $this->setData('js_form_object', $form);
303:         foreach ($this->getConditions() as $condition) {
304:             $condition->setJsFormObject($form);
305:         }
306:         return $this;
307:     }
308: 
309:     /**
310:      * Get conditions, if current prefix is undefined use 'conditions' key
311:      *
312:      * @return array
313:      */
314:     public function getConditions()
315:     {
316:         $key = $this->getPrefix() ? $this->getPrefix() : 'conditions';
317:         return $this->getData($key);
318:     }
319: 
320:     /**
321:      * Set conditions, if current prefix is undefined use 'conditions' key
322:      *
323:      * @param array $conditions
324:      * @return Mage_Rule_Model_Condition_Combine
325:      */
326:     public function setConditions($conditions)
327:     {
328:         $key = $this->getPrefix() ? $this->getPrefix() : 'conditions';
329:         return $this->setData($key, $conditions);
330:     }
331: 
332:     /**
333:      * Getter for "Conditions Combination" select option for recursive combines
334:      */
335:     protected function _getRecursiveChildSelectOption()
336:     {
337:         return array('value' => $this->getType(), 'label' => Mage::helper('rule')->__('Conditions Combination'));
338:     }
339: }
340: 
Magento 1.7.0.2 API documentation generated by ApiGen 2.8.0