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 $oItem->description = $oArticle->getLongDescription()->value;
00175 if (trim(str_replace(' ', '', (strip_tags($oItem->description)))) == '') {
00176 $oItem->description = $oArticle->oxarticles__oxshortdesc->value;
00177 }
00178
00179 $oItem->description = trim($oItem->description);
00180 if ( $sThumb = $oArticle->getThumbnailUrl() ) {
00181 $oItem->description = "<img src='$sThumb' border=0 align='left' hspace=5>".$oItem->description;
00182 }
00183 $oItem->description = $oStr->htmlspecialchars( $oItem->description );
00184
00185 if ( $oArticle->oxarticles__oxtimestamp->value ) {
00186 list($date, $time) = explode(' ', $oArticle->oxarticles__oxtimestamp->value);
00187 $date = explode('-', $date);
00188 $time = explode(':', $time);
00189 $oItem->date = date( 'D, d M Y H:i:s O', mktime($time[0], $time[1], $time[2], $date[1], $date[2], $date[0]) );
00190 } else {
00191 $oItem->date = date( 'D, d M Y H:i:s O', time() );
00192 }
00193
00194 $aItems[] = $oItem;
00195 }
00196 return $aItems;
00197 }
00198
00209 protected function _prepareUrl($sUri, $sTitle)
00210 {
00211 $iLang = oxLang::getInstance()->getBaseLanguage();
00212 $sUrl = $this->_getShopUrl();
00213 $sUrl .= $sUri.'&lang='.$iLang;
00214
00215 if ( oxUtils::getInstance()->seoIsActive() ) {
00216 $oEncoder = oxSeoEncoder::getInstance();
00217 $sUrl = $oEncoder->getDynamicUrl( $sUrl, "rss/{$sTitle}/", $iLang );
00218 }
00219
00220 return oxUtilsUrl::getInstance()->prepareUrlForNoSession( $sUrl );
00221 }
00222
00232 protected function _prepareFeedName($sTitle)
00233 {
00234 $oShop = $this->getConfig()->getActiveShop();
00235
00236 return $oShop->oxshops__oxname->value . "/" . $sTitle;
00237 }
00238
00245 protected function _getShopUrl()
00246 {
00247 $sUrl = $this->getConfig()->getShopUrl();
00248 $oStr = getStr();
00249 if ( $oStr->strpos($sUrl, '?') !== false ) {
00250 if ( !$oStr->preg_match('/[?&](amp;)?$/i', $sUrl)) {
00251 $sUrl .= '&';
00252 }
00253 } else {
00254 $sUrl .= '?';
00255 }
00256 return $sUrl;
00257 }
00258
00272 protected function _loadData($sTag, $sTitle, $sDesc, $aItems, $sRssUrl, $sTargetUrl = null)
00273 {
00274 $this->_loadBaseChannel();
00275
00276 $this->_aChannel['selflink'] = $sRssUrl;
00277
00278 if ($sTargetUrl) {
00279 $this->_aChannel['link'] = $this->_aChannel['image']['link'] = $sTargetUrl;
00280 }
00281
00282 $this->_aChannel['image']['title'] = $this->_aChannel['title'] = $sTitle;
00283 $this->_aChannel['image']['description'] = $this->_aChannel['description'] = $sDesc;
00284
00285 $this->_aChannel['items'] = $aItems;
00286
00287 if ($sTag) {
00288 $this->_aChannel['lastBuildDate'] = $this->_getLastBuildDate($sTag, $this->_aChannel);
00289 $this->_saveToCache($sTag, $this->_aChannel);
00290 } else {
00291 $this->_aChannel['lastBuildDate'] = date( 'D, d M Y H:i:s O', oxUtilsDate::getInstance()->getTime() );
00292 }
00293 }
00294
00302 public function getTopInShopTitle()
00303 {
00304 $oLang = oxLang::getInstance();
00305 $iLang = $oLang->getBaseLanguage();
00306 return $this->_prepareFeedName( $oLang->translateString( 'RSS_TOPSHOP_TITLE', $iLang ) );
00307 }
00308
00316 public function getTopInShopUrl()
00317 {
00318 return $this->_prepareUrl("cl=rss&fnc=topshop", $this->getTopInShopTitle());
00319 }
00320
00328 public function loadTopInShop()
00329 {
00330 if ( ( $this->_aChannel = $this->_loadFromCache( self::RSS_TOPSHOP ) ) ) {
00331 return;
00332 }
00333
00334 $oArtList = oxNew( 'oxarticlelist' );
00335 $oArtList->loadTop5Articles( $this->getConfig()->getConfigParam( 'iRssItemsCount' ) );
00336
00337 $oLang = oxLang::getInstance();
00338 $this->_loadData(
00339 self::RSS_TOPSHOP,
00340 $this->getTopInShopTitle(),
00341 $oLang->translateString( 'RSS_TOPSHOP_DESCRIPTION', $oLang->getBaseLanguage() ),
00342 $this->_getArticleItems($oArtList),
00343 $this->getTopInShopUrl()
00344 );
00345 }
00346
00347
00348
00356 public function getNewestArticlesTitle()
00357 {
00358 $oLang = oxLang::getInstance();
00359 $iLang = $oLang->getBaseLanguage();
00360 return $this->_prepareFeedName( $oLang->translateString( 'RSS_NEWESTARTICLES_TITLE', $iLang ) );
00361 }
00362
00370 public function getNewestArticlesUrl()
00371 {
00372 return $this->_prepareUrl("cl=rss&fnc=newarts", $this->getNewestArticlesTitle());
00373 }
00374
00382 public function loadNewestArticles()
00383 {
00384 if ( ( $this->_aChannel = $this->_loadFromCache(self::RSS_NEWARTS ) ) ) {
00385 return;
00386 }
00387 $oArtList = oxNew( 'oxarticlelist' );
00388 $oArtList->loadNewestArticles( $this->getConfig()->getConfigParam( 'iRssItemsCount' ) );
00389
00390 $oLang = oxLang::getInstance();
00391 $this->_loadData(
00392 self::RSS_NEWARTS,
00393 $this->getNewestArticlesTitle( ),
00394 $oLang->translateString( 'RSS_NEWESTARTICLES_DESCRIPTION', $oLang->getBaseLanguage() ),
00395 $this->_getArticleItems($oArtList),
00396 $this->getNewestArticlesUrl()
00397 );
00398 }
00399
00400
00410 public function getCategoryArticlesTitle(oxCategory $oCat)
00411 {
00412 $oLang = oxLang::getInstance();
00413 $iLang = $oLang->getBaseLanguage();
00414 $sTitle = $this->_getCatPath($oCat);
00415 return $this->_prepareFeedName( $sTitle . $oLang->translateString( 'RSS_CATEGORYARTICLES_TITLE', $iLang ) );
00416 }
00417
00425 protected function _getCatPath( $oCat )
00426 {
00427 $sCatPathString = '';
00428 $sSep = '';
00429 while ( $oCat ) {
00430
00431 $sCatPathString = $oCat->oxcategories__oxtitle->value.$sSep.$sCatPathString ;
00432 $sSep = '/';
00433
00434 $oCat = $oCat->getParentCategory();
00435 }
00436 return $sCatPathString;
00437 }
00438
00448 public function getCategoryArticlesUrl(oxCategory $oCat)
00449 {
00450 $oLang = oxLang::getInstance();
00451 return $this->_prepareUrl("cl=rss&fnc=catarts&cat=".urlencode($oCat->getId()),
00452 sprintf($oLang->translateString( 'RSS_CATEGORYARTICLES_URL', $oLang->getBaseLanguage() ), $oCat->oxcategories__oxtitle->value));
00453 }
00454
00464 public function loadCategoryArticles( oxCategory $oCat )
00465 {
00466 $sId = $oCat->getId();
00467 if ( ( $this->_aChannel = $this->_loadFromCache( self::RSS_CATARTS.$sId ) ) ) {
00468 return;
00469 }
00470
00471 $oArtList = oxNew( 'oxarticlelist' );
00472 $oArtList->setCustomSorting('oc.oxtime desc');
00473 $oArtList->loadCategoryArticles($oCat->getId(), null, $this->getConfig()->getConfigParam( 'iRssItemsCount' ));
00474
00475 $oLang = oxLang::getInstance();
00476 $this->_loadData(
00477 self::RSS_CATARTS.$sId,
00478 $this->getCategoryArticlesTitle($oCat),
00479 sprintf($oLang->translateString( 'RSS_CATEGORYARTICLES_DESCRIPTION', $oLang->getBaseLanguage() ), $oCat->oxcategories__oxtitle->value),
00480 $this->_getArticleItems($oArtList),
00481 $this->getCategoryArticlesUrl($oCat),
00482 $oCat->getLink()
00483 );
00484 }
00485
00486
00499 public function getSearchArticlesTitle($sSearch, $sCatId, $sVendorId, $sManufacturerId)
00500 {
00501 return $this->_prepareFeedName( getStr()->htmlspecialchars($this->_getSearchParamsTranslation('RSS_SEARCHARTICLES_TITLE', $sSearch, $sCatId, $sVendorId, $sManufacturerId)) );
00502 }
00503
00516 protected function _getSearchParamsUrl($sSearch, $sCatId, $sVendorId, $sManufacturerId)
00517 {
00518 $sParams = "searchparam=".urlencode($sSearch);
00519 if ($sCatId) {
00520 $sParams .= "&searchcnid=".urlencode($sCatId);
00521 }
00522
00523 if ($sVendorId) {
00524 $sParams .= "&searchvendor=".urlencode($sVendorId);
00525 }
00526
00527 if ($sManufacturerId) {
00528 $sParams .= "&searchmanufacturer=".urlencode($sManufacturerId);
00529 }
00530
00531 return $sParams;
00532 }
00533
00544 protected function _getObjectField($sId, $sObject, $sField)
00545 {
00546 if (!$sId) {
00547 return '';
00548 }
00549 $oObj = oxNew($sObject);
00550 if ($oObj->load($sId)) {
00551 return $oObj->$sField->value;
00552 }
00553 return '';
00554 }
00555
00569 protected function _getSearchParamsTranslation($sId, $sSearch, $sCatId, $sVendorId, $sManufacturerId)
00570 {
00571 $oLang = oxLang::getInstance();
00572 $sCatTitle = '';
00573 if ($sTitle = $this->_getObjectField($sCatId, 'oxcategory', 'oxcategories__oxtitle')) {
00574 $sCatTitle = sprintf($oLang->translateString( 'RSS_SEARCHARTICLES_TAG_CATEGORY', $oLang->getBaseLanguage() ), $sTitle);
00575 }
00576 $sVendorTitle = '';
00577 if ($sTitle = $this->_getObjectField($sVendorId, 'oxvendor', 'oxvendor__oxtitle')) {
00578 $sVendorTitle = sprintf($oLang->translateString( 'RSS_SEARCHARTICLES_TAG_VENDOR', $oLang->getBaseLanguage() ), $sTitle);
00579 }
00580 $sManufacturerTitle = '';
00581 if ($sTitle = $this->_getObjectField($sManufacturerId, 'oxmanufacturer', 'oxmanufacturers__oxtitle')) {
00582 $sManufacturerTitle = sprintf($oLang->translateString( 'RSS_SEARCHARTICLES_TAG_MANUFACTURER', $oLang->getBaseLanguage() ), $sTitle);
00583 }
00584
00585 $sRet = sprintf($oLang->translateString( $sId, $oLang->getBaseLanguage() ), $sSearch);
00586
00587 $sRet = str_replace('<TAG_CATEGORY>', $sCatTitle, $sRet);
00588 $sRet = str_replace('<TAG_VENDOR>', $sVendorTitle, $sRet);
00589 $sRet = str_replace('<TAG_MANUFACTURER>', $sManufacturerTitle, $sRet);
00590
00591 return $sRet;
00592 }
00593
00606 public function getSearchArticlesUrl( $sSearch, $sCatId, $sVendorId, $sManufacturerId )
00607 {
00608 $oLang = oxLang::getInstance();
00609 $sUrl = $this->_prepareUrl("cl=rss&fnc=searcharts", $oLang->translateString( 'RSS_SEARCHARTICLES_URL', $oLang->getBaseLanguage()));
00610
00611 $sJoin = '?';
00612 if (strpos($sUrl, '?') !== false) {
00613 $sJoin = '&';
00614 }
00615 return $sUrl.$sJoin.$this->_getSearchParamsUrl($sSearch, $sCatId, $sVendorId, $sManufacturerId);
00616 }
00617
00630 public function loadSearchArticles( $sSearch, $sCatId, $sVendorId, $sManufacturerId )
00631 {
00632
00633
00634
00635
00636
00637 $oConfig = $this->getConfig();
00638 $oConfig->setConfigParam('iNrofCatArticles', $oConfig->getConfigParam( 'iRssItemsCount' ));
00639
00640 $oArtList = oxNew( 'oxsearch' )->getSearchArticles($sSearch, $sCatId, $sVendorId, $sManufacturerId, oxNew('oxarticle')->getViewName().'.oxtimestamp desc');
00641
00642 $this->_loadData(
00643
00644 null,
00645
00646 $this->getSearchArticlesTitle($sSearch, $sCatId, $sVendorId, $sManufacturerId),
00647 $this->_getSearchParamsTranslation('RSS_SEARCHARTICLES_DESCRIPTION', getStr()->htmlspecialchars( $sSearch ), $sCatId, $sVendorId, $sManufacturerId),
00648 $this->_getArticleItems($oArtList),
00649 $this->getSearchArticlesUrl($sSearch, $sCatId, $sVendorId, $sManufacturerId),
00650 $this->_getShopUrl()."cl=search&".$this->_getSearchParamsUrl($sSearch, $sCatId, $sVendorId, $sManufacturerId)
00651 );
00652 }
00653
00661 public function getRecommListsTitle(oxArticle $oArticle)
00662 {
00663 $oLang = oxLang::getInstance();
00664 $iLang = $oLang->getBaseLanguage();
00665 return $this->_prepareFeedName( sprintf($oLang->translateString( 'RSS_ARTRECOMMLISTS_TITLE', $iLang ), $oArticle->oxarticles__oxtitle->value) );
00666 }
00667
00675 public function getRecommListsUrl(oxArticle $oArticle)
00676 {
00677 $oLang = oxLang::getInstance();
00678 $iLang = $oLang->getBaseLanguage();
00679 return $this->_prepareUrl("cl=rss&fnc=recommlists&anid=".$oArticle->getId(),
00680 sprintf($oLang->translateString( 'RSS_ARTRECOMMLISTS_URL', $iLang ), $oArticle->oxarticles__oxtitle->value));
00681 }
00682
00690 protected function _getRecommListItems($oList)
00691 {
00692 $myUtilsUrl = oxUtilsUrl::getInstance();
00693 $aItems = array();
00694 foreach ($oList as $oRecommList) {
00695 $oItem = new oxStdClass();
00696 $oItem->title = $oRecommList->oxrecommlists__oxtitle->value;
00697 $oItem->guid = $oItem->link = $myUtilsUrl->prepareUrlForNoSession($oRecommList->getLink());
00698 $oItem->isGuidPermalink = true;
00699 $oItem->description = $oRecommList->oxrecommlists__oxdesc->value;
00700
00701 $aItems[] = $oItem;
00702 }
00703 return $aItems;
00704 }
00705
00713 public function loadRecommLists(oxArticle $oArticle)
00714 {
00715 if ( ( $this->_aChannel = $this->_loadFromCache( self::RSS_ARTRECOMMLISTS.$oArticle->getId() ) ) ) {
00716 return;
00717 }
00718
00719 $oConfig = $this->getConfig();
00720 $oConfig->setConfigParam('iNrofCrossellArticles', $oConfig->getConfigParam( 'iRssItemsCount' ));
00721
00722 $oList = oxNew( 'oxrecommlist' )->getRecommListsByIds(array($oArticle->getId()));
00723 if ($oList == null) {
00724 $oList = oxNew('oxlist');
00725 }
00726
00727 $oLang = oxLang::getInstance();
00728 $this->_loadData(
00729 self::RSS_ARTRECOMMLISTS.$oArticle->getId(),
00730 $this->getRecommListsTitle($oArticle),
00731 sprintf($oLang->translateString( 'RSS_ARTRECOMMLISTS_DESCRIPTION', $oLang->getBaseLanguage() ), $oArticle->oxarticles__oxtitle->value),
00732 $this->_getRecommListItems($oList),
00733 $this->getRecommListsUrl($oArticle),
00734 $oArticle->getLink()
00735 );
00736 }
00737
00745 public function getRecommListArticlesTitle(oxRecommList $oRecommList)
00746 {
00747 $oLang = oxLang::getInstance();
00748 $iLang = $oLang->getBaseLanguage();
00749 return $this->_prepareFeedName( sprintf($oLang->translateString( 'RSS_RECOMMLISTARTICLES_TITLE', $iLang ), $oRecommList->oxrecommlists__oxtitle->value) );
00750 }
00751
00759 public function getRecommListArticlesUrl(oxRecommList $oRecommList)
00760 {
00761 $oLang = oxLang::getInstance();
00762 $iLang = $oLang->getBaseLanguage();
00763 return $this->_prepareUrl("cl=rss&fnc=recommlistarts&recommid=".$oRecommList->getId(),
00764 sprintf($oLang->translateString( 'RSS_RECOMMLISTARTICLES_URL', $iLang ), $oRecommList->oxrecommlists__oxtitle->value));
00765 }
00766
00774 public function loadRecommListArticles(oxRecommList $oRecommList)
00775 {
00776 if ( ( $this->_aChannel = $this->_loadFromCache( self::RSS_RECOMMLISTARTS.$oRecommList->getId() ) ) ) {
00777 return;
00778 }
00779
00780 $oList = oxNew( 'oxarticlelist' );
00781 $oList->loadRecommArticles( $oRecommList->getId(), ' order by oxobject2list.oxtimestamp desc limit '. $this->getConfig()->getConfigParam( 'iRssItemsCount' ) );
00782
00783 $oLang = oxLang::getInstance();
00784 $this->_loadData(
00785 self::RSS_RECOMMLISTARTS.$oRecommList->getId(),
00786 $this->getRecommListArticlesTitle($oRecommList),
00787 sprintf($oLang->translateString( 'RSS_RECOMMLISTARTICLES_DESCRIPTION', $oLang->getBaseLanguage() ), $oRecommList->oxrecommlists__oxtitle->value),
00788 $this->_getArticleItems($oList),
00789 $this->getRecommListArticlesUrl($oRecommList),
00790 $oRecommList->getLink()
00791 );
00792 }
00793
00801 public function getBargainTitle()
00802 {
00803 $oLang = oxLang::getInstance();
00804 $iLang = $oLang->getBaseLanguage();
00805 return $this->_prepareFeedName( $oLang->translateString( 'RSS_BARGAIN_TITLE', $iLang ) );
00806 }
00807
00815 public function getBargainUrl()
00816 {
00817 return $this->_prepareUrl("cl=rss&fnc=bargain", $this->getBargainTitle());
00818 }
00819
00827 public function loadBargain()
00828 {
00829 if ( ( $this->_aChannel = $this->_loadFromCache( self::RSS_BARGAIN ) ) ) {
00830 return;
00831 }
00832
00833 $oArtList = oxNew( 'oxarticlelist' );
00834 $oArtList->loadAktionArticles( 'OXBARGAIN', $this->getConfig()->getConfigParam( 'iRssItemsCount' ) );
00835
00836 $oLang = oxLang::getInstance();
00837 $this->_loadData(
00838 self::RSS_BARGAIN,
00839 $this->getBargainTitle(),
00840 $oLang->translateString( 'RSS_BARGAIN_DESCRIPTION', $oLang->getBaseLanguage() ),
00841 $this->_getArticleItems($oArtList),
00842 $this->getBargainUrl()
00843 );
00844 }
00845
00846 }
00847