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()->getShopUrl());
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
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
00175 if ( oxConfig::getInstance()->getConfigParam( 'bl_perfParseLongDescinSmarty' ) ) {
00176 $oItem->description = $oArticle->getLongDesc();
00177 } else {
00178 $oItem->description = $oArticle->getArticleLongDesc()->value;
00179 }
00180
00181 if (trim(str_replace(' ', '', (strip_tags($oItem->description)))) == '') {
00182 $oItem->description = $oArticle->oxarticles__oxshortdesc->value;
00183 }
00184
00185 $oItem->description = trim($oItem->description);
00186 if ($sIcon = $oArticle->getIconUrl()) {
00187 $oItem->description = "<img src='$sIcon' border=0 align='left' hspace=5>".$oItem->description;
00188 }
00189 $oItem->description = $oStr->htmlspecialchars( $oItem->description );
00190
00191 $aItems[] = $oItem;
00192 }
00193 return $aItems;
00194 }
00195
00206 protected function _prepareUrl($sUri, $sTitle)
00207 {
00208 $iLang = oxLang::getInstance()->getBaseLanguage();
00209 $sUrl = $this->_getShopUrl();
00210 $sUrl .= $sUri.'&lang='.$iLang;
00211
00212 if ( oxUtils::getInstance()->seoIsActive() ) {
00213 $oEncoder = oxSeoEncoder::getInstance();
00214 $sUrl = $oEncoder->getDynamicUrl( $sUrl, "rss/{$sTitle}/", $iLang );
00215 }
00216
00217 return oxUtilsUrl::getInstance()->prepareUrlForNoSession( $sUrl );
00218 }
00219
00229 protected function _prepareFeedName($sTitle)
00230 {
00231 $oShop = $this->getConfig()->getActiveShop();
00232
00233 return $oShop->oxshops__oxname->value . "/" . $sTitle;
00234 }
00235
00242 protected function _getShopUrl()
00243 {
00244 $sUrl = $this->getConfig()->getShopUrl();
00245 $oStr = getStr();
00246 if ( $oStr->strpos($sUrl, '?') !== false ) {
00247 if ( !$oStr->preg_match('/[?&](amp;)?$/i', $sUrl)) {
00248 $sUrl .= '&';
00249 }
00250 } else {
00251 $sUrl .= '?';
00252 }
00253 return $sUrl;
00254 }
00255
00269 protected function _loadData($sTag, $sTitle, $sDesc, $aItems, $sRssUrl, $sTargetUrl = null)
00270 {
00271 $this->_loadBaseChannel();
00272
00273 $this->_aChannel['selflink'] = $sRssUrl;
00274
00275 if ($sTargetUrl) {
00276 $this->_aChannel['link'] = $this->_aChannel['image']['link'] = $sTargetUrl;
00277 }
00278
00279 $this->_aChannel['image']['title'] = $this->_aChannel['title'] = $sTitle;
00280 $this->_aChannel['image']['description'] = $this->_aChannel['description'] = $sDesc;
00281
00282 $this->_aChannel['items'] = $aItems;
00283
00284 if ($sTag) {
00285 $this->_aChannel['lastBuildDate'] = $this->_getLastBuildDate($sTag, $this->_aChannel);
00286 $this->_saveToCache($sTag, $this->_aChannel);
00287 } else {
00288 $this->_aChannel['lastBuildDate'] = date( 'D, d M Y H:i:s O', oxUtilsDate::getInstance()->getTime() );
00289 }
00290 }
00291
00299 public function getTopInShopTitle()
00300 {
00301 $oLang = oxLang::getInstance();
00302 $iLang = $oLang->getBaseLanguage();
00303 return $this->_prepareFeedName( $oLang->translateString( 'RSS_TOPSHOP_TITLE', $iLang ) );
00304 }
00305
00313 public function getTopInShopUrl()
00314 {
00315 return $this->_prepareUrl("cl=rss&fnc=topshop", $this->getTopInShopTitle());
00316 }
00317
00325 public function loadTopInShop()
00326 {
00327 if ( ( $this->_aChannel = $this->_loadFromCache( self::RSS_TOPSHOP ) ) ) {
00328 return;
00329 }
00330
00331 $oArtList = oxNew( 'oxarticlelist' );
00332 $oArtList->loadTop5Articles( $this->getConfig()->getConfigParam( 'iRssItemsCount' ) );
00333
00334 $oLang = oxLang::getInstance();
00335 $this->_loadData(
00336 self::RSS_TOPSHOP,
00337 $this->getTopInShopTitle(),
00338 $oLang->translateString( 'RSS_TOPSHOP_DESCRIPTION', $oLang->getBaseLanguage() ),
00339 $this->_getArticleItems($oArtList),
00340 $this->getTopInShopUrl()
00341 );
00342 }
00343
00344
00345
00353 public function getNewestArticlesTitle()
00354 {
00355 $oLang = oxLang::getInstance();
00356 $iLang = $oLang->getBaseLanguage();
00357 return $this->_prepareFeedName( $oLang->translateString( 'RSS_NEWESTARTICLES_TITLE', $iLang ) );
00358 }
00359
00367 public function getNewestArticlesUrl()
00368 {
00369 return $this->_prepareUrl("cl=rss&fnc=newarts", $this->getNewestArticlesTitle());
00370 }
00371
00379 public function loadNewestArticles()
00380 {
00381 if ( ( $this->_aChannel = $this->_loadFromCache(self::RSS_NEWARTS ) ) ) {
00382 return;
00383 }
00384 $oArtList = oxNew( 'oxarticlelist' );
00385 $oArtList->loadNewestArticles( $this->getConfig()->getConfigParam( 'iRssItemsCount' ) );
00386
00387 $oLang = oxLang::getInstance();
00388 $this->_loadData(
00389 self::RSS_NEWARTS,
00390 $this->getNewestArticlesTitle( ),
00391 $oLang->translateString( 'RSS_NEWESTARTICLES_DESCRIPTION', $oLang->getBaseLanguage() ),
00392 $this->_getArticleItems($oArtList),
00393 $this->getNewestArticlesUrl()
00394 );
00395 }
00396
00397
00407 public function getCategoryArticlesTitle(oxCategory $oCat)
00408 {
00409 $oLang = oxLang::getInstance();
00410 $iLang = $oLang->getBaseLanguage();
00411 $sTitle = $this->_getCatPath($oCat);
00412 return $this->_prepareFeedName( $sTitle . $oLang->translateString( 'RSS_CATEGORYARTICLES_TITLE', $iLang ) );
00413 }
00414
00422 protected function _getCatPath( $oCat )
00423 {
00424 $sCatPathString = '';
00425 $sSep = '';
00426 while ( $oCat ) {
00427
00428 $sCatPathString = $oCat->oxcategories__oxtitle->value.$sSep.$sCatPathString ;
00429 $sSep = '/';
00430
00431 $oCat = $oCat->getParentCategory();
00432 }
00433 return $sCatPathString;
00434 }
00435
00445 public function getCategoryArticlesUrl(oxCategory $oCat)
00446 {
00447 $oLang = oxLang::getInstance();
00448 return $this->_prepareUrl("cl=rss&fnc=catarts&cat=".urlencode($oCat->getId()),
00449 sprintf($oLang->translateString( 'RSS_CATEGORYARTICLES_URL', $oLang->getBaseLanguage() ), $oCat->oxcategories__oxtitle->value));
00450 }
00451
00461 public function loadCategoryArticles( oxCategory $oCat )
00462 {
00463 $sId = $oCat->getId();
00464 if ( ( $this->_aChannel = $this->_loadFromCache( self::RSS_CATARTS.$sId ) ) ) {
00465 return;
00466 }
00467
00468 $oArtList = oxNew( 'oxarticlelist' );
00469 $oArtList->setCustomSorting('oc.oxtime desc');
00470 $oArtList->loadCategoryArticles($oCat->getId(), null, $this->getConfig()->getConfigParam( 'iRssItemsCount' ));
00471
00472 $oLang = oxLang::getInstance();
00473 $this->_loadData(
00474 self::RSS_CATARTS.$sId,
00475 $this->getCategoryArticlesTitle($oCat),
00476 sprintf($oLang->translateString( 'RSS_CATEGORYARTICLES_DESCRIPTION', $oLang->getBaseLanguage() ), $oCat->oxcategories__oxtitle->value),
00477 $this->_getArticleItems($oArtList),
00478 $this->getCategoryArticlesUrl($oCat),
00479 $oCat->getLink()
00480 );
00481 }
00482
00483
00496 public function getSearchArticlesTitle($sSearch, $sCatId, $sVendorId, $sManufacturerId)
00497 {
00498 return $this->_prepareFeedName( getStr()->htmlspecialchars($this->_getSearchParamsTranslation('RSS_SEARCHARTICLES_TITLE', $sSearch, $sCatId, $sVendorId, $sManufacturerId)) );
00499 }
00500
00513 protected function _getSearchParamsUrl($sSearch, $sCatId, $sVendorId, $sManufacturerId)
00514 {
00515 $sParams = "searchparam=".urlencode($sSearch);
00516 if ($sCatId) {
00517 $sParams .= "&searchcnid=".urlencode($sCatId);
00518 }
00519
00520 if ($sVendorId) {
00521 $sParams .= "&searchvendor=".urlencode($sVendorId);
00522 }
00523
00524 if ($sManufacturerId) {
00525 $sParams .= "&searchmanufacturer=".urlencode($sManufacturerId);
00526 }
00527
00528 return $sParams;
00529 }
00530
00541 protected function _getObjectField($sId, $sObject, $sField)
00542 {
00543 if (!$sId) {
00544 return '';
00545 }
00546 $oObj = oxNew($sObject);
00547 if ($oObj->load($sId)) {
00548 return $oObj->$sField->value;
00549 }
00550 return '';
00551 }
00552
00566 protected function _getSearchParamsTranslation($sId, $sSearch, $sCatId, $sVendorId, $sManufacturerId)
00567 {
00568 $oLang = oxLang::getInstance();
00569 $sCatTitle = '';
00570 if ($sTitle = $this->_getObjectField($sCatId, 'oxcategory', 'oxcategories__oxtitle')) {
00571 $sCatTitle = sprintf($oLang->translateString( 'RSS_SEARCHARTICLES_TAG_CATEGORY', $oLang->getBaseLanguage() ), $sTitle);
00572 }
00573 $sVendorTitle = '';
00574 if ($sTitle = $this->_getObjectField($sVendorId, 'oxvendor', 'oxvendor__oxtitle')) {
00575 $sVendorTitle = sprintf($oLang->translateString( 'RSS_SEARCHARTICLES_TAG_VENDOR', $oLang->getBaseLanguage() ), $sTitle);
00576 }
00577 $sManufacturerTitle = '';
00578 if ($sTitle = $this->_getObjectField($sManufacturerId, 'oxmanufacturer', 'oxmanufacturers__oxtitle')) {
00579 $sManufacturerTitle = sprintf($oLang->translateString( 'RSS_SEARCHARTICLES_TAG_MANUFACTURER', $oLang->getBaseLanguage() ), $sTitle);
00580 }
00581
00582 $sRet = sprintf($oLang->translateString( $sId, $oLang->getBaseLanguage() ), $sSearch);
00583
00584 $sRet = str_replace('<TAG_CATEGORY>', $sCatTitle, $sRet);
00585 $sRet = str_replace('<TAG_VENDOR>', $sVendorTitle, $sRet);
00586 $sRet = str_replace('<TAG_MANUFACTURER>', $sManufacturerTitle, $sRet);
00587
00588 return $sRet;
00589 }
00590
00603 public function getSearchArticlesUrl( $sSearch, $sCatId, $sVendorId, $sManufacturerId )
00604 {
00605 $oLang = oxLang::getInstance();
00606 $sUrl = $this->_prepareUrl("cl=rss&fnc=searcharts", $oLang->translateString( 'RSS_SEARCHARTICLES_URL', $oLang->getBaseLanguage()));
00607
00608 $sJoin = '?';
00609 if (strpos($sUrl, '?') !== false) {
00610 $sJoin = '&';
00611 }
00612 return $sUrl.$sJoin.$this->_getSearchParamsUrl($sSearch, $sCatId, $sVendorId, $sManufacturerId);
00613 }
00614
00627 public function loadSearchArticles( $sSearch, $sCatId, $sVendorId, $sManufacturerId )
00628 {
00629
00630
00631
00632
00633
00634 $oConfig = $this->getConfig();
00635 $oConfig->setConfigParam('iNrofCatArticles', $oConfig->getConfigParam( 'iRssItemsCount' ));
00636
00637 $oArtList = oxNew( 'oxsearch' )->getSearchArticles($sSearch, $sCatId, $sVendorId, $sManufacturerId, oxNew('oxarticle')->getViewName().'.oxtimestamp desc');
00638
00639 $this->_loadData(
00640
00641 null,
00642
00643 $this->getSearchArticlesTitle($sSearch, $sCatId, $sVendorId, $sManufacturerId),
00644 $this->_getSearchParamsTranslation('RSS_SEARCHARTICLES_DESCRIPTION', getStr()->htmlspecialchars( $sSearch ), $sCatId, $sVendorId, $sManufacturerId),
00645 $this->_getArticleItems($oArtList),
00646 $this->getSearchArticlesUrl($sSearch, $sCatId, $sVendorId, $sManufacturerId),
00647 $this->_getShopUrl()."cl=search&".$this->_getSearchParamsUrl($sSearch, $sCatId, $sVendorId, $sManufacturerId)
00648 );
00649 }
00650
00658 public function getRecommListsTitle(oxArticle $oArticle)
00659 {
00660 $oLang = oxLang::getInstance();
00661 $iLang = $oLang->getBaseLanguage();
00662 return $this->_prepareFeedName( sprintf($oLang->translateString( 'RSS_ARTRECOMMLISTS_TITLE', $iLang ), $oArticle->oxarticles__oxtitle->value) );
00663 }
00664
00672 public function getRecommListsUrl(oxArticle $oArticle)
00673 {
00674 $oLang = oxLang::getInstance();
00675 $iLang = $oLang->getBaseLanguage();
00676 return $this->_prepareUrl("cl=rss&fnc=recommlists&anid=".$oArticle->getId(),
00677 sprintf($oLang->translateString( 'RSS_ARTRECOMMLISTS_URL', $iLang ), $oArticle->oxarticles__oxtitle->value));
00678 }
00679
00687 protected function _getRecommListItems($oList)
00688 {
00689 $myUtilsUrl = oxUtilsUrl::getInstance();
00690 $aItems = array();
00691 foreach ($oList as $oRecommList) {
00692 $oItem = new oxStdClass();
00693 $oItem->title = $oRecommList->oxrecommlists__oxtitle->value;
00694 $oItem->guid = $oItem->link = $myUtilsUrl->prepareUrlForNoSession($oRecommList->getLink());
00695 $oItem->isGuidPermalink = true;
00696 $oItem->description = $oRecommList->oxrecommlists__oxdesc->value;
00697
00698 $aItems[] = $oItem;
00699 }
00700 return $aItems;
00701 }
00702
00710 public function loadRecommLists(oxArticle $oArticle)
00711 {
00712 if ( ( $this->_aChannel = $this->_loadFromCache( self::RSS_ARTRECOMMLISTS.$oArticle->getId() ) ) ) {
00713 return;
00714 }
00715
00716 $oConfig = $this->getConfig();
00717 $oConfig->setConfigParam('iNrofCrossellArticles', $oConfig->getConfigParam( 'iRssItemsCount' ));
00718
00719 $oList = oxNew( 'oxrecommlist' )->getRecommListsByIds(array($oArticle->getId()));
00720 if ($oList == null) {
00721 $oList = oxNew('oxlist');
00722 }
00723
00724 $oLang = oxLang::getInstance();
00725 $this->_loadData(
00726 self::RSS_ARTRECOMMLISTS.$oArticle->getId(),
00727 $this->getRecommListsTitle($oArticle),
00728 sprintf($oLang->translateString( 'RSS_ARTRECOMMLISTS_DESCRIPTION', $oLang->getBaseLanguage() ), $oArticle->oxarticles__oxtitle->value),
00729 $this->_getRecommListItems($oList),
00730 $this->getRecommListsUrl($oArticle),
00731 $oArticle->getLink()
00732 );
00733 }
00734
00742 public function getRecommListArticlesTitle(oxRecommList $oRecommList)
00743 {
00744 $oLang = oxLang::getInstance();
00745 $iLang = $oLang->getBaseLanguage();
00746 return $this->_prepareFeedName( sprintf($oLang->translateString( 'RSS_RECOMMLISTARTICLES_TITLE', $iLang ), $oRecommList->oxrecommlists__oxtitle->value) );
00747 }
00748
00756 public function getRecommListArticlesUrl(oxRecommList $oRecommList)
00757 {
00758 $oLang = oxLang::getInstance();
00759 $iLang = $oLang->getBaseLanguage();
00760 return $this->_prepareUrl("cl=rss&fnc=recommlistarts&recommid=".$oRecommList->getId(),
00761 sprintf($oLang->translateString( 'RSS_RECOMMLISTARTICLES_URL', $iLang ), $oRecommList->oxrecommlists__oxtitle->value));
00762 }
00763
00771 public function loadRecommListArticles(oxRecommList $oRecommList)
00772 {
00773 if ( ( $this->_aChannel = $this->_loadFromCache( self::RSS_RECOMMLISTARTS.$oRecommList->getId() ) ) ) {
00774 return;
00775 }
00776
00777 $oList = oxNew( 'oxarticlelist' );
00778 $oList->loadRecommArticles( $oRecommList->getId(), ' order by oxobject2list.oxtimestamp desc limit '. $this->getConfig()->getConfigParam( 'iRssItemsCount' ) );
00779
00780 $oLang = oxLang::getInstance();
00781 $this->_loadData(
00782 self::RSS_RECOMMLISTARTS.$oRecommList->getId(),
00783 $this->getRecommListArticlesTitle($oRecommList),
00784 sprintf($oLang->translateString( 'RSS_RECOMMLISTARTICLES_DESCRIPTION', $oLang->getBaseLanguage() ), $oRecommList->oxrecommlists__oxtitle->value),
00785 $this->_getArticleItems($oList),
00786 $this->getRecommListArticlesUrl($oRecommList),
00787 $oRecommList->getLink()
00788 );
00789 }
00790
00798 public function getBargainTitle()
00799 {
00800 $oLang = oxLang::getInstance();
00801 $iLang = $oLang->getBaseLanguage();
00802 return $this->_prepareFeedName( $oLang->translateString( 'RSS_BARGAIN_TITLE', $iLang ) );
00803 }
00804
00812 public function getBargainUrl()
00813 {
00814 return $this->_prepareUrl("cl=rss&fnc=bargain", $this->getBargainTitle());
00815 }
00816
00824 public function loadBargain()
00825 {
00826 if ( ( $this->_aChannel = $this->_loadFromCache( self::RSS_BARGAIN ) ) ) {
00827 return;
00828 }
00829
00830 $oArtList = oxNew( 'oxarticlelist' );
00831 $oArtList->loadAktionArticles( 'OXBARGAIN', $this->getConfig()->getConfigParam( 'iRssItemsCount' ) );
00832
00833 $oLang = oxLang::getInstance();
00834 $this->_loadData(
00835 self::RSS_BARGAIN,
00836 $this->getBargainTitle(),
00837 $oLang->translateString( 'RSS_BARGAIN_DESCRIPTION', $oLang->getBaseLanguage() ),
00838 $this->_getArticleItems($oArtList),
00839 $this->getBargainUrl()
00840 );
00841 }
00842
00843 }
00844