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
00029
00030
00031
00032 abstract class Mage_Rule_Model_Condition_Abstract
00033 extends Varien_Object
00034 implements Mage_Rule_Model_Condition_Interface
00035 {
00036 public function __construct()
00037 {
00038 parent::__construct();
00039
00040 $this->loadAttributeOptions()->loadOperatorOptions()->loadValueOptions();
00041
00042 if ($options = $this->getAttributeOptions()) {
00043 foreach ($options as $attr=>$dummy) { $this->setAttribute($attr); break; }
00044 }
00045 if ($options = $this->getOperatorOptions()) {
00046 foreach ($options as $operator=>$dummy) { $this->setOperator($operator); break; }
00047 }
00048 }
00049
00050 public function getForm()
00051 {
00052 return $this->getRule()->getForm();
00053 }
00054
00055 public function asArray(array $arrAttributes = array())
00056 {
00057 $out = array(
00058 'type'=>$this->getType(),
00059 'attribute'=>$this->getAttribute(),
00060 'operator'=>$this->getOperator(),
00061 'value'=>$this->getValue(),
00062 'is_value_processed'=>$this->getIsValueParsed(),
00063 );
00064 return $out;
00065 }
00066
00067 public function asXml()
00068 {
00069 $xml = "<type>".$this->getType()."</type>"
00070 ."<attribute>".$this->getAttribute()."</attribute>"
00071 ."<operator>".$this->getOperator()."</operator>"
00072 ."<value>".$this->getValue()."</value>";
00073 return $xml;
00074 }
00075
00076 public function loadArray($arr)
00077 {
00078 $this->setType($arr['type']);
00079 $this->setAttribute(isset($arr['attribute']) ? $arr['attribute'] : false);
00080 $this->setOperator(isset($arr['operator']) ? $arr['operator'] : false);
00081 $this->setValue(isset($arr['value']) ? $arr['value'] : false);
00082 $this->setIsValueParsed(isset($arr['is_value_parsed']) ? $arr['is_value_parsed'] : false);
00083
00084
00085
00086
00087 return $this;
00088 }
00089
00090 public function loadXml($xml)
00091 {
00092 if (is_string($xml)) {
00093 $xml = simplexml_load_string($xml);
00094 }
00095 $arr = (array)$xml;
00096 $this->loadArray($arr);
00097 return $this;
00098 }
00099
00100 public function loadAttributeOptions()
00101 {
00102 return $this;
00103 }
00104
00105 public function getAttributeOptions()
00106 {
00107 return array();
00108 }
00109
00110 public function getAttributeSelectOptions()
00111 {
00112 $opt = array();
00113 foreach ($this->getAttributeOption() as $k=>$v) {
00114 $opt[] = array('value'=>$k, 'label'=>$v);
00115 }
00116 return $opt;
00117 }
00118
00119 public function getAttributeName()
00120 {
00121 return $this->getAttributeOption($this->getAttribute());
00122 }
00123
00124 public function loadOperatorOptions()
00125 {
00126 $this->setOperatorOption(array(
00127 '==' => Mage::helper('rule')->__('is'),
00128 '!=' => Mage::helper('rule')->__('is not'),
00129 '>=' => Mage::helper('rule')->__('equals or greater than'),
00130 '<=' => Mage::helper('rule')->__('equals or less than'),
00131 '>' => Mage::helper('rule')->__('greater than'),
00132 '<' => Mage::helper('rule')->__('less than'),
00133 '{}' => Mage::helper('rule')->__('contains'),
00134 '!{}' => Mage::helper('rule')->__('does not contain'),
00135 '()' => Mage::helper('rule')->__('is one of'),
00136 '!()' => Mage::helper('rule')->__('is not one of'),
00137 ));
00138 $this->setOperatorByInputType(array(
00139 'string' => array('==', '!=', '>=', '>', '<=', '<', '{}', '!{}', '()', '!()'),
00140 'numeric' => array('==', '!=', '>=', '>', '<=', '<', '()', '!()'),
00141 'date' => array('==', '>=', '<='),
00142 'select' => array('==', '!='),
00143 'multiselect' => array('==', '!=', '{}', '!{}'),
00144 'grid' => array('()', '!()'),
00145 ));
00146 return $this;
00147 }
00148
00149
00150
00151
00152
00153
00154
00155
00156 public function getInputType()
00157 {
00158 return 'string';
00159 }
00160
00161 public function getOperatorSelectOptions()
00162 {
00163 $type = $this->getInputType();
00164 $opt = array();
00165 $operatorByType = $this->getOperatorByInputType();
00166 foreach ($this->getOperatorOption() as $k=>$v) {
00167 if (!$operatorByType || in_array($k, $operatorByType[$type])) {
00168 $opt[] = array('value'=>$k, 'label'=>$v);
00169 }
00170 }
00171 return $opt;
00172 }
00173
00174 public function getOperatorName()
00175 {
00176 return $this->getOperatorOption($this->getOperator());
00177 }
00178
00179 public function loadValueOptions()
00180 {
00181
00182
00183
00184
00185 $this->setValueOption(array());
00186 return $this;
00187 }
00188
00189 public function getValueSelectOptions()
00190 {
00191 $opt = array();
00192 foreach ($this->getValueOption() as $k=>$v) {
00193 $opt[] = array('value'=>$k, 'label'=>$v);
00194 }
00195 return $opt;
00196 }
00197
00198 public function getValueParsed()
00199 {
00200 $value = $this->getData('value');
00201
00202 $op = $this->getOperator();
00203 if (($op==='()' || $op==='!()') && is_string($value)) {
00204 $value = preg_split('#\s*[,;]\s*#', $value, null, PREG_SPLIT_NO_EMPTY);
00205 $this->setValue($value);
00206 }
00207
00208 return $value;
00209 }
00210
00211 public function getValue()
00212 {
00213 if ($this->getInputType()=='date' && !$this->getIsValueParsed()) {
00214
00215 $this->setValue(Mage::app()->getLocale()->date($this->getData('value'), Varien_Date::DATE_INTERNAL_FORMAT, null, false)
00216 ->toString(Varien_Date::DATE_INTERNAL_FORMAT));
00217 $this->setIsValueParsed(true);
00218 }
00219 return $this->getData('value');
00220 }
00221
00222 public function getValueName()
00223 {
00224 $value = $this->getValue();
00225 if (is_null($value) || ''===$value) {
00226 return '...';
00227 }
00228
00229 $options = $this->getValueSelectOptions();
00230 $valueArr = array();
00231 if (!empty($options)) {
00232 foreach ($options as $o) {
00233 if (is_array($value)) {
00234 if (in_array($o['value'], $value)) {
00235 $valueArr[] = $o['label'];
00236 }
00237 } else {
00238 if (is_array($o['value'])) {
00239 foreach ($o['value'] as $v) {
00240 if ($v['value']==$value) {
00241 return $v['label'];
00242 }
00243 }
00244 }
00245 if ($o['value']==$value) {
00246 return $o['label'];
00247 }
00248 }
00249 }
00250 }
00251 if (!empty($valueArr)) {
00252 $value = implode(', ', $valueArr);
00253 }
00254 return $value;
00255 }
00256
00257 public function getNewChildSelectOptions()
00258 {
00259 return array(
00260 array('value'=>'', 'label'=>Mage::helper('rule')->__('Please choose a condition to add...')),
00261 );
00262 }
00263
00264 public function getNewChildName()
00265 {
00266 return $this->getAddLinkHtml();
00267 }
00268
00269 public function asHtml()
00270 {
00271 $html = $this->getTypeElementHtml()
00272 .$this->getAttributeElementHtml()
00273 .$this->getOperatorElementHtml()
00274 .$this->getValueElementHtml()
00275 .$this->getRemoveLinkHtml()
00276 .$this->getChooserContainerHtml();
00277 return $html;
00278 }
00279
00280 public function asHtmlRecursive()
00281 {
00282 $html = $this->asHtml();
00283 return $html;
00284 }
00285
00286 public function getTypeElement()
00287 {
00288 return $this->getForm()->addField($this->getPrefix().'__'.$this->getId().'__type', 'hidden', array(
00289 'name'=>'rule['.$this->getPrefix().']['.$this->getId().'][type]',
00290 'value'=>$this->getType(),
00291 'no_span'=>true,
00292 'class' => 'hidden',
00293 ));
00294 }
00295
00296 public function getTypeElementHtml()
00297 {
00298 return $this->getTypeElement()->getHtml();
00299 }
00300
00301 public function getAttributeElement()
00302 {
00303 if (is_null($this->getAttribute())) {
00304 foreach ($this->getAttributeOption() as $k=>$v) {
00305 $this->setAttribute($k);
00306 break;
00307 }
00308 }
00309 return $this->getForm()->addField($this->getPrefix().'__'.$this->getId().'__attribute', 'select', array(
00310 'name'=>'rule['.$this->getPrefix().']['.$this->getId().'][attribute]',
00311 'values'=>$this->getAttributeSelectOptions(),
00312 'value'=>$this->getAttribute(),
00313 'value_name'=>$this->getAttributeName(),
00314 ))->setRenderer(Mage::getBlockSingleton('rule/editable'));
00315 }
00316
00317 public function getAttributeElementHtml()
00318 {
00319 return $this->getAttributeElement()->getHtml();
00320 }
00321
00322 public function getOperatorElement()
00323 {
00324 if (is_null($this->getOperator())) {
00325 foreach ($this->getOperatorOption() as $k=>$v) {
00326 $this->setOperator($k);
00327 break;
00328 }
00329 }
00330 return $this->getForm()->addField($this->getPrefix().'__'.$this->getId().'__operator', 'select', array(
00331 'name'=>'rule['.$this->getPrefix().']['.$this->getId().'][operator]',
00332 'values'=>$this->getOperatorSelectOptions(),
00333 'value'=>$this->getOperator(),
00334 'value_name'=>$this->getOperatorName(),
00335 ))->setRenderer(Mage::getBlockSingleton('rule/editable'));
00336 }
00337
00338 public function getOperatorElementHtml()
00339 {
00340 return $this->getOperatorElement()->getHtml();
00341 }
00342
00343
00344
00345
00346
00347
00348
00349 public function getValueElementType()
00350 {
00351 return 'text';
00352 }
00353
00354 public function getValueElementRenderer()
00355 {
00356 if (strpos($this->getValueElementType(), '/')!==false) {
00357 return Mage::getBlockSingleton($this->getValueElementType());
00358 }
00359 return Mage::getBlockSingleton('rule/editable');
00360 }
00361
00362 public function getValueElement()
00363 {
00364 $elementParams = array(
00365 'name' => 'rule['.$this->getPrefix().']['.$this->getId().'][value]',
00366 'value' => $this->getValue(),
00367 'values' => $this->getValueSelectOptions(),
00368 'value_name' => $this->getValueName(),
00369 'after_element_html' => $this->getValueAfterElementHtml(),
00370 'explicit_apply' => $this->getExplicitApply(),
00371 );
00372 if ($this->getInputType()=='date') {
00373
00374 $elementParams['input_format'] = Varien_Date::DATE_INTERNAL_FORMAT;
00375 $elementParams['format'] = Varien_Date::DATE_INTERNAL_FORMAT;
00376 }
00377 return $this->getForm()->addField($this->getPrefix().'__'.$this->getId().'__value',
00378 $this->getValueElementType(),
00379 $elementParams
00380 )->setRenderer($this->getValueElementRenderer());
00381 }
00382
00383 public function getValueElementHtml()
00384 {
00385 return $this->getValueElement()->getHtml();
00386 }
00387
00388 public function getAddLinkHtml()
00389 {
00390 $src = Mage::getDesign()->getSkinUrl('images/rule_component_add.gif');
00391 $html = '<img src="'.$src.'" class="rule-param-add v-middle" alt="" title="'.Mage::helper('rule')->__('Add').'"/>';
00392 return $html;
00393 }
00394
00395 public function getRemoveLinkHtml()
00396 {
00397 $src = Mage::getDesign()->getSkinUrl('images/rule_component_remove.gif');
00398 $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>';
00399 return $html;
00400 }
00401
00402 public function getChooserContainerHtml()
00403 {
00404 $url = $this->getValueElementChooserUrl();
00405 $html = '';
00406 if ($url) {
00407 $html = '<div class="rule-chooser" url="'.$url.'"></div>';
00408 }
00409 return $html;
00410 }
00411
00412 public function asString($format='')
00413 {
00414 $str = $this->getAttributeName().' '.$this->getOperatorName().' '.$this->getValueName();
00415 return $str;
00416 }
00417
00418 public function asStringRecursive($level=0)
00419 {
00420 $str = str_pad('', $level*3, ' ', STR_PAD_LEFT).$this->asString();
00421 return $str;
00422 }
00423
00424
00425
00426
00427
00428
00429
00430 public function validateAttribute($validatedValue)
00431 {
00432 if (is_object($validatedValue)) {
00433 return false;
00434 }
00435
00436
00437
00438
00439 $value = $this->getValueParsed();
00440
00441
00442
00443
00444 $op = $this->getOperator();
00445
00446
00447 if ((($op=='()' || $op=='!()') && !is_array($value))
00448 || (!($op=='()' || $op=='!()' || $op=='!=' || $op=='==' || $op=='{}' || $op=='!{}') && is_array($value))) {
00449 return false;
00450 }
00451
00452 $result = false;
00453
00454 switch ($op) {
00455 case '==': case '!=':
00456 if (is_array($value)) {
00457 if (is_array($validatedValue)) {
00458 $result = array_diff($validatedValue, $value);
00459 $result = empty($result) && (sizeof($validatedValue) == sizeof($value));
00460 } else {
00461 return false;
00462 }
00463 } else {
00464 if (is_array($validatedValue)) {
00465 $result = in_array($value, $validatedValue);
00466 } else {
00467 $result = $validatedValue==$value;
00468 }
00469 }
00470 break;
00471
00472 case '<=': case '>':
00473 if (is_array($validatedValue)) {
00474 $result = false;
00475 } else {
00476 $result = $validatedValue<=$value;
00477 }
00478 break;
00479
00480 case '>=': case '<':
00481 if (is_array($validatedValue)) {
00482 $result = false;
00483 } else {
00484 $result = $validatedValue>=$value;
00485 }
00486 break;
00487
00488 case '{}': case '!{}':
00489 if (is_array($value)) {
00490 if (is_array($validatedValue)) {
00491 $result = array_diff($value, $validatedValue);
00492 $result = empty($result);
00493 } else {
00494 return false;
00495 }
00496 } else {
00497 if (is_array($validatedValue)) {
00498 $result = false;
00499 } else {
00500 $result = stripos((string)$validatedValue, (string)$value)!==false;
00501 }
00502 }
00503 break;
00504
00505 case '()': case '!()':
00506 if (is_array($validatedValue)) {
00507 $result = count(array_intersect($validatedValue, (array)$value))>0;
00508 } else {
00509 $result = in_array($validatedValue, (array)$value);
00510 }
00511 break;
00512 }
00513
00514 if ('!='==$op || '>'==$op || '<'==$op || '!{}'==$op || '!()'==$op) {
00515 $result = !$result;
00516 }
00517
00518 return $result;
00519 }
00520
00521 public function validate(Varien_Object $object)
00522 {
00523 return $this->validateAttribute($object->getData($this->getAttribute()));
00524 }
00525 }