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: class Mage_Rule_Model_Condition_Combine extends Mage_Rule_Model_Condition_Abstract
29: {
30: 31: 32: 33: 34:
35: static protected $_conditionModels = array();
36:
37:
38:
39:
40:
41: 42: 43: 44: 45: 46: 47: 48: 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:
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:
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: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 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: 311: 312: 313:
314: public function getConditions()
315: {
316: $key = $this->getPrefix() ? $this->getPrefix() : 'conditions';
317: return $this->getData($key);
318: }
319:
320: 321: 322: 323: 324: 325:
326: public function setConditions($conditions)
327: {
328: $key = $this->getPrefix() ? $this->getPrefix() : 'conditions';
329: return $this->setData($key, $conditions);
330: }
331:
332: 333: 334:
335: protected function _getRecursiveChildSelectOption()
336: {
337: return array('value' => $this->getType(), 'label' => Mage::helper('rule')->__('Conditions Combination'));
338: }
339: }
340: