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
00028 class Mage_Rule_Model_Condition_Combine extends Mage_Rule_Model_Condition_Abstract
00029 {
00030 public function __construct()
00031 {
00032 parent::__construct();
00033 $this->setType('rule/condition_combine')
00034 ->setAggregator('all')
00035 ->setValue(true)
00036 ->setConditions(array())
00037 ->setActions(array());
00038
00039
00040 $this->loadAggregatorOptions();
00041 if ($options = $this->getAggregatorOptions()) {
00042 foreach ($options as $aggregator=>$dummy) { $this->setAggregator($aggregator); break; }
00043 }
00044 }
00045
00046 public function loadAggregatorOptions()
00047 {
00048 $this->setAggregatorOption(array(
00049 'all' => Mage::helper('rule')->__('ALL'),
00050 'any' => Mage::helper('rule')->__('ANY'),
00051 ));
00052 return $this;
00053 }
00054
00055 public function getAggregatorSelectOptions()
00056 {
00057 $opt = array();
00058 foreach ($this->getAggregatorOption() as $k=>$v) {
00059 $opt[] = array('value'=>$k, 'label'=>$v);
00060 }
00061 return $opt;
00062 }
00063
00064 public function getAggregatorName()
00065 {
00066 return $this->getAggregatorOption($this->getAggregator());
00067 }
00068
00069 public function getAggregatorElement()
00070 {
00071 if (is_null($this->getAggregator())) {
00072 foreach ($this->getAggregatorOption() as $k=>$v) {
00073 $this->setAggregator($k);
00074 break;
00075 }
00076 }
00077 return $this->getForm()->addField($this->getPrefix().'__'.$this->getId().'__aggregator', 'select', array(
00078 'name'=>'rule['.$this->getPrefix().']['.$this->getId().'][aggregator]',
00079 'values'=>$this->getAggregatorSelectOptions(),
00080 'value'=>$this->getAggregator(),
00081 'value_name'=>$this->getAggregatorName(),
00082 ))->setRenderer(Mage::getBlockSingleton('rule/editable'));
00083 }
00084
00085
00086 public function loadValueOptions()
00087 {
00088 $this->setValueOption(array(
00089 1 => Mage::helper('rule')->__('TRUE'),
00090 0 => Mage::helper('rule')->__('FALSE'),
00091 ));
00092 return $this;
00093 }
00094
00095 public function addCondition($condition)
00096 {
00097 $condition->setRule($this->getRule());
00098 $condition->setObject($this->getObject());
00099 $condition->setPrefix($this->getPrefix());
00100
00101 $conditions = $this->getConditions();
00102 $conditions[] = $condition;
00103
00104 if (!$condition->getId()) {
00105 $condition->setId($this->getId().'--'.sizeof($conditions));
00106 }
00107
00108 $this->setData($this->getPrefix(), $conditions);
00109 return $this;
00110 }
00111
00112 public function getValueElementType()
00113 {
00114 return 'select';
00115 }
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134 public function asArray(array $arrAttributes = array())
00135 {
00136 $out = parent::asArray();
00137 $out['aggregator'] = $this->getAggregator();
00138
00139 foreach ($this->getConditions() as $condition) {
00140 $out['conditions'][] = $condition->asArray();
00141 }
00142
00143 return $out;
00144 }
00145
00146 public function asXml($containerKey='conditions', $itemKey='condition')
00147 {
00148 $xml = "<aggregator>".$this->getAggregator()."</aggregator>"
00149 ."<value>".$this->getValue()."</value>"
00150 ."<$containerKey>";
00151 foreach ($this->getConditions() as $condition) {
00152 $xml .= "<$itemKey>".$condition->asXml()."</$itemKey>";
00153 }
00154 $xml .= "</$containerKey>";
00155 return $xml;
00156 }
00157
00158 public function loadArray($arr, $key='conditions')
00159 {
00160 $this->setAggregator(isset($arr['aggregator']) ? $arr['aggregator'] : $arr['attribute'])
00161 ->setValue(isset($arr['value']) ? $arr['value'] : $arr['operator']);
00162
00163 if (!empty($arr[$key]) && is_array($arr[$key])) {
00164 foreach ($arr[$key] as $condArr) {
00165 try {
00166 $cond = @Mage::getModel($condArr['type']);
00167 if (!empty($cond)) {
00168 $this->addCondition($cond);
00169 $cond->loadArray($condArr, $key);
00170 }
00171 } catch (Exception $e) {
00172
00173 }
00174 }
00175 }
00176 return $this;
00177 }
00178
00179 public function loadXml($xml)
00180 {
00181 if (is_string($xml)) {
00182 $xml = simplexml_load_string($xml);
00183 }
00184 $arr = parent::loadXml($xml);
00185 foreach ($xml->conditions->children() as $condition) {
00186 $arr['conditions'] = parent::loadXml($condition);
00187 }
00188 $this->loadArray($arr);
00189 return $this;
00190 }
00191
00192 public function asHtml()
00193 {
00194 $html = $this->getTypeElement()->getHtml().
00195 Mage::helper('rule')->__("If %s of these conditions are %s:",
00196 $this->getAggregatorElement()->getHtml(),
00197 $this->getValueElement()->getHtml()
00198 );
00199 if ($this->getId()!='1') {
00200 $html.= $this->getRemoveLinkHtml();
00201 }
00202 return $html;
00203 }
00204
00205 public function getNewChildElement()
00206 {
00207 return $this->getForm()->addField($this->getPrefix().'__'.$this->getId().'__new_child', 'select', array(
00208 'name'=>'rule['.$this->getPrefix().']['.$this->getId().'][new_child]',
00209 'values'=>$this->getNewChildSelectOptions(),
00210 'value_name'=>$this->getNewChildName(),
00211 ))->setRenderer(Mage::getBlockSingleton('rule/newchild'));
00212 }
00213
00214 public function asHtmlRecursive()
00215 {
00216 $html = $this->asHtml().'<ul id="'.$this->getPrefix().'__'.$this->getId().'__children" class="rule-param-children">';
00217 foreach ($this->getConditions() as $cond) {
00218 $html .= '<li>'.$cond->asHtmlRecursive().'</li>';
00219 }
00220 $html .= '<li>'.$this->getNewChildElement()->getHtml().'</li></ul>';
00221 return $html;
00222 }
00223
00224 public function asString($format='')
00225 {
00226 $str = Mage::helper('rule')->__("If %s of these conditions are %s:", $this->getAggregatorName(), $this->getValueName());
00227 return $str;
00228 }
00229
00230 public function asStringRecursive($level=0)
00231 {
00232 $str = parent::asStringRecursive($level);
00233 foreach ($this->getConditions() as $cond) {
00234 $str .= "\n".$cond->asStringRecursive($level+1);
00235 }
00236 return $str;
00237 }
00238
00239 public function validate(Varien_Object $object)
00240 {
00241 if (!$this->getConditions()) {
00242 return true;
00243 }
00244
00245 $all = $this->getAggregator() === 'all';
00246 $true = (bool)$this->getValue();
00247
00248 foreach ($this->getConditions() as $cond) {
00249 $validated = $cond->validate($object);
00250
00251 if ($all && $validated !== $true) {
00252 return false;
00253 } elseif (!$all && $validated === $true) {
00254 return true;
00255 }
00256 }
00257 return $all ? true : false;
00258 }
00259
00260 public function setJsFormObject($form)
00261 {
00262 $this->setData('js_form_object', $form);
00263 foreach ($this->getConditions() as $condition) {
00264 $condition->setJsFormObject($form);
00265 }
00266 return $this;
00267 }
00268
00269 public function getConditions()
00270 {
00271 return $this->getData($this->getPrefix());
00272 }
00273 }