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: /**
 29:  * Abstract Rule condition data model
 30:  *
 31:  * @category Mage
 32:  * @package Mage_Rule
 33:  * @author Magento Core Team <core@magentocommerce.com>
 34:  */
 35: abstract class Mage_Rule_Model_Condition_Abstract
 36:     extends Varien_Object
 37:     implements Mage_Rule_Model_Condition_Interface
 38: {
 39:     /**
 40:      * Defines which operators will be available for this condition
 41:      *
 42:      * @var string
 43:      */
 44:     protected $_inputType = null;
 45: 
 46:     /**
 47:      * Default values for possible operator options
 48:      * @var array
 49:      */
 50:     protected $_defaultOperatorOptions = null;
 51: 
 52:     /**
 53:      * Default combinations of operator options, depending on input type
 54:      * @var array
 55:      */
 56:     protected $_defaultOperatorInputByType = null;
 57: 
 58:     /**
 59:      * List of input types for values which should be array
 60:      * @var array
 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:      * Default operator input by type map getter
 80:      *
 81:      * @return array
 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:      * Default operator options getter
102:      * Provides all possible operator options
103:      *
104:      * @return array
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: //        $this->loadAttributeOptions();
160: //        $this->loadOperatorOptions();
161: //        $this->loadValueOptions();
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:      * This value will define which operators will be available for this condition.
208:      *
209:      * Possible values are: string, numeric, date, select, multiselect, grid, boolean
210:      *
211:      * @return string
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: //        $this->setValueOption(array(
242: //            true  => Mage::helper('rule')->__('TRUE'),
243: //            false => Mage::helper('rule')->__('FALSE'),
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:      * Retrieve parsed value
263:      *
264:      * @return array|string|int|float
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:      * Check if value should be array
280:      *
281:      * Depends on operator input type
282:      *
283:      * @return bool
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:             // date format intentionally hard-coded
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:      * Get inherited conditions selectors
341:      *
342:      * @return array
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:      * Retrieve Condition Operator element Instance
411:      * If the operator value is empty - define first available operator value as default
412:      *
413:      * @return Varien_Data_Form_Element_Select
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:      * Value element type will define renderer for condition value element
445:      *
446:      * @see Varien_Data_Form_Element
447:      * @return string
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:             // date format intentionally hard-coded
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:      * Validate product attrbute value for condition
526:      *
527:      * @param   mixed $validatedValue product attribute value
528:      * @return  bool
529:      */
530:     public function validateAttribute($validatedValue)
531:     {
532:         if (is_object($validatedValue)) {
533:             return false;
534:         }
535: 
536:         /**
537:          * Condition attribute value
538:          */
539:         $value = $this->getValueParsed();
540: 
541:         /**
542:          * Comparison operator
543:          */
544:         $op = $this->getOperatorForValidate();
545: 
546:         // if operator requires array and it is not, or on opposite, return false
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:      * Case and type insensitive comparison of values
635:      *
636:      * @param  string|int|float $validatedValue
637:      * @param  string|int|float $value
638:      * @return bool
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:      * Retrieve operator for php validation
660:      *
661:      * @return string
662:      */
663:     public function getOperatorForValidate()
664:     {
665:         return $this->getOperator();
666:     }
667: }
668: 
Magento 1.7.0.2 API documentation generated by ApiGen 2.8.0