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 
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   = oxUtils::getInstance();
00124 
00125             $blSeo = $myUtils->seoIsActive();
00126 
00127             // loading data for article navigation
00128             $oIdList = oxNew( "oxarticlelist" );
00129             if ( $oLocatorTarget->showSorting() ) {
00130                 $oLocatorTarget->prepareSortColumns();
00131                 $oIdList->setCustomSorting( $oLocatorTarget->getSortingSql( "$sVendorId:vendor" ) );
00132             }
00133             $oIdList->loadVendorIds( $sVendorId );
00134 
00135             //page number
00136             $iPage = $this->_findActPageNumber( $oLocatorTarget->getActPage(), $oIdList, $oCurrArticle );
00137 
00138             $sAdd = null;
00139             if ( !$blSeo ) {
00140                 $sAdd = 'listtype=vendor&amp;cnid=v_'.$sVendorId;
00141             }
00142 
00143             // setting product position in list, amount of articles etc
00144             $oVendor->iCntOfProd  = $oIdList->count();
00145             $oVendor->iProductPos = $this->_getProductPos( $oCurrArticle, $oIdList, $oLocatorTarget );
00146 
00147             if ( $blSeo && $iPage ) {
00148                 $oVendor->toListLink = oxSeoEncoderVendor::getInstance()->getVendorPageUrl( $oVendor, $iPage );
00149             } else {
00150                 $oVendor->toListLink  = $this->_makeLink( $oVendor->getLink(), $this->_getPageNumber( $iPage ) );
00151             }
00152 
00153             $oVendor->nextProductLink = $this->_oNextProduct?$this->_makeLink( $this->_oNextProduct->getLink(), $sAdd ):null;
00154             $oVendor->prevProductLink = $this->_oBackProduct?$this->_makeLink( $this->_oBackProduct->getLink(), $sAdd ):null;
00155 
00156             // active vendor
00157             $oLocatorTarget->setActiveCategory( $oVendor );
00158 
00159             // vendor path
00160             if ( ( $oVendorTree = $oLocatorTarget->getVendorTree() ) ) {
00161                 $oLocatorTarget->setCatTreePath( $oVendorTree->getPath() );
00162             }
00163         }
00164     }
00165 
00174     protected function _setManufacturerLocatorData( $oLocatorTarget, $oCurrArticle )
00175     {
00176         if ( ( $oManufacturer = $oLocatorTarget->getActManufacturer() ) ) {
00177             $sManufacturerId = $oManufacturer->getId();
00178             $myUtils = oxUtils::getInstance();
00179 
00180             $blSeo = $myUtils->seoIsActive();
00181 
00182             // loading data for article navigation
00183             $oIdList = oxNew( "oxarticlelist" );
00184             if ( $oLocatorTarget->showSorting() ) {
00185                 $oLocatorTarget->prepareSortColumns();
00186                 $oIdList->setCustomSorting( $oLocatorTarget->getSortingSql( "$sManufacturerId:manufacturer" ) );
00187             }
00188             $oIdList->loadManufacturerIds( $sManufacturerId );
00189 
00190             //page number
00191             $iPage = $this->_findActPageNumber( $oLocatorTarget->getActPage(), $oIdList, $oCurrArticle );
00192 
00193             $sAdd = null;
00194             if ( !$blSeo ) {
00195                 $sAdd = 'listtype=manufacturer&amp;mnid='.$sManufacturerId;
00196             }
00197 
00198             // setting product position in list, amount of articles etc
00199             $oManufacturer->iCntOfProd  = $oIdList->count();
00200             $oManufacturer->iProductPos = $this->_getProductPos( $oCurrArticle, $oIdList, $oLocatorTarget );
00201 
00202             if ( $blSeo && $iPage ) {
00203                 $oManufacturer->toListLink = oxSeoEncoderManufacturer::getInstance()->getManufacturerPageUrl( $oManufacturer, $iPage );
00204             } else {
00205                 $oManufacturer->toListLink  = $this->_makeLink( $oManufacturer->getLink(), $this->_getPageNumber( $iPage ) );
00206             }
00207 
00208             $oManufacturer->nextProductLink = $this->_oNextProduct?$this->_makeLink( $this->_oNextProduct->getLink(), $sAdd ):null;
00209             $oManufacturer->prevProductLink = $this->_oBackProduct?$this->_makeLink( $this->_oBackProduct->getLink(), $sAdd ):null;
00210 
00211             // active Manufacturer
00212             $oLocatorTarget->setActiveCategory( $oManufacturer );
00213 
00214             // Manufacturer path
00215             if ( ( $oManufacturerTree = $oLocatorTarget->getManufacturerTree() ) ) {
00216                 $oLocatorTarget->setCatTreePath( $oManufacturerTree->getPath() );
00217             }
00218         }
00219     }
00220 
00229     protected function _setSearchLocatorData( $oLocatorTarget, $oCurrArticle )
00230     {
00231         if ( ( $oSearchCat = $oLocatorTarget->getActSearch() ) ) {
00232 
00233             // #1834/1184M - specialchar search
00234             $sSearchParam = oxConfig::getParameter( 'searchparam', true );
00235             $sSearchFormParam = oxConfig::getParameter( 'searchparam' );
00236             $sSearchLinkParam = rawurlencode( $sSearchParam );
00237 
00238             $sSearchCat = oxConfig::getParameter( 'searchcnid' );
00239             $sSearchCat = $sSearchCat?rawurldecode( $sSearchCat ):$sSearchCat;
00240 
00241             $sSearchVendor = oxConfig::getParameter( 'searchvendor' );
00242             $sSearchVendor = $sSearchVendor?rawurldecode( $sSearchVendor ):$sSearchVendor;
00243 
00244             $sSearchManufacturer = oxConfig::getParameter( 'searchmanufacturer' );
00245             $sSearchManufacturer = $sSearchManufacturer?rawurldecode( $sSearchManufacturer ):$sSearchManufacturer;
00246 
00247             // loading data for article navigation
00248             $oIdList = oxNew( 'oxarticlelist' );
00249             if ( $oLocatorTarget->showSorting() ) {
00250                 $oLocatorTarget->prepareSortColumns();
00251                 $oIdList->setCustomSorting( $oLocatorTarget->getSortingSql( 'oxsearch' ) );
00252             }
00253             $oIdList->loadSearchIds( $sSearchParam, $sSearchCat, $sSearchVendor, $sSearchManufacturer );
00254 
00255             //page number
00256             $iPage = $this->_findActPageNumber( $oLocatorTarget->getActPage(), $oIdList, $oCurrArticle );
00257 
00258             $sAddSearch = "searchparam={$sSearchLinkParam}";
00259             $sAddSearch .= '&amp;listtype=search';
00260 
00261             if ( $sSearchCat !== null ) {
00262                 $sAddSearch .= "&amp;searchcnid={$sSearchCat}";
00263             }
00264 
00265             if ( $sSearchVendor !== null ) {
00266                 $sAddSearch .= "&amp;searchvendor={$sSearchVendor}";
00267             }
00268 
00269             if ( $sSearchManufacturer !== null ) {
00270                 $sAddSearch .= "&amp;searchmanufacturer={$sSearchManufacturer}";
00271             }
00272 
00273             // setting product position in list, amount of articles etc
00274             $oSearchCat->iCntOfProd  = $oIdList->count();
00275             $oSearchCat->iProductPos = $this->_getProductPos( $oCurrArticle, $oIdList, $oLocatorTarget );
00276 
00277             $sPageNr = $this->_getPageNumber( $iPage );
00278             $oSearchCat->toListLink  = $this->_makeLink( $oSearchCat->link, $sPageNr.($sPageNr?'&amp;':'').$sAddSearch );
00279             $oSearchCat->nextProductLink = $this->_oNextProduct?$this->_makeLink( $this->_oNextProduct->getLink(), $sAddSearch ):null;
00280             $oSearchCat->prevProductLink = $this->_oBackProduct?$this->_makeLink( $this->_oBackProduct->getLink(), $sAddSearch ):null;
00281 
00282             $sFormat = oxLang::getInstance()->translateString( 'searchResult' );
00283             $oLocatorTarget->setSearchTitle( sprintf( $sFormat, $sSearchFormParam ) );
00284             $oLocatorTarget->setActiveCategory( $oSearchCat );
00285         }
00286     }
00287 
00296     protected function _setTagLocatorData( $oLocatorTarget, $oCurrArticle )
00297     {
00298         if ( ( $oTag = $oLocatorTarget->getActTag() ) ) {
00299 
00300             $myUtils = oxUtils::getInstance();
00301 
00302             // loading data for article navigation
00303             $oIdList = oxNew( 'oxarticlelist' );
00304             $oLang = oxLang::getInstance();
00305 
00306             if ( $oLocatorTarget->showSorting() ) {
00307                 $oLocatorTarget->prepareSortColumns();
00308                 $oIdList->setCustomSorting( $oLocatorTarget->getSortingSql( 'oxtags' ) );
00309             }
00310 
00311             $oIdList->getTagArticleIds( $oTag->sTag, $oLang->getBaseLanguage() );
00312 
00313             //page number
00314             $iPage = $this->_findActPageNumber( $oLocatorTarget->getActPage(), $oIdList, $oCurrArticle );
00315 
00316             // setting product position in list, amount of articles etc
00317             $oTag->iCntOfProd  = $oIdList->count();
00318             $oTag->iProductPos = $this->_getProductPos( $oCurrArticle, $oIdList, $oLocatorTarget );
00319 
00320             $sPageNr = $this->_getPageNumber( $iPage );
00321             $oTag->toListLink  = $this->_makeLink( $oTag->link, $sPageNr );
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 = oxUtils::getInstance()->seoIsActive();
00372 
00373             if ( $blSeo && $iPage ) {
00374                 $oRecommList->toListLink = oxSeoEncoderRecomm::getInstance()->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  = oxLang::getInstance();
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 }