oxrssfeed.php

Go to the documentation of this file.
00001 <?php
00002 
00008 class oxRssFeed extends oxSuperCfg
00009 {
00013     const CACHE_TTL = 10800;
00014 
00018     const RSS_TOPSHOP    = 'RSS_TopShop';
00019     const RSS_NEWARTS    = 'RSS_NewArts';
00020     const RSS_CATARTS    = 'RSS_CatArts';
00021     const RSS_ARTRECOMMLISTS = 'RSS_ARTRECOMMLISTS';
00022     const RSS_RECOMMLISTARTS = 'RSS_RECOMMLISTARTS';
00023     const RSS_BARGAIN    = 'RSS_Bargain';
00024 
00031     protected $_aChannel = array();
00032 
00039     public function getChannel()
00040     {
00041         return $this->_aChannel;
00042     }
00043 
00050     protected function _loadBaseChannel()
00051     {
00052         $myUtils = oxUtils::getInstance();
00053         $oShop = $this->getConfig()->getActiveShop();
00054         $this->_aChannel['title'] = $oShop->oxshops__oxname->value;
00055         $this->_aChannel['link']  = $myUtils->prepareUrlForNoSession($this->getConfig()->getShopHomeURL());
00056         $this->_aChannel['description'] = '';
00057         $oLang = oxLang::getInstance();
00058         $aLangIds = $oLang->getLanguageIds();
00059         $this->_aChannel['language']  = $aLangIds[$oLang->getBaseLanguage()];
00060         $this->_aChannel['copyright'] = $oShop->oxshops__oxname->value;
00061         $this->_aChannel['selflink'] = '';
00062         if ( $myUtils->isValidEmail( $oShop->oxshops__oxinfoemail->value )) {
00063             $this->_aChannel['managingEditor'] = $oShop->oxshops__oxinfoemail->value;
00064         }
00065         //$this->_aChannel['webMaster']      = '';
00066 
00067         $this->_aChannel['generator']      = 'OXID eShop '.$oShop->oxshops__oxversion->value;
00068         $this->_aChannel['image']['url']   = $this->getConfig()->getImageUrl().'logo.png';
00069 
00070 
00071         $this->_aChannel['image']['title'] = $this->_aChannel['title'];
00072         $this->_aChannel['image']['link']  = $this->_aChannel['link'];
00073     }
00074 
00083     protected function _getCacheId($name)
00084     {
00085         return $name.'_'.$this->getConfig()->getShopId().'_'.oxLang::getInstance()->getBaseLanguage().'_'.(int) oxConfig::getParameter('currency');
00086     }
00087 
00096     protected function _loadFromCache($name)
00097     {
00098         if ($aRes = oxUtils::getInstance()->fromFileCache($this->_getCacheId($name))) {
00099             if ($aRes['timestamp'] > time() - self::CACHE_TTL) {
00100                 return $aRes['content'];
00101             }
00102         }
00103         return false;
00104     }
00105 
00106 
00117     protected function _getLastBuildDate($name, $aData)
00118     {
00119         if ($aData2 = oxUtils::getInstance()->fromFileCache($this->_getCacheId($name))) {
00120             $sLastBuildDate = $aData2['content']['lastBuildDate'];
00121             $aData2['content']['lastBuildDate'] = '';
00122             $aData['lastBuildDate'] = '';
00123             if (!strcmp(serialize($aData), serialize($aData2['content']))) {
00124                 return $sLastBuildDate;
00125             }
00126         }
00127         return date('D, d M Y H:i:s O');
00128     }
00129 
00142     protected function _saveToCache($name, $aContent)
00143     {
00144         $aData = array( 'timestamp' => time(), 'content'   => $aContent );
00145         return oxUtils::getInstance()->toFileCache($this->_getCacheId($name), $aData);
00146     }
00147 
00148 
00157     protected function _getArticleItems(oxArticleList $oList)
00158     {
00159         $myUtils = oxUtils::getInstance();
00160         $aItems = array();
00161         $oLang = oxLang::getInstance();
00162         $oStr  = getStr();
00163 
00164         foreach ($oList as $oArticle) {
00165             $oItem = new oxStdClass();
00166             $oActCur = $this->getConfig()->getActShopCurrencyObject();
00167             $sPrice = '';
00168             if ( $oPrice = $oArticle->getPrice() ) {
00169                 $sPrice =  " " . $oArticle->getPriceFromPrefix().$oLang->formatCurrency( $oPrice->getBruttoPrice(), $oActCur ) . " ". $oActCur->sign;
00170             }
00171             $oItem->title                   = strip_tags($oArticle->oxarticles__oxtitle->value . $sPrice);
00172             $oItem->guid     = $oItem->link = $myUtils->prepareUrlForNoSession($oArticle->getLink());
00173             $oItem->isGuidPermalink         = true;
00174             $oItem->description             = $oArticle->getArticleLongDesc()->value; //oxarticles__oxshortdesc->value;
00175             if (trim(str_replace('&nbsp;', '', (strip_tags($oItem->description)))) == '') {
00176                 $oItem->description             = $oArticle->oxarticles__oxshortdesc->value;
00177             }
00178 
00179             $oItem->description = trim($oItem->description);
00180             if ($sIcon = $oArticle->getIconUrl()) {
00181                 $oItem->description = "<img src='$sIcon' border=0 align='left' hspace=5>".$oItem->description;
00182             }
00183             $oItem->description = $oStr->htmlspecialchars( $oItem->description );
00184 
00185             $aItems[] = $oItem;
00186         }
00187         return $aItems;
00188     }
00189 
00200     protected function _prepareUrl($sUri, $sTitle)
00201     {
00202         $iLang = oxLang::getInstance()->getBaseLanguage();
00203         $sUrl  = $this->_getShopUrl();
00204         $sUrl .= $sUri.'&amp;lang='.$iLang;
00205 
00206         if ( oxUtils::getInstance()->seoIsActive() ) {
00207             $oEncoder = oxSeoEncoder::getInstance();
00208             $sUrl = $oEncoder->getDynamicUrl( $sUrl, "rss/{$sTitle}/", $iLang );
00209         }
00210 
00211         return oxUtils::getInstance()->prepareUrlForNoSession( $sUrl );
00212     }
00213 
00223     protected function _prepareFeedName($sTitle)
00224     {
00225         $oShop = $this->getConfig()->getActiveShop();
00226 
00227         return $oShop->oxshops__oxname->value . "/" . $sTitle;
00228     }
00229 
00236     protected function _getShopUrl()
00237     {
00238         $sUrl = $this->getConfig()->getShopUrl();
00239         if (strpos($sUrl, '?') !== false ) {
00240             if (!preg_match('/[?&](amp;)?$/i', $sUrl)) {
00241                 $sUrl .= '&amp;';
00242             }
00243         } else {
00244             $sUrl .= '?';
00245         }
00246         return $sUrl;
00247     }
00248 
00262     protected function _loadData($sTag, $sTitle, $sDesc, $aItems, $sRssUrl, $sTargetUrl = null)
00263     {
00264         $this->_loadBaseChannel();
00265 
00266         $this->_aChannel['selflink'] = $sRssUrl;
00267 
00268         if ($sTargetUrl) {
00269             $this->_aChannel['link'] = $this->_aChannel['image']['link'] = $sTargetUrl;
00270         }
00271 
00272         $this->_aChannel['image']['title']        = $this->_aChannel['title']       = $sTitle;
00273         $this->_aChannel['image']['description']  = $this->_aChannel['description'] = $sDesc;
00274 
00275         $this->_aChannel['items'] = $aItems;
00276 
00277         if ($sTag) {
00278             $this->_aChannel['lastBuildDate'] = $this->_getLastBuildDate($sTag, $this->_aChannel);
00279             $this->_saveToCache($sTag, $this->_aChannel);
00280         } else {
00281             $this->_aChannel['lastBuildDate'] = date( 'D, d M Y H:i:s O', oxUtilsDate::getInstance()->getTime() );
00282         }
00283     }
00284 
00292     public function getTopInShopTitle()
00293     {
00294         $oLang = oxLang::getInstance();
00295         $iLang = $oLang->getBaseLanguage();
00296         return $this->_prepareFeedName( $oLang->translateString( 'RSS_TOPSHOP_TITLE', $iLang ) );
00297     }
00298 
00306     public function getTopInShopUrl()
00307     {
00308         return $this->_prepareUrl("cl=rss&amp;fnc=topshop", $this->getTopInShopTitle());
00309     }
00310 
00318     public function loadTopInShop()
00319     {
00320         if ( ( $this->_aChannel = $this->_loadFromCache( self::RSS_TOPSHOP ) ) ) {
00321             return;
00322         }
00323 
00324         $oArtList = oxNew( 'oxarticlelist' );
00325         $oArtList->loadTop5Articles();
00326 
00327         $oLang = oxLang::getInstance();
00328         $this->_loadData(
00329             self::RSS_TOPSHOP,
00330             $this->getTopInShopTitle(),
00331             $oLang->translateString( 'RSS_TOPSHOP_DESCRIPTION', $oLang->getBaseLanguage() ),
00332             $this->_getArticleItems($oArtList),
00333             $this->getTopInShopUrl()
00334         );
00335     }
00336 
00337 
00338 
00346     public function getNewestArticlesTitle()
00347     {
00348         $oLang = oxLang::getInstance();
00349         $iLang = $oLang->getBaseLanguage();
00350         return $this->_prepareFeedName( $oLang->translateString( 'RSS_NEWESTARTICLES_TITLE', $iLang ) );
00351     }
00352 
00360     public function getNewestArticlesUrl()
00361     {
00362         return $this->_prepareUrl("cl=rss&amp;fnc=newarts", $this->getNewestArticlesTitle());
00363     }
00364 
00372     public function loadNewestArticles()
00373     {
00374         if ( ( $this->_aChannel = $this->_loadFromCache(self::RSS_NEWARTS ) ) ) {
00375             return;
00376         }
00377         $oArtList = oxNew( 'oxarticlelist' );
00378         $oArtList->loadNewestArticles( $this->getConfig()->getConfigParam( 'iRssItemsCount' ) );
00379 
00380         $oLang = oxLang::getInstance();
00381         $this->_loadData(
00382             self::RSS_NEWARTS,
00383             $this->getNewestArticlesTitle( ),
00384             $oLang->translateString( 'RSS_NEWESTARTICLES_DESCRIPTION', $oLang->getBaseLanguage() ),
00385             $this->_getArticleItems($oArtList),
00386             $this->getNewestArticlesUrl()
00387         );
00388     }
00389 
00390 
00400     public function getCategoryArticlesTitle(oxCategory $oCat)
00401     {
00402         $oLang  = oxLang::getInstance();
00403         $iLang  = $oLang->getBaseLanguage();
00404         $sTitle = $this->_getCatPath($oCat);
00405         return $this->_prepareFeedName( $sTitle . $oLang->translateString( 'RSS_CATEGORYARTICLES_TITLE', $iLang ) );
00406     }
00407 
00415     protected function _getCatPath( $oCat )
00416     {
00417         $sCatPathString = '';
00418         $sSep = '';
00419         while ( $oCat ) {
00420             // prepare oCat title part
00421             $sCatPathString = $oCat->oxcategories__oxtitle->value.$sSep.$sCatPathString ;
00422             $sSep = '/';
00423             // load parent
00424             $oCat = $oCat->getParentCategory();
00425         }
00426         return $sCatPathString;
00427     }
00428 
00438     public function getCategoryArticlesUrl(oxCategory $oCat)
00439     {
00440         $oLang = oxLang::getInstance();
00441         return $this->_prepareUrl("cl=rss&amp;fnc=catarts&amp;cat=".urlencode($oCat->getId()),
00442                 sprintf($oLang->translateString( 'RSS_CATEGORYARTICLES_URL', $oLang->getBaseLanguage() ), $oCat->oxcategories__oxtitle->value));
00443     }
00444 
00454     public function loadCategoryArticles( oxCategory $oCat )
00455     {
00456         $sId = $oCat->getId();
00457         if ( ( $this->_aChannel = $this->_loadFromCache( self::RSS_CATARTS.$sId ) ) ) {
00458             return;
00459         }
00460 
00461         $oArtList = oxNew( 'oxarticlelist' );
00462         $oArtList->setCustomSorting('oc.oxtime desc');
00463         $oArtList->loadCategoryArticles($oCat->getId(), null, $this->getConfig()->getConfigParam( 'iRssItemsCount' ));
00464 
00465         $oLang = oxLang::getInstance();
00466         $this->_loadData(
00467             self::RSS_CATARTS.$sId,
00468             $this->getCategoryArticlesTitle($oCat),
00469             sprintf($oLang->translateString( 'RSS_CATEGORYARTICLES_DESCRIPTION', $oLang->getBaseLanguage() ), $oCat->oxcategories__oxtitle->value),
00470             $this->_getArticleItems($oArtList),
00471             $this->getCategoryArticlesUrl($oCat),
00472             $oCat->getLink()
00473         );
00474     }
00475 
00476 
00489     public function getSearchArticlesTitle($sSearch, $sCatId, $sVendorId, $sManufacturerId)
00490     {
00491         return $this->_prepareFeedName( getStr()->htmlspecialchars($this->_getSearchParamsTranslation('RSS_SEARCHARTICLES_TITLE', $sSearch, $sCatId, $sVendorId, $sManufacturerId)) );
00492     }
00493 
00506     protected function _getSearchParamsUrl($sSearch, $sCatId, $sVendorId, $sManufacturerId)
00507     {
00508         $sParams = "searchparam=".urlencode($sSearch);
00509         if ($sCatId) {
00510             $sParams .= "&amp;searchcnid=".urlencode($sCatId);
00511         }
00512 
00513         if ($sVendorId) {
00514             $sParams .= "&amp;searchvendor=".urlencode($sVendorId);
00515         }
00516 
00517         if ($sManufacturerId) {
00518             $sParams .= "&amp;searchmanufacturer=".urlencode($sManufacturerId);
00519         }
00520 
00521         return $sParams;
00522     }
00523 
00534     protected function _getObjectField($sId, $sObject, $sField)
00535     {
00536         if (!$sId) {
00537             return '';
00538         }
00539         $oObj = oxNew($sObject);
00540         if ($oObj->load($sId)) {
00541             return $oObj->$sField->value;
00542         }
00543         return '';
00544     }
00545 
00559     protected function _getSearchParamsTranslation($sId, $sSearch, $sCatId, $sVendorId, $sManufacturerId)
00560     {
00561         $oLang = oxLang::getInstance();
00562         $sCatTitle = '';
00563         if ($sTitle = $this->_getObjectField($sCatId, 'oxcategory', 'oxcategories__oxtitle')) {
00564             $sCatTitle = sprintf($oLang->translateString( 'RSS_SEARCHARTICLES_TAG_CATEGORY', $oLang->getBaseLanguage() ), $sTitle);
00565         }
00566         $sVendorTitle = '';
00567         if ($sTitle = $this->_getObjectField($sVendorId, 'oxvendor', 'oxvendor__oxtitle')) {
00568             $sVendorTitle = sprintf($oLang->translateString( 'RSS_SEARCHARTICLES_TAG_VENDOR', $oLang->getBaseLanguage() ), $sTitle);
00569         }
00570         $sManufacturerTitle = '';
00571         if ($sTitle = $this->_getObjectField($sManufacturerId, 'oxmanufacturer', 'oxmanufacturers__oxtitle')) {
00572             $sManufacturerTitle = sprintf($oLang->translateString( 'RSS_SEARCHARTICLES_TAG_MANUFACTURER', $oLang->getBaseLanguage() ), $sTitle);
00573         }
00574 
00575         $sRet = sprintf($oLang->translateString( $sId, $oLang->getBaseLanguage() ), $sSearch);
00576 
00577         $sRet = str_replace('<TAG_CATEGORY>', $sCatTitle, $sRet);
00578         $sRet = str_replace('<TAG_VENDOR>', $sVendorTitle, $sRet);
00579         $sRet = str_replace('<TAG_MANUFACTURER>', $sManufacturerTitle, $sRet);
00580 
00581         return $sRet;
00582     }
00583 
00596     public function getSearchArticlesUrl( $sSearch, $sCatId, $sVendorId, $sManufacturerId )
00597     {
00598         $oLang = oxLang::getInstance();
00599         $sUrl = $this->_prepareUrl("cl=rss&amp;fnc=searcharts", $oLang->translateString( 'RSS_SEARCHARTICLES_URL', $oLang->getBaseLanguage()));
00600 
00601         $sJoin = '?';
00602         if (strpos($sUrl, '?') !== false) {
00603             $sJoin = '&amp;';
00604         }
00605         return $sUrl.$sJoin.$this->_getSearchParamsUrl($sSearch, $sCatId, $sVendorId, $sManufacturerId);
00606     }
00607 
00620     public function loadSearchArticles( $sSearch, $sCatId, $sVendorId, $sManufacturerId )
00621     {
00622         // dont use cache for search
00623         //if ($this->_aChannel = $this->_loadFromCache(self::RSS_SEARCHARTS.md5($sSearch.$sCatId.$sVendorId))) {
00624         //    return;
00625         //}
00626 
00627         $oConfig = $this->getConfig();
00628         $oConfig->setConfigParam('iNrofCatArticles', $oConfig->getConfigParam( 'iRssItemsCount' ));
00629 
00630         $oArtList = oxNew( 'oxsearch' )->getSearchArticles($sSearch, $sCatId, $sVendorId, $sManufacturerId, oxNew('oxarticle')->getViewName().'.oxtimestamp desc');
00631 
00632         $this->_loadData(
00633             // dont use cache for search
00634             null,
00635             //self::RSS_SEARCHARTS.md5($sSearch.$sCatId.$sVendorId),
00636             $this->getSearchArticlesTitle($sSearch, $sCatId, $sVendorId, $sManufacturerId),
00637             $this->_getSearchParamsTranslation('RSS_SEARCHARTICLES_DESCRIPTION', getStr()->htmlspecialchars( $sSearch ), $sCatId, $sVendorId, $sManufacturerId),
00638             $this->_getArticleItems($oArtList),
00639             $this->getSearchArticlesUrl($sSearch, $sCatId, $sVendorId, $sManufacturerId),
00640             $this->_getShopUrl()."cl=search&amp;".$this->_getSearchParamsUrl($sSearch, $sCatId, $sVendorId, $sManufacturerId)
00641         );
00642     }
00643 
00651     public function getRecommListsTitle(oxArticle $oArticle)
00652     {
00653         $oLang = oxLang::getInstance();
00654         $iLang = $oLang->getBaseLanguage();
00655         return $this->_prepareFeedName( sprintf($oLang->translateString( 'RSS_ARTRECOMMLISTS_TITLE', $iLang ), $oArticle->oxarticles__oxtitle->value) );
00656     }
00657 
00665     public function getRecommListsUrl(oxArticle $oArticle)
00666     {
00667         $oLang = oxLang::getInstance();
00668         $iLang = $oLang->getBaseLanguage();
00669         return $this->_prepareUrl("cl=rss&amp;fnc=recommlists&amp;anid=".$oArticle->getId(),
00670                 sprintf($oLang->translateString( 'RSS_ARTRECOMMLISTS_URL', $iLang ), $oArticle->oxarticles__oxtitle->value));
00671     }
00672 
00680     protected function _getRecommListItems($oList)
00681     {
00682         $myUtils = oxUtils::getInstance();
00683         $aItems = array();
00684         foreach ($oList as $oRecommList) {
00685             $oItem = new oxStdClass();
00686             $oItem->title                   = $oRecommList->oxrecommlists__oxtitle->value;
00687             $oItem->guid     = $oItem->link = $myUtils->prepareUrlForNoSession($oRecommList->getLink());
00688             $oItem->isGuidPermalink         = true;
00689             $oItem->description             = $oRecommList->oxrecommlists__oxdesc->value;
00690 
00691             $aItems[] = $oItem;
00692         }
00693         return $aItems;
00694     }
00695 
00703     public function loadRecommLists(oxArticle $oArticle)
00704     {
00705         if ( ( $this->_aChannel = $this->_loadFromCache( self::RSS_ARTRECOMMLISTS.$oArticle->getId() ) ) ) {
00706             return;
00707         }
00708 
00709         $oConfig = $this->getConfig();
00710         $oConfig->setConfigParam('iNrofCrossellArticles', $oConfig->getConfigParam( 'iRssItemsCount' ));
00711 
00712         $oList = oxNew( 'oxrecommlist' )->getRecommListsByIds(array($oArticle->getId()));
00713         if ($oList == null) {
00714             $oList = oxNew('oxlist');
00715         }
00716 
00717         $oLang = oxLang::getInstance();
00718         $this->_loadData(
00719             self::RSS_ARTRECOMMLISTS.$oArticle->getId(),
00720             $this->getRecommListsTitle($oArticle),
00721             sprintf($oLang->translateString( 'RSS_ARTRECOMMLISTS_DESCRIPTION', $oLang->getBaseLanguage() ), $oArticle->oxarticles__oxtitle->value),
00722             $this->_getRecommListItems($oList),
00723             $this->getRecommListsUrl($oArticle),
00724             $oArticle->getLink()
00725         );
00726     }
00727 
00735     public function getRecommListArticlesTitle(oxRecommList $oRecommList)
00736     {
00737         $oLang = oxLang::getInstance();
00738         $iLang = $oLang->getBaseLanguage();
00739         return $this->_prepareFeedName( sprintf($oLang->translateString( 'RSS_RECOMMLISTARTICLES_TITLE', $iLang ), $oRecommList->oxrecommlists__oxtitle->value) );
00740     }
00741 
00749     public function getRecommListArticlesUrl(oxRecommList $oRecommList)
00750     {
00751         $oLang = oxLang::getInstance();
00752         $iLang = $oLang->getBaseLanguage();
00753         return $this->_prepareUrl("cl=rss&amp;fnc=recommlistarts&amp;recommid=".$oRecommList->getId(),
00754                 sprintf($oLang->translateString( 'RSS_RECOMMLISTARTICLES_URL', $iLang ), $oRecommList->oxrecommlists__oxtitle->value));
00755     }
00756 
00764     public function loadRecommListArticles(oxRecommList $oRecommList)
00765     {
00766         if ( ( $this->_aChannel = $this->_loadFromCache( self::RSS_RECOMMLISTARTS.$oRecommList->getId() ) ) ) {
00767             return;
00768         }
00769 
00770         $oList = oxNew( 'oxarticlelist' );
00771         $oList->loadRecommArticles( $oRecommList->getId(), ' order by oxobject2list.oxtimestamp desc limit '. $this->getConfig()->getConfigParam( 'iRssItemsCount' ) );
00772 
00773         $oLang = oxLang::getInstance();
00774         $this->_loadData(
00775             self::RSS_RECOMMLISTARTS.$oRecommList->getId(),
00776             $this->getRecommListArticlesTitle($oRecommList),
00777             sprintf($oLang->translateString( 'RSS_RECOMMLISTARTICLES_DESCRIPTION', $oLang->getBaseLanguage() ), $oRecommList->oxrecommlists__oxtitle->value),
00778             $this->_getArticleItems($oList),
00779             $this->getRecommListArticlesUrl($oRecommList),
00780             $oRecommList->getLink()
00781         );
00782     }
00783 
00791     public function getBargainTitle()
00792     {
00793         $oLang = oxLang::getInstance();
00794         $iLang = $oLang->getBaseLanguage();
00795         return $this->_prepareFeedName( $oLang->translateString( 'RSS_BARGAIN_TITLE', $iLang ) );
00796     }
00797 
00805     public function getBargainUrl()
00806     {
00807         return $this->_prepareUrl("cl=rss&amp;fnc=bargain", $this->getBargainTitle());
00808     }
00809 
00817     public function loadBargain()
00818     {
00819         if ( ( $this->_aChannel = $this->_loadFromCache( self::RSS_BARGAIN ) ) ) {
00820             return;
00821         }
00822 
00823         $oArtList = oxNew( 'oxarticlelist' );
00824         $oArtList->loadAktionArticles( 'OXBARGAIN' );
00825 
00826         $oLang = oxLang::getInstance();
00827         $this->_loadData(
00828             self::RSS_BARGAIN,
00829             $this->getBargainTitle(),
00830             $oLang->translateString( 'RSS_BARGAIN_DESCRIPTION', $oLang->getBaseLanguage() ),
00831             $this->_getArticleItems($oArtList),
00832             $this->getBargainUrl()
00833         );
00834     }
00835 
00836 }
00837 

Generated on Tue Aug 4 09:09:57 2009 for OXID eShop CE by  doxygen 1.5.5