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