/** * Render required attributes * @param Varien_Data_Form_Element_Fieldset $fldAttr * @param Amasty_Banners_Model_Rule $model */ protected function prepareAttributes($fldAttr, $model) { $hlp = Mage::helper('ambanners'); /* * Add Empty Fields user for new conditions */ $fieldSet = $this->getForm()->addFieldset('attributestmp', array('legend' => $hlp->__('Attribute Tmp'))); $fieldSet->addField('attr_code[][]', 'select', array('label' => $hlp->__('Has attribute'), 'name' => 'attr_code[]', 'values' => $this->getAttributes(), 'onchange' => 'showOptions(this)')); $fieldSet->addField('attr_value[][]', 'text', array('label' => $hlp->__('Attribute value is'), 'name' => 'attr_value[]')); $array = $model->getAttributesAsArray(); foreach ($array as $attributeCode => $attributeValue) { if (empty($attributeCode)) { continue; } if (is_array($attributeValue)) { foreach ($attributeValue as $i => $value) { /* * Add Attribute Names */ $elementCode = $attributeCode . '-' . $value . '-' . $i; $fldAttr->addField('attr_code[' . $elementCode . ']', 'select', array('label' => $hlp->__('Has attribute'), 'name' => 'attr_code[' . $elementCode . ']', 'values' => $this->getAttributes(), 'onchange' => 'showOptions(this)', 'value' => $attributeCode, 'note' => $hlp->__('If attribute is related to configurable products, please make sure that attribute is used in layered navigation'), 'after_element_html' => '<a href="#" onclick="landingRemove(this);return false;" title="' . $hlp->__('Remove') . '">' . $hlp->__('X') . '</a>')); /* * Add Attribute Options */ $attribute = Mage::getModel('catalog/product')->getResource()->getAttribute($attributeCode); if ('select' === $attribute->getFrontendInput() || 'multiselect' === $attribute->getFrontendInput()) { $options = $attribute->getFrontend()->getSelectOptions(); $fldAttr->addField('attr_value[' . $elementCode . ']', 'select', array('label' => $hlp->__('Attribute value is'), 'name' => 'attr_value[' . $elementCode . ']', 'values' => $options, 'value' => $value)); } else { $fldAttr->addField('attr_value[' . $elementCode . ']', 'text', array('label' => $hlp->__('Attribute value is'), 'name' => 'attr_value[' . $elementCode . ']', 'value' => $value)); } } } } }
/** * Perform internal banner validation * @param Amasty_Banners_Model_Rule $banner * @return boolean */ public function bannerValidate($banner) { $pos = array(Amasty_Banners_Model_Rule::POS_SIDEBAR_LEFT, Amasty_Banners_Model_Rule::POS_SIDEBAR_RIGHT, Amasty_Banners_Model_Rule::POS_PROD_PAGE); if ($product = Mage::registry('current_product')) { if (in_array($this->getCurrentPosition(), $pos)) { $sku = $product->getSku(); /* * Filter By SKU */ $allowed = $banner->getShowOnProducts(); if (!empty($allowed)) { $skus = explode(",", trim($allowed)); if (!in_array($sku, $skus)) { return false; } } /* * Filter By Category */ $cats = $banner->getCats(); if (!empty($cats)) { $cats = explode(",", trim($cats)); //$catid = $product->getCategory()->getId(); $categoryIds = $product->getCategoryIds(); $hasCategory = false; $hasValue = false; if (is_array($categoryIds) and count($categoryIds) > 1) { foreach ($categoryIds as $catid) { foreach ($cats as $id) { if ($id != '') { $hasValue = true || $hasValue; if ($id == $catid) { $hasCategory = true || $hasCategory; } } } } } if ($hasValue && !$hasCategory) { return false; } } /* * Filter By Product Attributes */ $productOptions = $banner->getAttributesAsArray(); if (count($productOptions) > 0) { /* * Use AND or OR logic */ $logic = 'AND'; $matches = 0; $total = 0; foreach ($productOptions as $attributeCode => $options) { if ($product->getData($attributeCode)) { $attrCodes = explode(",", $product->getData($attributeCode)); if (array_intersect($attrCodes, $options)) { $matches++; } $total++; } } if ($logic == 'AND') { return $total == $matches && $total != 0; } if ($logic == 'OR') { return $matches > 0; } } } } else { if (count($banner->getAttributesAsArray()) > 0) { return false; } } return true; }
/** * Prepare model * @param Amasty_Banners_Model_Rule $model * @return boolean */ public function prepareForSave($model) { $fields = array('stores', 'cust_groups', 'cats'); foreach ($fields as $f) { // convert data from array to string $val = $model->getData($f); $model->setData($f, ''); if (is_array($val)) { // need commas to simplify sql query $modelValue = ''; foreach ($val as $value) { if ($value != '') { $modelValue .= ',' . $value; } } if ($modelValue != '') { $modelValue .= ',' . $value . ','; } $model->setData($f, $modelValue); } } $ids = $model->getSelectedProducts(); if (!is_null($ids)) { $ids = Mage::helper('adminhtml/js')->decodeGridSerializedInput($ids); $model->assignProducts($ids); } $attributeCodes = $model->getData('attr_code'); $attributeValues = $model->getData('attr_value'); $validArray = array(); foreach ($attributeValues as $index => $value) { if (isset($attributeCodes[$index]) && $attributeCodes[$index] != '') { if (!isset($validArray[$attributeCodes[$index]])) { $validArray[$attributeCodes[$index]] = array(); } if ($value != '') { $validArray[$attributeCodes[$index]][] = $value; } } } $model->setData('attributes', serialize($validArray)); return true; }