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     }
00155 
00164     protected function _setManufacturerLocatorData( $oLocatorTarget, $oCurrArticle )
00165     {
00166         if ( ( $oManufacturer = $oLocatorTarget->getActManufacturer() ) ) {
00167             $sManufacturerId = $oManufacturer->getId();
00168             $myUtils = oxRegistry::getUtils();
00169 
00170             $blSeo = $myUtils->seoIsActive();
00171 
00172             // loading data for article navigation
00173             $oIdList = oxNew( "oxarticlelist" );
00174             if ( $oLocatorTarget->showSorting() ) {
00175                 $oIdList->setCustomSorting( $oLocatorTarget->getSortingSql( $oLocatorTarget->getSortIdent() ) );
00176             }
00177             $oIdList->loadManufacturerIds( $sManufacturerId );
00178 
00179             //page number
00180             $iPage = $this->_findActPageNumber( $oLocatorTarget->getActPage(), $oIdList, $oCurrArticle );
00181 
00182             $sAdd = null;
00183             if ( !$blSeo ) {
00184                 $sAdd = 'listtype=manufacturer&amp;mnid='.$sManufacturerId;
00185             }
00186 
00187             // setting product position in list, amount of articles etc
00188             $oManufacturer->iCntOfProd  = $oIdList->count();
00189             $oManufacturer->iProductPos = $this->_getProductPos( $oCurrArticle, $oIdList, $oLocatorTarget );
00190 
00191             if ( $blSeo && $iPage ) {
00192                 $oManufacturer->toListLink = oxRegistry::get("oxSeoEncoderManufacturer")->getManufacturerPageUrl( $oManufacturer, $iPage );
00193             } else {
00194                 $oManufacturer->toListLink  = $this->_makeLink( $oManufacturer->getLink(), $this->_getPageNumber( $iPage ) );
00195             }
00196 
00197             $oManufacturer->nextProductLink = $this->_oNextProduct?$this->_makeLink( $this->_oNextProduct->getLink(), $sAdd ):null;
00198             $oManufacturer->prevProductLink = $this->_oBackProduct?$this->_makeLink( $this->_oBackProduct->getLink(), $sAdd ):null;
00199 
00200             // active Manufacturer
00201             $oLocatorTarget->setActiveCategory( $oManufacturer );
00202 
00203             // Manufacturer path
00204             if ( ( $oManufacturerTree = $oLocatorTarget->getManufacturerTree() ) ) {
00205                 $oLocatorTarget->setCatTreePath( $oManufacturerTree->getPath() );
00206             }
00207         }
00208     }
00209 
00218     protected function _setSearchLocatorData( $oLocatorTarget, $oCurrArticle )
00219     {
00220         if ( ( $oSearchCat = $oLocatorTarget->getActSearch() ) ) {
00221 
00222             // #1834/1184M - specialchar search
00223             $sSearchParam = oxConfig::getParameter( 'searchparam', true );
00224             $sSearchFormParam = oxConfig::getParameter( 'searchparam' );
00225             $sSearchLinkParam = rawurlencode( $sSearchParam );
00226 
00227             $sSearchCat = oxConfig::getParameter( 'searchcnid' );
00228             $sSearchCat = $sSearchCat?rawurldecode( $sSearchCat ):$sSearchCat;
00229 
00230             $sSearchVendor = oxConfig::getParameter( 'searchvendor' );
00231             $sSearchVendor = $sSearchVendor?rawurldecode( $sSearchVendor ):$sSearchVendor;
00232 
00233             $sSearchManufacturer = oxConfig::getParameter( 'searchmanufacturer' );
00234             $sSearchManufacturer = $sSearchManufacturer?rawurldecode( $sSearchManufacturer ):$sSearchManufacturer;
00235 
00236             // loading data for article navigation
00237             $oIdList = oxNew( 'oxarticlelist' );
00238             if ( $oLocatorTarget->showSorting() ) {
00239                 $oIdList->setCustomSorting( $oLocatorTarget->getSortingSql( $oLocatorTarget->getSortIdent() ) );
00240             }
00241             $oIdList->loadSearchIds( $sSearchParam, $sSearchCat, $sSearchVendor, $sSearchManufacturer );
00242 
00243             //page number
00244             $iPage = $this->_findActPageNumber( $oLocatorTarget->getActPage(), $oIdList, $oCurrArticle );
00245 
00246             $sAddSearch = "searchparam={$sSearchLinkParam}";
00247             $sAddSearch .= '&amp;listtype=search';
00248 
00249             if ( $sSearchCat !== null ) {
00250                 $sAddSearch .= "&amp;searchcnid={$sSearchCat}";
00251             }
00252 
00253             if ( $sSearchVendor !== null ) {
00254                 $sAddSearch .= "&amp;searchvendor={$sSearchVendor}";
00255             }
00256 
00257             if ( $sSearchManufacturer !== null ) {
00258                 $sAddSearch .= "&amp;searchmanufacturer={$sSearchManufacturer}";
00259             }
00260 
00261             // setting product position in list, amount of articles etc
00262             $oSearchCat->iCntOfProd  = $oIdList->count();
00263             $oSearchCat->iProductPos = $this->_getProductPos( $oCurrArticle, $oIdList, $oLocatorTarget );
00264 
00265             $sPageNr = $this->_getPageNumber( $iPage );
00266             $oSearchCat->toListLink  = $this->_makeLink( $oSearchCat->link, $sPageNr.($sPageNr?'&amp;':'').$sAddSearch );
00267             $oSearchCat->nextProductLink = $this->_oNextProduct?$this->_makeLink( $this->_oNextProduct->getLink(), $sAddSearch ):null;
00268             $oSearchCat->prevProductLink = $this->_oBackProduct?$this->_makeLink( $this->_oBackProduct->getLink(), $sAddSearch ):null;
00269 
00270             $sFormat = oxRegistry::getLang()->translateString( 'SEARCH_RESULT' );
00271             $oLocatorTarget->setSearchTitle( sprintf( $sFormat, $sSearchFormParam ) );
00272             $oLocatorTarget->setActiveCategory( $oSearchCat );
00273         }
00274     }
00275 
00284     protected function _setTagLocatorData( $oLocatorTarget, $oCurrArticle )
00285     {
00286         if ( ( $oTag = $oLocatorTarget->getActTag() ) ) {
00287 
00288             $myUtils = oxRegistry::getUtils();
00289 
00290             // loading data for article navigation
00291             $oIdList = oxNew( 'oxarticlelist' );
00292             $oLang = oxRegistry::getLang();
00293 
00294             if ( $oLocatorTarget->showSorting() ) {
00295                 $oIdList->setCustomSorting( $oLocatorTarget->getSortingSql( $oLocatorTarget->getSortIdent() ) );
00296             }
00297 
00298             $oIdList->getTagArticleIds( $oTag->sTag, $oLang->getBaseLanguage() );
00299 
00300             //page number
00301             $iPage = $this->_findActPageNumber( $oLocatorTarget->getActPage(), $oIdList, $oCurrArticle );
00302 
00303             // setting product position in list, amount of articles etc
00304             $oTag->iCntOfProd  = $oIdList->count();
00305             $oTag->iProductPos = $this->_getProductPos( $oCurrArticle, $oIdList, $oLocatorTarget );
00306 
00307             if ( oxRegistry::getUtils()->seoIsActive() ) {
00308                 $oTag->toListLink  = oxRegistry::get("oxSeoEncoderTag")->getTagPageUrl( $oTag->sTag, $iPage );
00309             } else {
00310                 $sPageNr = $this->_getPageNumber( $iPage );
00311                 $oTag->toListLink  = $this->_makeLink( $oTag->link, $sPageNr );
00312             }
00313 
00314             $sAddSearch = '';
00315             // setting parameters when seo is Off
00316             if ( !$myUtils->seoIsActive() ) {
00317                 $sAddSearch  = 'searchtag='.rawurlencode( oxRegistry::getConfig()->getRequestParameter( 'searchtag', true ) );
00318                 $sAddSearch .= '&amp;listtype=tag';
00319             }
00320 
00321             $oTag->nextProductLink = $this->_oNextProduct?$this->_makeLink( $this->_oNextProduct->getLink(), $sAddSearch ):null;
00322             $oTag->prevProductLink = $this->_oBackProduct?$this->_makeLink( $this->_oBackProduct->getLink(), $sAddSearch ):null;
00323             $oStr = getStr();
00324             $oLocatorTarget->setSearchTitle( $oLang->translateString('TAGS').' / '.$oStr->htmlspecialchars( $oStr->ucfirst( $oTag->sTag ) ) );
00325             $oLocatorTarget->setActiveCategory( $oTag );
00326         }
00327     }
00328 
00340     protected function _setRecommlistLocatorData( $oLocatorTarget, $oCurrArticle )
00341     {
00342         if ( ( $oRecommList = $oLocatorTarget->getActiveRecommList() ) ) {
00343 
00344             // loading data for article navigation
00345             $oIdList = oxNew( 'oxarticlelist' );
00346             $oIdList->loadRecommArticleIds( $oRecommList->getId(), null );
00347 
00348             //page number
00349             $iPage = $this->_findActPageNumber( $oLocatorTarget->getActPage(), $oIdList, $oCurrArticle );
00350 
00351             $sSearchRecomm = oxConfig::getParameter( 'searchrecomm', true );
00352 
00353             if ( $sSearchRecomm !== null ) {
00354                 $sSearchFormRecomm = oxConfig::getParameter( 'searchrecomm' );
00355                 $sSearchLinkRecomm = rawurlencode( $sSearchRecomm );
00356                 $sAddSearch = 'searchrecomm='.$sSearchLinkRecomm;
00357             }
00358 
00359             // setting product position in list, amount of articles etc
00360             $oRecommList->iCntOfProd  = $oIdList->count();
00361             $oRecommList->iProductPos = $this->_getProductPos( $oCurrArticle, $oIdList, $oLocatorTarget );
00362             $blSeo = oxRegistry::getUtils()->seoIsActive();
00363 
00364             if ( $blSeo && $iPage ) {
00365                 $oRecommList->toListLink = oxRegistry::get("oxSeoEncoderRecomm")->getRecommPageUrl( $oRecommList, $iPage );
00366             } else {
00367                 $oRecommList->toListLink  = $this->_makeLink( $oRecommList->getLink(), $this->_getPageNumber( $iPage ) );
00368             }
00369             $oRecommList->toListLink  = $this->_makeLink( $oRecommList->toListLink, $sAddSearch );
00370 
00371             $sAdd = '';
00372             if ( !$blSeo ) {
00373                 $sAdd = 'recommid='.$oRecommList->getId().'&amp;listtype=recommlist'.($sAddSearch?'&amp;':'');
00374             }
00375             $sAdd .= $sAddSearch;
00376             $oRecommList->nextProductLink = $this->_oNextProduct?$this->_makeLink( $this->_oNextProduct->getLink(), $sAdd ):null;
00377             $oRecommList->prevProductLink = $this->_oBackProduct?$this->_makeLink( $this->_oBackProduct->getLink(), $sAdd ):null;
00378 
00379             $oLang  = oxRegistry::getLang();
00380             $sTitle = $oLang->translateString('RECOMMLIST');
00381             if ( $sSearchRecomm !== null ) {
00382                 $sTitle .= " / ".$oLang->translateString('RECOMMLIST_SEARCH').' "'.$sSearchFormRecomm.'"';
00383             }
00384             $oLocatorTarget->setSearchTitle( $sTitle );
00385             $oLocatorTarget->setActiveCategory( $oRecommList );
00386         }
00387     }
00388 
00398     protected function _loadIdsInList( $oCategory, $oCurrArticle, $sOrderBy = null )
00399     {
00400         $oIdList = oxNew( 'oxarticlelist' );
00401         $oIdList->setCustomSorting( $sOrderBy ) ;
00402 
00403         // additionally check if this category is loaded and is price category ?
00404         if ( $oCategory->isPriceCategory() ) {
00405             $oIdList->loadPriceIds( $oCategory->oxcategories__oxpricefrom->value, $oCategory->oxcategories__oxpriceto->value );
00406         } else {
00407             $sActCat = $oCategory->getId();
00408             $oIdList->loadCategoryIDs( $sActCat, oxSession::getVar( 'session_attrfilter' ) );
00409             // if not found - reloading with empty filter
00410             if ( !isset( $oIdList[$oCurrArticle->getId()] ) ) {
00411                 $oIdList->loadCategoryIDs( $sActCat, null );
00412             }
00413         }
00414 
00415         return $oIdList;
00416     }
00417 
00426     protected function _makeLink( $sLink, $sParams )
00427     {
00428         if ( $sParams ) {
00429             $sLink .= ( ( strpos( $sLink, '?' ) !== false ) ? '&amp;' : '?' ) . $sParams;
00430         }
00431         return $sLink;
00432     }
00433 
00444     protected function _findActPageNumber( $iPageNr, $oIdList = null, $oArticle = null )
00445     {
00446         //page number
00447         $iPageNr = (int) $iPageNr;
00448 
00449         // maybe there is no page number passed, but we still can find the position in id's list
00450         if ( !$iPageNr && $oIdList && $oArticle ) {
00451             $iNrofCatArticles = (int) $this->getConfig()->getConfigParam( 'iNrofCatArticles' );
00452             $iNrofCatArticles = $iNrofCatArticles?$iNrofCatArticles:1;
00453 
00454             $sArticleId = $oArticle->oxarticles__oxparentid->value ? $oArticle->oxarticles__oxparentid->value : $oArticle->getId();
00455             $iPos = array_search( $sArticleId, $oIdList->arrayKeys() );
00456             $iPageNr = floor( $iPos / $iNrofCatArticles );
00457         }
00458 
00459         return $iPageNr;
00460     }
00461 
00469     protected function _getPageNumber( $iPageNr )
00470     {
00471         //page number
00472         $iPageNr = (int) $iPageNr;
00473         return ( ($iPageNr > 0 )?"pgNr=$iPageNr":'' );
00474     }
00475 
00485     protected function _getProductPos( $oArticle, $oIdList, $oLocatorTarget )
00486     {
00487         $iCnt = 1;
00488         $iPos = 0;
00489 
00490         // variant handling
00491         $sOxid = $oArticle->oxarticles__oxparentid->value?$oArticle->oxarticles__oxparentid->value:$oArticle->getId();
00492         if ( $oIdList->count() && isset( $oIdList[$sOxid] ) ) {
00493 
00494             $aIds = $oIdList->arrayKeys();
00495             $iPos = array_search( $sOxid, $aIds );
00496 
00497             if ( array_key_exists( $iPos-1, $aIds ) ) {
00498                 $oBackProduct = oxNew( 'oxarticle' );
00499                 $oBackProduct->modifyCacheKey('_locator');
00500                 $oBackProduct->setNoVariantLoading( true );
00501                 if ( $oBackProduct->load( $aIds[$iPos-1] ) ) {
00502                     $oBackProduct->setLinkType( $oLocatorTarget->getLinkType() );
00503                     $this->_oBackProduct = $oBackProduct;
00504                 }
00505             }
00506 
00507             if ( array_key_exists( $iPos+1, $aIds ) ) {
00508                 $oNextProduct = oxNew( 'oxarticle' );
00509                 $oNextProduct->modifyCacheKey('_locator');
00510                 $oNextProduct->setNoVariantLoading( true );
00511                 if ( $oNextProduct->load( $aIds[$iPos+1] ) ) {
00512                     $oNextProduct->setLinkType( $oLocatorTarget->getLinkType() );
00513                     $this->_oNextProduct = $oNextProduct;
00514                 }
00515             }
00516             return $iPos+1;
00517         }
00518         return 0;
00519     }
00520 
00526     public function getErrorMessage()
00527     {
00528         return $this->_sErrorMessage;
00529     }
00530 }