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