setType('rule/condition_combine') ->setAggregator('all') ->setValue(true) ->setConditions(array()) ->setActions(array()); $this->loadAggregatorOptions(); if ($options = $this->getAggregatorOptions()) { foreach ($options as $aggregator=>$dummy) { $this->setAggregator($aggregator); break; } } } /* start aggregator methods */ public function loadAggregatorOptions() { $this->setAggregatorOption(array( 'all' => Mage::helper('rule')->__('ALL'), 'any' => Mage::helper('rule')->__('ANY'), )); return $this; } public function getAggregatorSelectOptions() { $opt = array(); foreach ($this->getAggregatorOption() as $k=>$v) { $opt[] = array('value'=>$k, 'label'=>$v); } return $opt; } public function getAggregatorName() { return $this->getAggregatorOption($this->getAggregator()); } public function getAggregatorElement() { if (is_null($this->getAggregator())) { foreach ($this->getAggregatorOption() as $k=>$v) { $this->setAggregator($k); break; } } return $this->getForm()->addField($this->getPrefix().'__'.$this->getId().'__aggregator', 'select', array( 'name'=>'rule['.$this->getPrefix().']['.$this->getId().'][aggregator]', 'values'=>$this->getAggregatorSelectOptions(), 'value'=>$this->getAggregator(), 'value_name'=>$this->getAggregatorName(), ))->setRenderer(Mage::getBlockSingleton('rule/editable')); } /* end aggregator methods */ public function loadValueOptions() { $this->setValueOption(array( 1 => Mage::helper('rule')->__('TRUE'), 0 => Mage::helper('rule')->__('FALSE'), )); return $this; } public function addCondition($condition) { $condition->setRule($this->getRule()); $condition->setObject($this->getObject()); $condition->setPrefix($this->getPrefix()); $conditions = $this->getConditions(); $conditions[] = $condition; if (!$condition->getId()) { $condition->setId($this->getId().'--'.sizeof($conditions)); } $this->setData($this->getPrefix(), $conditions); return $this; } public function getValueElementType() { return 'select'; } /** * Returns array containing conditions in the collection * * Output example: * array( * 'type'=>'combine', * 'operator'=>'ALL', * 'value'=>'TRUE', * 'conditions'=>array( * {condition::asArray}, * {combine::asArray}, * {quote_item_combine::asArray} * ) * ) * * @return array */ public function asArray(array $arrAttributes = array()) { $out = parent::asArray(); $out['aggregator'] = $this->getAggregator(); foreach ($this->getConditions() as $condition) { $out['conditions'][] = $condition->asArray(); } return $out; } public function asXml($containerKey='conditions', $itemKey='condition') { $xml = "".$this->getAggregator()."" ."".$this->getValue()."" ."<$containerKey>"; foreach ($this->getConditions() as $condition) { $xml .= "<$itemKey>".$condition->asXml().""; } $xml .= ""; return $xml; } public function loadArray($arr, $key='conditions') { $this->setAggregator(isset($arr['aggregator']) ? $arr['aggregator'] : (isset($arr['attribute']) ? $arr['attribute'] : null)) ->setValue(isset($arr['value']) ? $arr['value'] : (isset($arr['operator']) ? $arr['operator'] : null)); if (!empty($arr[$key]) && is_array($arr[$key])) { foreach ($arr[$key] as $condArr) { try { $cond = $this->_getNewConditionModelInstance($condArr['type']); if ($cond) { $this->addCondition($cond); $cond->loadArray($condArr, $key); } } catch (Exception $e) { Mage::logException($e); } } } return $this; } public function loadXml($xml) { if (is_string($xml)) { $xml = simplexml_load_string($xml); } $arr = parent::loadXml($xml); foreach ($xml->conditions->children() as $condition) { $arr['conditions'] = parent::loadXml($condition); } $this->loadArray($arr); return $this; } public function asHtml() { $html = $this->getTypeElement()->getHtml(). Mage::helper('rule')->__('If %s of these conditions are %s:', $this->getAggregatorElement()->getHtml(), $this->getValueElement()->getHtml()); if ($this->getId() != '1') { $html.= $this->getRemoveLinkHtml(); } return $html; } public function getNewChildElement() { return $this->getForm()->addField($this->getPrefix().'__'.$this->getId().'__new_child', 'select', array( 'name'=>'rule['.$this->getPrefix().']['.$this->getId().'][new_child]', 'values'=>$this->getNewChildSelectOptions(), 'value_name'=>$this->getNewChildName(), ))->setRenderer(Mage::getBlockSingleton('rule/newchild')); } public function asHtmlRecursive() { $html = $this->asHtml().''; return $html; } public function asString($format='') { $str = Mage::helper('rule')->__("If %s of these conditions are %s:", $this->getAggregatorName(), $this->getValueName()); return $str; } public function asStringRecursive($level=0) { $str = parent::asStringRecursive($level); foreach ($this->getConditions() as $cond) { $str .= "\n".$cond->asStringRecursive($level+1); } return $str; } public function validate(Varien_Object $object) { if (!$this->getConditions()) { return true; } $all = $this->getAggregator() === 'all'; $true = (bool)$this->getValue(); foreach ($this->getConditions() as $cond) { $validated = $cond->validate($object); if ($all && $validated !== $true) { return false; } elseif (!$all && $validated === $true) { return true; } } return $all ? true : false; } public function setJsFormObject($form) { $this->setData('js_form_object', $form); foreach ($this->getConditions() as $condition) { $condition->setJsFormObject($form); } return $this; } /** * Get conditions, if current prefix is undefined use 'conditions' key * * @return array */ public function getConditions() { $key = $this->getPrefix() ? $this->getPrefix() : 'conditions'; return $this->getData($key); } /** * Set conditions, if current prefix is undefined use 'conditions' key * * @param array $conditions * @return Mage_Rule_Model_Condition_Combine */ public function setConditions($conditions) { $key = $this->getPrefix() ? $this->getPrefix() : 'conditions'; return $this->setData($key, $conditions); } /** * Getter for "Conditions Combination" select option for recursive combines */ protected function _getRecursiveChildSelectOption() { return array('value' => $this->getType(), 'label' => Mage::helper('rule')->__('Conditions Combination')); } }