OXID eShop CE  4.8.12
 All Classes Files Functions Variables Pages
oxlocator.php
Go to the documentation of this file.
1 <?php
2 
6 class oxLocator extends oxSuperCfg
7 {
11  protected $_sType = "list";
12 
16  protected $_oNextProduct = null;
17 
21  protected $_oBackProduct = null;
22 
26  protected $_sSearchHandle = null;
27 
31  protected $_sErrorMessage = null;
32 
39  public function __construct( $sType = null )
40  {
41  // setting locator type
42  if ( $sType ) {
43  $this->_sType = trim( $sType );
44  }
45  }
46 
55  public function setLocatorData( $oCurrArticle, $oLocatorTarget )
56  {
57  $sLocfnc = "_set{$this->_sType}LocatorData";
58  $this->$sLocfnc( $oLocatorTarget, $oCurrArticle );
59 
60  // passing list type to view
61  $oLocatorTarget->setListType( $this->_sType );
62  }
63 
72  protected function _setListLocatorData( $oLocatorTarget, $oCurrArticle )
73  {
74  // if no active category is loaded - lets check for category passed by post/get
75  if ( ( $oCategory = $oLocatorTarget->getActiveCategory() ) ) {
76 
77  $sOrderBy = null;
78  if ( $oLocatorTarget->showSorting() ) {
79  $sOrderBy = $oLocatorTarget->getSortingSql( $oLocatorTarget->getSortIdent() );
80  }
81 
82  $oIdList = $this->_loadIdsInList( $oCategory, $oCurrArticle, $sOrderBy );
83 
84  //page number
85  $iPage = $this->_findActPageNumber( $oLocatorTarget->getActPage(), $oIdList, $oCurrArticle );
86 
87  // setting product position in list, amount of articles etc
88  $oCategory->iCntOfProd = $oIdList->count();
89  $oCategory->iProductPos = $this->_getProductPos( $oCurrArticle, $oIdList, $oLocatorTarget );
90 
91  if ( oxRegistry::getUtils()->seoIsActive() && $iPage ) {
92  $oCategory->toListLink = oxRegistry::get("oxSeoEncoderCategory")->getCategoryPageUrl( $oCategory, $iPage );
93  } else {
94  $oCategory->toListLink = $this->_makeLink( $oCategory->getLink(), $this->_getPageNumber( $iPage ) );
95  }
96 
97  $oCategory->nextProductLink = $this->_oNextProduct?$this->_makeLink( $this->_oNextProduct->getLink(), '' ):null;
98  $oCategory->prevProductLink = $this->_oBackProduct?$this->_makeLink( $this->_oBackProduct->getLink(), '' ):null;
99 
100  // active category
101  $oLocatorTarget->setActiveCategory( $oCategory );
102 
103  // category path
104  if ( ( $oCatTree = $oLocatorTarget->getCategoryTree() ) ) {
105  $oLocatorTarget->setCatTreePath( $oCatTree->getPath() );
106  }
107  }
108  }
109 
118  protected function _setVendorLocatorData( $oLocatorTarget, $oCurrArticle )
119  {
120  if ( ( $oVendor = $oLocatorTarget->getActVendor() ) ) {
121  $sVendorId = $oVendor->getId();
122  $myUtils = oxRegistry::getUtils();
123 
124  $blSeo = $myUtils->seoIsActive();
125 
126  // loading data for article navigation
127  $oIdList = oxNew( "oxArticleList" );
128  if ( $oLocatorTarget->showSorting() ) {
129  $oIdList->setCustomSorting( $oLocatorTarget->getSortingSql( $oLocatorTarget->getSortIdent() ) );
130  }
131  $oIdList->loadVendorIds( $sVendorId );
132 
133  //page number
134  $iPage = $this->_findActPageNumber( $oLocatorTarget->getActPage(), $oIdList, $oCurrArticle );
135 
136  $sAdd = null;
137  if ( !$blSeo ) {
138  $sAdd = 'listtype=vendor&amp;cnid=v_'.$sVendorId;
139  }
140 
141  // setting product position in list, amount of articles etc
142  $oVendor->iCntOfProd = $oIdList->count();
143  $oVendor->iProductPos = $this->_getProductPos( $oCurrArticle, $oIdList, $oLocatorTarget );
144 
145  if ( $blSeo && $iPage ) {
146  $oVendor->toListLink = oxRegistry::get("oxSeoEncoderVendor")->getVendorPageUrl( $oVendor, $iPage );
147  } else {
148  $oVendor->toListLink = $this->_makeLink( $oVendor->getLink(), $this->_getPageNumber( $iPage ) );
149  }
150 
151  $oVendor->nextProductLink = $this->_oNextProduct?$this->_makeLink( $this->_oNextProduct->getLink(), $sAdd ):null;
152  $oVendor->prevProductLink = $this->_oBackProduct?$this->_makeLink( $this->_oBackProduct->getLink(), $sAdd ):null;
153  }
154  }
155 
164  protected function _setManufacturerLocatorData( $oLocatorTarget, $oCurrArticle )
165  {
166  if ( ( $oManufacturer = $oLocatorTarget->getActManufacturer() ) ) {
167  $sManufacturerId = $oManufacturer->getId();
168  $myUtils = oxRegistry::getUtils();
169 
170  $blSeo = $myUtils->seoIsActive();
171 
172  // loading data for article navigation
173  $oIdList = oxNew( "oxarticlelist" );
174  if ( $oLocatorTarget->showSorting() ) {
175  $oIdList->setCustomSorting( $oLocatorTarget->getSortingSql( $oLocatorTarget->getSortIdent() ) );
176  }
177  $oIdList->loadManufacturerIds( $sManufacturerId );
178 
179  //page number
180  $iPage = $this->_findActPageNumber( $oLocatorTarget->getActPage(), $oIdList, $oCurrArticle );
181 
182  $sAdd = null;
183  if ( !$blSeo ) {
184  $sAdd = 'listtype=manufacturer&amp;mnid='.$sManufacturerId;
185  }
186 
187  // setting product position in list, amount of articles etc
188  $oManufacturer->iCntOfProd = $oIdList->count();
189  $oManufacturer->iProductPos = $this->_getProductPos( $oCurrArticle, $oIdList, $oLocatorTarget );
190 
191  if ( $blSeo && $iPage ) {
192  $oManufacturer->toListLink = oxRegistry::get("oxSeoEncoderManufacturer")->getManufacturerPageUrl( $oManufacturer, $iPage );
193  } else {
194  $oManufacturer->toListLink = $this->_makeLink( $oManufacturer->getLink(), $this->_getPageNumber( $iPage ) );
195  }
196 
197  $oManufacturer->nextProductLink = $this->_oNextProduct?$this->_makeLink( $this->_oNextProduct->getLink(), $sAdd ):null;
198  $oManufacturer->prevProductLink = $this->_oBackProduct?$this->_makeLink( $this->_oBackProduct->getLink(), $sAdd ):null;
199 
200  // active Manufacturer
201  $oLocatorTarget->setActiveCategory( $oManufacturer );
202 
203  // Manufacturer path
204  if ( ( $oManufacturerTree = $oLocatorTarget->getManufacturerTree() ) ) {
205  $oLocatorTarget->setCatTreePath( $oManufacturerTree->getPath() );
206  }
207  }
208  }
209 
218  protected function _setSearchLocatorData( $oLocatorTarget, $oCurrArticle )
219  {
220  if ( ( $oSearchCat = $oLocatorTarget->getActSearch() ) ) {
221 
222  // #1834/1184M - specialchar search
223  $sSearchParam = oxConfig::getParameter( 'searchparam', true );
224  $sSearchFormParam = oxConfig::getParameter( 'searchparam' );
225  $sSearchLinkParam = rawurlencode( $sSearchParam );
226 
227  $sSearchCat = oxConfig::getParameter( 'searchcnid' );
228  $sSearchCat = $sSearchCat?rawurldecode( $sSearchCat ):$sSearchCat;
229 
230  $sSearchVendor = oxConfig::getParameter( 'searchvendor' );
231  $sSearchVendor = $sSearchVendor?rawurldecode( $sSearchVendor ):$sSearchVendor;
232 
233  $sSearchManufacturer = oxConfig::getParameter( 'searchmanufacturer' );
234  $sSearchManufacturer = $sSearchManufacturer?rawurldecode( $sSearchManufacturer ):$sSearchManufacturer;
235 
236  // loading data for article navigation
237  $oIdList = oxNew( 'oxarticlelist' );
238  if ( $oLocatorTarget->showSorting() ) {
239  $oIdList->setCustomSorting( $oLocatorTarget->getSortingSql( $oLocatorTarget->getSortIdent() ) );
240  }
241  $oIdList->loadSearchIds( $sSearchParam, $sSearchCat, $sSearchVendor, $sSearchManufacturer );
242 
243  //page number
244  $iPage = $this->_findActPageNumber( $oLocatorTarget->getActPage(), $oIdList, $oCurrArticle );
245 
246  $sAddSearch = "searchparam={$sSearchLinkParam}";
247  $sAddSearch .= '&amp;listtype=search';
248 
249  if ( $sSearchCat !== null ) {
250  $sAddSearch .= "&amp;searchcnid={$sSearchCat}";
251  }
252 
253  if ( $sSearchVendor !== null ) {
254  $sAddSearch .= "&amp;searchvendor={$sSearchVendor}";
255  }
256 
257  if ( $sSearchManufacturer !== null ) {
258  $sAddSearch .= "&amp;searchmanufacturer={$sSearchManufacturer}";
259  }
260 
261  // setting product position in list, amount of articles etc
262  $oSearchCat->iCntOfProd = $oIdList->count();
263  $oSearchCat->iProductPos = $this->_getProductPos( $oCurrArticle, $oIdList, $oLocatorTarget );
264 
265  $sPageNr = $this->_getPageNumber( $iPage );
266  $oSearchCat->toListLink = $this->_makeLink( $oSearchCat->link, $sPageNr.($sPageNr?'&amp;':'').$sAddSearch );
267  $oSearchCat->nextProductLink = $this->_oNextProduct?$this->_makeLink( $this->_oNextProduct->getLink(), $sAddSearch ):null;
268  $oSearchCat->prevProductLink = $this->_oBackProduct?$this->_makeLink( $this->_oBackProduct->getLink(), $sAddSearch ):null;
269 
270  $sFormat = oxRegistry::getLang()->translateString( 'SEARCH_RESULT' );
271  $oLocatorTarget->setSearchTitle( sprintf( $sFormat, $sSearchFormParam ) );
272  $oLocatorTarget->setActiveCategory( $oSearchCat );
273  }
274  }
275 
284  protected function _setTagLocatorData( $oLocatorTarget, $oCurrArticle )
285  {
286  if ( ( $oTag = $oLocatorTarget->getActTag() ) ) {
287 
288  $myUtils = oxRegistry::getUtils();
289 
290  // loading data for article navigation
291  $oIdList = oxNew( 'oxarticlelist' );
292  $oLang = oxRegistry::getLang();
293 
294  if ( $oLocatorTarget->showSorting() ) {
295  $oIdList->setCustomSorting( $oLocatorTarget->getSortingSql( $oLocatorTarget->getSortIdent() ) );
296  }
297 
298  $oIdList->getTagArticleIds( $oTag->sTag, $oLang->getBaseLanguage() );
299 
300  //page number
301  $iPage = $this->_findActPageNumber( $oLocatorTarget->getActPage(), $oIdList, $oCurrArticle );
302 
303  // setting product position in list, amount of articles etc
304  $oTag->iCntOfProd = $oIdList->count();
305  $oTag->iProductPos = $this->_getProductPos( $oCurrArticle, $oIdList, $oLocatorTarget );
306 
307  if ( oxRegistry::getUtils()->seoIsActive() ) {
308  $oTag->toListLink = oxRegistry::get("oxSeoEncoderTag")->getTagPageUrl( $oTag->sTag, $iPage );
309  } else {
310  $sPageNr = $this->_getPageNumber( $iPage );
311  $oTag->toListLink = $this->_makeLink( $oTag->link, $sPageNr );
312  }
313 
314  $sAddSearch = '';
315  // setting parameters when seo is Off
316  if ( !$myUtils->seoIsActive() ) {
317  $sAddSearch = 'searchtag='.rawurlencode( oxRegistry::getConfig()->getRequestParameter( 'searchtag', true ) );
318  $sAddSearch .= '&amp;listtype=tag';
319  }
320 
321  $oTag->nextProductLink = $this->_oNextProduct?$this->_makeLink( $this->_oNextProduct->getLink(), $sAddSearch ):null;
322  $oTag->prevProductLink = $this->_oBackProduct?$this->_makeLink( $this->_oBackProduct->getLink(), $sAddSearch ):null;
323  $oStr = getStr();
324  $oLocatorTarget->setSearchTitle( $oLang->translateString('TAGS').' / '.$oStr->htmlspecialchars( $oStr->ucfirst( $oTag->sTag ) ) );
325  $oLocatorTarget->setActiveCategory( $oTag );
326  }
327  }
328 
340  protected function _setRecommlistLocatorData( $oLocatorTarget, $oCurrArticle )
341  {
342  if ( ( $oRecommList = $oLocatorTarget->getActiveRecommList() ) ) {
343 
344  // loading data for article navigation
345  $oIdList = oxNew( 'oxarticlelist' );
346  $oIdList->loadRecommArticleIds( $oRecommList->getId(), null );
347 
348  //page number
349  $iPage = $this->_findActPageNumber( $oLocatorTarget->getActPage(), $oIdList, $oCurrArticle );
350 
351  $sSearchRecomm = oxConfig::getParameter( 'searchrecomm', true );
352 
353  if ( $sSearchRecomm !== null ) {
354  $sSearchFormRecomm = oxConfig::getParameter( 'searchrecomm' );
355  $sSearchLinkRecomm = rawurlencode( $sSearchRecomm );
356  $sAddSearch = 'searchrecomm='.$sSearchLinkRecomm;
357  }
358 
359  // setting product position in list, amount of articles etc
360  $oRecommList->iCntOfProd = $oIdList->count();
361  $oRecommList->iProductPos = $this->_getProductPos( $oCurrArticle, $oIdList, $oLocatorTarget );
362  $blSeo = oxRegistry::getUtils()->seoIsActive();
363 
364  if ( $blSeo && $iPage ) {
365  $oRecommList->toListLink = oxRegistry::get("oxSeoEncoderRecomm")->getRecommPageUrl( $oRecommList, $iPage );
366  } else {
367  $oRecommList->toListLink = $this->_makeLink( $oRecommList->getLink(), $this->_getPageNumber( $iPage ) );
368  }
369  $oRecommList->toListLink = $this->_makeLink( $oRecommList->toListLink, $sAddSearch );
370 
371  $sAdd = '';
372  if ( !$blSeo ) {
373  $sAdd = 'recommid='.$oRecommList->getId().'&amp;listtype=recommlist'.($sAddSearch?'&amp;':'');
374  }
375  $sAdd .= $sAddSearch;
376  $oRecommList->nextProductLink = $this->_oNextProduct?$this->_makeLink( $this->_oNextProduct->getLink(), $sAdd ):null;
377  $oRecommList->prevProductLink = $this->_oBackProduct?$this->_makeLink( $this->_oBackProduct->getLink(), $sAdd ):null;
378 
379  $oLang = oxRegistry::getLang();
380  $sTitle = $oLang->translateString('RECOMMLIST');
381  if ( $sSearchRecomm !== null ) {
382  $sTitle .= " / ".$oLang->translateString('RECOMMLIST_SEARCH').' "'.$sSearchFormRecomm.'"';
383  }
384  $oLocatorTarget->setSearchTitle( $sTitle );
385  $oLocatorTarget->setActiveCategory( $oRecommList );
386  }
387  }
388 
398  protected function _loadIdsInList( $oCategory, $oCurrArticle, $sOrderBy = null )
399  {
400  $oIdList = oxNew( 'oxarticlelist' );
401  $oIdList->setCustomSorting( $sOrderBy ) ;
402 
403  // additionally check if this category is loaded and is price category ?
404  if ( $oCategory->isPriceCategory() ) {
405  $oIdList->loadPriceIds( $oCategory->oxcategories__oxpricefrom->value, $oCategory->oxcategories__oxpriceto->value );
406  } else {
407  $sActCat = $oCategory->getId();
408  $oIdList->loadCategoryIDs( $sActCat, oxSession::getVar( 'session_attrfilter' ) );
409  // if not found - reloading with empty filter
410  if ( !isset( $oIdList[$oCurrArticle->getId()] ) ) {
411  $oIdList->loadCategoryIDs( $sActCat, null );
412  }
413  }
414 
415  return $oIdList;
416  }
417 
426  protected function _makeLink( $sLink, $sParams )
427  {
428  if ( $sParams ) {
429  $sLink .= ( ( strpos( $sLink, '?' ) !== false ) ? '&amp;' : '?' ) . $sParams;
430  }
431  return $sLink;
432  }
433 
444  protected function _findActPageNumber( $iPageNr, $oIdList = null, $oArticle = null )
445  {
446  //page number
447  $iPageNr = (int) $iPageNr;
448 
449  // maybe there is no page number passed, but we still can find the position in id's list
450  if ( !$iPageNr && $oIdList && $oArticle ) {
451  $iNrofCatArticles = (int) $this->getConfig()->getConfigParam( 'iNrofCatArticles' );
452  $iNrofCatArticles = $iNrofCatArticles?$iNrofCatArticles:1;
453 
454  $sArticleId = $oArticle->oxarticles__oxparentid->value ? $oArticle->oxarticles__oxparentid->value : $oArticle->getId();
455  $iPos = array_search( $sArticleId, $oIdList->arrayKeys() );
456  $iPageNr = floor( $iPos / $iNrofCatArticles );
457  }
458 
459  return $iPageNr;
460  }
461 
469  protected function _getPageNumber( $iPageNr )
470  {
471  //page number
472  $iPageNr = (int) $iPageNr;
473  return ( ($iPageNr > 0 )?"pgNr=$iPageNr":'' );
474  }
475 
485  protected function _getProductPos( $oArticle, $oIdList, $oLocatorTarget )
486  {
487  $iCnt = 1;
488  $iPos = 0;
489 
490  // variant handling
491  $sOxid = $oArticle->oxarticles__oxparentid->value?$oArticle->oxarticles__oxparentid->value:$oArticle->getId();
492  if ( $oIdList->count() && isset( $oIdList[$sOxid] ) ) {
493 
494  $aIds = $oIdList->arrayKeys();
495  $iPos = array_search( $sOxid, $aIds );
496 
497  if ( array_key_exists( $iPos-1, $aIds ) ) {
498  $oBackProduct = oxNew( 'oxarticle' );
499  $oBackProduct->modifyCacheKey('_locator');
500  $oBackProduct->setNoVariantLoading( true );
501  if ( $oBackProduct->load( $aIds[$iPos-1] ) ) {
502  $oBackProduct->setLinkType( $oLocatorTarget->getLinkType() );
503  $this->_oBackProduct = $oBackProduct;
504  }
505  }
506 
507  if ( array_key_exists( $iPos+1, $aIds ) ) {
508  $oNextProduct = oxNew( 'oxarticle' );
509  $oNextProduct->modifyCacheKey('_locator');
510  $oNextProduct->setNoVariantLoading( true );
511  if ( $oNextProduct->load( $aIds[$iPos+1] ) ) {
512  $oNextProduct->setLinkType( $oLocatorTarget->getLinkType() );
513  $this->_oNextProduct = $oNextProduct;
514  }
515  }
516  return $iPos+1;
517  }
518  return 0;
519  }
520 
526  public function getErrorMessage()
527  {
528  return $this->_sErrorMessage;
529  }
530 }