1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Sitewards_B2BProfessional_Helper_Data |
5
|
|
|
* - Helper containing the checks for |
6
|
|
|
* - extension is active, |
7
|
|
|
* - product is active, |
8
|
|
|
* - is the category active, |
9
|
|
|
* |
10
|
|
|
* @category Sitewards |
11
|
|
|
* @package Sitewards_B2BProfessional |
12
|
|
|
* @copyright Copyright (c) 2014 Sitewards GmbH (http://www.sitewards.com/) |
13
|
|
|
*/ |
14
|
|
|
class Sitewards_B2BProfessional_Helper_Data extends Sitewards_B2BProfessional_Helper_Core |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* Path for the config for extension active status |
18
|
|
|
*/ |
19
|
|
|
const CONFIG_EXTENSION_ACTIVE = 'b2bprofessional/generalsettings/active'; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Path for the config for price block class names |
23
|
|
|
*/ |
24
|
|
|
const CONFIG_EXTENSION_PRICE_BLOCKS = 'b2bprofessional/generalsettings/priceblocks'; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Path for the config for login message |
28
|
|
|
*/ |
29
|
|
|
const CONFIG_EXTENSION_LOGIN_MESSAGE = 'b2bprofessional/generalsettings/login_message'; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Variable for if the extension is active |
33
|
|
|
* |
34
|
|
|
* @var bool |
35
|
|
|
*/ |
36
|
|
|
protected $bExtensionActive; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Variable for the login message |
40
|
|
|
* |
41
|
|
|
* @var string |
42
|
|
|
*/ |
43
|
|
|
protected $sLoginMessage; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Variable for if the extension is active by category |
47
|
|
|
* |
48
|
|
|
* @var bool |
49
|
|
|
*/ |
50
|
|
|
protected $bExtensionActiveByCategory; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Variable for if the extension is active by customer group |
54
|
|
|
* |
55
|
|
|
* @var bool |
56
|
|
|
*/ |
57
|
|
|
protected $bExtensionActiveByCustomerGroup; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Variable for the extension's price blocks |
61
|
|
|
* |
62
|
|
|
* @var string[] |
63
|
|
|
*/ |
64
|
|
|
protected $aPriceBlockClassNames; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Variable for the add-to-cart blocks' layout names |
68
|
|
|
* |
69
|
|
|
* @var string[] |
70
|
|
|
*/ |
71
|
|
|
protected $aAddToCartBlockLayoutNames = array( |
72
|
|
|
'product.info.addtocart' |
73
|
|
|
); |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Check to see if the extension is active |
77
|
|
|
* |
78
|
|
|
* @return bool |
79
|
|
|
*/ |
80
|
|
|
public function isExtensionActive() |
81
|
|
|
{ |
82
|
|
|
return $this->getStoreFlag(self::CONFIG_EXTENSION_ACTIVE, 'bExtensionActive'); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Return the login message to be displayed instead of the price block |
87
|
|
|
* |
88
|
|
|
* @return string |
89
|
|
|
*/ |
90
|
|
|
public function getLoginMessage() |
91
|
|
|
{ |
92
|
|
|
return $this->getStoreConfig(self::CONFIG_EXTENSION_LOGIN_MESSAGE, 'sLoginMessage'); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Check to see if the extension is active by category |
97
|
|
|
* |
98
|
|
|
* @return bool |
99
|
|
|
*/ |
100
|
|
|
protected function isExtensionActiveByCategory() |
101
|
|
|
{ |
102
|
|
|
if ($this->bExtensionActiveByCategory === null) { |
103
|
|
|
$this->bExtensionActiveByCategory = Mage::helper( |
104
|
|
|
'sitewards_b2bprofessional/category' |
105
|
|
|
)->isExtensionActivatedByCategory(); |
106
|
|
|
} |
107
|
|
|
return $this->bExtensionActiveByCategory; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Check to see if the extension is active by user group |
112
|
|
|
* |
113
|
|
|
* @return bool |
114
|
|
|
*/ |
115
|
|
|
protected function isExtensionActivatedByCustomerGroup() |
116
|
|
|
{ |
117
|
|
|
if ($this->bExtensionActiveByCustomerGroup === null) { |
118
|
|
|
$this->bExtensionActiveByCustomerGroup = Mage::helper( |
119
|
|
|
'sitewards_b2bprofessional/customer' |
120
|
|
|
)->isExtensionActivatedByCustomerGroup(); |
121
|
|
|
} |
122
|
|
|
return $this->bExtensionActiveByCustomerGroup; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Check to see if the block is a price block |
127
|
|
|
* |
128
|
|
|
* @param Mage_Core_Block_Template $oBlock |
129
|
|
|
* @return bool |
130
|
|
|
*/ |
131
|
|
|
public function isBlockPriceBlock($oBlock) |
132
|
|
|
{ |
133
|
|
|
$aPriceBlockClassNames = $this->getPriceBlocks(); |
134
|
|
|
return in_array(get_class($oBlock), $aPriceBlockClassNames); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Check to see if the block is an add-to-cart block |
139
|
|
|
* |
140
|
|
|
* @param Mage_Core_Block_Template $oBlock |
141
|
|
|
* @return bool |
142
|
|
|
*/ |
143
|
|
|
public function isBlockAddToCartBlock($oBlock) |
144
|
|
|
{ |
145
|
|
|
$aAddToCartBlockClassNames = $this->getAddToCartBlockLayoutNames(); |
146
|
|
|
return in_array($oBlock->getNameInLayout(), $aAddToCartBlockClassNames); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Check to see if it's the add-to-cart block should be hidden |
151
|
|
|
* |
152
|
|
|
* @param Mage_Core_Block_Template $oBlock |
153
|
|
|
* @return bool |
154
|
|
|
*/ |
155
|
|
|
public function isAddToCartBlockAndHidden($oBlock) |
156
|
|
|
{ |
157
|
|
|
if ($this->isBlockAddToCartBlock($oBlock)) { |
158
|
|
|
$oProduct = $oBlock->getProduct(); |
159
|
|
|
if ($this->isProductActive($oProduct) === false) { |
160
|
|
|
return true; |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
return false; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Check to see if the given product is active |
168
|
|
|
* - In this case active means product behaves as normal in a magento shop |
169
|
|
|
* |
170
|
|
|
* @param Mage_Catalog_Model_Product $oProduct |
171
|
|
|
* @return bool |
172
|
|
|
*/ |
173
|
|
View Code Duplication |
public function isProductActive(Mage_Catalog_Model_Product $oProduct) |
|
|
|
|
174
|
|
|
{ |
175
|
|
|
$bIsProductActive = true; |
176
|
|
|
if ($this->isExtensionActive() === true) { |
177
|
|
|
$bCheckCategory = $this->isExtensionActiveByCategory(); |
178
|
|
|
$bCheckUser = $this->isExtensionActivatedByCustomerGroup(); |
179
|
|
|
$bIsCustomerLoggedIn = $this->isCustomerLoggedIn(); |
180
|
|
|
|
181
|
|
|
/** @var Sitewards_B2BProfessional_Helper_Category $oCategoryHelper */ |
182
|
|
|
$oCategoryHelper = Mage::helper('sitewards_b2bprofessional/category'); |
183
|
|
|
/** @var Sitewards_B2BProfessional_Helper_Customer $oCustomerHelper */ |
184
|
|
|
$oCustomerHelper = Mage::helper('sitewards_b2bprofessional/customer'); |
185
|
|
|
|
186
|
|
|
$bIsCategoryEnabled = $oCategoryHelper->isCategoryActiveByProduct($oProduct); |
187
|
|
|
$bIsCustomerGroupEnabled = $oCustomerHelper->isCustomerGroupActive(); |
188
|
|
|
|
189
|
|
|
if ($bCheckCategory && $bCheckUser) { |
190
|
|
|
$bIsProductActive = !($bIsCategoryEnabled && $bIsCustomerGroupEnabled); |
191
|
|
|
} elseif ($bCheckUser) { |
192
|
|
|
$bIsProductActive = !$bIsCustomerGroupEnabled; |
193
|
|
|
} elseif ($bCheckCategory) { |
194
|
|
|
if ($bIsCustomerLoggedIn) { |
195
|
|
|
$bIsProductActive = true; |
196
|
|
|
} else { |
197
|
|
|
$bIsProductActive = !$bIsCategoryEnabled; |
198
|
|
|
} |
199
|
|
|
} else { |
200
|
|
|
$bIsProductActive = $bIsCustomerLoggedIn; |
201
|
|
|
} |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
return $bIsProductActive; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* From an array of category ids check to see if any are enabled via the extension to hide prices |
209
|
|
|
* |
210
|
|
|
* @param int[] $aCategoryIds |
211
|
|
|
* @return bool |
212
|
|
|
*/ |
213
|
|
View Code Duplication |
public function hasEnabledCategories($aCategoryIds) |
|
|
|
|
214
|
|
|
{ |
215
|
|
|
$bHasCategories = false; |
216
|
|
|
if ($this->isExtensionActive() === true) { |
217
|
|
|
$bCheckCategory = $this->isExtensionActiveByCategory(); |
218
|
|
|
$bCheckUser = $this->isExtensionActivatedByCustomerGroup(); |
219
|
|
|
$bIsCustomerLoggedIn = $this->isCustomerLoggedIn(); |
220
|
|
|
|
221
|
|
|
/** @var Sitewards_B2BProfessional_Helper_Category $oCategoryHelper */ |
222
|
|
|
$oCategoryHelper = Mage::helper('sitewards_b2bprofessional/category'); |
223
|
|
|
/** @var Sitewards_B2BProfessional_Helper_Customer $oCustomerHelper */ |
224
|
|
|
$oCustomerHelper = Mage::helper('sitewards_b2bprofessional/customer'); |
225
|
|
|
|
226
|
|
|
$bHasActiveCategories = $oCategoryHelper->hasActiveCategory($aCategoryIds); |
227
|
|
|
$bIsUserGroupActive = $oCustomerHelper->isCustomerGroupActive(); |
228
|
|
|
|
229
|
|
|
if ($bCheckCategory && $bCheckUser) { |
230
|
|
|
$bHasCategories = $bHasActiveCategories && $bIsUserGroupActive; |
231
|
|
|
} elseif ($bCheckUser) { |
232
|
|
|
$bHasCategories = $bIsUserGroupActive; |
233
|
|
|
} elseif ($bCheckCategory) { |
234
|
|
|
if ($bIsCustomerLoggedIn) { |
235
|
|
|
$bHasCategories = false; |
236
|
|
|
} else { |
237
|
|
|
$bHasCategories = $bHasActiveCategories; |
238
|
|
|
} |
239
|
|
|
} else { |
240
|
|
|
$bHasCategories = !$bIsCustomerLoggedIn; |
241
|
|
|
} |
242
|
|
|
} |
243
|
|
|
return $bHasCategories; |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* Check if the customer is logged in |
248
|
|
|
* |
249
|
|
|
* @return bool |
250
|
|
|
*/ |
251
|
|
|
protected function isCustomerLoggedIn() |
252
|
|
|
{ |
253
|
|
|
return Mage::helper('sitewards_b2bprofessional/customer')->isCustomerLoggedIn(); |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* Get the price blocks as defined in the xml |
258
|
|
|
* |
259
|
|
|
* @return string[] |
260
|
|
|
*/ |
261
|
|
|
protected function getPriceBlocks() |
262
|
|
|
{ |
263
|
|
|
if ($this->aPriceBlockClassNames === null) { |
264
|
|
|
$this->aPriceBlockClassNames = Mage::getStoreConfig(self::CONFIG_EXTENSION_PRICE_BLOCKS); |
265
|
|
|
} |
266
|
|
|
return $this->aPriceBlockClassNames; |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* Get the add-to-cart blocks' layout names |
271
|
|
|
* |
272
|
|
|
* @return string[] |
273
|
|
|
*/ |
274
|
|
|
protected function getAddToCartBlockLayoutNames() |
275
|
|
|
{ |
276
|
|
|
return $this->aAddToCartBlockLayoutNames; |
277
|
|
|
} |
278
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.