forked from OXID-eSales/oxideshop_ce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Locator.php
568 lines (478 loc) · 22.6 KB
/
Locator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
<?php
/**
* This file is part of OXID eShop Community Edition.
*
* OXID eShop Community Edition is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* OXID eShop Community Edition is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with OXID eShop Community Edition. If not, see <http://www.gnu.org/licenses/>.
*
* @link http://www.oxid-esales.com
* @copyright (C) OXID eSales AG 2003-2016
* @version OXID eShop CE
*/
namespace OxidEsales\Eshop\Application\Component;
use oxRegistry;
use oxSeoEncoderRecomm;
use oxSeoEncoderCategory;
/**
* Locator controller for: category, vendor, manufacturers and search lists.
*/
class Locator extends \oxSuperCfg
{
/**
* Locator type
*/
protected $_sType = "list";
/**
* Next product to currently loaded
*/
protected $_oNextProduct = null;
/**
* Previous product to currently loaded
*/
protected $_oBackProduct = null;
/**
* search handle
*/
protected $_sSearchHandle = null;
/**
* error message
*/
protected $_sErrorMessage = null;
/**
* Class constructor - sets locator type and parameters posted or loaded
* from GET/Session
*
* @param string $sType locator type
*/
public function __construct($sType = null)
{
// setting locator type
if ($sType) {
$this->_sType = trim($sType);
}
}
/**
* Executes locator method according locator type
*
* @param oxarticle $oCurrArticle current article
* @param oxubase $oLocatorTarget oxubase object
*/
public function setLocatorData($oCurrArticle, $oLocatorTarget)
{
$sLocfnc = "_set{$this->_sType}LocatorData";
$this->$sLocfnc($oLocatorTarget, $oCurrArticle);
// passing list type to view
$oLocatorTarget->setListType($this->_sType);
}
/**
* Sets details locator data for articles that came from regular list.
*
* @param oxUBase $oLocatorTarget view object
* @param oxArticle $oCurrArticle current article
*/
protected function _setListLocatorData($oLocatorTarget, $oCurrArticle)
{
// if no active category is loaded - lets check for category passed by post/get
if (($oCategory = $oLocatorTarget->getActiveCategory())) {
// Bug 6155: blShowSorting is set in the backend only to allow/deny sorting by user in the frontend - "Users can sort Product Lists"
$sOrderBy = $oLocatorTarget->getSortingSql($oLocatorTarget->getSortIdent());
$oIdList = $this->_loadIdsInList($oCategory, $oCurrArticle, $sOrderBy);
//page number
$iPage = $this->_findActPageNumber($oLocatorTarget->getActPage(), $oIdList, $oCurrArticle);
// setting product position in list, amount of articles etc
$oCategory->iCntOfProd = $oIdList->count();
$oCategory->iProductPos = $this->_getProductPos($oCurrArticle, $oIdList, $oLocatorTarget);
if (oxRegistry::getUtils()->seoIsActive() && $iPage) {
/** @var oxSeoEncoderCategory $oSeoEncoderCategory */
$oSeoEncoderCategory = oxRegistry::get("oxSeoEncoderCategory");
$oCategory->toListLink = $oSeoEncoderCategory->getCategoryPageUrl($oCategory, $iPage);
} else {
$oCategory->toListLink = $this->_makeLink($oCategory->getLink(), $this->_getPageNumber($iPage));
}
$oNextProduct = $this->_oNextProduct;
$oBackProduct = $this->_oBackProduct;
$oCategory->nextProductLink = $oNextProduct ? $this->_makeLink($oNextProduct->getLink(), '') : null;
$oCategory->prevProductLink = $oBackProduct ? $this->_makeLink($oBackProduct->getLink(), '') : null;
// active category
$oLocatorTarget->setActiveCategory($oCategory);
// category path
if (($oCatTree = $oLocatorTarget->getCategoryTree())) {
$oLocatorTarget->setCatTreePath($oCatTree->getPath());
}
}
}
/**
* Sets details locator data for articles that came from vendor list.
*
* @param oxUBase $oLocatorTarget oxUBase object
* @param oxArticle $oCurrArticle current article
*/
protected function _setVendorLocatorData($oLocatorTarget, $oCurrArticle)
{
if (($oVendor = $oLocatorTarget->getActVendor())) {
$sVendorId = $oVendor->getId();
$myUtils = oxRegistry::getUtils();
$blSeo = $myUtils->seoIsActive();
// loading data for article navigation
$oIdList = oxNew("oxArticleList");
// Bug 6155: blShowSorting is set in the backend only to allow/deny sorting by user in the frontend - "Users can sort Product Lists"
$oIdList->setCustomSorting($oLocatorTarget->getSortingSql($oLocatorTarget->getSortIdent()));
$oIdList->loadVendorIds($sVendorId);
//page number
$iPage = $this->_findActPageNumber($oLocatorTarget->getActPage(), $oIdList, $oCurrArticle);
$sAdd = null;
if (!$blSeo) {
$sAdd = 'listtype=vendor&cnid=v_' . $sVendorId;
}
// setting product position in list, amount of articles etc
$oVendor->iCntOfProd = $oIdList->count();
$oVendor->iProductPos = $this->_getProductPos($oCurrArticle, $oIdList, $oLocatorTarget);
if ($blSeo && $iPage) {
$oVendor->toListLink = oxRegistry::get("oxSeoEncoderVendor")->getVendorPageUrl($oVendor, $iPage);
} else {
$oVendor->toListLink = $this->_makeLink($oVendor->getLink(), $this->_getPageNumber($iPage));
}
$oNextProduct = $this->_oNextProduct;
$oBackProduct = $this->_oBackProduct;
$oVendor->nextProductLink = $oNextProduct ? $this->_makeLink($oNextProduct->getLink(), $sAdd) : null;
$oVendor->prevProductLink = $oBackProduct ? $this->_makeLink($oBackProduct->getLink(), $sAdd) : null;
}
}
/**
* Sets details locator data for articles that came from Manufacturer list.
*
* @param oxubase $oLocatorTarget oxubase object
* @param oxarticle $oCurrArticle current article
*/
protected function _setManufacturerLocatorData($oLocatorTarget, $oCurrArticle)
{
if (($oManufacturer = $oLocatorTarget->getActManufacturer())) {
$sManufacturerId = $oManufacturer->getId();
$myUtils = oxRegistry::getUtils();
$blSeo = $myUtils->seoIsActive();
// loading data for article navigation
$oIdList = oxNew("oxArticleList");
// Bug 6155: blShowSorting is set in the backend only to allow/deny sorting by user in the frontend - "Users can sort Product Lists"
$oIdList->setCustomSorting($oLocatorTarget->getSortingSql($oLocatorTarget->getSortIdent()));
$oIdList->loadManufacturerIds($sManufacturerId);
//page number
$iPage = $this->_findActPageNumber($oLocatorTarget->getActPage(), $oIdList, $oCurrArticle);
$sAdd = null;
if (!$blSeo) {
$sAdd = 'listtype=manufacturer&mnid=' . $sManufacturerId;
}
// setting product position in list, amount of articles etc
$oManufacturer->iCntOfProd = $oIdList->count();
$oManufacturer->iProductPos = $this->_getProductPos($oCurrArticle, $oIdList, $oLocatorTarget);
if ($blSeo && $iPage) {
/** @var oxSeoEncoderManufacturer $oSeoEncoderManufacturer */
$oSeoEncoderManufacturer = oxRegistry::get("oxSeoEncoderManufacturer");
$oManufacturer->toListLink = $oSeoEncoderManufacturer->getManufacturerPageUrl($oManufacturer, $iPage);
} else {
$oManufacturer->toListLink = $this->_makeLink($oManufacturer->getLink(), $this->_getPageNumber($iPage));
}
$oNextProduct = $this->_oNextProduct;
$oBackProduct = $this->_oBackProduct;
$oManufacturer->nextProductLink = $oNextProduct ? $this->_makeLink($oNextProduct->getLink(), $sAdd) : null;
$oManufacturer->prevProductLink = $oBackProduct ? $this->_makeLink($oBackProduct->getLink(), $sAdd) : null;
// active Manufacturer
$oLocatorTarget->setActiveCategory($oManufacturer);
// Manufacturer path
if (($oManufacturerTree = $oLocatorTarget->getManufacturerTree())) {
$oLocatorTarget->setCatTreePath($oManufacturerTree->getPath());
}
}
}
/**
* Sets details locator data for articles that came from search list.
*
* @param oxubase $oLocatorTarget oxubase object
* @param oxarticle $oCurrArticle current article
*/
protected function _setSearchLocatorData($oLocatorTarget, $oCurrArticle)
{
if (($oSearchCat = $oLocatorTarget->getActSearch())) {
// #1834/1184M - specialchar search
$sSearchParam = oxRegistry::getConfig()->getRequestParameter('searchparam', true);
$sSearchFormParam = oxRegistry::getConfig()->getRequestParameter('searchparam');
$sSearchLinkParam = rawurlencode($sSearchParam);
$sSearchCat = oxRegistry::getConfig()->getRequestParameter('searchcnid');
$sSearchCat = $sSearchCat ? rawurldecode($sSearchCat) : $sSearchCat;
$sSearchVendor = oxRegistry::getConfig()->getRequestParameter('searchvendor');
$sSearchVendor = $sSearchVendor ? rawurldecode($sSearchVendor) : $sSearchVendor;
$sSearchManufacturer = oxRegistry::getConfig()->getRequestParameter('searchmanufacturer');
$sSearchManufacturer = $sSearchManufacturer ? rawurldecode($sSearchManufacturer) : $sSearchManufacturer;
// loading data for article navigation
$oIdList = oxNew('oxArticleList');
// Bug 6155: blShowSorting is set in the backend only to allow/deny sorting by user in the frontend - "Users can sort Product Lists"
$oIdList->setCustomSorting($oLocatorTarget->getSortingSql($oLocatorTarget->getSortIdent()));
$oIdList->loadSearchIds($sSearchParam, $sSearchCat, $sSearchVendor, $sSearchManufacturer);
//page number
$iPage = $this->_findActPageNumber($oLocatorTarget->getActPage(), $oIdList, $oCurrArticle);
$sAddSearch = "searchparam={$sSearchLinkParam}";
$sAddSearch .= '&listtype=search';
if ($sSearchCat !== null) {
$sAddSearch .= "&searchcnid={$sSearchCat}";
}
if ($sSearchVendor !== null) {
$sAddSearch .= "&searchvendor={$sSearchVendor}";
}
if ($sSearchManufacturer !== null) {
$sAddSearch .= "&searchmanufacturer={$sSearchManufacturer}";
}
// setting product position in list, amount of articles etc
$oSearchCat->iCntOfProd = $oIdList->count();
$oSearchCat->iProductPos = $this->_getProductPos($oCurrArticle, $oIdList, $oLocatorTarget);
$sPageNr = $this->_getPageNumber($iPage);
$sParams = $sPageNr . ($sPageNr ? '&' : '') . $sAddSearch;
$oSearchCat->toListLink = $this->_makeLink($oSearchCat->link, $sParams);
$oNextProd = $this->_oNextProduct;
$oBackProd = $this->_oBackProduct;
$oSearchCat->nextProductLink = $oNextProd ? $this->_makeLink($oNextProd->getLink(), $sAddSearch) : null;
$oSearchCat->prevProductLink = $oBackProd ? $this->_makeLink($oBackProd->getLink(), $sAddSearch) : null;
$sFormat = oxRegistry::getLang()->translateString('SEARCH_RESULT');
$oLocatorTarget->setSearchTitle(sprintf($sFormat, $sSearchFormParam));
$oLocatorTarget->setActiveCategory($oSearchCat);
}
}
/**
* Sets details locator data for articles that came from tag list.
*
* @param oxubase $oLocatorTarget oxubase object
* @param oxarticle $oCurrArticle current article
*
* @deprecated v5.3 (2016-05-04); Tags will be moved to own module.
*/
protected function _setTagLocatorData($oLocatorTarget, $oCurrArticle)
{
if (($oTag = $oLocatorTarget->getActTag())) {
$myUtils = oxRegistry::getUtils();
// loading data for article navigation
$oIdList = oxNew('oxArticleList');
$oLang = oxRegistry::getLang();
if ($oLocatorTarget->showSorting()) {
$oIdList->setCustomSorting($oLocatorTarget->getSortingSql($oLocatorTarget->getSortIdent()));
}
$oIdList->getTagArticleIds($oTag->sTag, $oLang->getBaseLanguage());
//page number
$iPage = $this->_findActPageNumber($oLocatorTarget->getActPage(), $oIdList, $oCurrArticle);
// setting product position in list, amount of articles etc
$oTag->iCntOfProd = $oIdList->count();
$oTag->iProductPos = $this->_getProductPos($oCurrArticle, $oIdList, $oLocatorTarget);
if (oxRegistry::getUtils()->seoIsActive()) {
$oTag->toListLink = oxRegistry::get("oxSeoEncoderTag")->getTagPageUrl($oTag->sTag, $iPage);
} else {
$sPageNr = $this->_getPageNumber($iPage);
$oTag->toListLink = $this->_makeLink($oTag->link, $sPageNr);
}
$sAddSearch = '';
// setting parameters when seo is Off
if (!$myUtils->seoIsActive()) {
$sSearchTagParameter = oxRegistry::getConfig()->getRequestParameter('searchtag', true);
$sAddSearch = 'searchtag=' . rawurlencode($sSearchTagParameter);
$sAddSearch .= '&listtype=tag';
}
$oNextProduct = $this->_oNextProduct;
$oBackProduct = $this->_oBackProduct;
$oTag->nextProductLink = $oNextProduct ? $this->_makeLink($oNextProduct->getLink(), $sAddSearch) : null;
$oTag->prevProductLink = $oBackProduct ? $this->_makeLink($oBackProduct->getLink(), $sAddSearch) : null;
$oStr = getStr();
$sTitle = $oLang->translateString('TAGS') . ' / ' . $oStr->htmlspecialchars($oStr->ucfirst($oTag->sTag));
$oLocatorTarget->setSearchTitle($sTitle);
$oLocatorTarget->setActiveCategory($oTag);
}
}
/**
* Sets details locator data for articles that came from recommlist.
*
* Template variables:
* <b>sSearchTitle</b>, <b>searchparamforhtml</b>
*
* @param oxubase $oLocatorTarget oxubase object
* @param oxarticle $oCurrArticle current article
*
* @deprecated since v5.3 (2016-06-17); Listmania will be moved to an own module.
*/
protected function _setRecommlistLocatorData($oLocatorTarget, $oCurrArticle)
{
if (($oRecommList = $oLocatorTarget->getActiveRecommList())) {
// loading data for article navigation
$oIdList = oxNew('oxArticleList');
$oIdList->loadRecommArticleIds($oRecommList->getId(), null);
//page number
$iPage = $this->_findActPageNumber($oLocatorTarget->getActPage(), $oIdList, $oCurrArticle);
$sSearchRecomm = oxRegistry::getConfig()->getRequestParameter('searchrecomm', true);
if ($sSearchRecomm !== null) {
$sSearchFormRecomm = oxRegistry::getConfig()->getRequestParameter('searchrecomm');
$sSearchLinkRecomm = rawurlencode($sSearchRecomm);
$sAddSearch = 'searchrecomm=' . $sSearchLinkRecomm;
}
// setting product position in list, amount of articles etc
$oRecommList->iCntOfProd = $oIdList->count();
$oRecommList->iProductPos = $this->_getProductPos($oCurrArticle, $oIdList, $oLocatorTarget);
$blSeo = oxRegistry::getUtils()->seoIsActive();
if ($blSeo && $iPage) {
/** @var oxSeoEncoderRecomm $oSeoEncoderRecomm */
$oSeoEncoderRecomm = oxRegistry::get("oxSeoEncoderRecomm");
$oRecommList->toListLink = $oSeoEncoderRecomm->getRecommPageUrl($oRecommList, $iPage);
} else {
$oRecommList->toListLink = $this->_makeLink($oRecommList->getLink(), $this->_getPageNumber($iPage));
}
$oRecommList->toListLink = $this->_makeLink($oRecommList->toListLink, $sAddSearch);
$sAdd = '';
if (!$blSeo) {
$sAdd = 'recommid=' . $oRecommList->getId() . '&listtype=recommlist' . ($sAddSearch ? '&' : '');
}
$sAdd .= $sAddSearch;
$oNextProduct = $this->_oNextProduct;
$oBackProduct = $this->_oBackProduct;
$oRecommList->nextProductLink = $oNextProduct ? $this->_makeLink($oNextProduct->getLink(), $sAdd) : null;
$oRecommList->prevProductLink = $oBackProduct ? $this->_makeLink($oBackProduct->getLink(), $sAdd) : null;
$oLang = oxRegistry::getLang();
$sTitle = $oLang->translateString('RECOMMLIST');
if ($sSearchRecomm !== null) {
$sTitle .= " / " . $oLang->translateString('RECOMMLIST_SEARCH') . ' "' . $sSearchFormRecomm . '"';
}
$oLocatorTarget->setSearchTitle($sTitle);
$oLocatorTarget->setActiveCategory($oRecommList);
}
}
/**
* Setting product position in list, amount of articles etc
*
* @param oxcategory $oCategory active category id
* @param object $oCurrArticle current article
* @param string $sOrderBy order by fields
*
* @return object
*/
protected function _loadIdsInList($oCategory, $oCurrArticle, $sOrderBy = null)
{
$oIdList = oxNew('oxArticleList');
$oIdList->setCustomSorting($sOrderBy);
// additionally check if this category is loaded and is price category ?
if ($oCategory->isPriceCategory()) {
$oIdList->loadPriceIds($oCategory->oxcategories__oxpricefrom->value, $oCategory->oxcategories__oxpriceto->value);
} else {
$sActCat = $oCategory->getId();
$oIdList->loadCategoryIDs($sActCat, oxRegistry::getSession()->getVariable('session_attrfilter'));
// if not found - reloading with empty filter
if (!isset($oIdList[$oCurrArticle->getId()])) {
$oIdList->loadCategoryIDs($sActCat, null);
}
}
return $oIdList;
}
/**
* Appends urs with currently passed parameters
*
* @param string $sLink url to add parameters
* @param string $sParams parameters to add to url
*
* @return string
*/
protected function _makeLink($sLink, $sParams)
{
if ($sParams) {
$sLink .= ((strpos($sLink, '?') !== false) ? '&' : '?') . $sParams;
}
return $sLink;
}
/**
* If page number is not passed trying to fetch it from list of ids. To search
* for position in list, article ids list and current article id must be passed
*
* @param int $iPageNr current page number (user defined or passed by request)
* @param oxlist $oIdList list of article ids (optional)
* @param oxarticle $oArticle active article id (optional)
*
* @return int
*/
protected function _findActPageNumber($iPageNr, $oIdList = null, $oArticle = null)
{
//page number
$iPageNr = (int) $iPageNr;
// maybe there is no page number passed, but we still can find the position in id's list
if (!$iPageNr && $oIdList && $oArticle) {
$iNrofCatArticles = (int) $this->getConfig()->getConfigParam('iNrofCatArticles');
$iNrofCatArticles = $iNrofCatArticles ? $iNrofCatArticles : 1;
$sParentIdField = 'oxarticles__oxparentid';
$sArticleId = $oArticle->$sParentIdField->value ? $oArticle->$sParentIdField->value : $oArticle->getId();
$iPos = oxRegistry::getUtils()->arrayStringSearch($sArticleId, $oIdList->arrayKeys());
$iPageNr = floor($iPos / $iNrofCatArticles);
}
return $iPageNr;
}
/**
* Gets current page number.
*
* @param int $iPageNr page number
*
* @return string $sPageNum
*/
protected function _getPageNumber($iPageNr)
{
//page number
$iPageNr = (int) $iPageNr;
return (($iPageNr > 0) ? "pgNr=$iPageNr" : '');
}
/**
* Searches for current article in article list and sets previous/next product ids
*
* @param oxarticle $oArticle current Article
* @param object $oIdList articles list containing only fake article objects !!!
* @param oxubase $oLocatorTarget oxubase object
*
* @return integer
*/
protected function _getProductPos($oArticle, $oIdList, $oLocatorTarget)
{
$iCnt = 1;
$iPos = 0;
// variant handling
$sOxid = $oArticle->oxarticles__oxparentid->value ? $oArticle->oxarticles__oxparentid->value : $oArticle->getId();
if ($oIdList->count() && isset($oIdList[$sOxid])) {
$aIds = $oIdList->arrayKeys();
$iPos = oxRegistry::getUtils()->arrayStringSearch($sOxid, $aIds);
if (array_key_exists($iPos - 1, $aIds)) {
$oBackProduct = oxNew('oxArticle');
$oBackProduct->modifyCacheKey('_locator');
$oBackProduct->setNoVariantLoading(true);
if ($oBackProduct->load($aIds[$iPos - 1])) {
$oBackProduct->setLinkType($oLocatorTarget->getLinkType());
$this->_oBackProduct = $oBackProduct;
}
}
if (array_key_exists($iPos + 1, $aIds)) {
$oNextProduct = oxNew('oxArticle');
$oNextProduct->modifyCacheKey('_locator');
$oNextProduct->setNoVariantLoading(true);
if ($oNextProduct->load($aIds[$iPos + 1])) {
$oNextProduct->setLinkType($oLocatorTarget->getLinkType());
$this->_oNextProduct = $oNextProduct;
}
}
return $iPos + 1;
}
return 0;
}
/**
* Template variable getter. Returns error message
*
* @return string
*/
public function getErrorMessage()
{
return $this->_sErrorMessage;
}
}