forked from monarcmoso/staging
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Attribute.php
executable file
·121 lines (109 loc) · 5.23 KB
/
Attribute.php
1
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
<?php
/**
* @category Mana
* @package Mana_Filters
* @copyright Copyright (c) http://www.manadev.com
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/
/**
* Resource type which contains sql code for applying filters and related operations
* @author Mana Team
* Injected instead of standard resource catalog/layer_filter_attribute in
* Mana_Filters_Model_Filter_Attribute::_getResource().
*/
class Mana_Filters_Resource_Filter_Attribute
extends Mage_Catalog_Model_Resource_Eav_Mysql4_Layer_Filter_Attribute
{
/**
* @param Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection $collection
* @param Mana_Filters_Model_Filter_Attribute $model
* @return mixed
*/
public function countOnCollection($collection, $model) {
$select = $collection->getSelect();
// reset columns, order and limitation conditions
$select->reset(Zend_Db_Select::COLUMNS);
$select->reset(Zend_Db_Select::ORDER);
$select->reset(Zend_Db_Select::GROUP);
$select->reset(Zend_Db_Select::LIMIT_COUNT);
$select->reset(Zend_Db_Select::LIMIT_OFFSET);
$connection = $this->_getReadAdapter();
$attribute = $model->getAttributeModel();
$tableAlias = $attribute->getAttributeCode() . '_idx';
$conditions = array(
"{$tableAlias}.entity_id = e.entity_id",
$connection->quoteInto("{$tableAlias}.attribute_id = ?", $attribute->getAttributeId()),
$connection->quoteInto("{$tableAlias}.store_id = ?", $model->getStoreId()),
);
$select
->join(
array($tableAlias => $this->getMainTable()),
join(' AND ', $conditions),
array('value', 'count' => "COUNT(DISTINCT {$tableAlias}.entity_id)")
)
->group("{$tableAlias}.value");
return $connection->fetchPairs($select);
}
/**
* @param Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection $collection
* @param Mana_Filters_Model_Filter_Attribute $model
* @param int[] $attributeIds
* @return array
*/
public function optimizedCountOnCollection($collection, $model, $attributeIds) {
$select = $collection->getSelect();
// reset columns, order and limitation conditions
$select->reset(Zend_Db_Select::COLUMNS);
$select->reset(Zend_Db_Select::ORDER);
$select->reset(Zend_Db_Select::GROUP);
$select->reset(Zend_Db_Select::LIMIT_COUNT);
$select->reset(Zend_Db_Select::LIMIT_OFFSET);
$db = $this->_getReadAdapter();
$storeId = $model->getStoreId();
$attributeIds = implode(', ', $attributeIds);
$columns = array(
'attribute_id' => new Zend_Db_Expr("`_oc_o`.`attribute_id`"),
'label' => new Zend_Db_Expr("COALESCE(`_oc_ls`.`value`, `_oc_lg`.`value`)"),
'value' => new Zend_Db_Expr("`_oc_o`.`option_id`"),
'count' => new Zend_Db_Expr("COUNT(DISTINCT `_oc_idx`.`entity_id`)"),
);
$select
->from(array('_oc_o' => $this->getTable('eav/attribute_option')), null)
->joinLeft(array('_oc_lg' => $this->getTable('eav/attribute_option_value')),
"`_oc_lg`.`option_id` = `_oc_o`.`option_id` AND `_oc_lg`.`store_id` = 0", null)
->joinLeft(array('_oc_ls' => $this->getTable('eav/attribute_option_value')),
$db->quoteInto("`_oc_ls`.`option_id` = `_oc_o`.`option_id` AND `_oc_ls`.`store_id` = ?", $storeId), null)
->joinLeft(array('_oc_idx' => $this->getMainTable()),
"`_oc_idx`.`entity_id` = `e`.`entity_id` AND " .
"`_oc_idx`.`attribute_id` = `_oc_o`.`attribute_id` AND " .
"`_oc_idx`.`value` = `_oc_o`.`option_id` AND " .
$db->quoteInto("`_oc_idx`.`store_id` = ?", $storeId), null)
->where("`_oc_o`.`attribute_id` IN ($attributeIds)")
->columns($columns)
->group(array('attribute_id', 'label', 'value'));
$sql = $select->__toString();
return $db->fetchAll($select);
}
/**
* @param Mage_Catalog_Model_Resource_Eav_Mysql4_Product_Collection $collection
* @param Mana_Filters_Model_Filter_Attribute $model
* @param array $value
* @return Mana_Filters_Resource_Filter_Attribute
*/
public function applyToCollection($collection, $model, $value) {
$attribute = $model->getAttributeModel();
$connection = $this->_getReadAdapter();
$tableAlias = $attribute->getAttributeCode() . '_idx';
$conditions = array(
"{$tableAlias}.entity_id = e.entity_id",
$connection->quoteInto("{$tableAlias}.attribute_id = ?", $attribute->getAttributeId()),
$connection->quoteInto("{$tableAlias}.store_id = ?", $collection->getStoreId()),
"{$tableAlias}.value in (" . implode(',', array_filter($value)) . ")"
);
$conditions = join(' AND ', $conditions);
$collection->getSelect()
->distinct()
->join(array($tableAlias => $this->getMainTable()), $conditions, array());
return $this;
}
}