Skip to content

Commit 138e073

Browse files
committed
Merge pull request #49 from Smile-SA/release_1.3
Release 1.3
2 parents e0dd617 + 5bf10aa commit 138e073

File tree

20 files changed

+1010
-410
lines changed

20 files changed

+1010
-410
lines changed

src/app/code/community/Smile/ElasticSearch/Block/Catalogsearch/Autocomplete/Suggest/Category.php

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,33 +19,31 @@
1919
class Smile_ElasticSearch_Block_Catalogsearch_Autocomplete_Suggest_Category extends Mage_Core_Block_Template
2020
{
2121
/**
22-
* Block cache key
22+
* Constructor
23+
* Set cache policy for this block
2324
*
24-
* @return string
25+
* @return Smile_ElasticSearch_Block_Catalogsearch_Autocomplete_Suggest_Category
2526
*/
26-
public function getCacheKey()
27+
protected function _construct()
2728
{
28-
return __CLASS__ . md5($this->_getQuery()) . '_' . Mage::app()->getStore()->getId();
29-
}
29+
$this->addData(
30+
array(
31+
'cache_lifetime' => Mage_Core_Model_Cache::DEFAULT_LIFETIME,
32+
'cache_tags' => array(Mage_Catalog_Model_Category::CACHE_TAG),
33+
)
34+
);
3035

31-
/**
32-
* Block cache lifetime
33-
*
34-
* @return int
35-
*/
36-
public function getCacheLifetime()
37-
{
38-
return Mage_Core_Model_Cache::DEFAULT_LIFETIME;
36+
parent::_construct();
3937
}
4038

4139
/**
42-
* Block cache tags
40+
* Block cache key
4341
*
44-
* @return array
42+
* @return string
4543
*/
46-
public function getCacheTags()
44+
public function getCacheKey()
4745
{
48-
return array(Mage_Catalog_Model_Category::CACHE_TAG);
46+
return __CLASS__ . md5($this->_getQuery()) . '_' . Mage::app()->getStore()->getId();
4947
}
5048

5149
/**

src/app/code/community/Smile/ElasticSearch/Block/Catalogsearch/Autocomplete/Suggest/Product.php

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,33 +26,31 @@ class Smile_ElasticSearch_Block_Catalogsearch_Autocomplete_Suggest_Product exten
2626
const AUTOCOMPLETE_ATTRIBUTES_XPATH = 'global/smile_elasticsearch/autocomplete/product/attributes';
2727

2828
/**
29-
* Block cache key
29+
* Constructor
30+
* Set cache policy for this block
3031
*
31-
* @return string
32+
* @return Smile_ElasticSearch_Block_Catalogsearch_Autocomplete_Suggest_Product
3233
*/
33-
public function getCacheKey()
34+
protected function _construct()
3435
{
35-
return __CLASS__ . md5($this->_getQuery() . $this->getTemplate()) . '_' . Mage::app()->getStore()->getId();
36+
$this->addData(
37+
array(
38+
'cache_lifetime' => Mage_Core_Model_Cache::DEFAULT_LIFETIME,
39+
'cache_tags' => array(Mage_Catalog_Model_Product::CACHE_TAG),
40+
)
41+
);
42+
43+
parent::_construct();
3644
}
3745

3846
/**
39-
* Block cache lifetime
40-
*
41-
* @return int
42-
*/
43-
public function getCacheLifetime()
44-
{
45-
return Mage_Core_Model_Cache::DEFAULT_LIFETIME;
46-
}
47-
48-
/**
49-
* Block cache tags
47+
* Block cache key
5048
*
51-
* @return array
49+
* @return string
5250
*/
53-
public function getCacheTags()
51+
public function getCacheKey()
5452
{
55-
return array(Mage_Catalog_Model_Product::CACHE_TAG);
53+
return __CLASS__ . md5($this->_getQuery() . $this->getTemplate()) . '_' . Mage::app()->getStore()->getId();
5654
}
5755

5856
/**

src/app/code/community/Smile/ElasticSearch/Block/Catalogsearch/Autocomplete/Suggest/Terms.php

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,33 +26,31 @@ class Smile_ElasticSearch_Block_Catalogsearch_Autocomplete_Suggest_Terms extends
2626
protected $_suggestData = null;
2727

2828
/**
29-
* Block cache key
29+
* Constructor
30+
* Set cache policy for this block
3031
*
31-
* @return string
32+
* @return Smile_ElasticSearch_Block_Catalogsearch_Autocomplete_Suggest_Terms
3233
*/
33-
public function getCacheKey()
34+
protected function _construct()
3435
{
35-
return __CLASS__ . md5($this->_getQuery()) . '_' . Mage::app()->getStore()->getId();
36-
}
36+
$this->addData(
37+
array(
38+
'cache_lifetime' => Mage_Core_Model_Cache::DEFAULT_LIFETIME,
39+
'cache_tags' => array(Mage_CatalogSearch_Model_Query::CACHE_TAG),
40+
)
41+
);
3742

38-
/**
39-
* Block cache lifetime
40-
*
41-
* @return int
42-
*/
43-
public function getCacheLifetime()
44-
{
45-
return Mage_Core_Model_Cache::DEFAULT_LIFETIME;
43+
parent::_construct();
4644
}
4745

4846
/**
49-
* Block cache tags
47+
* Block cache key
5048
*
51-
* @return array
49+
* @return string
5250
*/
53-
public function getCacheTags()
51+
public function getCacheKey()
5452
{
55-
return array(Mage_CatalogSearch_Model_Query::CACHE_TAG);
53+
return __CLASS__ . md5($this->_getQuery()) . '_' . Mage::app()->getStore()->getId();
5654
}
5755

5856
/**

src/app/code/community/Smile/ElasticSearch/Model/Adminhtml/Catalog/Product/Attribute/Edit/Form/Search.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,10 @@ public function addSearchParams($attribute, $form)
144144
'value' => Smile_ElasticSearch_Model_Catalog_Layer_Filter_Attribute::SORT_ORDER_COUNT,
145145
'label' => Mage::helper('smile_elasticsearch')->__('Result count'),
146146
),
147+
array(
148+
'value' => Smile_ElasticSearch_Model_Catalog_Layer_Filter_Attribute::SORT_ORDER_ADMIN,
149+
'label' => Mage::helper('smile_elasticsearch')->__('Admin sort'),
150+
),
147151
array(
148152
'value' => Smile_ElasticSearch_Model_Catalog_Layer_Filter_Attribute::SORT_ORDER_TERM,
149153
'label' => Mage::helper('smile_elasticsearch')->__('Name'),

src/app/code/community/Smile/ElasticSearch/Model/Catalog/Layer/Filter/Attribute.php

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ class Smile_ElasticSearch_Model_Catalog_Layer_Filter_Attribute extends Mage_Cata
3434
*/
3535
const SORT_ORDER_TERM = 'term';
3636

37+
/**
38+
* @var string
39+
*/
40+
const SORT_ORDER_ADMIN = 'admin';
41+
3742
/**
3843
* @var string
3944
*/
@@ -65,14 +70,21 @@ public function addFacetCondition()
6570
$options = array(
6671
'size' => $this->_getFacetMaxSize(),
6772
);
68-
if ($this->_getFacetSortOrder() == self::SORT_ORDER_RELEVANCE) {
73+
74+
$facetSortOrder = $this->_getFacetSortOrder();
75+
76+
if (!in_array($facetSortOrder, array(self::SORT_ORDER_RELEVANCE, self::SORT_ORDER_COUNT, self::SORT_ORDER_TERM))) {
77+
$facetSortOrder = self::SORT_ORDER_COUNT;
78+
}
79+
80+
if ($facetSortOrder == self::SORT_ORDER_RELEVANCE) {
6981
$options['key_field'] = $this->_getFilterField();
7082
$options['value_script'] = self::SORT_ORDER_RELEVANCE;
7183
$options['order'] = self::TERM_STAT_AGGREGATOR;
7284
$facetType = "termsStats";
7385
} else {
7486
$options['field'] = $this->_getFilterField();
75-
$options['order'] = $this->_getFacetSortOrder();
87+
$options['order'] = $facetSortOrder;
7688
}
7789

7890
$options = $this->_addSuggestFacetFilter($options);
@@ -281,6 +293,7 @@ protected function _getItemsData()
281293
'count' => (int) $count,
282294
);
283295
}
296+
284297
if ($this->getSuggestConfig() == null) {
285298
foreach ($this->_rawFilter as $value) {
286299
if (!isset($data[$value])) {
@@ -289,13 +302,39 @@ protected function _getItemsData()
289302
}
290303
}
291304

305+
if ($this->_getFacetSortOrder() == self::SORT_ORDER_ADMIN && $attribute->getSource()) {
306+
$data = $this->_applyAdminSort($data);
307+
}
308+
292309
$data = array_values($data);
293310
}
294311

