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: abstract class Mage_Rule_Model_Condition_Product_Abstract extends Mage_Rule_Model_Condition_Abstract
35: {
36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51:
52: protected $_entityAttributeValues = null;
53:
54: 55: 56: 57: 58:
59: protected $_isUsedForRuleProperty = 'is_used_for_promo_rules';
60:
61: 62: 63: 64: 65:
66: public function getDefaultOperatorInputByType()
67: {
68: if (null === $this->_defaultOperatorInputByType) {
69: parent::getDefaultOperatorInputByType();
70: 71: 72:
73: $this->_defaultOperatorInputByType['category'] = array('==', '!=', '{}', '!{}', '()', '!()');
74: $this->_arrayInputTypes[] = 'category';
75: }
76: return $this->_defaultOperatorInputByType;
77: }
78:
79: 80: 81: 82: 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: 100: 101: 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: 111: 112: 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:
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: 141: 142: 143: 144: 145: 146:
147: protected function _prepareValueOptions()
148: {
149:
150: $selectReady = $this->getData('value_select_options');
151: $hashedReady = $this->getData('value_option');
152: if ($selectReady && $hashedReady) {
153: return $this;
154: }
155:
156:
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:
178: if ($selectOptions !== null) {
179:
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;
188: }
189: $hashedOptions[$o['value']] = $o['label'];
190: }
191: $this->setData('value_option', $hashedOptions);
192: }
193: }
194:
195: return $this;
196: }
197:
198: 199: 200: 201: 202: 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: 212: 213: 214:
215: public function getValueSelectOptions()
216: {
217: $this->_prepareValueOptions();
218: return $this->getData('value_select_options');
219: }
220:
221: 222: 223: 224: 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: 244: 245: 246:
247: public function getAttributeElement()
248: {
249: $element = parent::getAttributeElement();
250: $element->setShowAsText(true);
251: return $element;
252: }
253:
254: 255: 256: 257: 258: 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: 279: 280: 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: 313: 314: 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: 342: 343: 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: 361: 362: 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: 381: 382: 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: 401: 402: 403: 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: 437: 438: 439: 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;
468:
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: 499: 500: 501: 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: