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 = $oArticle->getPriceFromPrefix().$oLang->formatCurrency( $oArticle->getPrice()->getBruttoPrice(), $oActCur );
00168             $oItem->title                   = strip_tags($oArticle->oxarticles__oxtitle->value . " " . $sPrice . " ". $oActCur->sign);
00169             $oItem->guid     = $oItem->link = $myUtils->prepareUrlForNoSession($oArticle->getLink());
00170             $oItem->isGuidPermalink         = true;
00171             $oItem->description             = $oArticle->getArticleLongDesc()->value; //oxarticles__oxshortdesc->value;
00172             if (trim(str_replace('&nbsp;', '', (strip_tags($oItem->description)))) == '') {
00173                 $oItem->description             = $oArticle->oxarticles__oxshortdesc->value;
00174             }
00175 
00176             $oItem->description = trim($oItem->description);
00177             if ($sIcon = $oArticle->getIconUrl()) {
00178                 $oItem->description = "<img src='$sIcon' border=0 align='left' hspace=5>".$oItem->description;
00179             }
00180             $oItem->description = $oStr->htmlspecialchars( $oItem->description );
00181 
00182             $aItems[] = $oItem;
00183         }
00184         return $aItems;
00185     }
00186 
00197     protected function _prepareUrl($sUri, $sTitle)
00198     {
00199         $iLang = oxLang::getInstance()->getBaseLanguage();
00200         $sUrl  = $this->_getShopUrl();
00201         $sUrl .= $sUri.'&amp;lang='.$iLang;
00202 
00203         if ( oxUtils::getInstance()->seoIsActive() ) {
00204             $oEncoder = oxSeoEncoder::getInstance();
00205             $sUrl = $oEncoder->getDynamicUrl( $sUrl, "rss/{$sTitle}/", $iLang );
00206         }
00207 
00208         return oxUtils::getInstance()->prepareUrlForNoSession( $sUrl );
00209     }
00210 
00220     protected function _prepareFeedName($sTitle)
00221     {
00222         $oShop = $this->getConfig()->getActiveShop();
00223 
00224         return $oShop->oxshops__oxname->value . "/" . $sTitle;
00225     }
00226 
00233     protected function _getShopUrl()
00234     {
00235         $sUrl = $this->getConfig()->getShopUrl();
00236         if (strpos($sUrl, '?') !== false ) {
00237             if (!preg_match('/[?&](amp;)?$/i', $sUrl)) {
00238                 $sUrl .= '&amp;';
00239             }
00240         } else {
00241             $sUrl .= '?';
00242         }
00243         return $sUrl;
00244     }
00245 
00259     protected function _loadData($sTag, $sTitle, $sDesc, $aItems, $sRssUrl, $sTargetUrl = null)
00260     {
00261         $this->_loadBaseChannel();
00262 
00263         $this->_aChannel['selflink'] = $sRssUrl;
00264 
00265         if ($sTargetUrl) {
00266             $this->_aChannel['link'] = $this->_aChannel['image']['link'] = $sTargetUrl;
00267         }
00268 
00269         $this->_aChannel['image']['title']        = $this->_aChannel['title']       = $sTitle;
00270         $this->_aChannel['image']['description']  = $this->_aChannel['description'] = $sDesc;
00271 
00272         $this->_aChannel['items'] = $aItems;
00273 
00274         if ($sTag) {
00275             $this->_aChannel['lastBuildDate'] = $this->_getLastBuildDate($sTag, $this->_aChannel);
00276             $this->_saveToCache($sTag, $this->_aChannel);
00277         } else {
00278             $this->_aChannel['lastBuildDate'] = date( 'D, d M Y H:i:s O', oxUtilsDate::getInstance()->getTime() );
00279         }
00280     }
00281 
00289     public function getTopInShopTitle()
00290     {
00291         $oLang = oxLang::getInstance();
00292         $iLang = $oLang->getBaseLanguage();
00293         return $this->_prepareFeedName( $oLang->translateString( 'RSS_TOPSHOP_TITLE', $iLang ) );
00294     }
00295 
00303     public function getTopInShopUrl()
00304     {
00305         return $this->_prepareUrl("cl=rss&amp;fnc=topshop", $this->getTopInShopTitle());
00306     }
00307 
00315     public function loadTopInShop()
00316     {
00317         if ( ( $this->_aChannel = $this->_loadFromCache( self::RSS_TOPSHOP ) ) ) {
00318             return;
00319         }
00320 
00321         $oArtList = oxNew( 'oxarticlelist' );
00322         $oArtList->loadTop5Articles();
00323 
00324         $oLang = oxLang::getInstance();
00325         $this->_loadData(
00326             self::RSS_TOPSHOP,
00327             $this->getTopInShopTitle(),
00328             $oLang->translateString( 'RSS_TOPSHOP_DESCRIPTION', $oLang->getBaseLanguage() ),
00329             $this->_getArticleItems($oArtList),
00330             $this->getTopInShopUrl()
00331         );
00332     }
00333 
00334 
00335 
00343     public function getNewestArticlesTitle()
00344     {
00345         $oLang = oxLang::getInstance();
00346         $iLang = $oLang->getBaseLanguage();
00347         return $this->_prepareFeedName( $oLang->translateString( 'RSS_NEWESTARTICLES_TITLE', $iLang ) );
00348     }
00349 
00357     public function getNewestArticlesUrl()
00358     {
00359         return $this->_prepareUrl("cl=rss&amp;fnc=newarts", $this->getNewestArticlesTitle());
00360     }
00361 
00369     public function loadNewestArticles()
00370     {
00371         if ( ( $this->_aChannel = $this->_loadFromCache(self::RSS_NEWARTS ) ) ) {
00372             return;
00373         }
00374         $oArtList = oxNew( 'oxarticlelist' );
00375         $oArtList->loadNewestArticles( $this->getConfig()->getConfigParam( 'iRssItemsCount' ) );
00376 
00377         $oLang = oxLang::getInstance();
00378         $this->_loadData(
00379             self::RSS_NEWARTS,
00380             $this->getNewestArticlesTitle( ),
00381             $oLang->translateString( 'RSS_NEWESTARTICLES_DESCRIPTION', $oLang->getBaseLanguage() ),
00382             $this->_getArticleItems($oArtList),
00383             $this->getNewestArticlesUrl()
00384         );
00385     }
00386 
00387 
00397     public function getCategoryArticlesTitle(oxCategory $oCat)
00398     {
00399         $oLang  = oxLang::getInstance();
00400         $iLang  = $oLang->getBaseLanguage();
00401         $sTitle = $this->_getCatPath($oCat);
00402         return $this->_prepareFeedName( $sTitle . $oLang->translateString( 'RSS_CATEGORYARTICLES_TITLE', $iLang ) );
00403     }
00404 
00412     protected function _getCatPath( $oCat )
00413     {
00414         $sCatPathString = '';
00415         $sSep = '';
00416         while ( $oCat ) {
00417             // prepare oCat title part
00418             $sCatPathString = $oCat->oxcategories__oxtitle->value.$sSep.$sCatPathString ;
00419             $sSep = '/';
00420             // load parent
00421             $oCat = $oCat->getParentCategory();
00422         }
00423         return $sCatPathString;
00424     }
00425 
00435     public function getCategoryArticlesUrl(oxCategory $oCat)
00436     {
00437         $oLang = oxLang::getInstance();
00438         return $this->_prepareUrl("cl=rss&amp;fnc=catarts&amp;cat=".urlencode($oCat->getId()),
00439                 sprintf($oLang->translateString( 'RSS_CATEGORYARTICLES_URL', $oLang->getBaseLanguage() ), $oCat->oxcategories__oxtitle->value));
00440     }
00441 
00451     public function loadCategoryArticles( oxCategory $oCat )
00452     {
00453         $sId = $oCat->getId();
00454         if ( ( $this->_aChannel = $this->_loadFromCache( self::RSS_CATARTS.$sId ) ) ) {
00455             return;
00456         }
00457 
00458         $oArtList = oxNew( 'oxarticlelist' );
00459         $oArtList->setCustomSorting('oc.oxtime desc');
00460         $oArtList->loadCategoryArticles($oCat->getId(), null, $this->getConfig()->getConfigParam( 'iRssItemsCount' ));
00461 
00462         $oLang = oxLang::getInstance();
00463         $this->_loadData(
00464             self::RSS_CATARTS.$sId,
00465             $this->getCategoryArticlesTitle($oCat),
00466             sprintf($oLang->translateString( 'RSS_CATEGORYARTICLES_DESCRIPTION', $oLang->getBaseLanguage() ), $oCat->oxcategories__oxtitle->value),
00467             $this->_getArticleItems($oArtList),
00468             $this->getCategoryArticlesUrl($oCat),
00469             $oCat->getLink()
00470         );
00471     }
00472 
00473 
00486     public function getSearchArticlesTitle($sSearch, $sCatId, $sVendorId, $sManufacturerId)
00487     {
00488         return $this->_prepareFeedName( getStr()->htmlspecialchars($this->_getSearchParamsTranslation('RSS_SEARCHARTICLES_TITLE', $sSearch, $sCatId, $sVendorId, $sManufacturerId)) );
00489     }
00490 
00503     protected function _getSearchParamsUrl($sSearch, $sCatId, $sVendorId, $sManufacturerId)
00504     {
00505         $sParams = "searchparam=".urlencode($sSearch);
00506         if ($sCatId) {
00507             $sParams .= "&amp;searchcnid=".urlencode($sCatId);
00508         }
00509 
00510         if ($sVendorId) {
00511             $sParams .= "&amp;searchvendor=".urlencode($sVendorId);
00512         }
00513 
00514         if ($sManufacturerId) {
00515             $sParams .= "&amp;searchmanufacturer=".urlencode($sManufacturerId);
00516         }
00517 
00518         return $sParams;
00519     }
00520 
00531     protected function _getObjectField($sId, $sObject, $sField)
00532     {
00533         if (!$sId) {
00534             return '';
00535         }
00536         $oObj = oxNew($sObject);
00537         if ($oObj->load($sId)) {
00538             return $oObj->$sField->value;
00539         }
00540         return '';
00541     }
00542 
00556     protected function _getSearchParamsTranslation($sId, $sSearch, $sCatId, $sVendorId, $sManufacturerId)
00557     {
00558         $oLang = oxLang::getInstance();
00559         $sCatTitle = '';
00560         if ($sTitle = $this->_getObjectField($sCatId, 'oxcategory', 'oxcategories__oxtitle')) {
00561             $sCatTitle = sprintf($oLang->translateString( 'RSS_SEARCHARTICLES_TAG_CATEGORY', $oLang->getBaseLanguage() ), $sTitle);
00562         }
00563         $sVendorTitle = '';
00564         if ($sTitle = $this->_getObjectField($sVendorId, 'oxvendor', 'oxvendor__oxtitle')) {
00565             $sVendorTitle = sprintf($oLang->translateString( 'RSS_SEARCHARTICLES_TAG_VENDOR', $oLang->getBaseLanguage() ), $sTitle);
00566         }
00567         $sManufacturerTitle = '';
00568         if ($sTitle = $this->_getObjectField($sManufacturerId, 'oxmanufacturer', 'oxmanufacturers__oxtitle')) {
00569             $sManufacturerTitle = sprintf($oLang->translateString( 'RSS_SEARCHARTICLES_TAG_MANUFACTURER', $oLang->getBaseLanguage() ), $sTitle);
00570         }
00571 
00572         $sRet = sprintf($oLang->translateString( $sId, $oLang->getBaseLanguage() ), $sSearch);
00573 
00574         $sRet = str_replace('<TAG_CATEGORY>', $sCatTitle, $sRet);
00575         $sRet = str_replace('<TAG_VENDOR>', $sVendorTitle, $sRet);
00576         $sRet = str_replace('<TAG_MANUFACTURER>', $sManufacturerTitle, $sRet);
00577 
00578         return $sRet;
00579     }
00580 
00593     public function getSearchArticlesUrl( $sSearch, $sCatId, $sVendorId, $sManufacturerId )
00594     {
00595         $oLang = oxLang::getInstance();
00596         $sUrl = $this->_prepareUrl("cl=rss&amp;fnc=searcharts", $oLang->translateString( 'RSS_SEARCHARTICLES_URL', $oLang->getBaseLanguage()));
00597 
00598         $sJoin = '?';
00599         if (strpos($sUrl, '?') !== false) {
00600             $sJoin = '&amp;';
00601         }
00602         return $sUrl.$sJoin.$this->_getSearchParamsUrl($sSearch, $sCatId, $sVendorId, $sManufacturerId);
00603     }
00604 
00617     public function loadSearchArticles( $sSearch, $sCatId, $sVendorId, $sManufacturerId )
00618     {
00619         // dont use cache for search
00620         //if ($this->_aChannel = $this->_loadFromCache(self::RSS_SEARCHARTS.md5($sSearch.$sCatId.$sVendorId))) {
00621         //    return;
00622         //}
00623 
00624         $oConfig = $this->getConfig();
00625         $oConfig->setConfigParam('iNrofCatArticles', $oConfig->getConfigParam( 'iRssItemsCount' ));
00626 
00627         $oArtList = oxNew( 'oxsearch' )->getSearchArticles($sSearch, $sCatId, $sVendorId, $sManufacturerId, oxNew('oxarticle')->getViewName().'.oxtimestamp desc');
00628 
00629         $this->_loadData(
00630             // dont use cache for search
00631             null,
00632             //self::RSS_SEARCHARTS.md5($sSearch.$sCatId.$sVendorId),
00633             $this->getSearchArticlesTitle($sSearch, $sCatId, $sVendorId, $sManufacturerId),
00634             $this->_getSearchParamsTranslation('RSS_SEARCHARTICLES_DESCRIPTION', getStr()->htmlspecialchars( $sSearch ), $sCatId, $sVendorId, $sManufacturerId),
00635             $this->_getArticleItems($oArtList),
00636             $this->getSearchArticlesUrl($sSearch, $sCatId, $sVendorId, $sManufacturerId),
00637             $this->_getShopUrl()."cl=search&amp;".$this->_getSearchParamsUrl($sSearch, $sCatId, $sVendorId, $sManufacturerId)
00638         );
00639     }
00640 
00648     public function getRecommListsTitle(oxArticle $oArticle)
00649     {
00650         $oLang = oxLang::getInstance();
00651         $iLang = $oLang->getBaseLanguage();
00652         return $this->_prepareFeedName( sprintf($oLang->translateString( 'RSS_ARTRECOMMLISTS_TITLE', $iLang ), $oArticle->oxarticles__oxtitle->value) );
00653     }
00654 
00662     public function getRecommListsUrl(oxArticle $oArticle)
00663     {
00664         $oLang = oxLang::getInstance();
00665         $iLang = $oLang->getBaseLanguage();
00666         return $this->_prepareUrl("cl=rss&amp;fnc=recommlists&amp;anid=".$oArticle->getId(),
00667                 sprintf($oLang->translateString( 'RSS_ARTRECOMMLISTS_URL', $iLang ), $oArticle->oxarticles__oxtitle->value));
00668     }
00669 
00677     protected function _getRecommListItems($oList)
00678     {
00679         $myUtils = oxUtils::getInstance();
00680         $aItems = array();
00681         foreach ($oList as $oRecommList) {
00682             $oItem = new oxStdClass();
00683             $oItem->title                   = $oRecommList->oxrecommlists__oxtitle->value;
00684             $oItem->guid     = $oItem->link = $myUtils->prepareUrlForNoSession($oRecommList->getLink());
00685             $oItem->isGuidPermalink         = true;
00686             $oItem->description             = $oRecommList->oxrecommlists__oxdesc->value;
00687 
00688             $aItems[] = $oItem;
00689         }
00690         return $aItems;
00691     }
00692 
00700     public function loadRecommLists(oxArticle $oArticle)
00701     {
00702         if ( ( $this->_aChannel = $this->_loadFromCache( self::RSS_ARTRECOMMLISTS.$oArticle->getId() ) ) ) {
00703             return;
00704         }
00705 
00706         $oConfig = $this->getConfig();
00707         $oConfig->setConfigParam('iNrofCrossellArticles', $oConfig->getConfigParam( 'iRssItemsCount' ));
00708 
00709         $oList = oxNew( 'oxrecommlist' )->getRecommListsByIds(array($oArticle->getId()));
00710         if ($oList == null) {
00711             $oList = oxNew('oxlist');
00712         }
00713 
00714         $oLang = oxLang::getInstance();
00715         $this->_loadData(
00716             self::RSS_ARTRECOMMLISTS.$oArticle->getId(),
00717             $this->getRecommListsTitle($oArticle),
00718             sprintf($oLang->translateString( 'RSS_ARTRECOMMLISTS_DESCRIPTION', $oLang->getBaseLanguage() ), $oArticle->oxarticles__oxtitle->value),
00719             $this->_getRecommListItems($oList),
00720             $this->getRecommListsUrl($oArticle),
00721             $oArticle->getLink()
00722         );
00723     }
00724 
00732     public function getRecommListArticlesTitle(oxRecommList $oRecommList)
00733     {
00734         $oLang = oxLang::getInstance();
00735         $iLang = $oLang->getBaseLanguage();
00736         return $this->_prepareFeedName( sprintf($oLang->translateString( 'RSS_RECOMMLISTARTICLES_TITLE', $iLang ), $oRecommList->oxrecommlists__oxtitle->value) );
00737     }
00738 
00746     public function getRecommListArticlesUrl(oxRecommList $oRecommList)
00747     {
00748         $oLang = oxLang::getInstance();
00749         $iLang = $oLang->getBaseLanguage();
00750         return $this->_prepareUrl("cl=rss&amp;fnc=recommlistarts&amp;recommid=".$oRecommList->getId(),
00751                 sprintf($oLang->translateString( 'RSS_RECOMMLISTARTICLES_URL', $iLang ), $oRecommList->oxrecommlists__oxtitle->value));
00752     }
00753 
00761     public function loadRecommListArticles(oxRecommList $oRecommList)
00762     {
00763         if ( ( $this->_aChannel = $this->_loadFromCache( self::RSS_RECOMMLISTARTS.$oRecommList->getId() ) ) ) {
00764             return;
00765         }
00766 
00767         $oList = oxNew( 'oxarticlelist' );
00768         $oList->loadRecommArticles( $oRecommList->getId(), ' order by oxobject2list.oxtimestamp desc limit '. $this->getConfig()->getConfigParam( 'iRssItemsCount' ) );
00769 
00770         $oLang = oxLang::getInstance();
00771         $this->_loadData(
00772             self::RSS_RECOMMLISTARTS.$oRecommList->getId(),
00773             $this->getRecommListArticlesTitle($oRecommList),
00774             sprintf($oLang->translateString( 'RSS_RECOMMLISTARTICLES_DESCRIPTION', $oLang->getBaseLanguage() ), $oRecommList->oxrecommlists__oxtitle->value),
00775             $this->_getArticleItems($oList),
00776             $this->getRecommListArticlesUrl($oRecommList),
00777             $oRecommList->getLink()
00778         );
00779     }
00780 
00788     public function getBargainTitle()
00789     {
00790         $oLang = oxLang::getInstance();
00791         $iLang = $oLang->getBaseLanguage();
00792         return $this->_prepareFeedName( $oLang->translateString( 'RSS_BARGAIN_TITLE', $iLang ) );
00793     }
00794 
00802     public function getBargainUrl()
00803     {
00804         return $this->_prepareUrl("cl=rss&amp;fnc=bargain", $this->getBargainTitle());
00805     }
00806 
00814     public function loadBargain()
00815     {
00816         if ( ( $this->_aChannel = $this->_loadFromCache( self::RSS_BARGAIN ) ) ) {
00817             return;
00818         }
00819 
00820         $oArtList = oxNew( 'oxarticlelist' );
00821         $oArtList->loadAktionArticles( 'OXBARGAIN' );
00822 
00823         $oLang = oxLang::getInstance();
00824         $this->_loadData(
00825             self::RSS_BARGAIN,
00826             $this->getBargainTitle(),
00827             $oLang->translateString( 'RSS_BARGAIN_DESCRIPTION', $oLang->getBaseLanguage() ),
00828             $this->_getArticleItems($oArtList),
00829             $this->getBargainUrl()
00830         );
00831     }
00832 
00833 }
00834 

Generated on Wed Apr 22 12:26:31 2009 for OXID eShop CE by  doxygen 1.5.5