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: 29: 30: 31: 32: 33: 34:
35: abstract class Mage_Rule_Model_Condition_Abstract
36: extends Varien_Object
37: implements Mage_Rule_Model_Condition_Interface
38: {
39: 40: 41: 42: 43:
44: protected $_inputType = null;
45:
46: 47: 48: 49:
50: protected $_defaultOperatorOptions = null;
51:
52: 53: 54: 55:
56: protected $_defaultOperatorInputByType = null;
57:
58: 59: 60: 61:
62: protected $_arrayInputTypes = array();
63:
64: public function __construct()
65: {
66: parent::__construct();
67:
68: $this->loadAttributeOptions()->loadOperatorOptions()->loadValueOptions();
69:
70: if ($options = $this->getAttributeOptions()) {
71: foreach ($options as $attr=>$dummy) { $this->setAttribute($attr); break; }
72: }
73: if ($options = $this->getOperatorOptions()) {
74: foreach ($options as $operator=>$dummy) { $this->setOperator($operator); break; }
75: }
76: }
77:
78: 79: 80: 81: 82:
83: public function getDefaultOperatorInputByType()
84: {
85: if (null === $this->_defaultOperatorInputByType) {
86: $this->_defaultOperatorInputByType = array(
87: 'string' => array('==', '!=', '>=', '>', '<=', '<', '{}', '!{}', '()', '!()'),
88: 'numeric' => array('==', '!=', '>=', '>', '<=', '<', '()', '!()'),
89: 'date' => array('==', '>=', '<='),
90: 'select' => array('==', '!='),
91: 'boolean' => array('==', '!='),
92: 'multiselect' => array('{}', '!{}', '()', '!()'),
93: 'grid' => array('()', '!()'),
94: );
95: $this->_arrayInputTypes = array('multiselect', 'grid');
96: }
97: return $this->_defaultOperatorInputByType;
98: }
99:
100: 101: 102: 103: 104: 105:
106: public function getDefaultOperatorOptions()
107: {
108: if (null === $this->_defaultOperatorOptions) {
109: $this->_defaultOperatorOptions = array(
110: '==' => Mage::helper('rule')->__('is'),
111: '!=' => Mage::helper('rule')->__('is not'),
112: '>=' => Mage::helper('rule')->__('equals or greater than'),
113: '<=' => Mage::helper('rule')->__('equals or less than'),
114: '>' => Mage::helper('rule')->__('greater than'),
115: '<' => Mage::helper('rule')->__('less than'),
116: '{}' => Mage::helper('rule')->__('contains'),
117: '!{}' => Mage::helper('rule')->__('does not contain'),
118: '()' => Mage::helper('rule')->__('is one of'),
119: '!()' => Mage::helper('rule')->__('is not one of')
120: );
121: }
122: return $this->_defaultOperatorOptions;
123: }
124:
125: public function getForm()
126: {
127: return $this->getRule()->getForm();
128: }
129:
130: public function asArray(array $arrAttributes = array())
131: {
132: $out = array(
133: 'type'=>$this->getType(),
134: 'attribute'=>$this->getAttribute(),
135: 'operator'=>$this->getOperator(),
136: 'value'=>$this->getValue(),
137: 'is_value_processed'=>$this->getIsValueParsed(),
138: );
139: return $out;
140: }
141:
142: public function asXml()
143: {
144: $xml = "<type>".$this->getType()."</type>"
145: ."<attribute>".$this->getAttribute()."</attribute>"
146: ."<operator>".$this->getOperator()."</operator>"
147: ."<value>".$this->getValue()."</value>";
148: return $xml;
149: }
150:
151: public function loadArray($arr)
152: {
153: $this->setType($arr['type']);
154: $this->setAttribute(isset($arr['attribute']) ? $arr['attribute'] : false);
155: $this->setOperator(isset($arr['operator']) ? $arr['operator'] : false);
156: $this->setValue(isset($arr['value']) ? $arr['value'] : false);
157: $this->setIsValueParsed(isset($arr['is_value_parsed']) ? $arr['is_value_parsed'] : false);
158:
159:
160:
161:
162: return $this;
163: }
164:
165: public function loadXml($xml)
166: {
167: if (is_string($xml)) {
168: $xml = simplexml_load_string($xml);
169: }
170: $arr = (array)$xml;
171: $this->loadArray($arr);
172: return $this;
173: }
174:
175: public function loadAttributeOptions()
176: {
177: return $this;
178: }
179:
180: public function getAttributeOptions()
181: {
182: return array();
183: }
184:
185: public function getAttributeSelectOptions()
186: {
187: $opt = array();
188: foreach ($this->getAttributeOption() as $k=>$v) {
189: $opt[] = array('value'=>$k, 'label'=>$v);
190: }
191: return $opt;
192: }
193:
194: public function getAttributeName()
195: {
196: return $this->getAttributeOption($this->getAttribute());
197: }
198:
199: public function loadOperatorOptions()
200: {
201: $this->setOperatorOption($this->getDefaultOperatorOptions());
202: $this->setOperatorByInputType($this->getDefaultOperatorInputByType());
203: return $this;
204: }
205:
206: 207: 208: 209: 210: 211: 212:
213: public function getInputType()
214: {
215: if (null === $this->_inputType) {
216: return 'string';
217: }
218: return $this->_inputType;
219: }
220:
221: public function getOperatorSelectOptions()
222: {
223: $type = $this->getInputType();
224: $opt = array();
225: $operatorByType = $this->getOperatorByInputType();
226: foreach ($this->getOperatorOption() as $k => $v) {
227: if (!$operatorByType || in_array($k, $operatorByType[$type])) {
228: $opt[] = array('value' => $k, 'label' => $v);
229: }
230: }
231: return $opt;
232: }
233:
234: public function getOperatorName()
235: {
236: return $this->getOperatorOption($this->getOperator());
237: }
238:
239: public function loadValueOptions()
240: {
241:
242:
243:
244:
245: $this->setValueOption(array());
246: return $this;
247: }
248:
249: public function getValueSelectOptions()
250: {
251: $valueOption = $opt = array();
252: if ($this->hasValueOption()) {
253: $valueOption = (array) $this->getValueOption();
254: }
255: foreach ($valueOption as $k => $v) {
256: $opt[] = array('value' => $k, 'label' => $v);
257: }
258: return $opt;
259: }
260:
261: 262: 263: 264: 265:
266: public function getValueParsed()
267: {
268: if (!$this->hasValueParsed()) {
269: $value = $this->getData('value');
270: if ($this->isArrayOperatorType() && is_string($value)) {
271: $value = preg_split('#\s*[,;]\s*#', $value, null, PREG_SPLIT_NO_EMPTY);
272: }
273: $this->setValueParsed($value);
274: }
275: return $this->getData('value_parsed');
276: }
277:
278: 279: 280: 281: 282: 283: 284:
285: public function isArrayOperatorType()
286: {
287: $op = $this->getOperator();
288: return $op === '()' || $op === '!()' || in_array($this->getInputType(), $this->_arrayInputTypes);
289: }
290:
291: public function getValue()
292: {
293: if ($this->getInputType()=='date' && !$this->getIsValueParsed()) {
294:
295: $this->setValue(
296: Mage::app()->getLocale()->date($this->getData('value'),
297: Varien_Date::DATE_INTERNAL_FORMAT, null, false)->toString(Varien_Date::DATE_INTERNAL_FORMAT)
298: );
299: $this->setIsValueParsed(true);
300: }
301: return $this->getData('value');
302: }
303:
304: public function getValueName()
305: {
306: $value = $this->getValue();
307: if (is_null($value) || '' === $value) {
308: return '...';
309: }
310:
311: $options = $this->getValueSelectOptions();
312: $valueArr = array();
313: if (!empty($options)) {
314: foreach ($options as $o) {
315: if (is_array($value)) {
316: if (in_array($o['value'], $value)) {
317: $valueArr[] = $o['label'];
318: }
319: } else {
320: if (is_array($o['value'])) {
321: foreach ($o['value'] as $v) {
322: if ($v['value']==$value) {
323: return $v['label'];
324: }
325: }
326: }
327: if ($o['value'] == $value) {
328: return $o['label'];
329: }
330: }
331: }
332: }
333: if (!empty($valueArr)) {
334: $value = implode(', ', $valueArr);
335: }
336: return $value;
337: }
338:
339: 340: 341: 342: 343:
344: public function getNewChildSelectOptions()
345: {
346: return array(
347: array('value'=>'', 'label'=>Mage::helper('rule')->__('Please choose a condition to add...')),
348: );
349: }
350:
351: public function getNewChildName()
352: {
353: return $this->getAddLinkHtml();
354: }
355:
356: public function asHtml()
357: {
358: $html = $this->getTypeElementHtml()
359: .$this->getAttributeElementHtml()
360: .$this->getOperatorElementHtml()
361: .$this->getValueElementHtml()
362: .$this->getRemoveLinkHtml()
363: .$this->getChooserContainerHtml();
364: return $html;
365: }
366:
367: public function asHtmlRecursive()
368: {
369: $html = $this->asHtml();
370: return $html;
371: }
372:
373: public function getTypeElement()
374: {
375: return $this->getForm()->addField($this->getPrefix() . '__' . $this->getId() . '__type', 'hidden', array(
376: 'name' => 'rule[' . $this->getPrefix() . '][' . $this->getId() . '][type]',
377: 'value' => $this->getType(),
378: 'no_span' => true,
379: 'class' => 'hidden',
380: ));
381: }
382:
383: public function getTypeElementHtml()
384: {
385: return $this->getTypeElement()->getHtml();
386: }
387:
388: public function getAttributeElement()
389: {
390: if (is_null($this->getAttribute())) {
391: foreach ($this->getAttributeOption() as $k => $v) {
392: $this->setAttribute($k);
393: break;
394: }
395: }
396: return $this->getForm()->addField($this->getPrefix().'__'.$this->getId().'__attribute', 'select', array(
397: 'name'=>'rule['.$this->getPrefix().']['.$this->getId().'][attribute]',
398: 'values'=>$this->getAttributeSelectOptions(),
399: 'value'=>$this->getAttribute(),
400: 'value_name'=>$this->getAttributeName(),
401: ))->setRenderer(Mage::getBlockSingleton('rule/editable'));
402: }
403:
404: public function getAttributeElementHtml()
405: {
406: return $this->getAttributeElement()->getHtml();
407: }
408:
409: 410: 411: 412: 413: 414:
415: public function getOperatorElement()
416: {
417: $options = $this->getOperatorSelectOptions();
418: if (is_null($this->getOperator())) {
419: foreach ($options as $option) {
420: $this->setOperator($option['value']);
421: break;
422: }
423: }
424:
425: $elementId = sprintf('%s__%s__operator', $this->getPrefix(), $this->getId());
426: $elementName = sprintf('rule[%s][%s][operator]', $this->getPrefix(), $this->getId());
427: $element = $this->getForm()->addField($elementId, 'select', array(
428: 'name' => $elementName,
429: 'values' => $options,
430: 'value' => $this->getOperator(),
431: 'value_name' => $this->getOperatorName(),
432: ));
433: $element->setRenderer(Mage::getBlockSingleton('rule/editable'));
434:
435: return $element;
436: }
437:
438: public function getOperatorElementHtml()
439: {
440: return $this->getOperatorElement()->getHtml();
441: }
442:
443: 444: 445: 446: 447: 448:
449: public function getValueElementType()
450: {
451: return 'text';
452: }
453:
454: public function getValueElementRenderer()
455: {
456: if (strpos($this->getValueElementType(), '/')!==false) {
457: return Mage::getBlockSingleton($this->getValueElementType());
458: }
459: return Mage::getBlockSingleton('rule/editable');
460: }
461:
462: public function getValueElement()
463: {
464: $elementParams = array(
465: 'name' => 'rule['.$this->getPrefix().']['.$this->getId().'][value]',
466: 'value' => $this->getValue(),
467: 'values' => $this->getValueSelectOptions(),
468: 'value_name' => $this->getValueName(),
469: 'after_element_html' => $this->getValueAfterElementHtml(),
470: 'explicit_apply' => $this->getExplicitApply(),
471: );
472: if ($this->getInputType()=='date') {
473:
474: $elementParams['input_format'] = Varien_Date::DATE_INTERNAL_FORMAT;
475: $elementParams['format'] = Varien_Date::DATE_INTERNAL_FORMAT;
476: }
477: return $this->getForm()->addField($this->getPrefix().'__'.$this->getId().'__value',
478: $this->getValueElementType(),
479: $elementParams
480: )->setRenderer($this->getValueElementRenderer());
481: }
482:
483: public function getValueElementHtml()
484: {
485: return $this->getValueElement()->getHtml();
486: }
487:
488: public function getAddLinkHtml()
489: {
490: $src = Mage::getDesign()->getSkinUrl('images/rule_component_add.gif');
491: $html = '<img src="' . $src . '" class="rule-param-add v-middle" alt="" title="' . Mage::helper('rule')->__('Add') . '"/>';
492: return $html;
493: }
494:
495: public function getRemoveLinkHtml()
496: {
497: $src = Mage::getDesign()->getSkinUrl('images/rule_component_remove.gif');
498: $html = ' <span class="rule-param"><a href="javascript:void(0)" class="rule-param-remove" title="' . Mage::helper('rule')->__('Remove') . '"><img src="' . $src . '" alt="" class="v-middle" /></a></span>';
499: return $html;
500: }
501:
502: public function getChooserContainerHtml()
503: {
504: $url = $this->getValueElementChooserUrl();
505: $html = '';
506: if ($url) {
507: $html = '<div class="rule-chooser" url="' . $url . '"></div>';
508: }
509: return $html;
510: }
511:
512: public function asString($format = '')
513: {
514: $str = $this->getAttributeName() . ' ' . $this->getOperatorName() . ' ' . $this->getValueName();
515: return $str;
516: }
517:
518: public function asStringRecursive($level=0)
519: {
520: $str = str_pad('', $level * 3, ' ', STR_PAD_LEFT) . $this->asString();
521: return $str;
522: }
523:
524: 525: 526: 527: 528: 529:
530: public function validateAttribute($validatedValue)
531: {
532: if (is_object($validatedValue)) {
533: return false;
534: }
535:
536: 537: 538:
539: $value = $this->getValueParsed();
540:
541: 542: 543:
544: $op = $this->getOperatorForValidate();
545:
546:
547: if ($this->isArrayOperatorType() xor is_array($value)) {
548: return false;
549: }
550:
551: $result = false;
552:
553: switch ($op) {
554: case '==': case '!=':
555: if (is_array($value)) {
556: if (is_array($validatedValue)) {
557: $result = array_intersect($value, $validatedValue);
558: $result = !empty($result);
559: } else {
560: return false;
561: }
562: } else {
563: if (is_array($validatedValue)) {
564: $result = count($validatedValue) == 1 && array_shift($validatedValue) == $value;
565: } else {
566: $result = $this->_compareValues($validatedValue, $value);
567: }
568: }
569: break;
570:
571: case '<=': case '>':
572: if (!is_scalar($validatedValue)) {
573: return false;
574: } else {
575: $result = $validatedValue <= $value;
576: }
577: break;
578:
579: case '>=': case '<':
580: if (!is_scalar($validatedValue)) {
581: return false;
582: } else {
583: $result = $validatedValue >= $value;
584: }
585: break;
586:
587: case '{}': case '!{}':
588: if (is_scalar($validatedValue) && is_array($value)) {
589: foreach ($value as $item) {
590: if (stripos($validatedValue,$item)!==false) {
591: $result = true;
592: break;
593: }
594: }
595: } elseif (is_array($value)) {
596: if (is_array($validatedValue)) {
597: $result = array_intersect($value, $validatedValue);
598: $result = !empty($result);
599: } else {
600: return false;
601: }
602: } else {
603: if (is_array($validatedValue)) {
604: $result = in_array($value, $validatedValue);
605: } else {
606: $result = $this->_compareValues($value, $validatedValue, false);
607: }
608: }
609: break;
610:
611: case '()': case '!()':
612: if (is_array($validatedValue)) {
613: $result = count(array_intersect($validatedValue, (array)$value))>0;
614: } else {
615: $value = (array)$value;
616: foreach ($value as $item) {
617: if ($this->_compareValues($validatedValue, $item)) {
618: $result = true;
619: break;
620: }
621: }
622: }
623: break;
624: }
625:
626: if ('!=' == $op || '>' == $op || '<' == $op || '!{}' == $op || '!()' == $op) {
627: $result = !$result;
628: }
629:
630: return $result;
631: }
632:
633: 634: 635: 636: 637: 638: 639:
640: protected function _compareValues($validatedValue, $value, $strict = true)
641: {
642: if ($strict && is_numeric($validatedValue) && is_numeric($value)) {
643: return $validatedValue == $value;
644: } else {
645: $validatePattern = preg_quote($validatedValue, '~');
646: if ($strict) {
647: $validatePattern = '^' . $validatePattern . '$';
648: }
649: return (bool)preg_match('~' . $validatePattern . '~iu', $value);
650: }
651: }
652:
653: public function validate(Varien_Object $object)
654: {
655: return $this->validateAttribute($object->getData($this->getAttribute()));
656: }
657:
658: 659: 660: 661: 662:
663: public function getOperatorForValidate()
664: {
665: return $this->getOperator();
666: }
667: }
668: