oxlocator.php

Go to the documentation of this file.
00001 <?php
00002 
00008 class oxLocator extends oxSuperCfg
00009 {
00013     protected $_sType = "list";
00014 
00018     protected $_oNextProduct = null;
00019 
00023     protected $_oBackProduct = null;
00024 
00028     protected $_sSearchHandle = null;
00029 
00033     protected $_sErrorMessage = null;
00034 
00041     public function __construct( $sType = null )
00042     {
00043         // setting locator type
00044         if ( $sType ) {
00045             $this->_sType = trim( $sType );
00046         }
00047     }
00048 
00057     public function setLocatorData( $oCurrArticle, $oLocatorTarget )
00058     {
00059         $sLocfnc = "_set{$this->_sType}LocatorData";
00060         $this->$sLocfnc( $oLocatorTarget, $oCurrArticle );
00061 
00062         // passing list type to view
00063         $oLocatorTarget->setListType( $this->_sType );
00064     }
00065 
00076     protected function _setListLocatorData( $oLocatorTarget, $oCurrArticle )
00077     {
00078         // if no active category is loaded - lets check for category passed by post/get
00079         if ( ( $oCategory = $oLocatorTarget->getActiveCategory() ) ) {
00080 
00081             $sOrderBy = null;
00082             if ( $oLocatorTarget->showSorting() ) {
00083                 $sOrderBy = $oLocatorTarget->getSortingSql( $oLocatorTarget->getSortIdent() );
00084             }
00085 
00086             $oIdList = $this->_loadIdsInList( $oCategory, $oCurrArticle, $sOrderBy );
00087 
00088             //page number
00089             $iPage = $this->_findActPageNumber( $oLocatorTarget->getActPage(), $oIdList, $oCurrArticle );
00090 
00091             // setting product position in list, amount of articles etc
00092             $oCategory->iCntOfProd  = $oIdList->count();
00093             $oCategory->iProductPos = $this->_getProductPos( $oCurrArticle, $oIdList, $oLocatorTarget );
00094 
00095             if ( oxRegistry::getUtils()->seoIsActive() && $iPage ) {
00096                 $oCategory->toListLink = oxRegistry::get("oxSeoEncoderCategory")->getCategoryPageUrl( $oCategory, $iPage );
00097             } else {
00098                 $oCategory->toListLink  = $this->_makeLink( $oCategory->getLink(), $this->_getPageNumber( $iPage ) );
00099             }
00100 
00101             $oCategory->nextProductLink = $this->_oNextProduct?$this->_makeLink( $this->_oNextProduct->getLink(), '' ):null;
00102             $oCategory->prevProductLink = $this->_oBackProduct?$this->_makeLink( $this->_oBackProduct->getLink(), '' ):null;
00103 
00104             // active category
00105             $oLocatorTarget->setActiveCategory( $oCategory );
00106 
00107             // category path
00108             if ( ( $oCatTree = $oLocatorTarget->getCategoryTree() ) ) {
00109                 $oLocatorTarget->setCatTreePath( $oCatTree->getPath() );
00110             }
00111         }
00112     }
00113 
00124     protected function _setVendorLocatorData( $oLocatorTarget, $oCurrArticle )
00125     {
00126         if ( ( $oVendor = $oLocatorTarget->getActVendor() ) ) {
00127             $sVendorId = $oVendor->getId();
00128             $myUtils   = oxRegistry::getUtils();
00129 
00130             $blSeo = $myUtils->seoIsActive();
00131 
00132             // loading data for article navigation
00133             $oIdList = oxNew( "oxArticleList" );
00134             if ( $oLocatorTarget->showSorting() ) {
00135                 $oIdList->setCustomSorting( $oLocatorTarget->getSortingSql( $oLocatorTarget->getSortIdent() ) );
00136             }
00137             $oIdList->loadVendorIds( $sVendorId );
00138 
00139             //page number
00140             $iPage = $this->_findActPageNumber( $oLocatorTarget->getActPage(), $oIdList, $oCurrArticle );
00141 
00142             $sAdd = null;
00143             if ( !$blSeo ) {
00144                 $sAdd = 'listtype=vendor&amp;cnid=v_'.$sVendorId;
00145             }
00146 
00147             // setting product position in list, amount of articles etc
00148             $oVendor->iCntOfProd  = $oIdList->count();
00149             $oVendor->iProductPos = $this->_getProductPos( $oCurrArticle, $oIdList, $oLocatorTarget );
00150 
00151             if ( $blSeo && $iPage ) {
00152                 $oVendor->toListLink = oxRegistry::get("oxSeoEncoderVendor")->getVendorPageUrl( $oVendor, $iPage );
00153             } else {
00154                 $oVendor->toListLink  = $this->_makeLink( $oVendor->getLink(), $this->_getPageNumber( $iPage ) );
00155             }
00156 
00157             $oVendor->nextProductLink = $this->_oNextProduct?$this->_makeLink( $this->_oNextProduct->getLink(), $sAdd ):null;
00158             $oVendor->prevProductLink = $this->_oBackProduct?$this->_makeLink( $this->_oBackProduct->getLink(), $sAdd ):null;
00159         }
00160     }
00161 
00172     protected function _setManufacturerLocatorData( $oLocatorTarget, $oCurrArticle )
00173     {
00174         if ( ( $oManufacturer = $oLocatorTarget->getActManufacturer() ) ) {
00175             $sManufacturerId = $oManufacturer->getId();
00176             $myUtils = oxRegistry::getUtils();
00177 
00178             $blSeo = $myUtils->seoIsActive();
00179 
00180             // loading data for article navigation
00181             $oIdList = oxNew( "oxarticlelist" );
00182             if ( $oLocatorTarget->showSorting() ) {
00183                 $oIdList->setCustomSorting( $oLocatorTarget->getSortingSql( $oLocatorTarget->getSortIdent() ) );
00184             }
00185             $oIdList->loadManufacturerIds( $sManufacturerId );
00186 
00187             //page number
00188             $iPage = $this->_findActPageNumber( $oLocatorTarget->getActPage(), $oIdList, $oCurrArticle );
00189 
00190             $sAdd = null;
00191             if ( !$blSeo ) {
00192                 $sAdd = 'listtype=manufacturer&amp;mnid='.$sManufacturerId;
00193             }
00194 
00195             // setting product position in list, amount of articles etc
00196             $oManufacturer->iCntOfProd  = $oIdList->count();
00197             $oManufacturer->iProductPos = $this->_getProductPos( $oCurrArticle, $oIdList, $oLocatorTarget );
00198 
00199             if ( $blSeo && $iPage ) {
00200                 $oManufacturer->toListLink = oxRegistry::get("oxSeoEncoderManufacturer")->getManufacturerPageUrl( $oManufacturer, $iPage );
00201             } else {
00202                 $oManufacturer->toListLink  = $this->_makeLink( $oManufacturer->getLink(), $this->_getPageNumber( $iPage ) );
00203             }
00204 
00205             $oManufacturer->nextProductLink = $this->_oNextProduct?$this->_makeLink( $this->_oNextProduct->getLink(), $sAdd ):null;
00206             $oManufacturer->prevProductLink = $this->_oBackProduct?$this->_makeLink( $this->_oBackProduct->getLink(), $sAdd ):null;
00207 
00208             // active Manufacturer
00209             $oLocatorTarget->setActiveCategory( $oManufacturer );
00210 
00211             // Manufacturer path
00212             if ( ( $oManufacturerTree = $oLocatorTarget->getManufacturerTree() ) ) {
00213                 $oLocatorTarget->setCatTreePath( $oManufacturerTree->getPath() );
00214             }
00215         }
00216     }
00217 
00228     protected function _setSearchLocatorData( $oLocatorTarget, $oCurrArticle )
00229     {
00230         if ( ( $oSearchCat = $oLocatorTarget->getActSearch() ) ) {
00231 
00232             // #1834/1184M - specialchar search
00233             $sSearchParam = oxConfig::getParameter( 'searchparam', true );
00234             $sSearchFormParam = oxConfig::getParameter( 'searchparam' );
00235             $sSearchLinkParam = rawurlencode( $sSearchParam );
00236 
00237             $sSearchCat = oxConfig::getParameter( 'searchcnid' );
00238             $sSearchCat = $sSearchCat?rawurldecode( $sSearchCat ):$sSearchCat;
00239 
00240             $sSearchVendor = oxConfig::getParameter( 'searchvendor' );
00241             $sSearchVendor = $sSearchVendor?rawurldecode( $sSearchVendor ):$sSearchVendor;
00242 
00243             $sSearchManufacturer = oxConfig::getParameter( 'searchmanufacturer' );
00244             $sSearchManufacturer = $sSearchManufacturer?rawurldecode( $sSearchManufacturer ):$sSearchManufacturer;
00245 
00246             // loading data for article navigation
00247             $oIdList = oxNew( 'oxarticlelist' );
00248             if ( $oLocatorTarget->showSorting() ) {
00249                 $oIdList->setCustomSorting( $oLocatorTarget->getSortingSql( $oLocatorTarget->getSortIdent() ) );
00250             }
00251             $oIdList->loadSearchIds( $sSearchParam, $sSearchCat, $sSearchVendor, $sSearchManufacturer );
00252 
00253             //page number
00254             $iPage = $this->_findActPageNumber( $oLocatorTarget->getActPage(), $oIdList, $oCurrArticle );
00255 
00256             $sAddSearch = "searchparam={$sSearchLinkParam}";
00257             $sAddSearch .= '&amp;listtype=search';
00258 
00259             if ( $sSearchCat !== null ) {
00260                 $sAddSearch .= "&amp;searchcnid={$sSearchCat}";
00261             }
00262 
00263             if ( $sSearchVendor !== null ) {
00264                 $sAddSearch .= "&amp;searchvendor={$sSearchVendor}";
00265             }
00266 
00267             if ( $sSearchManufacturer !== null ) {
00268                 $sAddSearch .= "&amp;searchmanufacturer={$sSearchManufacturer}";
00269             }
00270 
00271             // setting product position in list, amount of articles etc
00272             $oSearchCat->iCntOfProd  = $oIdList->count();
00273             $oSearchCat->iProductPos = $this->_getProductPos( $oCurrArticle, $oIdList, $oLocatorTarget );
00274 
00275             $sPageNr = $this->_getPageNumber( $iPage );
00276             $oSearchCat->toListLink  = $this->_makeLink( $oSearchCat->link, $sPageNr.($sPageNr?'&amp;':'').$sAddSearch );
00277             $oSearchCat->nextProductLink = $this->_oNextProduct?$this->_makeLink( $this->_oNextProduct->getLink(), $sAddSearch ):null;
00278             $oSearchCat->prevProductLink = $this->_oBackProduct?$this->_makeLink( $this->_oBackProduct->getLink(), $sAddSearch ):null;
00279 
00280             $sFormat = oxRegistry::getLang()->translateString( 'SEARCH_RESULT' );
00281             $oLocatorTarget->setSearchTitle( sprintf( $sFormat, $sSearchFormParam ) );
00282             $oLocatorTarget->setActiveCategory( $oSearchCat );
00283         }
00284     }
00285 
00296     protected function _setTagLocatorData( $oLocatorTarget, $oCurrArticle )
00297     {
00298         if ( ( $oTag = $oLocatorTarget->getActTag() ) ) {
00299 
00300             $myUtils = oxRegistry::getUtils();
00301 
00302             // loading data for article navigation
00303             $oIdList = oxNew( 'oxarticlelist' );
00304             $oLang = oxRegistry::getLang();
00305 
00306             if ( $oLocatorTarget->showSorting() ) {
00307                 $oIdList->setCustomSorting( $oLocatorTarget->getSortingSql( $oLocatorTarget->getSortIdent() ) );
00308             }
00309 
00310             $oIdList->getTagArticleIds( $oTag->sTag, $oLang->getBaseLanguage() );
00311 
00312             //page number
00313             $iPage = $this->_findActPageNumber( $oLocatorTarget->getActPage(), $oIdList, $oCurrArticle );
00314 
00315             // setting product position in list, amount of articles etc
00316             $oTag->iCntOfProd  = $oIdList->count();
00317             $oTag->iProductPos = $this->_getProductPos( $oCurrArticle, $oIdList, $oLocatorTarget );
00318 
00319             if ( oxRegistry::getUtils()->seoIsActive() ) {
00320                 $oTag->toListLink  = oxRegistry::get("oxSeoEncoderTag")->getTagPageUrl( $oTag->sTag, $iPage );
00321             } else {
00322                 $sPageNr = $this->_getPageNumber( $iPage );
00323                 $oTag->toListLink  = $this->_makeLink( $oTag->link, $sPageNr );
00324             }
00325 
00326             $sAddSearch = '';
00327             // setting parameters when seo is Off
00328             if ( !$myUtils->seoIsActive() ) {
00329                 $sAddSearch  = 'searchtag='.rawurlencode( oxRegistry::getConfig()->getRequestParameter( 'searchtag', true ) );
00330                 $sAddSearch .= '&amp;listtype=tag';
00331             }
00332 
00333             $oTag->nextProductLink = $this->_oNextProduct?$this->_makeLink( $this->_oNextProduct->getLink(), $sAddSearch ):null;
00334             $oTag->prevProductLink = $this->_oBackProduct?$this->_makeLink( $this->_oBackProduct->getLink(), $sAddSearch ):null;
00335             $oStr = getStr();
00336             $oLocatorTarget->setSearchTitle( $oLang->translateString('TAGS').' / '.$oStr->htmlspecialchars( $oStr->ucfirst( $oTag->sTag ) ) );
00337             $oLocatorTarget->setActiveCategory( $oTag );
00338         }
00339     }
00340 
00354     protected function _setRecommlistLocatorData( $oLocatorTarget, $oCurrArticle )
00355     {
00356         if ( ( $oRecommList = $oLocatorTarget->getActiveRecommList() ) ) {
00357 
00358             // loading data for article navigation
00359             $oIdList = oxNew( 'oxarticlelist' );
00360             $oIdList->loadRecommArticleIds( $oRecommList->getId(), null );
00361 
00362             //page number
00363             $iPage = $this->_findActPageNumber( $oLocatorTarget->getActPage(), $oIdList, $oCurrArticle );
00364 
00365             $sSearchRecomm = oxConfig::getParameter( 'searchrecomm', true );
00366 
00367             if ( $sSearchRecomm !== null ) {
00368                 $sSearchFormRecomm = oxConfig::getParameter( 'searchrecomm' );
00369                 $sSearchLinkRecomm = rawurlencode( $sSearchRecomm );
00370                 $sAddSearch = 'searchrecomm='.$sSearchLinkRecomm;
00371             }
00372 
00373             // setting product position in list, amount of articles etc
00374             $oRecommList->iCntOfProd  = $oIdList->count();
00375             $oRecommList->iProductPos = $this->_getProductPos( $oCurrArticle, $oIdList, $oLocatorTarget );
00376             $blSeo = oxRegistry::getUtils()->seoIsActive();
00377 
00378             if ( $blSeo && $iPage ) {
00379                 $oRecommList->toListLink = oxRegistry::get("oxSeoEncoderRecomm")->getRecommPageUrl( $oRecommList, $iPage );
00380             } else {
00381                 $oRecommList->toListLink  = $this->_makeLink( $oRecommList->getLink(), $this->_getPageNumber( $iPage ) );
00382             }
00383             $oRecommList->toListLink  = $this->_makeLink( $oRecommList->toListLink, $sAddSearch );
00384 
00385             $sAdd = '';
00386             if ( !$blSeo ) {
00387                 $sAdd = 'recommid='.$oRecommList->getId().'&amp;listtype=recommlist'.($sAddSearch?'&amp;':'');
00388             }
00389             $sAdd .= $sAddSearch;
00390             $oRecommList->nextProductLink = $this->_oNextProduct?$this->_makeLink( $this->_oNextProduct->getLink(), $sAdd ):null;
00391             $oRecommList->prevProductLink = $this->_oBackProduct?$this->_makeLink( $this->_oBackProduct->getLink(), $sAdd ):null;
00392 
00393             $oLang  = oxRegistry::getLang();
00394             $sTitle = $oLang->translateString('RECOMMLIST');
00395             if ( $sSearchRecomm !== null ) {
00396                 $sTitle .= " / ".$oLang->translateString('RECOMMLIST_SEARCH').' "'.$sSearchFormRecomm.'"';
00397             }
00398             $oLocatorTarget->setSearchTitle( $sTitle );
00399             $oLocatorTarget->setActiveCategory( $oRecommList );
00400         }
00401     }
00402 
00414     protected function _loadIdsInList( $oCategory, $oCurrArticle, $sOrderBy = null )
00415     {
00416         $oIdList = oxNew( 'oxarticlelist' );
00417         $oIdList->setCustomSorting( $sOrderBy ) ;
00418 
00419         // additionally check if this category is loaded and is price category ?
00420         if ( $oCategory->isPriceCategory() ) {
00421             $oIdList->loadPriceIds( $oCategory->oxcategories__oxpricefrom->value, $oCategory->oxcategories__oxpriceto->value );
00422         } else {
00423             $sActCat = $oCategory->getId();
00424             $oIdList->loadCategoryIDs( $sActCat, oxSession::getVar( 'session_attrfilter' ) );
00425             // if not found - reloading with empty filter
00426             if ( !isset( $oIdList[$oCurrArticle->getId()] ) ) {
00427                 $oIdList->loadCategoryIDs( $sActCat, null );
00428             }
00429         }
00430 
00431         return $oIdList;
00432     }
00433 
00444     protected function _makeLink( $sLink, $sParams )
00445     {
00446         if ( $sParams ) {
00447             $sLink .= ( ( strpos( $sLink, '?' ) !== false ) ? '&amp;' : '?' ) . $sParams;
00448         }
00449         return $sLink;
00450     }
00451 
00464     protected function _findActPageNumber( $iPageNr, $oIdList = null, $oArticle = null )
00465     {
00466         //page number
00467         $iPageNr = (int) $iPageNr;
00468 
00469         // maybe there is no page number passed, but we still can find the position in id's list
00470         if ( !$iPageNr && $oIdList && $oArticle ) {
00471             $iNrofCatArticles = (int) $this->getConfig()->getConfigParam( 'iNrofCatArticles' );
00472             $iNrofCatArticles = $iNrofCatArticles?$iNrofCatArticles:1;
00473 
00474             $sArticleId = $oArticle->oxarticles__oxparentid->value ? $oArticle->oxarticles__oxparentid->value : $oArticle->getId();
00475             $iPos = array_search( $sArticleId, $oIdList->arrayKeys() );
00476             $iPageNr = floor( $iPos / $iNrofCatArticles );
00477         }
00478 
00479         return $iPageNr;
00480     }
00481 
00491     protected function _getPageNumber( $iPageNr )
00492     {
00493         //page number
00494         $iPageNr = (int) $iPageNr;
00495         return ( ($iPageNr > 0 )?"pgNr=$iPageNr":'' );
00496     }
00497 
00509     protected function _getProductPos( $oArticle, $oIdList, $oLocatorTarget )
00510     {
00511         $iCnt = 1;
00512         $iPos = 0;
00513 
00514         // variant handling
00515         $sOxid = $oArticle->oxarticles__oxparentid->value?$oArticle->oxarticles__oxparentid->value:$oArticle->getId();
00516         if ( $oIdList->count() && isset( $oIdList[$sOxid] ) ) {
00517 
00518             $aIds = $oIdList->arrayKeys();
00519             $iPos = array_search( $sOxid, $aIds );
00520 
00521             if ( array_key_exists( $iPos-1, $aIds ) ) {
00522                 $oBackProduct = oxNew( 'oxarticle' );
00523                 $oBackProduct->modifyCacheKey('_locator');
00524                 $oBackProduct->setNoVariantLoading( true );
00525                 if ( $oBackProduct->load( $aIds[$iPos-1] ) ) {
00526                     $oBackProduct->setLinkType( $oLocatorTarget->getLinkType() );
00527                     $this->_oBackProduct = $oBackProduct;
00528                 }
00529             }
00530 
00531             if ( array_key_exists( $iPos+1, $aIds ) ) {
00532                 $oNextProduct = oxNew( 'oxarticle' );
00533                 $oNextProduct->modifyCacheKey('_locator');
00534                 $oNextProduct->setNoVariantLoading( true );
00535                 if ( $oNextProduct->load( $aIds[$iPos+1] ) ) {
00536                     $oNextProduct->setLinkType( $oLocatorTarget->getLinkType() );
00537                     $this->_oNextProduct = $oNextProduct;
00538                 }
00539             }
00540             return $iPos+1;
00541         }
00542         return 0;
00543     }
00544 
00550     public function getErrorMessage()
00551     {
00552         return $this->_sErrorMessage;
00553     }
00554 }