Пример #1
0
 /**
  * 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));
                 }
             }
         }
     }
 }
Пример #2
0
 /**
  * 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;
 }