295312
return $data;
296313
}
297314

298315

316+
/**
317+
* Sort of the facet result according admin sort.
318+
*
319+
* @param array $data Data loaded with the search engine.
320+
*
321+
* @return array
322+
*/
323+
protected function _applyAdminSort($data)
324+
{
325+
$attribute = $this->getAttributeModel();
326+
$options = $attribute->getSource()->getAllOptions();
327+
$sortedData = array();
328+
329+
foreach ($options as $currentOption) {
330+
if (isset($data[$currentOption['label']])) {
331+
$sortedData[$currentOption['label']] = $data[$currentOption['label']];
332+
}
333+
}
334+
335+
return $sortedData;
336+
}
337+
299338
/**
300339
* Checks if given filter is valid before being applied to product collection.
301340
*

src/app/code/community/Smile/ElasticSearch/Model/Catalog/Layer/Filter/Boolean.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,39 @@ public function hasOthers()
2727
{
2828
return false;
2929
}
30+
31+
/**
32+
* Create filter item object and transform the option numeric value to boolean label
33+
*
34+
* @param string $label Label of the filter value
35+
* @param mixed $value Value of the filter
36+
* @param int $count Number of result (default is 0)
37+
*
38+
* @return Mage_Catalog_Model_Layer_Filter_Item
39+
*/
40+
protected function _createItem($label, $value, $count=0)
41+
{
42+
$attributeModel = $this->getAttributeModel();
43+
$source = $attributeModel->getSource();
44+
45+
if (is_numeric($label)) {
46+
$label = $source->getOptionText((int) $value);
47+
}
48+
49+
return parent::_createItem($label, $value, $count);
50+
}
51+
52+
/**
53+
* Returns attribute field name.
54+
* Booleans are not processed on options_ field
55+
*
56+
* @return string
57+
*/
58+
protected function _getFilterField()
59+
{
60+
$attribute = $this->getAttributeModel();
61+
$fieldName = $attribute->getAttributeCode();
62+
63+
return $fieldName;
64+
}
3065
}

0 commit comments

Comments
 (0)