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
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( 'timestamp' => time(), 'content' => $aContent );
00144 return oxUtils::getInstance()->toFileCache($this->_getCacheId($name), $aData);
00145 }
00146
00147
00156 protected function _getArticleItems(oxArticleList $oList)
00157 {
00158 $aItems = array();
00159 foreach ($oList as $oArticle) {
00160 $oItem = new oxStdClass();
00161 $oActCur = $this->getConfig()->getActShopCurrencyObject();
00162 $sPrice = $oArticle->getPriceFromPrefix().oxLang::getInstance()->formatCurrency( $oArticle->getPrice()->getBruttoPrice(), $oActCur );
00163 $oItem->title = strip_tags($oArticle->oxarticles__oxtitle->value . " " . $sPrice . " ". $oActCur->sign);
00164 $oItem->guid = $oItem->link = oxUtils::getInstance()->prepareUrlForNoSession($oArticle->getLink());
00165 $oItem->isGuidPermalink = true;
00166 $oItem->description = $oArticle->getArticleLongDesc()->value;
00167 if (trim(str_replace(' ', '', (strip_tags($oItem->description)))) == '') {
00168 $oItem->description = $oArticle->oxarticles__oxshortdesc->value;
00169 }
00170
00171 $oItem->description = trim($oItem->description);
00172 if ($sIcon = $oArticle->getIconUrl()) {
00173 $oItem->description = "<img src='$sIcon' border=0 align='left' hspace=5>".$oItem->description;
00174 }
00175 $oItem->description = htmlspecialchars($oItem->description);
00176
00177 $aItems[] = $oItem;
00178 }
00179 return $aItems;
00180 }
00181
00192 protected function _prepareUrl($sUri, $sTitle)
00193 {
00194 $iLang = oxLang::getInstance()->getBaseLanguage();
00195 $sUrl = $this->_getShopUrl();
00196 $sUrl .= $sUri.'&lang='.$iLang;
00197
00198 if ( oxUtils::getInstance()->seoIsActive() ) {
00199 $oEncoder = oxSeoEncoder::getInstance();
00200 $sUrl = $oEncoder->getDynamicUrl( $sUrl, "rss/{$sTitle}/", $iLang );
00201 }
00202
00203 return oxUtils::getInstance()->prepareUrlForNoSession( $sUrl );
00204 }
00205
00215 protected function _prepareFeedName($sTitle)
00216 {
00217 $oShop = $this->getConfig()->getActiveShop();
00218
00219 return $oShop->oxshops__oxname->value . "/" . $sTitle;
00220 }
00221
00228 protected function _getShopUrl()
00229 {
00230 $sUrl = $this->getConfig()->getShopUrl();
00231 if (strpos($sUrl, '?')) {
00232 if (!preg_match('/[?&](amp;)?$/i', $sUrl)) {
00233 $sUrl .= '&';
00234 }
00235 } else {
00236 $sUrl .= '?';
00237 }
00238 return $sUrl;
00239 }
00240
00254 protected function _loadData($sTag, $sTitle, $sDesc, $aItems, $sRssUrl, $sTargetUrl = null)
00255 {
00256 $this->_loadBaseChannel();
00257
00258 $this->_aChannel['selflink'] = $sRssUrl;
00259
00260 if ($sTargetUrl) {
00261 $this->_aChannel['link'] = $this->_aChannel['image']['link'] = $sTargetUrl;
00262 }
00263
00264 $this->_aChannel['image']['title'] = $this->_aChannel['title'] = $sTitle;
00265 $this->_aChannel['image']['description'] = $this->_aChannel['description'] = $sDesc;
00266
00267 $this->_aChannel['items'] = $aItems;
00268
00269 if ($sTag) {
00270 $this->_aChannel['lastBuildDate'] = $this->_getLastBuildDate($sTag, $this->_aChannel);
00271 $this->_saveToCache($sTag, $this->_aChannel);
00272 } else {
00273 $this->_aChannel['lastBuildDate'] = date('D, d M Y H:i:s O');
00274 }
00275 }
00276
00284 public function getTopInShopTitle()
00285 {
00286 $oLang = oxLang::getInstance();
00287 $iLang = $oLang->getBaseLanguage();
00288 return $this->_prepareFeedName( $oLang->translateString( 'RSS_TOPSHOP_TITLE', $iLang ) );
00289 }
00290
00298 public function getTopInShopUrl()
00299 {
00300 return $this->_prepareUrl("cl=rss&fnc=topshop", $this->getTopInShopTitle());
00301 }
00302
00310 public function loadTopInShop()
00311 {
00312 if ( ( $this->_aChannel = $this->_loadFromCache( self::RSS_TOPSHOP ) ) ) {
00313 return;
00314 }
00315
00316 $oArtList = oxNew( 'oxarticlelist' );
00317 $oArtList->loadTop5Articles();
00318
00319 $oLang = oxLang::getInstance();
00320 $this->_loadData(
00321 self::RSS_TOPSHOP,
00322 $this->getTopInShopTitle(),
00323 $oLang->translateString( 'RSS_TOPSHOP_DESCRIPTION', $oLang->getBaseLanguage() ),
00324 $this->_getArticleItems($oArtList),
00325 $this->getTopInShopUrl()
00326 );
00327 }
00328
00329
00330
00338 public function getNewestArticlesTitle()
00339 {
00340 $oLang = oxLang::getInstance();
00341 $iLang = $oLang->getBaseLanguage();
00342 return $this->_prepareFeedName( $oLang->translateString( 'RSS_NEWESTARTICLES_TITLE', $iLang ) );
00343 }
00344
00352 public function getNewestArticlesUrl()
00353 {
00354 return $this->_prepareUrl("cl=rss&fnc=newarts", $this->getNewestArticlesTitle());
00355 }
00356
00364 public function loadNewestArticles()
00365 {
00366 if ( ( $this->_aChannel = $this->_loadFromCache(self::RSS_NEWARTS ) ) ) {
00367 return;
00368 }
00369 $oArtList = oxNew( 'oxarticlelist' );
00370 $oArtList->loadNewestArticles( $this->getConfig()->getConfigParam( 'iRssItemsCount' ) );
00371
00372 $oLang = oxLang::getInstance();
00373 $this->_loadData(
00374 self::RSS_NEWARTS,
00375 $this->getNewestArticlesTitle( ),
00376 $oLang->translateString( 'RSS_NEWESTARTICLES_DESCRIPTION', $oLang->getBaseLanguage() ),
00377 $this->_getArticleItems($oArtList),
00378 $this->getNewestArticlesUrl()
00379 );
00380 }
00381
00382
00392 public function getCategoryArticlesTitle(oxCategory $oCat)
00393 {
00394 $oLang = oxLang::getInstance();
00395 $iLang = $oLang->getBaseLanguage();
00396 $sTitle = $this->_getCatPath($oCat);
00397 return $this->_prepareFeedName( $sTitle . $oLang->translateString( 'RSS_CATEGORYARTICLES_TITLE', $iLang ) );
00398 }
00399
00407 protected function _getCatPath( $oCat )
00408 {
00409 $sCatPathString = '';
00410 $sSep = '';
00411 while ( $oCat ) {
00412
00413 $sCatPathString = $oCat->oxcategories__oxtitle->value.$sSep.$sCatPathString ;
00414 $sSep = '/';
00415
00416 $oCat = $oCat->getParentCategory();
00417 }
00418 return $sCatPathString;
00419 }
00420
00430 public function getCategoryArticlesUrl(oxCategory $oCat)
00431 {
00432 $oLang = oxLang::getInstance();
00433 return $this->_prepareUrl("cl=rss&fnc=catarts&cat=".urlencode($oCat->getId()),
00434 sprintf($oLang->translateString( 'RSS_CATEGORYARTICLES_URL', $oLang->getBaseLanguage() ), $oCat->oxcategories__oxtitle->value));
00435 }
00436
00446 public function loadCategoryArticles( oxCategory $oCat )
00447 {
00448 $sId = $oCat->getId();
00449 if ( ( $this->_aChannel = $this->_loadFromCache( self::RSS_CATARTS.$sId ) ) ) {
00450 return;
00451 }
00452
00453 $oArtList = oxNew( 'oxarticlelist' );
00454 $oArtList->setCustomSorting('oc.oxtime desc');
00455 $oArtList->loadCategoryArticles($oCat->getId(), null, $this->getConfig()->getConfigParam( 'iRssItemsCount' ));
00456
00457 $oLang = oxLang::getInstance();
00458 $this->_loadData(
00459 self::RSS_CATARTS.$sId,
00460 $this->getCategoryArticlesTitle($oCat),
00461 sprintf($oLang->translateString( 'RSS_CATEGORYARTICLES_DESCRIPTION', $oLang->getBaseLanguage() ), $oCat->oxcategories__oxtitle->value),
00462 $this->_getArticleItems($oArtList),
00463 $this->getCategoryArticlesUrl($oCat),
00464 $oCat->getLink()
00465 );
00466 }
00467
00468
00480 public function getSearchArticlesTitle($sSearch, $sCatId, $sVendorId)
00481 {
00482 return $this->_prepareFeedName( htmlspecialchars($this->_getSearchParamsTranslation('RSS_SEARCHARTICLES_TITLE', $sSearch, $sCatId, $sVendorId)) );
00483 }
00484
00496 protected function _getSearchParamsUrl($sSearch, $sCatId, $sVendorId)
00497 {
00498 $sParams = "searchparam=".urlencode($sSearch);
00499 if ($sCatId) {
00500 $sParams .= "&searchcnid=".urlencode($sCatId);
00501 }
00502
00503 if ($sVendorId) {
00504 $sParams .= "&searchvendor=".urlencode($sVendorId);
00505 }
00506
00507 return $sParams;
00508 }
00509
00520 protected function _getObjectField($sId, $sObject, $sField)
00521 {
00522 if (!$sId) {
00523 return '';
00524 }
00525 $oObj = oxNew($sObject);
00526 if ($oObj->load($sId)) {
00527 return $oObj->$sField->value;
00528 }
00529 return '';
00530 }
00531
00544 protected function _getSearchParamsTranslation($sId, $sSearch, $sCatId, $sVendorId)
00545 {
00546 $oLang = oxLang::getInstance();
00547 $sCatTitle = '';
00548 if ($sTitle = $this->_getObjectField($sCatId, 'oxcategory', 'oxcategories__oxtitle')) {
00549 $sCatTitle = sprintf($oLang->translateString( 'RSS_SEARCHARTICLES_TAG_CATEGORY', $oLang->getBaseLanguage() ), $sTitle);
00550 }
00551 $sVendorTitle = '';
00552 if ($sTitle = $this->_getObjectField($sVendorId, 'oxvendor', 'oxvendor__oxtitle')) {
00553 $sVendorTitle = sprintf($oLang->translateString( 'RSS_SEARCHARTICLES_TAG_VENDOR', $oLang->getBaseLanguage() ), $sTitle);
00554 }
00555
00556 $sRet = sprintf($oLang->translateString( $sId, $oLang->getBaseLanguage() ), $sSearch);
00557
00558 $sRet = str_replace('<TAG_CATEGORY>', $sCatTitle, $sRet);
00559 $sRet = str_replace('<TAG_VENDOR>', $sVendorTitle, $sRet);
00560
00561 return $sRet;
00562 }
00563
00575 public function getSearchArticlesUrl( $sSearch, $sCatId, $sVendorId )
00576 {
00577 $oLang = oxLang::getInstance();
00578 $sUrl = $this->_prepareUrl("cl=rss&fnc=searcharts", $oLang->translateString( 'RSS_SEARCHARTICLES_URL', $oLang->getBaseLanguage()));
00579
00580 $sJoin = '?';
00581 if (strpos($sUrl, '?')) {
00582 $sJoin = '&';
00583 }
00584 return $sUrl.$sJoin.$this->_getSearchParamsUrl($sSearch, $sCatId, $sVendorId);
00585 }
00586
00598 public function loadSearchArticles( $sSearch, $sCatId, $sVendorId )
00599 {
00600
00601
00602
00603
00604
00605 $oConfig = $this->getConfig();
00606 $oConfig->setConfigParam('iNrofCatArticles', $oConfig->getConfigParam( 'iRssItemsCount' ));
00607
00608 $oArtList = oxNew( 'oxsearch' )->getSearchArticles($sSearch, $sCatId, $sVendorId, oxNew('oxarticle')->getViewName().'.oxtimestamp desc');
00609
00610 $this->_loadData(
00611
00612 null,
00613
00614 $this->getSearchArticlesTitle($sSearch, $sCatId, $sVendorId),
00615 $this->_getSearchParamsTranslation('RSS_SEARCHARTICLES_DESCRIPTION', htmlspecialchars($sSearch), $sCatId, $sVendorId),
00616 $this->_getArticleItems($oArtList),
00617 $this->getSearchArticlesUrl($sSearch, $sCatId, $sVendorId),
00618 $this->_getShopUrl()."cl=search&".$this->_getSearchParamsUrl($sSearch, $sCatId, $sVendorId)
00619 );
00620 }
00621
00629 public function getRecommListsTitle(oxArticle $oArticle)
00630 {
00631 $oLang = oxLang::getInstance();
00632 $iLang = $oLang->getBaseLanguage();
00633 return $this->_prepareFeedName( sprintf($oLang->translateString( 'RSS_ARTRECOMMLISTS_TITLE', $iLang ), $oArticle->oxarticles__oxtitle->value) );
00634 }
00635
00643 public function getRecommListsUrl(oxArticle $oArticle)
00644 {
00645 $oLang = oxLang::getInstance();
00646 $iLang = $oLang->getBaseLanguage();
00647 return $this->_prepareUrl("cl=rss&fnc=recommlists&anid=".$oArticle->getId(),
00648 sprintf($oLang->translateString( 'RSS_ARTRECOMMLISTS_URL', $iLang ), $oArticle->oxarticles__oxtitle->value));
00649 }
00650
00658 protected function _getRecommListItems($oList)
00659 {
00660 $aItems = array();
00661 foreach ($oList as $oRecommList) {
00662 $oItem = new oxStdClass();
00663 $oItem->title = $oRecommList->oxrecommlists__oxtitle->value;
00664 $oItem->guid = $oItem->link = oxUtils::getInstance()->prepareUrlForNoSession($oRecommList->getLink());
00665 $oItem->isGuidPermalink = true;
00666 $oItem->description = $oRecommList->oxrecommlists__oxdesc->value;
00667
00668 $aItems[] = $oItem;
00669 }
00670 return $aItems;
00671 }
00672
00680 public function loadRecommLists(oxArticle $oArticle)
00681 {
00682 if ( ( $this->_aChannel = $this->_loadFromCache( self::RSS_ARTRECOMMLISTS.$oArticle->getId() ) ) ) {
00683 return;
00684 }
00685
00686 $oConfig = $this->getConfig();
00687 $oConfig->setConfigParam('iNrofCrossellArticles', $oConfig->getConfigParam( 'iRssItemsCount' ));
00688
00689 $oList = oxNew( 'oxrecommlist' )->getRecommListsByIds(array($oArticle->getId()));
00690 if ($oList == null) {
00691 $oList = oxNew('oxlist');
00692 }
00693
00694 $oLang = oxLang::getInstance();
00695 $this->_loadData(
00696 self::RSS_ARTRECOMMLISTS.$oArticle->getId(),
00697 $this->getRecommListsTitle($oArticle),
00698 sprintf($oLang->translateString( 'RSS_ARTRECOMMLISTS_DESCRIPTION', $oLang->getBaseLanguage() ), $oArticle->oxarticles__oxtitle->value),
00699 $this->_getRecommListItems($oList),
00700 $this->getRecommListsUrl($oArticle),
00701 $oArticle->getLink()
00702 );
00703 }
00704
00712 public function getRecommListArticlesTitle(oxRecommList $oRecommList)
00713 {
00714 $oLang = oxLang::getInstance();
00715 $iLang = $oLang->getBaseLanguage();
00716 return $this->_prepareFeedName( sprintf($oLang->translateString( 'RSS_RECOMMLISTARTICLES_TITLE', $iLang ), $oRecommList->oxrecommlists__oxtitle->value) );
00717 }
00718
00726 public function getRecommListArticlesUrl(oxRecommList $oRecommList)
00727 {
00728 $oLang = oxLang::getInstance();
00729 $iLang = $oLang->getBaseLanguage();
00730 return $this->_prepareUrl("cl=rss&fnc=recommlistarts&recommid=".$oRecommList->getId(),
00731 sprintf($oLang->translateString( 'RSS_RECOMMLISTARTICLES_URL', $iLang ), $oRecommList->oxrecommlists__oxtitle->value));
00732 }
00733
00741 public function loadRecommListArticles(oxRecommList $oRecommList)
00742 {
00743 if ( ( $this->_aChannel = $this->_loadFromCache( self::RSS_RECOMMLISTARTS.$oRecommList->getId() ) ) ) {
00744 return;
00745 }
00746
00747 $oList = oxNew( 'oxarticlelist' );
00748 $oList->loadRecommArticles( $oRecommList->getId(), ' order by oxobject2list.oxtimestamp desc limit '. $this->getConfig()->getConfigParam( 'iRssItemsCount' ) );
00749
00750 $oLang = oxLang::getInstance();
00751 $this->_loadData(
00752 self::RSS_RECOMMLISTARTS.$oRecommList->getId(),
00753 $this->getRecommListArticlesTitle($oRecommList),
00754 sprintf($oLang->translateString( 'RSS_RECOMMLISTARTICLES_DESCRIPTION', $oLang->getBaseLanguage() ), $oRecommList->oxrecommlists__oxtitle->value),
00755 $this->_getArticleItems($oList),
00756 $this->getRecommListArticlesUrl($oRecommList),
00757 $oRecommList->getLink()
00758 );
00759 }
00760
00768 public function getBargainTitle()
00769 {
00770 $oLang = oxLang::getInstance();
00771 $iLang = $oLang->getBaseLanguage();
00772 return $this->_prepareFeedName( $oLang->translateString( 'RSS_BARGAIN_TITLE', $iLang ) );
00773 }
00774
00782 public function getBargainUrl()
00783 {
00784 return $this->_prepareUrl("cl=rss&fnc=bargain", $this->getBargainTitle());
00785 }
00786
00794 public function loadBargain()
00795 {
00796 if ( ( $this->_aChannel = $this->_loadFromCache( self::RSS_BARGAIN ) ) ) {
00797 return;
00798 }
00799
00800 $oArtList = oxNew( 'oxarticlelist' );
00801 $oArtList->loadAktionArticles( 'OXBARGAIN' );
00802
00803 $oLang = oxLang::getInstance();
00804 $this->_loadData(
00805 self::RSS_BARGAIN,
00806 $this->getBargainTitle(),
00807 $oLang->translateString( 'RSS_BARGAIN_DESCRIPTION', $oLang->getBaseLanguage() ),
00808 $this->_getArticleItems($oArtList),
00809 $this->getBargainUrl()
00810 );
00811 }
00812
00813 }
00814