oxrssfeed.php

Go to the documentation of this file.
00001 <?php
00002 
00009 class oxRssFeed extends oxSuperCfg
00010 {
00014     const CACHE_TTL = 10800;
00015 
00019     const RSS_TOPSHOP    = 'RSS_TopShop';
00020     const RSS_NEWARTS    = 'RSS_NewArts';
00021     const RSS_CATARTS    = 'RSS_CatArts';
00022     const RSS_ARTRECOMMLISTS = 'RSS_ARTRECOMMLISTS';
00023     const RSS_RECOMMLISTARTS = 'RSS_RECOMMLISTARTS';
00024     const RSS_BARGAIN    = 'RSS_Bargain';
00025 
00032     protected $_aChannel = array();
00033 
00040     public function getChannel()
00041     {
00042         return $this->_aChannel;
00043     }
00044 
00051     protected function _loadBaseChannel()
00052     {
00053         $oShop = $this->getConfig()->getActiveShop();
00054         $this->_aChannel['title'] = $oShop->oxshops__oxname->value;
00055         $this->_aChannel['link']  = oxUtils::getInstance()->prepareUrlForNoSession($this->getConfig()->getShopHomeURL());
00056         $this->_aChannel['description'] = '';
00057         $aLangIds = oxLang::getInstance()->getLanguageIds();
00058         $this->_aChannel['language']  = $aLangIds[oxLang::getInstance()->getBaseLanguage()];
00059         $this->_aChannel['copyright'] = $oShop->oxshops__oxname->value;
00060         $this->_aChannel['selflink'] = '';
00061         if (oxUtils::getInstance()->isValidEmail( $oShop->oxshops__oxinfoemail->value )) {
00062             $this->_aChannel['managingEditor'] = $oShop->oxshops__oxinfoemail->value;
00063         }
00064         //$this->_aChannel['webMaster']      = '';
00065 
00066         $this->_aChannel['generator']      = 'OXID eShop '.$oShop->oxshops__oxversion->value;
00067         $this->_aChannel['image']['url']   = $this->getConfig()->getImageUrl().'logo.png';
00068         
00069         
00070         $this->_aChannel['image']['title'] = $this->_aChannel['title'];
00071         $this->_aChannel['image']['link']  = $this->_aChannel['link'];
00072     }
00073 
00082     protected function _getCacheId($name)
00083     {
00084         return $name.'_'.$this->getConfig()->getShopId().'_'.oxLang::getInstance()->getBaseLanguage().'_'.(int) oxConfig::getParameter('currency');
00085     }
00086 
00095     protected function _loadFromCache($name)
00096     {
00097         if ($aRes = oxUtils::getInstance()->fromFileCache($this->_getCacheId($name))) {
00098             if ($aRes['timestamp'] > time() - self::CACHE_TTL) {
00099                 return $aRes['content'];
00100             }
00101         }
00102         return false;
00103     }
00104 
00105 
00116     protected function _getLastBuildDate($name, $aData)
00117     {
00118         if ($aData2 = oxUtils::getInstance()->fromFileCache($this->_getCacheId($name))) {
00119             $sLastBuildDate = $aData2['content']['lastBuildDate'];
00120             $aData2['content']['lastBuildDate'] = '';
00121             $aData['lastBuildDate'] = '';
00122             if (!strcmp(serialize($aData), serialize($aData2['content']))) {
00123                 return $sLastBuildDate;
00124             }
00125         }
00126         return date('D, d M Y H:i:s O');
00127     }
00128 
00141     protected function _saveToCache($name, $aContent)
00142     {
00143         $aData = array(
00144                 'timestamp' => time(),
00145                 'content'   => $aContent
00146             );
00147         return oxUtils::getInstance()->toFileCache($this->_getCacheId($name), $aData);
00148     }
00149 
00150 
00159     protected function _getArticleItems(oxArticleList $oList)
00160     {
00161         $aItems = array();
00162         foreach ($oList as $oArticle) {
00163             $oItem = new oxStdClass();
00164             $oActCur = $this->getConfig()->getActShopCurrencyObject();
00165             $sPrice = $oArticle->getPriceFromPrefix().oxLang::getInstance()->formatCurrency( $oArticle->getPrice()->getBruttoPrice(), $oActCur );
00166             $oItem->title                   = strip_tags($oArticle->oxarticles__oxtitle->value . " " . $sPrice . " ". $oActCur->sign);
00167             $oItem->guid     = $oItem->link = oxUtils::getInstance()->prepareUrlForNoSession($oArticle->getLink());
00168             $oItem->isGuidPermalink         = true;
00169             $oItem->description             = $oArticle->getArticleLongDesc()->value; //oxarticles__oxshortdesc->value;
00170             if (trim(str_replace('&nbsp;', '', (strip_tags($oItem->description)))) == '') {
00171                 $oItem->description             = $oArticle->oxarticles__oxshortdesc->value;
00172             }
00173             $oItem->description = htmlspecialchars(trim($oItem->description));
00174 
00175             $aItems[] = $oItem;
00176         }
00177         return $aItems;
00178     }
00179 
00190     protected function _prepareUrl($sUri, $sTitle)
00191     {
00192         $iLang = oxLang::getInstance()->getBaseLanguage();
00193         $sUrl  = $this->_getShopUrl();
00194         $sUrl .= $sUri.'&amp;lang='.$iLang;
00195 
00196         if ( oxUtils::getInstance()->seoIsActive() ) {
00197             $oEncoder = oxSeoEncoder::getInstance();
00198             $sUrl = $oEncoder->getDynamicUrl( $sUrl, "rss/{$sTitle}/", $iLang );
00199         }
00200 
00201         return oxUtils::getInstance()->prepareUrlForNoSession( $sUrl );
00202     }
00203 
00213     protected function _prepareFeedName($sTitle)
00214     {
00215         $oShop = $this->getConfig()->getActiveShop();
00216 
00217         return $oShop->oxshops__oxname->value . "/" . $sTitle;
00218     }
00219 
00226     protected function _getShopUrl()
00227     {
00228         $sUrl = $this->getConfig()->getShopUrl();
00229         if (strpos($sUrl, '?')) {
00230             if (!preg_match('/[?&](amp;)?$/i', $sUrl)) {
00231                 $sUrl .= '&amp;';
00232             }
00233         } else {
00234             $sUrl .= '?';
00235         }
00236         return $sUrl;
00237     }
00238 
00252     protected function _loadData($sTag, $sTitle, $sDesc, $aItems, $sRssUrl, $sTargetUrl = null)
00253     {
00254         $this->_loadBaseChannel();
00255 
00256         $this->_aChannel['selflink'] = $sRssUrl;
00257 
00258         if ($sTargetUrl) {
00259             $this->_aChannel['link'] = $this->_aChannel['image']['link'] = $sTargetUrl;
00260         }
00261 
00262         $this->_aChannel['image']['title']        = $this->_aChannel['title']       = $sTitle;
00263         $this->_aChannel['image']['description']  = $this->_aChannel['description'] = $sDesc;
00264 
00265         $this->_aChannel['items'] = $aItems;
00266 
00267         if ($sTag) {
00268             $this->_aChannel['lastBuildDate'] = $this->_getLastBuildDate($sTag, $this->_aChannel);
00269             $this->_saveToCache($sTag, $this->_aChannel);
00270         } else {
00271             $this->_aChannel['lastBuildDate'] = date('D, d M Y H:i:s O');
00272         }
00273     }
00274 
00282     public function getTopInShopTitle()
00283     {
00284         $oLang = oxLang::getInstance();
00285         $iLang = $oLang->getBaseLanguage();
00286         return $this->_prepareFeedName( $oLang->translateString( 'RSS_TOPSHOP_TITLE', $iLang ) );
00287     }
00288 
00296     public function getTopInShopUrl()
00297     {
00298         return $this->_prepareUrl("cl=rss&amp;fnc=topshop", $this->getTopInShopTitle());
00299     }
00300 
00308     public function loadTopInShop()
00309     {
00310         if ( ( $this->_aChannel = $this->_loadFromCache( self::RSS_TOPSHOP ) ) ) {
00311             return;
00312         }
00313 
00314         $oArtList = oxNew( 'oxarticlelist' );
00315         $oArtList->loadTop5Articles();
00316 
00317         $oLang = oxLang::getInstance();
00318         $this->_loadData(
00319             self::RSS_TOPSHOP,
00320             $this->getTopInShopTitle(),
00321             $oLang->translateString( 'RSS_TOPSHOP_DESCRIPTION', $oLang->getBaseLanguage() ),
00322             $this->_getArticleItems($oArtList),
00323             $this->getTopInShopUrl()
00324         );
00325     }
00326 
00327 
00328 
00336     public function getNewestArticlesTitle()
00337     {
00338         $oLang = oxLang::getInstance();
00339         $iLang = $oLang->getBaseLanguage();
00340         return $this->_prepareFeedName( $oLang->translateString( 'RSS_NEWESTARTICLES_TITLE', $iLang ) );
00341     }
00342 
00350     public function getNewestArticlesUrl()
00351     {
00352         return $this->_prepareUrl("cl=rss&amp;fnc=newarts", $this->getNewestArticlesTitle());
00353     }
00354 
00362     public function loadNewestArticles()
00363     {
00364         if ( ( $this->_aChannel = $this->_loadFromCache(self::RSS_NEWARTS ) ) ) {
00365             return;
00366         }
00367         $oArtList = oxNew( 'oxarticlelist' );
00368         $oArtList->loadNewestArticles( $this->getConfig()->getConfigParam( 'iRssItemsCount' ) );
00369 
00370         $oLang = oxLang::getInstance();
00371         $this->_loadData(
00372             self::RSS_NEWARTS,
00373             $this->getNewestArticlesTitle( ),
00374             $oLang->translateString( 'RSS_NEWESTARTICLES_DESCRIPTION', $oLang->getBaseLanguage() ),
00375             $this->_getArticleItems($oArtList),
00376             $this->getNewestArticlesUrl()
00377         );
00378     }
00379 
00380 
00390     public function getCategoryArticlesTitle(oxCategory $oCat)
00391     {
00392         $oLang  = oxLang::getInstance();
00393         $iLang  = $oLang->getBaseLanguage();
00394         $sTitle = $this->_getCatPath($oCat);
00395         return $this->_prepareFeedName( $sTitle . $oLang->translateString( 'RSS_CATEGORYARTICLES_TITLE', $iLang ) );
00396     }
00397 
00405     protected function _getCatPath( $oCat )
00406     {
00407         $sCatPathString = '';
00408         $sSep = '';
00409         while ( $oCat ) {
00410             // prepare oCat title part
00411             $sCatPathString = $oCat->oxcategories__oxtitle->value.$sSep.$sCatPathString ;
00412             $sSep = '/';
00413             // load parent
00414             $oCat = $oCat->getParentCategory();
00415         }
00416         return $sCatPathString;
00417     }
00418 
00428     public function getCategoryArticlesUrl(oxCategory $oCat)
00429     {
00430         $oLang = oxLang::getInstance();
00431         return $this->_prepareUrl("cl=rss&amp;fnc=catarts&amp;cat=".urlencode($oCat->getId()),
00432                 sprintf($oLang->translateString( 'RSS_CATEGORYARTICLES_URL', $oLang->getBaseLanguage() ), $oCat->oxcategories__oxtitle->value));
00433     }
00434 
00444     public function loadCategoryArticles( oxCategory $oCat )
00445     {
00446         $sId = $oCat->getId();
00447         if ( ( $this->_aChannel = $this->_loadFromCache( self::RSS_CATARTS.$sId ) ) ) {
00448             return;
00449         }
00450 
00451         $oArtList = oxNew( 'oxarticlelist' );
00452         $oArtList->setCustomSorting('oc.oxtime desc');
00453         $oArtList->loadCategoryArticles($oCat->getId(), null, $this->getConfig()->getConfigParam( 'iRssItemsCount' ));
00454 
00455         $oLang = oxLang::getInstance();
00456         $this->_loadData(
00457             self::RSS_CATARTS.$sId,
00458             $this->getCategoryArticlesTitle($oCat),
00459             sprintf($oLang->translateString( 'RSS_CATEGORYARTICLES_DESCRIPTION', $oLang->getBaseLanguage() ), $oCat->oxcategories__oxtitle->value),
00460             $this->_getArticleItems($oArtList),
00461             $this->getCategoryArticlesUrl($oCat),
00462             $oCat->getLink()
00463         );
00464     }
00465 
00466 
00478     public function getSearchArticlesTitle($sSearch, $sCatId, $sVendorId)
00479     {
00480         return $this->_prepareFeedName( htmlspecialchars($this->_getSearchParamsTranslation('RSS_SEARCHARTICLES_TITLE', $sSearch, $sCatId, $sVendorId)) );
00481     }
00482 
00494     protected function _getSearchParamsUrl($sSearch, $sCatId, $sVendorId)
00495     {
00496         $sParams = "searchparam=".urlencode($sSearch);
00497         if ($sCatId) {
00498             $sParams .= "&amp;searchcnid=".urlencode($sCatId);
00499         }
00500 
00501         if ($sVendorId) {
00502             $sParams .= "&amp;searchvendor=".urlencode($sVendorId);
00503         }
00504 
00505         return $sParams;
00506     }
00507 
00518     protected function _getObjectField($sId, $sObject, $sField)
00519     {
00520         if (!$sId) {
00521             return '';
00522         }
00523         $oObj = oxNew($sObject);
00524         if ($oObj->load($sId)) {
00525             return $oObj->$sField->value;
00526         }
00527         return '';
00528     }
00529 
00542     protected function _getSearchParamsTranslation($sId, $sSearch, $sCatId, $sVendorId)
00543     {
00544         $oLang = oxLang::getInstance();
00545         $sCatTitle = '';
00546         if ($sTitle = $this->_getObjectField($sCatId, 'oxcategory', 'oxcategories__oxtitle')) {
00547             $sCatTitle = sprintf($oLang->translateString( 'RSS_SEARCHARTICLES_TAG_CATEGORY', $oLang->getBaseLanguage() ), $sTitle);
00548         }
00549         $sVendorTitle = '';
00550         if ($sTitle = $this->_getObjectField($sVendorId, 'oxvendor', 'oxvendor__oxtitle')) {
00551             $sVendorTitle = sprintf($oLang->translateString( 'RSS_SEARCHARTICLES_TAG_VENDOR', $oLang->getBaseLanguage() ), $sTitle);
00552         }
00553 
00554         $sRet = sprintf($oLang->translateString( $sId, $oLang->getBaseLanguage() ), $sSearch);
00555 
00556         $sRet = str_replace('<TAG_CATEGORY>', $sCatTitle, $sRet);
00557         $sRet = str_replace('<TAG_VENDOR>', $sVendorTitle, $sRet);
00558 
00559         return $sRet;
00560     }
00561 
00573     public function getSearchArticlesUrl( $sSearch, $sCatId, $sVendorId )
00574     {
00575         $oLang = oxLang::getInstance();
00576         $sUrl = $this->_prepareUrl("cl=rss&amp;fnc=searcharts", $oLang->translateString( 'RSS_SEARCHARTICLES_URL', $oLang->getBaseLanguage()));
00577 
00578         $sJoin = '?';
00579         if (strpos($sUrl, '?')) {
00580             $sJoin = '&amp;';
00581         }
00582         return $sUrl.$sJoin.$this->_getSearchParamsUrl($sSearch, $sCatId, $sVendorId);
00583     }
00584 
00596     public function loadSearchArticles( $sSearch, $sCatId, $sVendorId )
00597     {
00598         // dont use cache for search
00599         //if ($this->_aChannel = $this->_loadFromCache(self::RSS_SEARCHARTS.md5($sSearch.$sCatId.$sVendorId))) {
00600         //    return;
00601         //}
00602 
00603         $oConfig = $this->getConfig();
00604         $oConfig->setConfigParam('iNrofCatArticles', $oConfig->getConfigParam( 'iRssItemsCount' ));
00605 
00606         $oArtList = oxNew( 'oxsearch' )->getSearchArticles($sSearch, $sCatId, $sVendorId, oxNew('oxarticle')->getViewName().'.oxtimestamp desc');
00607 
00608         $this->_loadData(
00609             // dont use cache for search
00610             null,
00611             //self::RSS_SEARCHARTS.md5($sSearch.$sCatId.$sVendorId),
00612             $this->getSearchArticlesTitle($sSearch, $sCatId, $sVendorId),
00613             $this->_getSearchParamsTranslation('RSS_SEARCHARTICLES_DESCRIPTION', htmlspecialchars($sSearch), $sCatId, $sVendorId),
00614             $this->_getArticleItems($oArtList),
00615             $this->getSearchArticlesUrl($sSearch, $sCatId, $sVendorId),
00616             $this->_getShopUrl()."cl=search&amp;".$this->_getSearchParamsUrl($sSearch, $sCatId, $sVendorId)
00617         );
00618     }
00619 
00627     public function getRecommListsTitle(oxArticle $oArticle)
00628     {
00629         $oLang = oxLang::getInstance();
00630         $iLang = $oLang->getBaseLanguage();
00631         return $this->_prepareFeedName( sprintf($oLang->translateString( 'RSS_ARTRECOMMLISTS_TITLE', $iLang ), $oArticle->oxarticles__oxtitle->value) );
00632     }
00633 
00641     public function getRecommListsUrl(oxArticle $oArticle)
00642     {
00643         $oLang = oxLang::getInstance();
00644         $iLang = $oLang->getBaseLanguage();
00645         return $this->_prepareUrl("cl=rss&amp;fnc=recommlists&amp;anid=".$oArticle->getId(),
00646                 sprintf($oLang->translateString( 'RSS_ARTRECOMMLISTS_URL', $iLang ), $oArticle->oxarticles__oxtitle->value));
00647     }
00648 
00656     protected function _getRecommListItems($oList)
00657     {
00658         $aItems = array();
00659         foreach ($oList as $oRecommList) {
00660             $oItem = new oxStdClass();
00661             $oItem->title                   = $oRecommList->oxrecommlists__oxtitle->value;
00662             $oItem->guid     = $oItem->link = oxUtils::getInstance()->prepareUrlForNoSession($oRecommList->getLink());
00663             $oItem->isGuidPermalink         = true;
00664             $oItem->description             = $oRecommList->oxrecommlists__oxdesc->value;
00665 
00666             $aItems[] = $oItem;
00667         }
00668         return $aItems;
00669     }
00670 
00678     public function loadRecommLists(oxArticle $oArticle)
00679     {
00680         if ( ( $this->_aChannel = $this->_loadFromCache( self::RSS_ARTRECOMMLISTS.$oArticle->getId() ) ) ) {
00681             return;
00682         }
00683 
00684         $oConfig = $this->getConfig();
00685         $oConfig->setConfigParam('iNrofCrossellArticles', $oConfig->getConfigParam( 'iRssItemsCount' ));
00686 
00687         $oList = oxNew( 'oxrecommlist' )->getRecommListsByIds(array($oArticle->getId()));
00688         if ($oList == null) {
00689             $oList = oxNew('oxlist');
00690         }
00691 
00692         $oLang = oxLang::getInstance();
00693         $this->_loadData(
00694             self::RSS_ARTRECOMMLISTS.$oArticle->getId(),
00695             $this->getRecommListsTitle($oArticle),
00696             sprintf($oLang->translateString( 'RSS_ARTRECOMMLISTS_DESCRIPTION', $oLang->getBaseLanguage() ), $oArticle->oxarticles__oxtitle->value),
00697             $this->_getRecommListItems($oList),
00698             $this->getRecommListsUrl($oArticle),
00699             $oArticle->getLink()
00700         );
00701     }
00702 
00710     public function getRecommListArticlesTitle(oxRecommList $oRecommList)
00711     {
00712         $oLang = oxLang::getInstance();
00713         $iLang = $oLang->getBaseLanguage();
00714         return $this->_prepareFeedName( sprintf($oLang->translateString( 'RSS_RECOMMLISTARTICLES_TITLE', $iLang ), $oRecommList->oxrecommlists__oxtitle->value) );
00715     }
00716 
00724     public function getRecommListArticlesUrl(oxRecommList $oRecommList)
00725     {
00726         $oLang = oxLang::getInstance();
00727         $iLang = $oLang->getBaseLanguage();
00728         return $this->_prepareUrl("cl=rss&amp;fnc=recommlistarts&amp;recommid=".$oRecommList->getId(),
00729                 sprintf($oLang->translateString( 'RSS_RECOMMLISTARTICLES_URL', $iLang ), $oRecommList->oxrecommlists__oxtitle->value));
00730     }
00731 
00739     public function loadRecommListArticles(oxRecommList $oRecommList)
00740     {
00741         if ( ( $this->_aChannel = $this->_loadFromCache( self::RSS_RECOMMLISTARTS.$oRecommList->getId() ) ) ) {
00742             return;
00743         }
00744 
00745         $oList = oxNew( 'oxarticlelist' );
00746         $oList->loadRecommArticles( $oRecommList->getId(), ' order by oxobject2list.oxtimestamp desc limit '. $this->getConfig()->getConfigParam( 'iRssItemsCount' ) );
00747 
00748         $oLang = oxLang::getInstance();
00749         $this->_loadData(
00750             self::RSS_RECOMMLISTARTS.$oRecommList->getId(),
00751             $this->getRecommListArticlesTitle($oRecommList),
00752             sprintf($oLang->translateString( 'RSS_RECOMMLISTARTICLES_DESCRIPTION', $oLang->getBaseLanguage() ), $oRecommList->oxrecommlists__oxtitle->value),
00753             $this->_getArticleItems($oList),
00754             $this->getRecommListArticlesUrl($oRecommList),
00755             $oRecommList->getLink()
00756         );
00757     }
00758 
00766     public function getBargainTitle()
00767     {
00768         $oLang = oxLang::getInstance();
00769         $iLang = $oLang->getBaseLanguage();
00770         return $this->_prepareFeedName( $oLang->translateString( 'RSS_BARGAIN_TITLE', $iLang ) );
00771     }
00772 
00780     public function getBargainUrl()
00781     {
00782         return $this->_prepareUrl("cl=rss&amp;fnc=bargain", $this->getBargainTitle());
00783     }
00784 
00792     public function loadBargain()
00793     {
00794         if ( ( $this->_aChannel = $this->_loadFromCache( self::RSS_BARGAIN ) ) ) {
00795             return;
00796         }
00797 
00798         $oArtList = oxNew( 'oxarticlelist' );
00799         $oArtList->loadAktionArticles( 'OXBARGAIN' );
00800 
00801         $oLang = oxLang::getInstance();
00802         $this->_loadData(
00803             self::RSS_BARGAIN,
00804             $this->getBargainTitle(),
00805             $oLang->translateString( 'RSS_BARGAIN_DESCRIPTION', $oLang->getBaseLanguage() ),
00806             $this->_getArticleItems($oArtList),
00807             $this->getBargainUrl()
00808         );
00809     }
00810 
00811 }
00812 

Generated on Thu Dec 4 12:04:57 2008 for OXID eShop CE by  doxygen 1.5.5