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         $oShop = $this->getConfig()->getActiveShop();
00053         $this->_aChannel['title'] = $oShop->oxshops__oxname->value;
00054         $this->_aChannel['link']  = oxUtilsUrl::getInstance()->prepareUrlForNoSession($this->getConfig()->getShopHomeURL());
00055         $this->_aChannel['description'] = '';
00056         $oLang = oxLang::getInstance();
00057         $aLangIds = $oLang->getLanguageIds();
00058         $this->_aChannel['language']  = $aLangIds[$oLang->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         $oConfig = $this->getConfig();
00085         return $name.'_'.$oConfig->getShopId().'_'.oxLang::getInstance()->getBaseLanguage().'_'.(int) $oConfig->getShopCurrency();
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         $myUtilsUrl = oxUtilsUrl::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 = $myUtilsUrl->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 oxUtilsUrl::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         $oStr = getStr();
00240         if ( $oStr->strpos($sUrl, '?') !== false ) {
00241             if ( !$oStr->preg_match('/[?&](amp;)?$/i', $sUrl)) {
00242                 $sUrl .= '&amp;';
00243             }
00244         } else {
00245             $sUrl .= '?';
00246         }
00247         return $sUrl;
00248     }
00249 
00263     protected function _loadData($sTag, $sTitle, $sDesc, $aItems, $sRssUrl, $sTargetUrl = null)
00264     {
00265         $this->_loadBaseChannel();
00266 
00267         $this->_aChannel['selflink'] = $sRssUrl;
00268 
00269         if ($sTargetUrl) {
00270             $this->_aChannel['link'] = $this->_aChannel['image']['link'] = $sTargetUrl;
00271         }
00272 
00273         $this->_aChannel['image']['title']        = $this->_aChannel['title']       = $sTitle;
00274         $this->_aChannel['image']['description']  = $this->_aChannel['description'] = $sDesc;
00275 
00276         $this->_aChannel['items'] = $aItems;
00277 
00278         if ($sTag) {
00279             $this->_aChannel['lastBuildDate'] = $this->_getLastBuildDate($sTag, $this->_aChannel);
00280             $this->_saveToCache($sTag, $this->_aChannel);
00281         } else {
00282             $this->_aChannel['lastBuildDate'] = date( 'D, d M Y H:i:s O', oxUtilsDate::getInstance()->getTime() );
00283         }
00284     }
00285 
00293     public function getTopInShopTitle()
00294     {
00295         $oLang = oxLang::getInstance();
00296         $iLang = $oLang->getBaseLanguage();
00297         return $this->_prepareFeedName( $oLang->translateString( 'RSS_TOPSHOP_TITLE', $iLang ) );
00298     }
00299 
00307     public function getTopInShopUrl()
00308     {
00309         return $this->_prepareUrl("cl=rss&amp;fnc=topshop", $this->getTopInShopTitle());
00310     }
00311 
00319     public function loadTopInShop()
00320     {
00321         if ( ( $this->_aChannel = $this->_loadFromCache( self::RSS_TOPSHOP ) ) ) {
00322             return;
00323         }
00324 
00325         $oArtList = oxNew( 'oxarticlelist' );
00326         $oArtList->loadTop5Articles( $this->getConfig()->getConfigParam( 'iRssItemsCount' ) );
00327 
00328         $oLang = oxLang::getInstance();
00329         $this->_loadData(
00330             self::RSS_TOPSHOP,
00331             $this->getTopInShopTitle(),
00332             $oLang->translateString( 'RSS_TOPSHOP_DESCRIPTION', $oLang->getBaseLanguage() ),
00333             $this->_getArticleItems($oArtList),
00334             $this->getTopInShopUrl()
00335         );
00336     }
00337 
00338 
00339 
00347     public function getNewestArticlesTitle()
00348     {
00349         $oLang = oxLang::getInstance();
00350         $iLang = $oLang->getBaseLanguage();
00351         return $this->_prepareFeedName( $oLang->translateString( 'RSS_NEWESTARTICLES_TITLE', $iLang ) );
00352     }
00353 
00361     public function getNewestArticlesUrl()
00362     {
00363         return $this->_prepareUrl("cl=rss&amp;fnc=newarts", $this->getNewestArticlesTitle());
00364     }
00365 
00373     public function loadNewestArticles()
00374     {
00375         if ( ( $this->_aChannel = $this->_loadFromCache(self::RSS_NEWARTS ) ) ) {
00376             return;
00377         }
00378         $oArtList = oxNew( 'oxarticlelist' );
00379         $oArtList->loadNewestArticles( $this->getConfig()->getConfigParam( 'iRssItemsCount' ) );
00380 
00381         $oLang = oxLang::getInstance();
00382         $this->_loadData(
00383             self::RSS_NEWARTS,
00384             $this->getNewestArticlesTitle( ),
00385             $oLang->translateString( 'RSS_NEWESTARTICLES_DESCRIPTION', $oLang->getBaseLanguage() ),
00386             $this->_getArticleItems($oArtList),
00387             $this->getNewestArticlesUrl()
00388         );
00389     }
00390 
00391 
00401     public function getCategoryArticlesTitle(oxCategory $oCat)
00402     {
00403         $oLang  = oxLang::getInstance();
00404         $iLang  = $oLang->getBaseLanguage();
00405         $sTitle = $this->_getCatPath($oCat);
00406         return $this->_prepareFeedName( $sTitle . $oLang->translateString( 'RSS_CATEGORYARTICLES_TITLE', $iLang ) );
00407     }
00408 
00416     protected function _getCatPath( $oCat )
00417     {
00418         $sCatPathString = '';
00419         $sSep = '';
00420         while ( $oCat ) {
00421             // prepare oCat title part
00422             $sCatPathString = $oCat->oxcategories__oxtitle->value.$sSep.$sCatPathString ;
00423             $sSep = '/';
00424             // load parent
00425             $oCat = $oCat->getParentCategory();
00426         }
00427         return $sCatPathString;
00428     }
00429 
00439     public function getCategoryArticlesUrl(oxCategory $oCat)
00440     {
00441         $oLang = oxLang::getInstance();
00442         return $this->_prepareUrl("cl=rss&amp;fnc=catarts&amp;cat=".urlencode($oCat->getId()),
00443                 sprintf($oLang->translateString( 'RSS_CATEGORYARTICLES_URL', $oLang->getBaseLanguage() ), $oCat->oxcategories__oxtitle->value));
00444     }
00445 
00455     public function loadCategoryArticles( oxCategory $oCat )
00456     {
00457         $sId = $oCat->getId();
00458         if ( ( $this->_aChannel = $this->_loadFromCache( self::RSS_CATARTS.$sId ) ) ) {
00459             return;
00460         }
00461 
00462         $oArtList = oxNew( 'oxarticlelist' );
00463         $oArtList->setCustomSorting('oc.oxtime desc');
00464         $oArtList->loadCategoryArticles($oCat->getId(), null, $this->getConfig()->getConfigParam( 'iRssItemsCount' ));
00465 
00466         $oLang = oxLang::getInstance();
00467         $this->_loadData(
00468             self::RSS_CATARTS.$sId,
00469             $this->getCategoryArticlesTitle($oCat),
00470             sprintf($oLang->translateString( 'RSS_CATEGORYARTICLES_DESCRIPTION', $oLang->getBaseLanguage() ), $oCat->oxcategories__oxtitle->value),
00471             $this->_getArticleItems($oArtList),
00472             $this->getCategoryArticlesUrl($oCat),
00473             $oCat->getLink()
00474         );
00475     }
00476 
00477 
00490     public function getSearchArticlesTitle($sSearch, $sCatId, $sVendorId, $sManufacturerId)
00491     {
00492         return $this->_prepareFeedName( getStr()->htmlspecialchars($this->_getSearchParamsTranslation('RSS_SEARCHARTICLES_TITLE', $sSearch, $sCatId, $sVendorId, $sManufacturerId)) );
00493     }
00494 
00507     protected function _getSearchParamsUrl($sSearch, $sCatId, $sVendorId, $sManufacturerId)
00508     {
00509         $sParams = "searchparam=".urlencode($sSearch);
00510         if ($sCatId) {
00511             $sParams .= "&amp;searchcnid=".urlencode($sCatId);
00512         }
00513 
00514         if ($sVendorId) {
00515             $sParams .= "&amp;searchvendor=".urlencode($sVendorId);
00516         }
00517 
00518         if ($sManufacturerId) {
00519             $sParams .= "&amp;searchmanufacturer=".urlencode($sManufacturerId);
00520         }
00521 
00522         return $sParams;
00523     }
00524 
00535     protected function _getObjectField($sId, $sObject, $sField)
00536     {
00537         if (!$sId) {
00538             return '';
00539         }
00540         $oObj = oxNew($sObject);
00541         if ($oObj->load($sId)) {
00542             return $oObj->$sField->value;
00543         }
00544         return '';
00545     }
00546 
00560     protected function _getSearchParamsTranslation($sId, $sSearch, $sCatId, $sVendorId, $sManufacturerId)
00561     {
00562         $oLang = oxLang::getInstance();
00563         $sCatTitle = '';
00564         if ($sTitle = $this->_getObjectField($sCatId, 'oxcategory', 'oxcategories__oxtitle')) {
00565             $sCatTitle = sprintf($oLang->translateString( 'RSS_SEARCHARTICLES_TAG_CATEGORY', $oLang->getBaseLanguage() ), $sTitle);
00566         }
00567         $sVendorTitle = '';
00568         if ($sTitle = $this->_getObjectField($sVendorId, 'oxvendor', 'oxvendor__oxtitle')) {
00569             $sVendorTitle = sprintf($oLang->translateString( 'RSS_SEARCHARTICLES_TAG_VENDOR', $oLang->getBaseLanguage() ), $sTitle);
00570         }
00571         $sManufacturerTitle = '';
00572         if ($sTitle = $this->_getObjectField($sManufacturerId, 'oxmanufacturer', 'oxmanufacturers__oxtitle')) {
00573             $sManufacturerTitle = sprintf($oLang->translateString( 'RSS_SEARCHARTICLES_TAG_MANUFACTURER', $oLang->getBaseLanguage() ), $sTitle);
00574         }
00575 
00576         $sRet = sprintf($oLang->translateString( $sId, $oLang->getBaseLanguage() ), $sSearch);
00577 
00578         $sRet = str_replace('<TAG_CATEGORY>', $sCatTitle, $sRet);
00579         $sRet = str_replace('<TAG_VENDOR>', $sVendorTitle, $sRet);
00580         $sRet = str_replace('<TAG_MANUFACTURER>', $sManufacturerTitle, $sRet);
00581 
00582         return $sRet;
00583     }
00584 
00597     public function getSearchArticlesUrl( $sSearch, $sCatId, $sVendorId, $sManufacturerId )
00598     {
00599         $oLang = oxLang::getInstance();
00600         $sUrl = $this->_prepareUrl("cl=rss&amp;fnc=searcharts", $oLang->translateString( 'RSS_SEARCHARTICLES_URL', $oLang->getBaseLanguage()));
00601 
00602         $sJoin = '?';
00603         if (strpos($sUrl, '?') !== false) {
00604             $sJoin = '&amp;';
00605         }
00606         return $sUrl.$sJoin.$this->_getSearchParamsUrl($sSearch, $sCatId, $sVendorId, $sManufacturerId);
00607     }
00608 
00621     public function loadSearchArticles( $sSearch, $sCatId, $sVendorId, $sManufacturerId )
00622     {
00623         // dont use cache for search
00624         //if ($this->_aChannel = $this->_loadFromCache(self::RSS_SEARCHARTS.md5($sSearch.$sCatId.$sVendorId))) {
00625         //    return;
00626         //}
00627 
00628         $oConfig = $this->getConfig();
00629         $oConfig->setConfigParam('iNrofCatArticles', $oConfig->getConfigParam( 'iRssItemsCount' ));
00630 
00631         $oArtList = oxNew( 'oxsearch' )->getSearchArticles($sSearch, $sCatId, $sVendorId, $sManufacturerId, oxNew('oxarticle')->getViewName().'.oxtimestamp desc');
00632 
00633         $this->_loadData(
00634             // dont use cache for search
00635             null,
00636             //self::RSS_SEARCHARTS.md5($sSearch.$sCatId.$sVendorId),
00637             $this->getSearchArticlesTitle($sSearch, $sCatId, $sVendorId, $sManufacturerId),
00638             $this->_getSearchParamsTranslation('RSS_SEARCHARTICLES_DESCRIPTION', getStr()->htmlspecialchars( $sSearch ), $sCatId, $sVendorId, $sManufacturerId),
00639             $this->_getArticleItems($oArtList),
00640             $this->getSearchArticlesUrl($sSearch, $sCatId, $sVendorId, $sManufacturerId),
00641             $this->_getShopUrl()."cl=search&amp;".$this->_getSearchParamsUrl($sSearch, $sCatId, $sVendorId, $sManufacturerId)
00642         );
00643     }
00644 
00652     public function getRecommListsTitle(oxArticle $oArticle)
00653     {
00654         $oLang = oxLang::getInstance();
00655         $iLang = $oLang->getBaseLanguage();
00656         return $this->_prepareFeedName( sprintf($oLang->translateString( 'RSS_ARTRECOMMLISTS_TITLE', $iLang ), $oArticle->oxarticles__oxtitle->value) );
00657     }
00658 
00666     public function getRecommListsUrl(oxArticle $oArticle)
00667     {
00668         $oLang = oxLang::getInstance();
00669         $iLang = $oLang->getBaseLanguage();
00670         return $this->_prepareUrl("cl=rss&amp;fnc=recommlists&amp;anid=".$oArticle->getId(),
00671                 sprintf($oLang->translateString( 'RSS_ARTRECOMMLISTS_URL', $iLang ), $oArticle->oxarticles__oxtitle->value));
00672     }
00673 
00681     protected function _getRecommListItems($oList)
00682     {
00683         $myUtilsUrl = oxUtilsUrl::getInstance();
00684         $aItems = array();
00685         foreach ($oList as $oRecommList) {
00686             $oItem = new oxStdClass();
00687             $oItem->title                   = $oRecommList->oxrecommlists__oxtitle->value;
00688             $oItem->guid     = $oItem->link = $myUtilsUrl->prepareUrlForNoSession($oRecommList->getLink());
00689             $oItem->isGuidPermalink         = true;
00690             $oItem->description             = $oRecommList->oxrecommlists__oxdesc->value;
00691 
00692             $aItems[] = $oItem;
00693         }
00694         return $aItems;
00695     }
00696 
00704     public function loadRecommLists(oxArticle $oArticle)
00705     {
00706         if ( ( $this->_aChannel = $this->_loadFromCache( self::RSS_ARTRECOMMLISTS.$oArticle->getId() ) ) ) {
00707             return;
00708         }
00709 
00710         $oConfig = $this->getConfig();
00711         $oConfig->setConfigParam('iNrofCrossellArticles', $oConfig->getConfigParam( 'iRssItemsCount' ));
00712 
00713         $oList = oxNew( 'oxrecommlist' )->getRecommListsByIds(array($oArticle->getId()));
00714         if ($oList == null) {
00715             $oList = oxNew('oxlist');
00716         }
00717 
00718         $oLang = oxLang::getInstance();
00719         $this->_loadData(
00720             self::RSS_ARTRECOMMLISTS.$oArticle->getId(),
00721             $this->getRecommListsTitle($oArticle),
00722             sprintf($oLang->translateString( 'RSS_ARTRECOMMLISTS_DESCRIPTION', $oLang->getBaseLanguage() ), $oArticle->oxarticles__oxtitle->value),
00723             $this->_getRecommListItems($oList),
00724             $this->getRecommListsUrl($oArticle),
00725             $oArticle->getLink()
00726         );
00727     }
00728 
00736     public function getRecommListArticlesTitle(oxRecommList $oRecommList)
00737     {
00738         $oLang = oxLang::getInstance();
00739         $iLang = $oLang->getBaseLanguage();
00740         return $this->_prepareFeedName( sprintf($oLang->translateString( 'RSS_RECOMMLISTARTICLES_TITLE', $iLang ), $oRecommList->oxrecommlists__oxtitle->value) );
00741     }
00742 
00750     public function getRecommListArticlesUrl(oxRecommList $oRecommList)
00751     {
00752         $oLang = oxLang::getInstance();
00753         $iLang = $oLang->getBaseLanguage();
00754         return $this->_prepareUrl("cl=rss&amp;fnc=recommlistarts&amp;recommid=".$oRecommList->getId(),
00755                 sprintf($oLang->translateString( 'RSS_RECOMMLISTARTICLES_URL', $iLang ), $oRecommList->oxrecommlists__oxtitle->value));
00756     }
00757 
00765     public function loadRecommListArticles(oxRecommList $oRecommList)
00766     {
00767         if ( ( $this->_aChannel = $this->_loadFromCache( self::RSS_RECOMMLISTARTS.$oRecommList->getId() ) ) ) {
00768             return;
00769         }
00770 
00771         $oList = oxNew( 'oxarticlelist' );
00772         $oList->loadRecommArticles( $oRecommList->getId(), ' order by oxobject2list.oxtimestamp desc limit '. $this->getConfig()->getConfigParam( 'iRssItemsCount' ) );
00773 
00774         $oLang = oxLang::getInstance();
00775         $this->_loadData(
00776             self::RSS_RECOMMLISTARTS.$oRecommList->getId(),
00777             $this->getRecommListArticlesTitle($oRecommList),
00778             sprintf($oLang->translateString( 'RSS_RECOMMLISTARTICLES_DESCRIPTION', $oLang->getBaseLanguage() ), $oRecommList->oxrecommlists__oxtitle->value),
00779             $this->_getArticleItems($oList),
00780             $this->getRecommListArticlesUrl($oRecommList),
00781             $oRecommList->getLink()
00782         );
00783     }
00784 
00792     public function getBargainTitle()
00793     {
00794         $oLang = oxLang::getInstance();
00795         $iLang = $oLang->getBaseLanguage();
00796         return $this->_prepareFeedName( $oLang->translateString( 'RSS_BARGAIN_TITLE', $iLang ) );
00797     }
00798 
00806     public function getBargainUrl()
00807     {
00808         return $this->_prepareUrl("cl=rss&amp;fnc=bargain", $this->getBargainTitle());
00809     }
00810 
00818     public function loadBargain()
00819     {
00820         if ( ( $this->_aChannel = $this->_loadFromCache( self::RSS_BARGAIN ) ) ) {
00821             return;
00822         }
00823 
00824         $oArtList = oxNew( 'oxarticlelist' );
00825         $oArtList->loadAktionArticles( 'OXBARGAIN', $this->getConfig()->getConfigParam( 'iRssItemsCount' ) );
00826 
00827         $oLang = oxLang::getInstance();
00828         $this->_loadData(
00829             self::RSS_BARGAIN,
00830             $this->getBargainTitle(),
00831             $oLang->translateString( 'RSS_BARGAIN_DESCRIPTION', $oLang->getBaseLanguage() ),
00832             $this->_getArticleItems($oArtList),
00833             $this->getBargainUrl()
00834         );
00835     }
00836 
00837 }
00838