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->getActiveCategory() ) ) {
00076 
00077             $sOrderBy = null;
00078             if ( $oLocatorTarget->showSorting() ) {
00079                 $sOrderBy = $oLocatorTarget->getSortingSql( $oLocatorTarget->getSortIdent() );
00080             }
00081 
00082             $oIdList = $this->_loadIdsInList( $oCategory, $oCurrArticle, $sOrderBy );
00083 
00084             //page number
00085             $iPage = $this->_findActPageNumber( $oLocatorTarget->getActPage(), $oIdList, $oCurrArticle );
00086 
00087             // setting product position in list, amount of articles etc
00088             $oCategory->iCntOfProd  = $oIdList->count();
00089             $oCategory->iProductPos = $this->_getProductPos( $oCurrArticle, $oIdList, $oLocatorTarget );
00090 
00091             if ( oxRegistry::getUtils()->seoIsActive() && $iPage ) {
00092                 $oCategory->toListLink = oxRegistry::get("oxSeoEncoderCategory")->getCategoryPageUrl( $oCategory, $iPage );
00093             } else {
00094                 $oCategory->toListLink  = $this->_makeLink( $oCategory->getLink(), $this->_getPageNumber( $iPage ) );
00095             }
00096 
00097             $oCategory->nextProductLink = $this->_oNextProduct?$this->_makeLink( $this->_oNextProduct->getLink(), '' ):null;
00098             $oCategory->prevProductLink = $this->_oBackProduct?$this->_makeLink( $this->_oBackProduct->getLink(), '' ):null;
00099 
00100             // active category
00101             $oLocatorTarget->setActiveCategory( $oCategory );
00102 
00103             // category path
00104             if ( ( $oCatTree = $oLocatorTarget->getCategoryTree() ) ) {
00105                 $oLocatorTarget->setCatTreePath( $oCatTree->getPath() );
00106             }
00107         }
00108     }
00109 
00118     protected function _setVendorLocatorData( $oLocatorTarget, $oCurrArticle )
00119     {
00120         if ( ( $oVendor = $oLocatorTarget->getActVendor() ) ) {
00121             $sVendorId = $oVendor->getId();
00122             $myUtils   = oxRegistry::getUtils();
00123 
00124             $blSeo = $myUtils->seoIsActive();
00125 
00126             // loading data for article navigation
00127             $oIdList = oxNew( "oxarticlelist" );
00128             if ( $oLocatorTarget->showSorting() ) {
00129                 $oIdList->setCustomSorting( $oLocatorTarget->getSortingSql( $oLocatorTarget->getSortIdent() ) );
00130             }
00131             $oIdList->loadVendorIds( $sVendorId );
00132 
00133             //page number
00134             $iPage = $this->_findActPageNumber( $oLocatorTarget->getActPage(), $oIdList, $oCurrArticle );
00135 
00136             $sAdd = null;
00137             if ( !$blSeo ) {
00138                 $sAdd = 'listtype=vendor&amp;cnid=v_'.$sVendorId;
00139             }
00140 
00141             // setting product position in list, amount of articles etc
00142             $oVendor->iCntOfProd  = $oIdList->count();
00143             $oVendor->iProductPos = $this->_getProductPos( $oCurrArticle, $oIdList, $oLocatorTarget );
00144 
00145             if ( $blSeo && $iPage ) {
00146                 $oVendor->toListLink = oxRegistry::get("oxSeoEncoderVendor")->getVendorPageUrl( $oVendor, $iPage );
00147             } else {
00148                 $oVendor->toListLink  = $this->_makeLink( $oVendor->getLink(), $this->_getPageNumber( $iPage ) );
00149             }
00150 
00151             $oVendor->nextProductLink = $this->_oNextProduct?$this->_makeLink( $this->_oNextProduct->getLink(), $sAdd ):null;
00152             $oVendor->prevProductLink = $this->_oBackProduct?$this->_makeLink( $this->_oBackProduct->getLink(), $sAdd ):null;
00153 
00154             // active vendor
00155             $oLocatorTarget->setActiveCategory( $oVendor );
00156 
00157             // vendor path
00158             if ( ( $oVendorTree = $oLocatorTarget->getVendorTree() ) ) {
00159                 $oLocatorTarget->setCatTreePath( $oVendorTree->getPath() );
00160             }
00161         }
00162     }
00163 
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 
00226     protected function _setSearchLocatorData( $oLocatorTarget, $oCurrArticle )
00227     {
00228         if ( ( $oSearchCat = $oLocatorTarget->getActSearch() ) ) {
00229 
00230             // #1834/1184M - specialchar search
00231             $sSearchParam = oxConfig::getParameter( 'searchparam', true );
00232             $sSearchFormParam = oxConfig::getParameter( 'searchparam' );
00233             $sSearchLinkParam = rawurlencode( $sSearchParam );
00234 
00235             $sSearchCat = oxConfig::getParameter( 'searchcnid' );
00236             $sSearchCat = $sSearchCat?rawurldecode( $sSearchCat ):$sSearchCat;
00237 
00238             $sSearchVendor = oxConfig::getParameter( 'searchvendor' );
00239             $sSearchVendor = $sSearchVendor?rawurldecode( $sSearchVendor ):$sSearchVendor;
00240 
00241             $sSearchManufacturer = oxConfig::getParameter( 'searchmanufacturer' );
00242             $sSearchManufacturer = $sSearchManufacturer?rawurldecode( $sSearchManufacturer ):$sSearchManufacturer;
00243 
00244             // loading data for article navigation
00245             $oIdList = oxNew( 'oxarticlelist' );
00246             if ( $oLocatorTarget->showSorting() ) {
00247                 $oIdList->setCustomSorting( $oLocatorTarget->getSortingSql( $oLocatorTarget->getSortIdent() ) );
00248             }
00249             $oIdList->loadSearchIds( $sSearchParam, $sSearchCat, $sSearchVendor, $sSearchManufacturer );
00250 
00251             //page number
00252             $iPage = $this->_findActPageNumber( $oLocatorTarget->getActPage(), $oIdList, $oCurrArticle );
00253 
00254             $sAddSearch = "searchparam={$sSearchLinkParam}";
00255             $sAddSearch .= '&amp;listtype=search';
00256 
00257             if ( $sSearchCat !== null ) {
00258                 $sAddSearch .= "&amp;searchcnid={$sSearchCat}";
00259             }
00260 
00261             if ( $sSearchVendor !== null ) {
00262                 $sAddSearch .= "&amp;searchvendor={$sSearchVendor}";
00263             }
00264 
00265             if ( $sSearchManufacturer !== null ) {
00266                 $sAddSearch .= "&amp;searchmanufacturer={$sSearchManufacturer}";
00267             }
00268 
00269             // setting product position in list, amount of articles etc
00270             $oSearchCat->iCntOfProd  = $oIdList->count();
00271             $oSearchCat->iProductPos = $this->_getProductPos( $oCurrArticle, $oIdList, $oLocatorTarget );
00272 
00273             $sPageNr = $this->_getPageNumber( $iPage );
00274             $oSearchCat->toListLink  = $this->_makeLink( $oSearchCat->link, $sPageNr.($sPageNr?'&amp;':'').$sAddSearch );
00275             $oSearchCat->nextProductLink = $this->_oNextProduct?$this->_makeLink( $this->_oNextProduct->getLink(), $sAddSearch ):null;
00276             $oSearchCat->prevProductLink = $this->_oBackProduct?$this->_makeLink( $this->_oBackProduct->getLink(), $sAddSearch ):null;
00277 
00278             $sFormat = oxRegistry::getLang()->translateString( 'SEARCH_RESULT' );
00279             $oLocatorTarget->setSearchTitle( sprintf( $sFormat, $sSearchFormParam ) );
00280             $oLocatorTarget->setActiveCategory( $oSearchCat );
00281         }
00282     }
00283 
00292     protected function _setTagLocatorData( $oLocatorTarget, $oCurrArticle )
00293     {
00294         if ( ( $oTag = $oLocatorTarget->getActTag() ) ) {
00295 
00296             $myUtils = oxRegistry::getUtils();
00297 
00298             // loading data for article navigation
00299             $oIdList = oxNew( 'oxarticlelist' );
00300             $oLang = oxRegistry::getLang();
00301 
00302             if ( $oLocatorTarget->showSorting() ) {
00303                 $oIdList->setCustomSorting( $oLocatorTarget->getSortingSql( $oLocatorTarget->getSortIdent() ) );
00304             }
00305 
00306             $oIdList->getTagArticleIds( $oTag->sTag, $oLang->getBaseLanguage() );
00307 
00308             //page number
00309             $iPage = $this->_findActPageNumber( $oLocatorTarget->getActPage(), $oIdList, $oCurrArticle );
00310 
00311             // setting product position in list, amount of articles etc
00312             $oTag->iCntOfProd  = $oIdList->count();
00313             $oTag->iProductPos = $this->_getProductPos( $oCurrArticle, $oIdList, $oLocatorTarget );
00314 
00315             if ( oxRegistry::getUtils()->seoIsActive() ) {
00316                 $oTag->toListLink  = oxRegistry::get("oxSeoEncoderTag")->getTagPageUrl( $oTag->sTag, $iPage );
00317             } else {
00318                 $sPageNr = $this->_getPageNumber( $iPage );
00319                 $oTag->toListLink  = $this->_makeLink( $oTag->link, $sPageNr );
00320             }
00321 
00322             $sAddSearch = '';
00323             // setting parameters when seo is Off
00324             if ( !$myUtils->seoIsActive() ) {
00325                 $sAddSearch  = 'searchtag='.rawurlencode( oxConfig::getParameter( 'searchtag', 1 ) );
00326                 $sAddSearch .= '&amp;listtype=tag';
00327             }
00328 
00329             $oTag->nextProductLink = $this->_oNextProduct?$this->_makeLink( $this->_oNextProduct->getLink(), $sAddSearch ):null;
00330             $oTag->prevProductLink = $this->_oBackProduct?$this->_makeLink( $this->_oBackProduct->getLink(), $sAddSearch ):null;
00331             $oStr = getStr();
00332             $oLocatorTarget->setSearchTitle( $oLang->translateString('TAGS').' / '.$oStr->htmlspecialchars( $oStr->ucfirst( $oTag->sTag ) ) );
00333             $oLocatorTarget->setActiveCategory( $oTag );
00334         }
00335     }
00336 
00348     protected function _setRecommlistLocatorData( $oLocatorTarget, $oCurrArticle )
00349     {
00350         if ( ( $oRecommList = $oLocatorTarget->getActiveRecommList() ) ) {
00351 
00352             // loading data for article navigation
00353             $oIdList = oxNew( 'oxarticlelist' );
00354             $oIdList->loadRecommArticleIds( $oRecommList->getId(), null );
00355 
00356             //page number
00357             $iPage = $this->_findActPageNumber( $oLocatorTarget->getActPage(), $oIdList, $oCurrArticle );
00358 
00359             $sSearchRecomm = oxConfig::getParameter( 'searchrecomm', true );
00360 
00361             if ( $sSearchRecomm !== null ) {
00362                 $sSearchFormRecomm = oxConfig::getParameter( 'searchrecomm' );
00363                 $sSearchLinkRecomm = rawurlencode( $sSearchRecomm );
00364                 $sAddSearch = 'searchrecomm='.$sSearchLinkRecomm;
00365             }
00366 
00367             // setting product position in list, amount of articles etc
00368             $oRecommList->iCntOfProd  = $oIdList->count();
00369             $oRecommList->iProductPos = $this->_getProductPos( $oCurrArticle, $oIdList, $oLocatorTarget );
00370             $blSeo = oxRegistry::getUtils()->seoIsActive();
00371 
00372             if ( $blSeo && $iPage ) {
00373                 $oRecommList->toListLink = oxRegistry::get("oxSeoEncoderRecomm")->getRecommPageUrl( $oRecommList, $iPage );
00374             } else {
00375                 $oRecommList->toListLink  = $this->_makeLink( $oRecommList->getLink(), $this->_getPageNumber( $iPage ) );
00376             }
00377             $oRecommList->toListLink  = $this->_makeLink( $oRecommList->toListLink, $sAddSearch );
00378 
00379             $sAdd = '';
00380             if ( !$blSeo ) {
00381                 $sAdd = 'recommid='.$oRecommList->getId().'&amp;listtype=recommlist'.($sAddSearch?'&amp;':'');
00382             }
00383             $sAdd .= $sAddSearch;
00384             $oRecommList->nextProductLink = $this->_oNextProduct?$this->_makeLink( $this->_oNextProduct->getLink(), $sAdd ):null;
00385             $oRecommList->prevProductLink = $this->_oBackProduct?$this->_makeLink( $this->_oBackProduct->getLink(), $sAdd ):null;
00386 
00387             $oLang  = oxRegistry::getLang();
00388             $sTitle = $oLang->translateString('RECOMMLIST');
00389             if ( $sSearchRecomm !== null ) {
00390                 $sTitle .= " / ".$oLang->translateString('RECOMMLIST_SEARCH').' "'.$sSearchFormRecomm.'"';
00391             }
00392             $oLocatorTarget->setSearchTitle( $sTitle );
00393             $oLocatorTarget->setActiveCategory( $oRecommList );
00394         }
00395     }
00396 
00406     protected function _loadIdsInList( $oCategory, $oCurrArticle, $sOrderBy = null )
00407     {
00408         $oIdList = oxNew( 'oxarticlelist' );
00409         $oIdList->setCustomSorting( $sOrderBy ) ;
00410 
00411         // additionally check if this category is loaded and is price category ?
00412         if ( $oCategory->isPriceCategory() ) {
00413             $oIdList->loadPriceIds( $oCategory->oxcategories__oxpricefrom->value, $oCategory->oxcategories__oxpriceto->value );
00414         } else {
00415             $sActCat = $oCategory->getId();
00416             $oIdList->loadCategoryIDs( $sActCat, oxSession::getVar( 'session_attrfilter' ) );
00417             // if not found - reloading with empty filter
00418             if ( !isset( $oIdList[$oCurrArticle->getId()] ) ) {
00419                 $oIdList->loadCategoryIDs( $sActCat, null );
00420             }
00421         }
00422 
00423         return $oIdList;
00424     }
00425 
00434     protected function _makeLink( $sLink, $sParams )
00435     {
00436         if ( $sParams ) {
00437             $sLink .= ( ( strpos( $sLink, '?' ) !== false ) ? '&amp;' : '?' ) . $sParams;
00438         }
00439         return $sLink;
00440     }
00441 
00452     protected function _findActPageNumber( $iPageNr, $oIdList = null, $oArticle = null )
00453     {
00454         //page number
00455         $iPageNr = (int) $iPageNr;
00456 
00457         // maybe there is no page number passed, but we still can find the position in id's list
00458         if ( !$iPageNr && $oIdList && $oArticle ) {
00459             $iNrofCatArticles = (int) $this->getConfig()->getConfigParam( 'iNrofCatArticles' );
00460             $iNrofCatArticles = $iNrofCatArticles?$iNrofCatArticles:1;
00461 
00462             $sArticleId = $oArticle->oxarticles__oxparentid->value ? $oArticle->oxarticles__oxparentid->value : $oArticle->getId();
00463             $iPos = array_search( $sArticleId, $oIdList->arrayKeys() );
00464             $iPageNr = floor( $iPos / $iNrofCatArticles );
00465         }
00466 
00467         return $iPageNr;
00468     }
00469 
00477     protected function _getPageNumber( $iPageNr )
00478     {
00479         //page number
00480         $iPageNr = (int) $iPageNr;
00481         return ( ($iPageNr > 0 )?"pgNr=$iPageNr":'' );
00482     }
00483 
00493     protected function _getProductPos( $oArticle, $oIdList, $oLocatorTarget )
00494     {
00495         $iCnt = 1;
00496         $iPos = 0;
00497 
00498         // variant handling
00499         $sOxid = $oArticle->oxarticles__oxparentid->value?$oArticle->oxarticles__oxparentid->value:$oArticle->getId();
00500         if ( $oIdList->count() && isset( $oIdList[$sOxid] ) ) {
00501 
00502             $aIds = $oIdList->arrayKeys();
00503             $iPos = array_search( $sOxid, $aIds );
00504 
00505             if ( array_key_exists( $iPos-1, $aIds ) ) {
00506                 $oBackProduct = oxNew( 'oxarticle' );
00507                 $oBackProduct->modifyCacheKey('_locator');
00508                 $oBackProduct->setNoVariantLoading( true );
00509                 if ( $oBackProduct->load( $aIds[$iPos-1] ) ) {
00510                     $oBackProduct->setLinkType( $oLocatorTarget->getLinkType() );
00511                     $this->_oBackProduct = $oBackProduct;
00512                 }
00513             }
00514 
00515             if ( array_key_exists( $iPos+1, $aIds ) ) {
00516                 $oNextProduct = oxNew( 'oxarticle' );
00517                 $oNextProduct->modifyCacheKey('_locator');
00518                 $oNextProduct->setNoVariantLoading( true );
00519                 if ( $oNextProduct->load( $aIds[$iPos+1] ) ) {
00520                     $oNextProduct->setLinkType( $oLocatorTarget->getLinkType() );
00521                     $this->_oNextProduct = $oNextProduct;
00522                 }
00523             }
00524             return $iPos+1;
00525         }
00526         return 0;
00527     }
00528 
00534     public function getErrorMessage()
00535     {
00536         return $this->_sErrorMessage;
00537     }
00538 }