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:  * Abstract Rule product condition data model
 29:  *
 30:  * @category Mage
 31:  * @package Mage_Rule
 32:  * @author Magento Core Team <core@magentocommerce.com>
 33:  */
 34: abstract class Mage_Rule_Model_Condition_Product_Abstract extends Mage_Rule_Model_Condition_Abstract
 35: {
 36:     /**
 37:      * All attribute values as array in form:
 38:      * array(
 39:      *   [entity_id_1] => array(
 40:      *          [store_id_1] => store_value_1,
 41:      *          [store_id_2] => store_value_2,
 42:      *          ...
 43:      *          [store_id_n] => store_value_n
 44:      *   ),
 45:      *   ...
 46:      * )
 47:      *
 48:      * Will be set only for not global scope attribute
 49:      *
 50:      * @var array
 51:      */
 52:     protected $_entityAttributeValues = null;
 53: 
 54:     /**
 55:      * Attribute data key that indicates whether it should be used for rules
 56:      *
 57:      * @var string
 58:      */
 59:     protected $_isUsedForRuleProperty = 'is_used_for_promo_rules';
 60: 
 61:     /**
 62:      * Customize default operator input by type mapper for some types
 63:      *
 64:      * @return array
 65:      */
 66:     public function getDefaultOperatorInputByType()
 67:     {
 68:         if (null === $this->_defaultOperatorInputByType) {
 69:             parent::getDefaultOperatorInputByType();
 70:             /*
 71:              * '{}' and '!{}' are left for back-compatibility and equal to '==' and '!='
 72:              */
 73:             $this->_defaultOperatorInputByType['category'] = array('==', '!=', '{}', '!{}', '()', '!()');
 74:             $this->_arrayInputTypes[] = 'category';
 75:         }
 76:         return $this->_defaultOperatorInputByType;
 77:     }
 78: 
 79:     /**
 80:      * Retrieve attribute object
 81:      *
 82:      * @return Mage_Catalog_Model_Resource_Eav_Attribute
 83:      */
 84:     public function getAttributeObject()
 85:     {
 86:         try {
 87:             $obj = Mage::getSingleton('eav/config')
 88:                 ->getAttribute(Mage_Catalog_Model_Product::ENTITY, $this->getAttribute());
 89:         }
 90:         catch (Exception $e) {
 91:             $obj = new Varien_Object();
 92:             $obj->setEntity(Mage::getResourceSingleton('catalog/product'))
 93:                 ->setFrontendInput('text');
 94:         }
 95:         return $obj;
 96:     }
 97: 
 98:     /**
 99:      * Add special attributes
100:      *
101:      * @param array $attributes
102:      */
103:     protected function _addSpecialAttributes(array &$attributes)
104:     {
105:         $attributes['attribute_set_id'] = Mage::helper('catalogrule')->__('Attribute Set');
106:         $attributes['category_ids'] = Mage::helper('catalogrule')->__('Category');
107:     }
108: 
109:     /**
110:      * Load attribute options
111:      *
112:      * @return Mage_CatalogRule_Model_Rule_Condition_Product
113:      */
114:     public function loadAttributeOptions()
115:     {
116:         $productAttributes = Mage::getResourceSingleton('catalog/product')
117:             ->loadAllAttributes()
118:             ->getAttributesByCode();
119: 
120:         $attributes = array();
121:         foreach ($productAttributes as $attribute) {
122:             /* @var $attribute Mage_Catalog_Model_Resource_Eav_Attribute */
123:             if (!$attribute->isAllowedForRuleCondition()
124:                 || !$attribute->getDataUsingMethod($this->_isUsedForRuleProperty)
125:             ) {
126:                 continue;
127:             }
128:             $attributes[$attribute->getAttributeCode()] = $attribute->getFrontendLabel();
129:         }
130: 
131:         $this->_addSpecialAttributes($attributes);
132: 
133:         asort($attributes);
134:         $this->setAttributeOption($attributes);
135: 
136:         return $this;
137:     }
138: 
139:     /**
140:      * Prepares values options to be used as select options or hashed array
141:      * Result is stored in following keys:
142:      *  'value_select_options' - normal select array: array(array('value' => $value, 'label' => $label), ...)
143:      *  'value_option' - hashed array: array($value => $label, ...),
144:      *
145:      * @return Mage_CatalogRule_Model_Rule_Condition_Product
146:      */
147:     protected function _prepareValueOptions()
148:     {
149:         // Check that both keys exist. Maybe somehow only one was set not in this routine, but externally.
150:         $selectReady = $this->getData('value_select_options');
151:         $hashedReady = $this->getData('value_option');
152:         if ($selectReady && $hashedReady) {
153:             return $this;
154:         }
155: 
156:         // Get array of select options. It will be used as source for hashed options
157:         $selectOptions = null;
158:         if ($this->getAttribute() === 'attribute_set_id') {
159:             $entityTypeId = Mage::getSingleton('eav/config')
160:                 ->getEntityType(Mage_Catalog_Model_Product::ENTITY)->getId();
161:             $selectOptions = Mage::getResourceModel('eav/entity_attribute_set_collection')
162:                 ->setEntityTypeFilter($entityTypeId)
163:                 ->load()
164:                 ->toOptionArray();
165:         } else if (is_object($this->getAttributeObject())) {
166:             $attributeObject = $this->getAttributeObject();
167:             if ($attributeObject->usesSource()) {
168:                 if ($attributeObject->getFrontendInput() == 'multiselect') {
169:                     $addEmptyOption = false;
170:                 } else {
171:                     $addEmptyOption = true;
172:                 }
173:                 $selectOptions = $attributeObject->getSource()->getAllOptions($addEmptyOption);
174:             }
175:         }
176: 
177:         // Set new values only if we really got them
178:         if ($selectOptions !== null) {
179:             // Overwrite only not already existing values
180:             if (!$selectReady) {
181:                 $this->setData('value_select_options', $selectOptions);
182:             }
183:             if (!$hashedReady) {
184:                 $hashedOptions = array();
185:                 foreach ($selectOptions as $o) {
186:                     if (is_array($o['value'])) {
187:                         continue; // We cannot use array as index
188:                     }
189:                     $hashedOptions[$o['value']] = $o['label'];
190:                 }
191:                 $this->setData('value_option', $hashedOptions);
192:             }
193:         }
194: 
195:         return $this;
196:     }
197: 
198:     /**
199:      * Retrieve value by option
200:      *
201:      * @param mixed $option
202:      * @return string
203:      */
204:     public function getValueOption($option=null)
205:     {
206:         $this->_prepareValueOptions();
207:         return $this->getData('value_option'.(!is_null($option) ? '/'.$option : ''));
208:     }
209: 
210:     /**
211:      * Retrieve select option values
212:      *
213:      * @return array
214:      */
215:     public function getValueSelectOptions()
216:     {
217:         $this->_prepareValueOptions();
218:         return $this->getData('value_select_options');
219:     }
220: 
221:     /**
222:      * Retrieve after element HTML
223:      *
224:      * @return string
225:      */
226:     public function getValueAfterElementHtml()
227:     {
228:         $html = '';
229: 
230:         switch ($this->getAttribute()) {
231:             case 'sku': case 'category_ids':
232:                 $image = Mage::getDesign()->getSkinUrl('images/rule_chooser_trigger.gif');
233:                 break;
234:         }
235: 
236:         if (!empty($image)) {
237:             $html = '<a href="javascript:void(0)" class="rule-chooser-trigger"><img src="' . $image . '" alt="" class="v-middle rule-chooser-trigger" title="' . Mage::helper('rule')->__('Open Chooser') . '" /></a>';
238:         }
239:         return $html;
240:     }
241: 
242:     /**
243:      * Retrieve attribute element
244:      *
245:      * @return Varien_Form_Element_Abstract
246:      */
247:     public function getAttributeElement()
248:     {
249:         $element = parent::getAttributeElement();
250:         $element->setShowAsText(true);
251:         return $element;
252:     }
253: 
254:     /**
255:      * Collect validated attributes
256:      *
257:      * @param Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection $productCollection
258:      * @return Mage_CatalogRule_Model_Rule_Condition_Product
259:      */
260:     public function collectValidatedAttributes($productCollection)
261:     {
262:         $attribute = $this->getAttribute();
263:         if ('category_ids' != $attribute) {
264:             if ($this->getAttributeObject()->isScopeGlobal()) {
265:                 $attributes = $this->getRule()->getCollectedAttributes();
266:                 $attributes[$attribute] = true;
267:                 $this->getRule()->setCollectedAttributes($attributes);
268:                 $productCollection->addAttributeToSelect($attribute, 'left');
269:             } else {
270:                 $this->_entityAttributeValues = $productCollection->getAllAttributeValues($attribute);
271:             }
272:         }
273: 
274:         return $this;
275:     }
276: 
277:     /**
278:      * Retrieve input type
279:      *
280:      * @return string
281:      */
282:     public function getInputType()
283:     {
284:         if ($this->getAttribute()==='attribute_set_id') {
285:             return 'select';
286:         }
287:         if (!is_object($this->getAttributeObject())) {
288:             return 'string';
289:         }
290:         if ($this->getAttributeObject()->getAttributeCode() == 'category_ids') {
291:             return 'category';
292:         }
293:         switch ($this->getAttributeObject()->getFrontendInput()) {
294:             case 'select':
295:                 return 'select';
296: 
297:             case 'multiselect':
298:                 return 'multiselect';
299: 
300:             case 'date':
301:                 return 'date';
302: 
303:             case 'boolean':
304:                 return 'boolean';
305: 
306:             default:
307:                 return 'string';
308:         }
309:     }
310: 
311:     /**
312:      * Retrieve value element type
313:      *
314:      * @return string
315:      */
316:     public function getValueElementType()
317:     {
318:         if ($this->getAttribute()==='attribute_set_id') {
319:             return 'select';
320:         }
321:         if (!is_object($this->getAttributeObject())) {
322:             return 'text';
323:         }
324:         switch ($this->getAttributeObject()->getFrontendInput()) {
325:             case 'select':
326:             case 'boolean':
327:                 return 'select';
328: 
329:             case 'multiselect':
330:                 return 'multiselect';
331: 
332:             case 'date':
333:                 return 'date';
334: 
335:             default:
336:                 return 'text';
337:         }
338:     }
339: 
340:     /**
341:      * Retrieve value element
342:      *
343:      * @return Varien_Data_Form_Element_Abstract
344:      */
345:     public function getValueElement()
346:     {
347:         $element = parent::getValueElement();
348:         if (is_object($this->getAttributeObject())) {
349:             switch ($this->getAttributeObject()->getFrontendInput()) {
350:                 case 'date':
351:                     $element->setImage(Mage::getDesign()->getSkinUrl('images/grid-cal.gif'));
352:                     break;
353:             }
354:         }
355: 
356:         return $element;
357:     }
358: 
359:     /**
360:      * Retrieve value element chooser URL
361:      *
362:      * @return string
363:      */
364:     public function getValueElementChooserUrl()
365:     {
366:         $url = false;
367:         switch ($this->getAttribute()) {
368:             case 'sku': case 'category_ids':
369:                 $url = 'adminhtml/promo_widget/chooser'
370:                     .'/attribute/'.$this->getAttribute();
371:                 if ($this->getJsFormObject()) {
372:                     $url .= '/form/'.$this->getJsFormObject();
373:                 }
374:                 break;
375:         }
376:         return $url!==false ? Mage::helper('adminhtml')->getUrl($url) : '';
377:     }
378: 
379:     /**
380:      * Retrieve Explicit Apply
381:      *
382:      * @return bool
383:      */
384:     public function getExplicitApply()
385:     {
386:         switch ($this->getAttribute()) {
387:             case 'sku': case 'category_ids':
388:                 return true;
389:         }
390:         if (is_object($this->getAttributeObject())) {
391:             switch ($this->getAttributeObject()->getFrontendInput()) {
392:                 case 'date':
393:                     return true;
394:             }
395:         }
396:         return false;
397:     }
398: 
399:     /**
400:      * Load array
401:      *
402:      * @param array $arr
403:      * @return Mage_CatalogRule_Model_Rule_Condition_Product
404:      */
405:     public function loadArray($arr)
406:     {
407:         $this->setAttribute(isset($arr['attribute']) ? $arr['attribute'] : false);
408:         $attribute = $this->getAttributeObject();
409: 
410:         $isContainsOperator = !empty($arr['operator']) && in_array($arr['operator'], array('{}', '!{}'));
411:         if ($attribute && $attribute->getBackendType() == 'decimal' && !$isContainsOperator) {
412:             if (isset($arr['value'])) {
413:                 if (!empty($arr['operator'])
414:                     && in_array($arr['operator'], array('!()', '()'))
415:                     && false !== strpos($arr['value'], ',')) {
416: 
417:                     $tmp = array();
418:                     foreach (explode(',', $arr['value']) as $value) {
419:                         $tmp[] = Mage::app()->getLocale()->getNumber($value);
420:                     }
421:                     $arr['value'] =  implode(',', $tmp);
422:                 } else {
423:                     $arr['value'] =  Mage::app()->getLocale()->getNumber($arr['value']);
424:                 }
425:             } else {
426:                 $arr['value'] = false;
427:             }
428:             $arr['is_value_parsed'] = isset($arr['is_value_parsed'])
429:                 ? Mage::app()->getLocale()->getNumber($arr['is_value_parsed']) : false;
430:         }
431: 
432:         return parent::loadArray($arr);
433:     }
434: 
435:     /**
436:      * Validate product attrbute value for condition
437:      *
438:      * @param Varien_Object $object
439:      * @return bool
440:      */
441:     public function validate(Varien_Object $object)
442:     {
443:         $attrCode = $this->getAttribute();
444: 
445:         if ('category_ids' == $attrCode) {
446:             return $this->validateAttribute($object->getAvailableInCategories());
447:         } elseif (! isset($this->_entityAttributeValues[$object->getId()])) {
448:             if (!$object->getResource()) {
449:                 return false;
450:             }
451:             $attr = $object->getResource()->getAttribute($attrCode);
452: 
453:             if ($attr && $attr->getBackendType() == 'datetime' && !is_int($this->getValue())) {
454:                 $this->setValue(strtotime($this->getValue()));
455:                 $value = strtotime($object->getData($attrCode));
456:                 return $this->validateAttribute($value);
457:             }
458: 
459:             if ($attr && $attr->getFrontendInput() == 'multiselect') {
460:                 $value = $object->getData($attrCode);
461:                 $value = strlen($value) ? explode(',', $value) : array();
462:                 return $this->validateAttribute($value);
463:             }
464: 
465:             return parent::validate($object);
466:         } else {
467:             $result = false; // any valid value will set it to TRUE
468:             // remember old attribute state
469:             $oldAttrValue = $object->hasData($attrCode) ? $object->getData($attrCode) : null;
470: 
471:             foreach ($this->_entityAttributeValues[$object->getId()] as $storeId => $value) {
472:                 $attr = $object->getResource()->getAttribute($attrCode);
473:                 if ($attr && $attr->getBackendType() == 'datetime') {
474:                     $value = strtotime($value);
475:                 } else if ($attr && $attr->getFrontendInput() == 'multiselect') {
476:                     $value = strlen($value) ? explode(',', $value) : array();
477:                 }
478: 
479:                 $object->setData($attrCode, $value);
480:                 $result |= parent::validate($object);
481: 
482:                 if ($result) {
483:                     break;
484:                 }
485:             }
486: 
487:             if (is_null($oldAttrValue)) {
488:                 $object->unsetData($attrCode);
489:             } else {
490:                 $object->setData($attrCode, $oldAttrValue);
491:             }
492: 
493:             return (bool) $result;
494:         }
495:     }
496: 
497:     /**
498:      * Correct '==' and '!=' operators
499:      * Categories can't be equal because product is included categories selected by administrator and in their parents
500:      *
501:      * @return string
502:      */
503:     public function getOperatorForValidate()
504:     {
505:         $op = $this->getOperator();
506:         if ($this->getInputType() == 'category') {
507:             if ($op == '==') {
508:                 $op = '{}';
509:             } elseif ($op == '!=') {
510:                 $op = '!{}';
511:             }
512:         }
513: 
514:         return $op;
515:     }
516: }
517: 
Magento 1.7.0.2 API documentation generated by ApiGen 2.8.0