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 class Mage_CatalogRule_Model_Rule_Condition_Product extends Mage_Rule_Model_Condition_Abstract
00029 {
00030
00031
00032
00033
00034
00035 public function getAttributeObject()
00036 {
00037 try {
00038 $obj = Mage::getSingleton('eav/config')
00039 ->getAttribute('catalog_product', $this->getAttribute());
00040 }
00041 catch (Exception $e) {
00042 $obj = new Varien_Object();
00043 $obj->setEntity(Mage::getResourceSingleton('catalog/product'))
00044 ->setFrontendInput('text');
00045 }
00046 return $obj;
00047 }
00048
00049
00050
00051
00052
00053
00054 protected function _addSpecialAttributes(array &$attributes)
00055 {
00056 $attributes['attribute_set_id'] = Mage::helper('catalogrule')->__('Attribute Set');
00057 $attributes['category_ids'] = Mage::helper('catalogrule')->__('Category');
00058 }
00059
00060
00061
00062
00063
00064
00065 public function loadAttributeOptions()
00066 {
00067 $productAttributes = Mage::getResourceSingleton('catalog/product')
00068 ->loadAllAttributes()
00069 ->getAttributesByCode();
00070
00071 $attributes = array();
00072 foreach ($productAttributes as $attribute) {
00073
00074 if (!$attribute->isAllowedForRuleCondition() || !$attribute->getIsUsedForPriceRules()) {
00075 continue;
00076 }
00077 $attributes[$attribute->getAttributeCode()] = $attribute->getFrontendLabel();
00078 }
00079
00080 $this->_addSpecialAttributes($attributes);
00081
00082 asort($attributes);
00083 $this->setAttributeOption($attributes);
00084
00085 return $this;
00086 }
00087
00088
00089
00090
00091
00092
00093
00094 public function getValueOption($option=null)
00095 {
00096 if (!$this->getData('value_option')) {
00097 if ($this->getAttribute()==='attribute_set_id') {
00098 $entityTypeId = Mage::getSingleton('eav/config')
00099 ->getEntityType('catalog_product')->getId();
00100 $options = Mage::getResourceModel('eav/entity_attribute_set_collection')
00101 ->setEntityTypeFilter($entityTypeId)
00102 ->load()
00103 ->toOptionHash();
00104 $this->setData('value_option', $options);
00105 } elseif (is_object($this->getAttributeObject()) && $this->getAttributeObject()->usesSource()) {
00106 if ($this->getAttributeObject()->getFrontendInput() == 'multiselect') {
00107 $addEmptyOption = false;
00108 } else {
00109 $addEmptyOption = true;
00110 }
00111
00112 $optionsArr = $this->getAttributeObject()->getSource()->getAllOptions($addEmptyOption);
00113 $options = array();
00114 foreach ($optionsArr as $o) {
00115 if (is_array($o['value'])) {
00116
00117 } else {
00118 $options[$o['value']] = $o['label'];
00119 }
00120 }
00121 $this->setData('value_option', $options);
00122 }
00123 }
00124 return $this->getData('value_option'.(!is_null($option) ? '/'.$option : ''));
00125 }
00126
00127
00128
00129
00130
00131
00132 public function getValueSelectOptions()
00133 {
00134 if (!$this->getData('value_select_options')) {
00135 if ($this->getAttribute()==='attribute_set_id') {
00136 $entityTypeId = Mage::getSingleton('eav/config')
00137 ->getEntityType('catalog_product')->getId();
00138 $options = Mage::getResourceModel('eav/entity_attribute_set_collection')
00139 ->setEntityTypeFilter($entityTypeId)
00140 ->load()->toOptionArray();
00141 $this->setData('value_select_options', $options);
00142 } elseif (is_object($this->getAttributeObject()) && $this->getAttributeObject()->usesSource()) {
00143 if ($this->getAttributeObject()->getFrontendInput() == 'multiselect') {
00144 $addEmptyOption = false;
00145 } else {
00146 $addEmptyOption = true;
00147 }
00148 $optionsArr = $this->getAttributeObject()->getSource()->getAllOptions($addEmptyOption);
00149 $this->setData('value_select_options', $optionsArr);
00150 }
00151 }
00152 return $this->getData('value_select_options');
00153 }
00154
00155
00156
00157
00158
00159
00160 public function getValueAfterElementHtml()
00161 {
00162 $html = '';
00163
00164 switch ($this->getAttribute()) {
00165 case 'sku': case 'category_ids':
00166 $image = Mage::getDesign()->getSkinUrl('images/rule_chooser_trigger.gif');
00167 break;
00168 }
00169
00170 if (!empty($image)) {
00171 $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>';
00172 }
00173 return $html;
00174 }
00175
00176
00177
00178
00179
00180
00181 public function getAttributeElement()
00182 {
00183 $element = parent::getAttributeElement();
00184 $element->setShowAsText(true);
00185 return $element;
00186 }
00187
00188
00189
00190
00191
00192
00193
00194 public function collectValidatedAttributes($productCollection)
00195 {
00196 $attributes = $this->getRule()->getCollectedAttributes();
00197 $attributes[$this->getAttribute()] = true;
00198 $this->getRule()->setCollectedAttributes($attributes);
00199 $productCollection->addAttributeToSelect($this->getAttribute(), 'left');
00200 return $this;
00201 }
00202
00203
00204
00205
00206
00207
00208 public function getInputType()
00209 {
00210 if ($this->getAttribute()==='attribute_set_id') {
00211 return 'select';
00212 }
00213 if (!is_object($this->getAttributeObject())) {
00214 return 'string';
00215 }
00216 switch ($this->getAttributeObject()->getFrontendInput()) {
00217 case 'select':
00218 return 'select';
00219
00220 case 'multiselect':
00221 return 'multiselect';
00222
00223 case 'date':
00224 return 'date';
00225
00226 default:
00227 return 'string';
00228 }
00229 }
00230
00231
00232
00233
00234
00235
00236 public function getValueElementType()
00237 {
00238 if ($this->getAttribute()==='attribute_set_id') {
00239 return 'select';
00240 }
00241 if (!is_object($this->getAttributeObject())) {
00242 return 'text';
00243 }
00244 switch ($this->getAttributeObject()->getFrontendInput()) {
00245 case 'select':
00246 return 'select';
00247
00248 case 'multiselect':
00249 return 'multiselect';
00250
00251 case 'date':
00252 return 'date';
00253
00254 default:
00255 return 'text';
00256 }
00257 }
00258
00259
00260
00261
00262
00263
00264 public function getValueElement()
00265 {
00266 $element = parent::getValueElement();
00267 if (is_object($this->getAttributeObject())) {
00268 switch ($this->getAttributeObject()->getFrontendInput()) {
00269 case 'date':
00270 $element->setImage(Mage::getDesign()->getSkinUrl('images/grid-cal.gif'));
00271 break;
00272 }
00273 }
00274
00275 return $element;
00276 }
00277
00278
00279
00280
00281
00282
00283 public function getValueElementChooserUrl()
00284 {
00285 $url = false;
00286 switch ($this->getAttribute()) {
00287 case 'sku': case 'category_ids':
00288 $url = 'adminhtml/promo_widget/chooser'
00289 .'/attribute/'.$this->getAttribute();
00290 if ($this->getJsFormObject()) {
00291 $url .= '/form/'.$this->getJsFormObject();
00292 }
00293 break;
00294 }
00295 return $url!==false ? Mage::helper('adminhtml')->getUrl($url) : '';
00296 }
00297
00298
00299
00300
00301
00302
00303 public function getExplicitApply()
00304 {
00305 switch ($this->getAttribute()) {
00306 case 'sku': case 'category_ids':
00307 return true;
00308 }
00309 if (is_object($this->getAttributeObject())) {
00310 switch ($this->getAttributeObject()->getFrontendInput()) {
00311 case 'date':
00312 return true;
00313 }
00314 }
00315 return false;
00316 }
00317
00318
00319
00320
00321
00322
00323
00324 public function loadArray($arr)
00325 {
00326 $this->setAttribute(isset($arr['attribute']) ? $arr['attribute'] : false);
00327 $attribute = $this->getAttributeObject();
00328
00329 if ($attribute && $attribute->getBackendType() == 'decimal') {
00330 $arr['value'] = isset($arr['value']) ? Mage::app()->getLocale()->getNumber($arr['value']) : false;
00331 $arr['is_value_parsed'] = isset($arr['is_value_parsed']) ? Mage::app()->getLocale()->getNumber($arr['is_value_parsed']) : false;
00332 }
00333
00334 return parent::loadArray($arr);
00335 }
00336
00337
00338
00339
00340
00341
00342
00343 public function validate(Varien_Object $object)
00344 {
00345 $attr = $object->getResource()->getAttribute($this->getAttribute());
00346 if ($attr && $attr->getBackendType()=='datetime' && !is_int($this->getValue())) {
00347 $this->setValue(strtotime($this->getValue()));
00348 $value = strtotime($object->getData($this->getAttribute()));
00349 return $this->validateAttribute($value);
00350 }
00351
00352 if ($this->getAttribute() == 'category_ids') {
00353 return $this->validateAttribute($object->getAvailableInCategories());
00354 }
00355
00356 if ($attr && $attr->getFrontendInput() == 'multiselect') {
00357 $value = $object->getData($this->getAttribute());
00358 if (!strlen($value)) {
00359 $value = array();
00360 } else {
00361 $value = split(',', $value);
00362 }
00363 return $this->validateAttribute($value);
00364 }
00365
00366 return parent::validate($object);
00367 }
00368 }