|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Sitewards_B2BProfessional_Helper_Category |
|
5
|
|
|
* - Helper containing the checks for |
|
6
|
|
|
* - if extension is activated by category |
|
7
|
|
|
* - checking if a category is active from a given product |
|
8
|
|
|
* |
|
9
|
|
|
* @category Sitewards |
|
10
|
|
|
* @package Sitewards_B2BProfessional |
|
11
|
|
|
* @copyright Copyright (c) 2014 Sitewards GmbH (http://www.sitewards.com/) |
|
12
|
|
|
*/ |
|
13
|
|
|
class Sitewards_B2BProfessional_Helper_Category extends Sitewards_B2BProfessional_Helper_Core |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* Path for the config for extension active by category |
|
17
|
|
|
*/ |
|
18
|
|
|
const CONFIG_EXTENSION_ACTIVE_BY_CATEGORY = 'b2bprofessional/activatebycategorysettings/activebycategory'; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Path for the config for activated categories |
|
22
|
|
|
*/ |
|
23
|
|
|
const CONFIG_EXTENSION_ACTIVATED_CATEGORIES = 'b2bprofessional/activatebycategorysettings/activecategories'; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Variable for if the extension is active by category |
|
27
|
|
|
* |
|
28
|
|
|
* @var bool |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $bExtensionActiveByCategory; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Check to see if the website is set-up to require a user login to view pages |
|
34
|
|
|
* |
|
35
|
|
|
* @return bool |
|
36
|
|
|
*/ |
|
37
|
|
|
public function isExtensionActivatedByCategory() |
|
38
|
|
|
{ |
|
39
|
|
|
return $this->getStoreFlag(self::CONFIG_EXTENSION_ACTIVE_BY_CATEGORY, 'bExtensionActiveByCategory'); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* For a product get all the parent and children product ids when they are set |
|
44
|
|
|
* |
|
45
|
|
|
* @param Mage_Catalog_Model_Product $oProduct |
|
46
|
|
|
* @return int[] |
|
47
|
|
|
*/ |
|
48
|
|
|
protected function getChildrenAndParentIds(Mage_Catalog_Model_Product $oProduct) |
|
49
|
|
|
{ |
|
50
|
|
|
$oProductType = $oProduct->getTypeInstance(); |
|
51
|
|
|
$aLinkedProductIds = $oProductType->getParentIdsByChild($oProduct->getId()); |
|
52
|
|
|
$aChildProductIdsByGroups = $oProductType->getChildrenIds($oProduct->getId()); |
|
53
|
|
|
foreach ($aChildProductIdsByGroups as $aChildProductIds) { |
|
54
|
|
|
$aLinkedProductIds = array_unique(array_merge($aLinkedProductIds, $aChildProductIds)); |
|
55
|
|
|
} |
|
56
|
|
|
return $aLinkedProductIds; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Validate that the category of a give product is activated in the module |
|
61
|
|
|
* |
|
62
|
|
|
* @param Mage_Catalog_Model_Product $oProduct |
|
63
|
|
|
* @return bool |
|
64
|
|
|
*/ |
|
65
|
|
|
public function isCategoryActiveByProduct(Mage_Catalog_Model_Product $oProduct) |
|
66
|
|
|
{ |
|
67
|
|
|
$aCurrentCategories = $oProduct->getCategoryIds(); |
|
68
|
|
|
|
|
69
|
|
|
$aLinkedProductIds = array(); |
|
70
|
|
|
if ($oProduct->isSuper()) { |
|
71
|
|
|
$aLinkedProductIds = $this->getChildrenAndParentIds($oProduct); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
if (!empty($aLinkedProductIds)) { |
|
75
|
|
|
$aCurrentCategories = $this->getAllCategoryIds($aLinkedProductIds, $aCurrentCategories); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
if (!is_array($aCurrentCategories)) { |
|
79
|
|
|
$aCurrentCategories = array( |
|
80
|
|
|
$aCurrentCategories |
|
81
|
|
|
); |
|
82
|
|
|
} |
|
83
|
|
|
return $this->hasActiveCategory($aCurrentCategories); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Check that at least one of the given category ids is active |
|
88
|
|
|
* |
|
89
|
|
|
* @param int[] $aCategoryIds |
|
90
|
|
|
* @return bool |
|
91
|
|
|
*/ |
|
92
|
|
|
public function hasActiveCategory($aCategoryIds) |
|
93
|
|
|
{ |
|
94
|
|
|
$aActiveCategoryIds = $this->getActiveCategories(); |
|
95
|
|
|
foreach ($aCategoryIds as $iCategoryId) { |
|
96
|
|
|
if (in_array($iCategoryId, $aActiveCategoryIds)) { |
|
97
|
|
|
return true; |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
return false; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Get all category ids activated via the system config |
|
105
|
|
|
* - Include the children category ids |
|
106
|
|
|
* |
|
107
|
|
|
* @return int[] |
|
108
|
|
|
*/ |
|
109
|
|
|
protected function getActiveCategories() |
|
110
|
|
|
{ |
|
111
|
|
|
$aCurrentActiveCategories = $this->getExtensionActivatedCategoryIds(); |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* Loop through each activated category ids and add children category ids |
|
115
|
|
|
*/ |
|
116
|
|
|
$aSubActiveCategories = $this->addCategoryChildren($aCurrentActiveCategories); |
|
117
|
|
|
return array_unique(array_merge($aCurrentActiveCategories, $aSubActiveCategories)); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* Get an array of category ids activated via the admin config section |
|
122
|
|
|
* |
|
123
|
|
|
* @return int[] |
|
124
|
|
|
*/ |
|
125
|
|
|
protected function getExtensionActivatedCategoryIds() |
|
126
|
|
|
{ |
|
127
|
|
|
/* |
|
128
|
|
|
* Category Ids are saved in the config in format |
|
129
|
|
|
* - "category1,category2" |
|
130
|
|
|
*/ |
|
131
|
|
|
$sActivatedCategoryIds = Mage::getStoreConfig(self::CONFIG_EXTENSION_ACTIVATED_CATEGORIES); |
|
132
|
|
|
return explode(',', $sActivatedCategoryIds); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* From given category id load all child ids into an array |
|
137
|
|
|
* |
|
138
|
|
|
* @param int[] $aCategoryIds |
|
139
|
|
|
* @return int[] |
|
140
|
|
|
*/ |
|
141
|
|
|
protected function addCategoryChildren($aCategoryIds) |
|
142
|
|
|
{ |
|
143
|
|
|
$oCategoryResource = Mage::getResourceModel('catalog/category'); |
|
144
|
|
|
$oAdapter = $oCategoryResource->getReadConnection(); |
|
145
|
|
|
|
|
146
|
|
|
$oSelect = $oAdapter->select(); |
|
147
|
|
|
$oSelect->from(array('m' => $oCategoryResource->getEntityTable()), 'entity_id'); |
|
148
|
|
|
|
|
149
|
|
|
foreach ($aCategoryIds as $iCategoryId) { |
|
150
|
|
|
$oSelect->orWhere($oAdapter->quoteIdentifier('path') . ' LIKE ?', '%/' . $iCategoryId . '/%'); |
|
151
|
|
|
} |
|
152
|
|
|
return $oAdapter->fetchCol($oSelect); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* From an array of all product ids get all unique entries in the product category table |
|
157
|
|
|
* |
|
158
|
|
|
* @param int[] $aProductIds |
|
159
|
|
|
* @param int[] $aCurrentCategories |
|
160
|
|
|
* @return int[] |
|
161
|
|
|
*/ |
|
162
|
|
|
protected function getAllCategoryIds($aProductIds, $aCurrentCategories) |
|
163
|
|
|
{ |
|
164
|
|
|
$oProductResource = Mage::getResourceModel('catalog/product'); |
|
165
|
|
|
$oAdapter = $oProductResource->getReadConnection(); |
|
166
|
|
|
|
|
167
|
|
|
$oSelect = $oAdapter->select(); |
|
168
|
|
|
$oSelect->from($oProductResource->getTable('catalog/category_product'), 'category_id'); |
|
169
|
|
|
$oSelect->where('product_id IN (?)', $aProductIds); |
|
170
|
|
|
|
|
171
|
|
|
return array_unique(array_merge($aCurrentCategories, $oAdapter->fetchCol($oSelect))); |
|
172
|
|
|
} |
|
173
|
|
|
} |