/** * Retrieve SELECT WHERE condition for product collection * * @param Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection $collection * @param Enterprise_TargetRule_Model_Index $object * @param array $bind * @return Zend_Db_Expr */ public function getConditionForCollection($collection, $object, &$bind) { /* @var $resource Enterprise_TargetRule_Model_Mysql4_Index */ $resource = $object->getResource(); $operator = $this->getOperator(); $where = $resource->getOperatorBindCondition('price_index.min_price', 'final_price', $operator, $bind, array(array('bindPercentOf', $this->getValue()))); return new Zend_Db_Expr(sprintf('(%s)', $where)); }
/** * Retrieve SELECT WHERE condition for product collection * * @param Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection $collection * @param Enterprise_TargetRule_Model_Index $object * @param array $bind * @return Zend_Db_Expr */ public function getConditionForCollection($collection, $object, &$bind) { /* @var $resource Enterprise_TargetRule_Model_Mysql4_Index */ $attributeCode = $this->getAttribute(); $valueType = $this->getValueType(); $operator = $this->getOperator(); $resource = $object->getResource(); if ($attributeCode == 'category_ids') { $select = $object->select()->from($resource->getTable('catalog/category_product'), 'COUNT(*)')->where('product_id=e.entity_id'); if ($valueType == self::VALUE_TYPE_SAME_AS) { $operator = '!{}' == $operator ? '!()' : '()'; $where = $resource->getOperatorBindCondition('category_id', 'category_ids', $operator, $bind, array('bindArrayOfIds')); $select->where($where); } else { if ($valueType == self::VALUE_TYPE_CHILD_OF) { $concatenated = $resource->getReadConnection()->getConcatSql(array('tp.path', "'/%'")); $subSelect = $resource->select()->from(array('tc' => $resource->getTable('catalog/category')), 'entity_id')->join(array('tp' => $resource->getTable('catalog/category')), "tc.path " . ($operator == '!()' ? 'NOT ' : '') . "LIKE {$concatenated}", array())->where($resource->getOperatorBindCondition('tp.entity_id', 'category_ids', '()', $bind, array('bindArrayOfIds'))); $select->where('category_id IN(?)', $subSelect); } else { //self::VALUE_TYPE_CONSTANT $operator = $operator == '==' ? '' : 'NOT'; $value = $resource->bindArrayOfIds($this->getValue()); $where = "category_id {$operator} IN(" . implode(',', $value) . ")"; $select->where($where); } } return new Zend_Db_Expr(sprintf('(%s) > 0', $select->assemble())); } if ($valueType == self::VALUE_TYPE_CONSTANT) { $useBind = false; $value = $this->getValue(); // split value by commas into array for operators with multiple operands if (($operator == '()' || $operator == '!()') && is_string($value) && trim($value) != '') { $value = preg_split('/\\s*,\\s*/', trim($value), -1, PREG_SPLIT_NO_EMPTY); } } else { //self::VALUE_TYPE_SAME_AS $useBind = true; } $attribute = $this->getAttributeObject(); if (!$attribute) { return false; } if ($attribute->isStatic()) { $field = "e.{$attributeCode}"; if ($useBind) { $where = $resource->getOperatorBindCondition($field, $attributeCode, $operator, $bind); } else { $where = $resource->getOperatorCondition($field, $operator, $value); } $where = sprintf('(%s)', $where); } else { if ($attribute->isScopeGlobal()) { $table = $attribute->getBackendTable(); $select = $object->select()->from(array('table' => $table), 'COUNT(*)')->where('table.entity_id = e.entity_id')->where('table.attribute_id=?', $attribute->getId())->where('table.store_id=?', 0); if ($useBind) { $select->where($resource->getOperatorBindCondition('table.value', $attributeCode, $operator, $bind)); } else { $select->where($resource->getOperatorCondition('table.value', $operator, $value)); } $select = $resource->getReadConnection()->getIfNullSql($select); $where = sprintf('(%s) > 0', $select); } else { //scope store and website $valueExpr = $resource->getReadConnection()->getCheckSql('attr_s.value_id > 0', 'attr_s.value', 'attr_d.value'); $table = $attribute->getBackendTable(); $select = $object->select()->from(array('attr_d' => $table), 'COUNT(*)')->joinLeft(array('attr_s' => $table), $resource->getReadConnection()->quoteInto('attr_s.entity_id = attr_d.entity_id AND attr_s.attribute_id = attr_d.attribute_id' . ' AND attr_s.store_id=?', $object->getStoreId()), array())->where('attr_d.entity_id = e.entity_id')->where('attr_d.attribute_id=?', $attribute->getId())->where('attr_d.store_id=?', 0); if ($useBind) { $select->where($resource->getOperatorBindCondition($valueExpr, $attributeCode, $operator, $bind)); } else { $select->where($resource->getOperatorCondition($valueExpr, $operator, $value)); } $where = sprintf('(%s) > 0', $select); } } return new Zend_Db_Expr($where); }
/** * Save matched product Ids * * @param Enterprise_TargetRule_Model_Index $object * @param string $value * @return Enterprise_TargetRule_Model_Resource_Index_Abstract * @deprecated after 1.12.0.0 */ public function saveResult($object, $value) { $adapter = $this->_getWriteAdapter(); $data = array('entity_id' => $object->getProduct()->getEntityId(), 'store_id' => $object->getStoreId(), 'customer_group_id' => $object->getCustomerGroupId(), 'product_ids' => $value); $adapter->insertOnDuplicate($this->getMainTable(), $data, array('product_ids')); return $this; }
/** * Save index flag by index object data * * @param Enterprise_TargetRule_Model_Index $object * @return Enterprise_TargetRule_Model_Resource_Index */ public function saveFlag($object, $segmentId = null) { $data = array('type_id' => $object->getType(), 'entity_id' => $object->getProduct()->getEntityId(), 'store_id' => $object->getStoreId(), 'customer_group_id' => $object->getCustomerGroupId(), 'customer_segment_id' => $segmentId, 'flag' => 1); $this->_getWriteAdapter()->insertOnDuplicate($this->getMainTable(), $data); return $this; }