Skip to content

Commit 488e2e7

Browse files
committed
5558 Introduce new IndexCategoryRepository for use in product indexer
1 parent e97bc7c commit 488e2e7

File tree

4 files changed

+198
-2
lines changed

4 files changed

+198
-2
lines changed

src/app/code/community/IntegerNet/Solr/Helper/Factory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ protected function _getAttributeRepository()
194194
*/
195195
protected function _getIndexCategoryRepository()
196196
{
197-
return $this->_bridgeFactory->getCategoryRepository();
197+
return $this->_bridgeFactory->getIndexCategoryRepository();
198198
}
199199

200200
/**

src/app/code/community/IntegerNet/Solr/Model/Bridge/CategoryRepository.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
use IntegerNet\SolrCategories\Implementor\CategoryRepository;
1414
use IntegerNet\SolrCategories\Implementor\CategoryIterator;
1515

16-
class IntegerNet_Solr_Model_Bridge_CategoryRepository implements IndexCategoryRepository, SuggestCategoryRepository, CategoryRepository
16+
class IntegerNet_Solr_Model_Bridge_CategoryRepository implements SuggestCategoryRepository, CategoryRepository
1717
{
1818
protected $_pathCategoryIds = array();
1919
protected $_excludedCategoryIds = array();

src/app/code/community/IntegerNet/Solr/Model/Bridge/Factory.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,14 @@ public function createCategoryRepository()
6161
return Mage::getModel('integernet_solr/bridge_categoryRepository');
6262
}
6363

64+
/**
65+
* @return IntegerNet_Solr_Model_Bridge_IndexCategoryRepository
66+
*/
67+
public function getIndexCategoryRepository()
68+
{
69+
return Mage::getSingleton('integernet_solr/bridge_indexCategoryRepository');
70+
}
71+
6472
/**
6573
* @return IntegerNet_Solr_Model_Bridge_CategoryRepository
6674
*/
Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,188 @@
1+
<?php
2+
/**
3+
* integer_net Magento Module
4+
*
5+
* @category IntegerNet
6+
* @package IntegerNet_Solr
7+
* @copyright Copyright (c) 2015 integer_net GmbH (http://www.integer-net.de/)
8+
* @author Fabian Schmengler <fs@integer-net.de>
9+
*/
10+
use IntegerNet\Solr\Implementor\Product;
11+
use IntegerNet\Solr\Implementor\IndexCategoryRepository;
12+
use IntegerNet\SolrSuggest\Implementor\SuggestCategoryRepository;
13+
use IntegerNet\SolrCategories\Implementor\CategoryRepository;
14+
use IntegerNet\SolrCategories\Implementor\CategoryIterator;
15+
16+
class IntegerNet_Solr_Model_Bridge_IndexCategoryRepository implements IndexCategoryRepository
17+
{
18+
protected $_pathCategoryIds = array();
19+
protected $_excludedCategoryIds = array();
20+
21+
protected $_categoryNames = array();
22+
/**
23+
* @var IntegerNet_Solr_Model_Bridge_Factory
24+
*/
25+
protected $_bridgeFactory;
26+
27+
public function __construct()
28+
{
29+
$this->_bridgeFactory = Mage::getModel('integernet_solr/bridge_factory');
30+
}
31+
32+
/**
33+
* @var int
34+
*/
35+
protected $_pageSize;
36+
37+
/**
38+
* @param $categoryIds
39+
* @param $storeId
40+
* @return array
41+
*/
42+
public function getCategoryNames($categoryIds, $storeId)
43+
{
44+
$categoryNames = array();
45+
46+
/** @var Mage_Catalog_Model_Resource_Category $categoryResource */
47+
$categoryResource = Mage::getResourceModel('catalog/category');
48+
foreach($categoryIds as $key => $categoryId) {
49+
if (!isset($this->_categoryNames[$storeId][$categoryId])) {
50+
$this->_categoryNames[$storeId][$categoryId] = $categoryResource->getAttributeRawValue($categoryId, 'name', $storeId);
51+
}
52+
$categoryNames[] = $this->_categoryNames[$storeId][$categoryId];
53+
}
54+
return $categoryNames;
55+
}
56+
57+
/**
58+
* Get category ids of assigned categories and all parents
59+
*
60+
* @param Product $product
61+
* @return int[]
62+
*/
63+
public function getCategoryIds($product)
64+
{
65+
$categoryIds = $product->getCategoryIds();
66+
67+
if (!sizeof($categoryIds)) {
68+
return array();
69+
}
70+
71+
$storeId = $product->getStoreId();
72+
if (!isset($this->_pathCategoryIds[$storeId])) {
73+
$this->_pathCategoryIds[$storeId] = array();
74+
}
75+
$lookupCategoryIds = array_diff($categoryIds, array_keys($this->_pathCategoryIds[$storeId]));
76+
$this->_lookupCategoryIdPaths($lookupCategoryIds, $storeId);
77+
78+
$foundCategoryIds = array();
79+
foreach($categoryIds as $categoryId) {
80+
if (isset($this->_pathCategoryIds[$storeId][$categoryId])) {
81+
$categoryPathIds = $this->_pathCategoryIds[$storeId][$categoryId];
82+
$foundCategoryIds = array_merge($foundCategoryIds, $categoryPathIds);
83+
}
84+
}
85+
86+
$foundCategoryIds = array_unique($foundCategoryIds);
87+
88+
$foundCategoryIds = array_diff($foundCategoryIds, $this->_getExcludedCategoryIds($storeId));
89+
90+
return $foundCategoryIds;
91+
}
92+
93+
/**
94+
* Lookup and store all parent category ids and its own id of given category ids
95+
*
96+
* @param int[] $categoryIds
97+
* @param int $storeId
98+
*/
99+
protected function _lookupCategoryIdPaths($categoryIds, $storeId)
100+
{
101+
if (!sizeof($categoryIds)) {
102+
return;
103+
}
104+
105+
/** @var $categories Mage_Catalog_Model_Resource_Category_Collection */
106+
$categories = Mage::getResourceModel('catalog/category_collection')
107+
->addAttributeToFilter('entity_id', array('in' => $categoryIds))
108+
->addAttributeToSelect(array('is_active', 'include_in_menu'));
109+
110+
foreach ($categories as $category) {
111+
/** @var Mage_Catalog_Model_Category $category */
112+
if (!$category->getIsActive() || !$category->getIncludeInMenu()) {
113+
$this->_pathCategoryIds[$storeId][$category->getId()] = array();
114+
continue;
115+
}
116+
117+
$categoryPathIds = explode('/', $category->getPath());
118+
if (!in_array(Mage::app()->getStore($storeId)->getGroup()->getRootCategoryId(), $categoryPathIds)) {
119+
$this->_pathCategoryIds[$storeId][$category->getId()] = array();
120+
continue;
121+
}
122+
123+
array_shift($categoryPathIds);
124+
array_shift($categoryPathIds);
125+
$this->_pathCategoryIds[$storeId][$category->getId()] = $categoryPathIds;
126+
}
127+
}
128+
129+
130+
/**
131+
* @param int $storeId
132+
* @return array
133+
*/
134+
protected function _getExcludedCategoryIds($storeId)
135+
{
136+
if (!isset($this->_excludedCategoryIds[$storeId])) {
137+
138+
// exclude categories which are configured as excluded
139+
/** @var $excludedCategories Mage_Catalog_Model_Resource_Category_Collection */
140+
$excludedCategories = Mage::getResourceModel('catalog/category_collection')
141+
->addFieldToFilter('solr_exclude', 1);
142+
143+
$this->_excludedCategoryIds[$storeId] = $excludedCategories->getAllIds();
144+
145+
// exclude children of categories which are configured as "children excluded"
146+
/** @var $categoriesWithChildrenExcluded Mage_Catalog_Model_Resource_Category_Collection */
147+
$categoriesWithChildrenExcluded = Mage::getResourceModel('catalog/category_collection')
148+
->setStoreId($storeId)
149+
->addFieldToFilter('solr_exclude_children', 1);
150+
$excludePaths = $categoriesWithChildrenExcluded->getColumnValues('path');
151+
152+
/** @var $excludedChildrenCategories Mage_Catalog_Model_Resource_Category_Collection */
153+
$excludedChildrenCategories = Mage::getResourceModel('catalog/category_collection')
154+
->setStoreId($storeId);
155+
156+
$excludePathConditions = array();
157+
foreach($excludePaths as $excludePath) {
158+
$excludePathConditions[] = array('like' => $excludePath . '/%');
159+
}
160+
if (sizeof($excludePathConditions)) {
161+
$excludedChildrenCategories->addAttributeToFilter('path', $excludePathConditions);
162+
$this->_excludedCategoryIds[$storeId] = array_merge($this->_excludedCategoryIds[$storeId], $excludedChildrenCategories->getAllIds());
163+
}
164+
}
165+
166+
return $this->_excludedCategoryIds[$storeId];
167+
}
168+
169+
/**
170+
* Retrieve product category identifiers
171+
*
172+
* @param Product $product
173+
* @return array
174+
*/
175+
public function getCategoryPositions($product)
176+
{
177+
/** @var $setup Mage_Catalog_Model_Resource_Setup */
178+
$setup = Mage::getResourceModel('catalog/setup', 'catalog_setup');
179+
$adapter = Mage::getSingleton('core/resource')->getConnection('catalog_read');
180+
181+
$select = $adapter->select()
182+
->from($setup->getTable('catalog/category_product_index'), array('category_id', 'position'))
183+
->where('product_id = ?', (int)$product->getId())
184+
->where('store_id = ?', $product->getStoreId());
185+
186+
return $adapter->fetchAll($select);
187+
}
188+
}

0 commit comments

Comments
 (0)