oxlocator.php

Go to the documentation of this file.
00001 <?php
00002 
00006 class oxLocator extends oxSuperCfg
00007 {
00011     protected $_sType = "list";
00012 
00016     protected $_oNextProduct = null;
00017 
00021     protected $_oBackProduct = null;
00022 
00026     protected $_sSearchHandle = null;
00027 
00031     protected $_sErrorMessage = null;
00032 
00039     public function __construct( $sType = null )
00040     {
00041         // setting locator type
00042         if ( $sType ) {
00043             $this->_sType = trim( $sType );
00044         }
00045     }
00046 
00055     public function setLocatorData( $oCurrArticle, $oLocatorTarget )
00056     {
00057         $sLocfnc = "_set{$this->_sType}LocatorData";
00058         $this->$sLocfnc( $oLocatorTarget, $oCurrArticle );
00059 
00060         // passing list type to view
00061         $oLocatorTarget->setListType( $this->_sType );
00062     }
00063 
00072     protected function _setListLocatorData( $oLocatorTarget, $oCurrArticle )
00073     {
00074         // if no active category is loaded - lets check for category passed by post/get
00075         if ( ( $oCategory = $oLocatorTarget->getActCategory() ) ) {
00076             $sCatId = $oCategory->getId();
00077 
00078             $sOrderBy = null;
00079             if ( $oLocatorTarget->showSorting() ) {
00080                 $oLocatorTarget->prepareSortColumns();
00081                 $sOrderBy = $oLocatorTarget->getSortingSql( $sCatId );
00082             }
00083             $oIdList = $this->_loadIdsInList( $oCategory, $oCurrArticle, $sOrderBy );
00084 
00085             //page number
00086             $iPage = $this->_findActPageNumber( $oLocatorTarget->getActPage(), $oIdList, $oCurrArticle );
00087 
00088             // setting product position in list, amount of articles etc
00089             $oCategory->iCntOfProd  = $oIdList->count();
00090             $oCategory->iProductPos = $this->_getProductPos( $oCurrArticle, $oIdList, $oLocatorTarget );
00091 
00092             if ( oxUtils::getInstance()->seoIsActive() && $iPage ) {
00093                 $oCategory->toListLink = oxSeoEncoderCategory::getInstance()->getCategoryPageUrl( $oCategory, $iPage );
00094             } else {
00095                 $oCategory->toListLink  = $this->_makeLink( $oCategory->getLink(), $this->_getPageNumber( $iPage ) );
00096             }
00097 
00098             $oCategory->nextProductLink = $this->_oNextProduct?$this->_makeLink( $this->_oNextProduct->getLink(), '' ):null;
00099             $oCategory->prevProductLink = $this->_oBackProduct?$this->_makeLink( $this->_oBackProduct->getLink(), '' ):null;
00100 
00101             // active category
00102             $oLocatorTarget->setActiveCategory( $oCategory );
00103             // for compatibility reasons for a while. will be removed in future
00104             $oLocatorTarget->addTplParam( 'actCategory', $oLocatorTarget->getActiveCategory() );
00105 
00106             // category path
00107             if ( ( $oCatTree = $oLocatorTarget->getCategoryTree() ) ) {
00108                 $oLocatorTarget->setCatTreePath( $oCatTree->getPath() );
00109                 // for compatibility reasons for a while. will be removed in future
00110                 $oLocatorTarget->addTplParam( 'actCatpath', $oLocatorTarget->getCatTreePath() );
00111             }
00112         }
00113     }
00114 
00126     protected function _setVendorLocatorData( $oLocatorTarget, $oCurrArticle )
00127     {
00128         if ( ( $oVendor = $oLocatorTarget->getActVendor() ) ) {
00129             $sVendorId = $oVendor->getId();
00130             $myUtils   = oxUtils::getInstance();
00131 
00132             $blSeo = $myUtils->seoIsActive();
00133 
00134             // loading data for article navigation
00135             $oIdList = oxNew( "oxarticlelist" );
00136             if ( $oLocatorTarget->showSorting() ) {
00137                 $oLocatorTarget->prepareSortColumns();
00138                 $oIdList->setCustomSorting( $oLocatorTarget->getSortingSql( "$sVendorId:vendor" ) );
00139             }
00140             $oIdList->loadVendorIds( $sVendorId );
00141 
00142             //page number
00143             $iPage = $this->_findActPageNumber( $oLocatorTarget->getActPage(), $oIdList, $oCurrArticle );
00144 
00145             $sAdd = null;
00146             if ( !$blSeo ) {
00147                 $sAdd = 'listtype=vendor&amp;cnid=v_'.$sVendorId;
00148             }
00149 
00150             // setting product position in list, amount of articles etc
00151             $oVendor->iCntOfProd  = $oIdList->count();
00152             $oVendor->iProductPos = $this->_getProductPos( $oCurrArticle, $oIdList, $oLocatorTarget );
00153 
00154             if ( $blSeo && $iPage ) {
00155                 $oVendor->toListLink = oxSeoEncoderVendor::getInstance()->getVendorPageUrl( $oVendor, $iPage );
00156             } else {
00157                 $oVendor->toListLink  = $this->_makeLink( $oVendor->getLink(), $this->_getPageNumber( $iPage ) );
00158             }
00159 
00160             $oVendor->nextProductLink = $this->_oNextProduct?$this->_makeLink( $this->_oNextProduct->getLink(), $sAdd ):null;
00161             $oVendor->prevProductLink = $this->_oBackProduct?$this->_makeLink( $this->_oBackProduct->getLink(), $sAdd ):null;
00162 
00163             // active vendor
00164             $oLocatorTarget->setActiveCategory( $oVendor );
00165             // for compatibility reasons for a while. will be removed in future
00166             $oLocatorTarget->addTplParam( 'actCategory', $oLocatorTarget->getActiveCategory() );
00167 
00168             // vendor path
00169             if ( ( $oVendorTree = $oLocatorTarget->getVendorTree() ) ) {
00170                 $oLocatorTarget->setCatTreePath( $oVendorTree->getPath() );
00171                 // for compatibility reasons for a while. will be removed in future
00172                 $oLocatorTarget->addTplParam( 'actCatpath', $oLocatorTarget->getCatTreePath() );
00173             }
00174         }
00175     }
00176 
00188     protected function _setManufacturerLocatorData( $oLocatorTarget, $oCurrArticle )
00189     {
00190         if ( ( $oManufacturer = $oLocatorTarget->getActManufacturer() ) ) {
00191             $sManufacturerId = $oManufacturer->getId();
00192             $myUtils = oxUtils::getInstance();
00193 
00194             $blSeo = $myUtils->seoIsActive();
00195 
00196             // loading data for article navigation
00197             $oIdList = oxNew( "oxarticlelist" );
00198             if ( $oLocatorTarget->showSorting() ) {
00199                 $oLocatorTarget->prepareSortColumns();
00200                 $oIdList->setCustomSorting( $oLocatorTarget->getSortingSql( "$sManufacturerId:manufacturer" ) );
00201             }
00202             $oIdList->loadManufacturerIds( $sManufacturerId );
00203 
00204             //page number
00205             $iPage = $this->_findActPageNumber( $oLocatorTarget->getActPage(), $oIdList, $oCurrArticle );
00206 
00207             $sAdd = null;
00208             if ( !$blSeo ) {
00209                 $sAdd = 'listtype=manufacturer&amp;mnid='.$sManufacturerId;
00210             }
00211 
00212             // setting product position in list, amount of articles etc
00213             $oManufacturer->iCntOfProd  = $oIdList->count();
00214             $oManufacturer->iProductPos = $this->_getProductPos( $oCurrArticle, $oIdList, $oLocatorTarget );
00215 
00216             if ( $blSeo && $iPage ) {
00217                 $oManufacturer->toListLink = oxSeoEncoderManufacturer::getInstance()->getManufacturerPageUrl( $oManufacturer, $iPage );
00218             } else {
00219                 $oManufacturer->toListLink  = $this->_makeLink( $oManufacturer->getLink(), $this->_getPageNumber( $iPage ) );
00220             }
00221 
00222             $oManufacturer->nextProductLink = $this->_oNextProduct?$this->_makeLink( $this->_oNextProduct->getLink(), $sAdd ):null;
00223             $oManufacturer->prevProductLink = $this->_oBackProduct?$this->_makeLink( $this->_oBackProduct->getLink(), $sAdd ):null;
00224 
00225             // active Manufacturer
00226             $oLocatorTarget->setActiveCategory( $oManufacturer );
00227             // for compatibility reasons for a while. will be removed in future
00228             $oLocatorTarget->addTplParam( 'actCategory', $oLocatorTarget->getActiveCategory() );
00229 
00230             // Manufacturer path
00231             if ( ( $oManufacturerTree = $oLocatorTarget->getManufacturerTree() ) ) {
00232                 $oLocatorTarget->setCatTreePath( $oManufacturerTree->getPath() );
00233                 // for compatibility reasons for a while. will be removed in future
00234                 $oLocatorTarget->addTplParam( 'actCatpath', $oLocatorTarget->getCatTreePath() );
00235             }
00236         }
00237     }
00238 
00251     protected function _setSearchLocatorData( $oLocatorTarget, $oCurrArticle )
00252     {
00253         if ( ( $oSearchCat = $oLocatorTarget->getActSearch() ) ) {
00254 
00255             // #1834/1184M - specialchar search
00256             $sSearchParam = oxConfig::getParameter( 'searchparam', true );
00257             $sSearchFormParam = oxConfig::getParameter( 'searchparam' );
00258             $sSearchLinkParam = rawurlencode( $sSearchParam );
00259 
00260             $sSearchCat = oxConfig::getParameter( 'searchcnid' );
00261             $sSearchCat = $sSearchCat?rawurldecode( $sSearchCat ):$sSearchCat;
00262 
00263             $sSearchVendor = oxConfig::getParameter( 'searchvendor' );
00264             $sSearchVendor = $sSearchVendor?rawurldecode( $sSearchVendor ):$sSearchVendor;
00265 
00266             $sSearchManufacturer = oxConfig::getParameter( 'searchmanufacturer' );
00267             $sSearchManufacturer = $sSearchManufacturer?rawurldecode( $sSearchManufacturer ):$sSearchManufacturer;
00268 
00269             // loading data for article navigation
00270             $oIdList = oxNew( 'oxarticlelist' );
00271             if ( $oLocatorTarget->showSorting() ) {
00272                 $oLocatorTarget->prepareSortColumns();
00273                 $oIdList->setCustomSorting( $oLocatorTarget->getSortingSql( 'oxsearch' ) );
00274             }
00275             $oIdList->loadSearchIds( $sSearchParam, $sSearchCat, $sSearchVendor, $sSearchManufacturer );
00276 
00277             //page number
00278             $iPage = $this->_findActPageNumber( $oLocatorTarget->getActPage(), $oIdList, $oCurrArticle );
00279 
00280             $sAddSearch = "searchparam={$sSearchLinkParam}";
00281             if ( $sSearchCat !== null ) {
00282                 $sAddSearch .= "&amp;searchcnid={$sSearchCat}";
00283             }
00284 
00285             if ( $sSearchVendor !== null ) {
00286                 $sAddSearch .= "&amp;searchvendor={$sSearchVendor}";
00287             }
00288 
00289             if ( $sSearchManufacturer !== null ) {
00290                 $sAddSearch .= "&amp;searchmanufacturer={$sSearchManufacturer}";
00291             }
00292 
00293             // setting product position in list, amount of articles etc
00294             $oSearchCat->iCntOfProd  = $oIdList->count();
00295             $oSearchCat->iProductPos = $this->_getProductPos( $oCurrArticle, $oIdList, $oLocatorTarget );
00296 
00297             $sPageNr = $this->_getPageNumber( $iPage );
00298             $oSearchCat->toListLink  = $this->_makeLink( $oSearchCat->link, $sPageNr.($sPageNr?'&amp;':'').$sAddSearch );
00299             $oSearchCat->nextProductLink = $this->_oNextProduct?$this->_makeLink( $this->_oNextProduct->getLink(), $sAddSearch ):null;
00300             $oSearchCat->prevProductLink = $this->_oBackProduct?$this->_makeLink( $this->_oBackProduct->getLink(), $sAddSearch ):null;
00301 
00302             $sFormat = oxLang::getInstance()->translateString( 'searchResult' );
00303             $oLocatorTarget->setSearchTitle( sprintf( $sFormat, $sSearchFormParam ) );
00304             // for compatibility reasons for a while. will be removed in future
00305             $oLocatorTarget->addTplParam( 'sSearchTitle', $oLocatorTarget->getSearchTitle() );
00306 
00307             $oLocatorTarget->addTplParam( 'searchparam', $sSearchLinkParam );
00308             $oLocatorTarget->addTplParam( 'searchcnid', $sSearchCat );
00309             $oLocatorTarget->addTplParam( 'searchvendor', $sSearchVendor );
00310             $oLocatorTarget->addTplParam( 'searchmanufacturer', $sSearchManufacturer );
00311             $oLocatorTarget->addTplParam( 'searchparamforhtml', $sSearchFormParam );
00312 
00313             $oLocatorTarget->setActiveCategory( $oSearchCat );
00314             // for compatibility reasons for a while. will be removed in future
00315             $oLocatorTarget->addTplParam( 'actCategory', $oLocatorTarget->getActiveCategory() );
00316         }
00317     }
00318 
00330     protected function _setTagLocatorData( $oLocatorTarget, $oCurrArticle )
00331     {
00332         if ( ( $oTag = $oLocatorTarget->getActTag() ) ) {
00333 
00334             $myUtils = oxUtils::getInstance();
00335 
00336             // loading data for article navigation
00337             $oIdList = oxNew( 'oxarticlelist' );
00338             $oLang = oxLang::getInstance();
00339 
00340             if ( $oLocatorTarget->showSorting() ) {
00341                 $oLocatorTarget->prepareSortColumns();
00342                 $oIdList->setCustomSorting( $oLocatorTarget->getSortingSql( 'oxtags' ) );
00343             }
00344 
00345             $oIdList->getTagArticleIds( $oTag->sTag, $oLang->getBaseLanguage() );
00346 
00347             //page number
00348             $iPage = $this->_findActPageNumber( $oLocatorTarget->getActPage(), $oIdList, $oCurrArticle );
00349 
00350             // setting product position in list, amount of articles etc
00351             $oTag->iCntOfProd  = $oIdList->count();
00352             $oTag->iProductPos = $this->_getProductPos( $oCurrArticle, $oIdList, $oLocatorTarget );
00353 
00354             $sPageNr = $this->_getPageNumber( $iPage );
00355             $oTag->toListLink  = $this->_makeLink( $oTag->link, $sPageNr );
00356 
00357             $sAddSearch = '';
00358             // setting parameters when seo is Off
00359             if ( !$myUtils->seoIsActive() ) {
00360                 $sAddSearch  = 'searchtag='.rawurlencode( oxConfig::getParameter( 'searchtag', 1 ) );
00361                 $sAddSearch .= '&amp;listtype=tag';
00362             }
00363 
00364             $oTag->nextProductLink = $this->_oNextProduct?$this->_makeLink( $this->_oNextProduct->getLink(), $sAddSearch ):null;
00365             $oTag->prevProductLink = $this->_oBackProduct?$this->_makeLink( $this->_oBackProduct->getLink(), $sAddSearch ):null;
00366             $oStr = getStr();
00367             $oLocatorTarget->setSearchTitle( $oLang->translateString('TAGS').' / '.$oStr->htmlspecialchars( $oStr->ucfirst( $oTag->sTag ) ) );
00368             // for compatibility reasons for a while. will be removed in future
00369             $oLocatorTarget->addTplParam( 'sSearchTitle', $oLocatorTarget->getSearchTitle() );
00370             $oLocatorTarget->addTplParam( 'searchtagforhtml', null );
00371             $oLocatorTarget->setActiveCategory( $oTag );
00372             // for compatibility reasons for a while. will be removed in future
00373             $oLocatorTarget->addTplParam( 'actCategory', $oLocatorTarget->getActiveCategory() );
00374         }
00375     }
00376 
00388     protected function _setRecommlistLocatorData( $oLocatorTarget, $oCurrArticle )
00389     {
00390         if ( ( $oRecommList = $oLocatorTarget->getActiveRecommList() ) ) {
00391 
00392             // loading data for article navigation
00393             $oIdList = oxNew( 'oxarticlelist' );
00394             $oIdList->loadRecommArticleIds( $oRecommList->getId(), null );
00395 
00396             //page number
00397             $iPage = $this->_findActPageNumber( $oLocatorTarget->getActPage(), $oIdList, $oCurrArticle );
00398 
00399             $sSearchRecomm = oxConfig::getParameter( 'searchrecomm', true );
00400 
00401             if ( $sSearchRecomm !== null ) {
00402                 $sSearchFormRecomm = oxConfig::getParameter( 'searchrecomm' );
00403                 $sSearchLinkRecomm = rawurlencode( $sSearchRecomm );
00404                 $sAddSearch = 'searchrecomm='.$sSearchLinkRecomm;
00405             }
00406 
00407             // setting product position in list, amount of articles etc
00408             $oRecommList->iCntOfProd  = $oIdList->count();
00409             $oRecommList->iProductPos = $this->_getProductPos( $oCurrArticle, $oIdList, $oLocatorTarget );
00410             $blSeo = oxUtils::getInstance()->seoIsActive();
00411 
00412             if ( $blSeo && $iPage ) {
00413                 $oRecommList->toListLink = oxSeoEncoderRecomm::getInstance()->getRecommPageUrl( $oRecommList, $iPage );
00414             } else {
00415                 $oRecommList->toListLink  = $this->_makeLink( $oRecommList->getLink(), $this->_getPageNumber( $iPage ) );
00416             }
00417             $oRecommList->toListLink  = $this->_makeLink( $oRecommList->toListLink, $sAddSearch );
00418 
00419             $sAdd = '';
00420             if ( !$blSeo ) {
00421                 $sAdd = 'recommid='.$oRecommList->getId().'&amp;listtype=recommlist'.($sAddSearch?'&amp;':'');
00422             }
00423             $sAdd .= $sAddSearch;
00424             $oRecommList->nextProductLink = $this->_oNextProduct?$this->_makeLink( $this->_oNextProduct->getLink(), $sAdd ):null;
00425             $oRecommList->prevProductLink = $this->_oBackProduct?$this->_makeLink( $this->_oBackProduct->getLink(), $sAdd ):null;
00426 
00427             $oLang  = oxLang::getInstance();
00428             $sTitle = $oLang->translateString('RECOMMLIST');
00429             if ( $sSearchRecomm !== null ) {
00430                 $sTitle .= " / ".$oLang->translateString('RECOMMLIST_SEARCH').' "'.$sSearchFormRecomm.'"';
00431                 $oLocatorTarget->addTplParam( 'searchrecomm', $sSearchLinkRecomm );
00432                 $oLocatorTarget->addTplParam( 'searchrecommforhtml', $sSearchFormRecomm );
00433             }
00434             $oLocatorTarget->setSearchTitle( $sTitle );
00435             // for compatibility reasons for a while. will be removed in future
00436             $oLocatorTarget->addTplParam( 'sSearchTitle', $oLocatorTarget->getSearchTitle() );
00437             $oLocatorTarget->setActiveCategory( $oRecommList );
00438             // for compatibility reasons for a while. will be removed in future
00439             $oLocatorTarget->addTplParam( 'actCategory', $oLocatorTarget->getActiveCategory() );
00440         }
00441     }
00442 
00452     protected function _loadIdsInList( $oCategory, $oCurrArticle, $sOrderBy = null )
00453     {
00454         $oIdList = oxNew( 'oxarticlelist' );
00455         $oIdList->setCustomSorting( $sOrderBy ) ;
00456 
00457         // additionally check if this category is loaded and is price category ?
00458         if ( $oCategory->isPriceCategory() ) {
00459             $oIdList->loadPriceIds( $oCategory->oxcategories__oxpricefrom->value, $oCategory->oxcategories__oxpriceto->value );
00460         } else {
00461             $sActCat = $oCategory->getId();
00462             $oIdList->loadCategoryIDs( $sActCat, oxSession::getVar( 'session_attrfilter' ) );
00463             // if not found - reloading with empty filter
00464             if ( !isset( $oIdList[$oCurrArticle->getId()] ) ) {
00465                 $oIdList->loadCategoryIDs( $sActCat, null );
00466             }
00467         }
00468 
00469         return $oIdList;
00470     }
00471 
00480     protected function _makeLink( $sLink, $sParams )
00481     {
00482         if ( $sParams ) {
00483             $sLink .= ( ( strpos( $sLink, '?' ) !== false ) ? '&amp;' : '?' ) . $sParams;
00484         }
00485         return $sLink;
00486     }
00487 
00498     protected function _findActPageNumber( $iPageNr, $oIdList = null, $oArticle = null )
00499     {
00500         //page number
00501         $iPageNr = (int) $iPageNr;
00502 
00503         // maybe there is no page number passed, but we still can find the position in id's list
00504         if ( !$iPageNr && $oIdList && $oArticle ) {
00505             $iNrofCatArticles = (int) $this->getConfig()->getConfigParam( 'iNrofCatArticles' );
00506             $iNrofCatArticles = $iNrofCatArticles?$iNrofCatArticles:1;
00507 
00508             $sArticleId = $oArticle->oxarticles__oxparentid->value ? $oArticle->oxarticles__oxparentid->value : $oArticle->getId();
00509             $iPos = array_search( $sArticleId, $oIdList->arrayKeys() );
00510             $iPageNr = floor( $iPos / $iNrofCatArticles );
00511         }
00512 
00513         return $iPageNr;
00514     }
00515 
00523     protected function _getPageNumber( $iPageNr )
00524     {
00525         //page number
00526         $iPageNr = (int) $iPageNr;
00527         return ( ($iPageNr > 0 )?"pgNr=$iPageNr":'' );
00528     }
00529 
00539     protected function _getProductPos( $oArticle, $oIdList, $oLocatorTarget )
00540     {
00541         $iCnt = 1;
00542         $iPos = 0;
00543 
00544         // variant handling
00545         $sOxid = $oArticle->oxarticles__oxparentid->value?$oArticle->oxarticles__oxparentid->value:$oArticle->getId();
00546         if ( $oIdList->count() && isset( $oIdList[$sOxid] ) ) {
00547 
00548             $aIds = $oIdList->arrayKeys();
00549             $iPos = array_search( $sOxid, $aIds );
00550 
00551             if ( array_key_exists( $iPos-1, $aIds ) ) {
00552                 $oBackProduct = oxNew( 'oxarticle' );
00553                 $oBackProduct->setNoVariantLoading( true );
00554                 if ( $oBackProduct->load( $aIds[$iPos-1] ) ) {
00555                     $oBackProduct->setLinkType( $oLocatorTarget->getLinkType() );
00556                     $this->_oBackProduct = $oBackProduct;
00557                 }
00558             }
00559 
00560             if ( array_key_exists( $iPos+1, $aIds ) ) {
00561                 $oNextProduct = oxNew( 'oxarticle' );
00562                 $oNextProduct->setNoVariantLoading( true );
00563                 if ( $oNextProduct->load( $aIds[$iPos+1] ) ) {
00564                     $oNextProduct->setLinkType( $oLocatorTarget->getLinkType() );
00565                     $this->_oNextProduct = $oNextProduct;
00566                 }
00567             }
00568             return $iPos+1;
00569         }
00570         return 0;
00571     }
00572 
00578     public function getErrorMessage()
00579     {
00580         return $this->_sErrorMessage;
00581     }
00582 }