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
00176 if ( oxConfig::getInstance()->getConfigParam( 'bl_perfParseLongDescinSmarty' ) ) {
00177 $oItem->description = $oArticle->getLongDesc();
00178 } else {
00179 $oItem->description = $oArticle->getLongDescription()->value;
00180 }
00181
00182 if (trim(str_replace(' ', '', (strip_tags($oItem->description)))) == '') {
00183 $oItem->description = $oArticle->oxarticles__oxshortdesc->value;
00184 }
00185
00186 $oItem->description = trim($oItem->description);
00187 if ( $sThumb = $oArticle->getThumbnailUrl() ) {
00188 $oItem->description = "<img src='$sThumb' border=0 align='left' hspace=5>".$oItem->description;
00189 }
00190 $oItem->description = $oStr->htmlspecialchars( $oItem->description );
00191
00192 if ( $oArticle->oxarticles__oxtimestamp->value ) {
00193 list($date, $time) = explode(' ', $oArticle->oxarticles__oxtimestamp->value);
00194 $date = explode('-', $date);
00195 $time = explode(':', $time);
00196 $oItem->date = date( 'D, d M Y H:i:s O', mktime($time[0], $time[1], $time[2], $date[1], $date[2], $date[0]) );
00197 } else {
00198 $oItem->date = date( 'D, d M Y H:i:s O', time() );
00199 }
00200
00201 $aItems[] = $oItem;
00202 }
00203 return $aItems;
00204 }
00205
00216 protected function _prepareUrl($sUri, $sTitle)
00217 {
00218 $iLang = oxLang::getInstance()->getBaseLanguage();
00219 $sUrl = $this->_getShopUrl();
00220 $sUrl .= $sUri.'&lang='.$iLang;
00221
00222 if ( oxUtils::getInstance()->seoIsActive() ) {
00223 $oEncoder = oxSeoEncoder::getInstance();
00224 $sUrl = $oEncoder->getDynamicUrl( $sUrl, "rss/{$sTitle}/", $iLang );
00225 }
00226
00227 return oxUtilsUrl::getInstance()->prepareUrlForNoSession( $sUrl );
00228 }
00229
00239 protected function _prepareFeedName($sTitle)
00240 {
00241 $oShop = $this->getConfig()->getActiveShop();
00242
00243 return $oShop->oxshops__oxname->value . "/" . $sTitle;
00244 }
00245
00252 protected function _getShopUrl()
00253 {
00254 $sUrl = $this->getConfig()->getShopUrl();
00255 $oStr = getStr();
00256 if ( $oStr->strpos($sUrl, '?') !== false ) {
00257 if ( !$oStr->preg_match('/[?&](amp;)?$/i', $sUrl)) {
00258 $sUrl .= '&';
00259 }
00260 } else {
00261 $sUrl .= '?';
00262 }
00263 return $sUrl;
00264 }
00265
00279 protected function _loadData($sTag, $sTitle, $sDesc, $aItems, $sRssUrl, $sTargetUrl = null)
00280 {
00281 $this->_loadBaseChannel();
00282
00283 $this->_aChannel['selflink'] = $sRssUrl;
00284
00285 if ($sTargetUrl) {
00286 $this->_aChannel['link'] = $this->_aChannel['image']['link'] = $sTargetUrl;
00287 }
00288
00289 $this->_aChannel['image']['title'] = $this->_aChannel['title'] = $sTitle;
00290 $this->_aChannel['image']['description'] = $this->_aChannel['description'] = $sDesc;
00291
00292 $this->_aChannel['items'] = $aItems;
00293
00294 if ($sTag) {
00295 $this->_aChannel['lastBuildDate'] = $this->_getLastBuildDate($sTag, $this->_aChannel);
00296 $this->_saveToCache($sTag, $this->_aChannel);
00297 } else {
00298 $this->_aChannel['lastBuildDate'] = date( 'D, d M Y H:i:s O', oxUtilsDate::getInstance()->getTime() );
00299 }
00300 }
00301
00309 public function getTopInShopTitle()
00310 {
00311 $oLang = oxLang::getInstance();
00312 $iLang = $oLang->getBaseLanguage();
00313 return $this->_prepareFeedName( $oLang->translateString( 'RSS_TOPSHOP_TITLE', $iLang ) );
00314 }
00315
00323 public function getTopInShopUrl()
00324 {
00325 return $this->_prepareUrl("cl=rss&fnc=topshop", $this->getTopInShopTitle());
00326 }
00327
00335 public function loadTopInShop()
00336 {
00337 if ( ( $this->_aChannel = $this->_loadFromCache( self::RSS_TOPSHOP ) ) ) {
00338 return;
00339 }
00340
00341 $oArtList = oxNew( 'oxarticlelist' );
00342 $oArtList->loadTop5Articles( $this->getConfig()->getConfigParam( 'iRssItemsCount' ) );
00343
00344 $oLang = oxLang::getInstance();
00345 $this->_loadData(
00346 self::RSS_TOPSHOP,
00347 $this->getTopInShopTitle(),
00348 $oLang->translateString( 'RSS_TOPSHOP_DESCRIPTION', $oLang->getBaseLanguage() ),
00349 $this->_getArticleItems($oArtList),
00350 $this->getTopInShopUrl()
00351 );
00352 }
00353
00354
00355
00363 public function getNewestArticlesTitle()
00364 {
00365 $oLang = oxLang::getInstance();
00366 $iLang = $oLang->getBaseLanguage();
00367 return $this->_prepareFeedName( $oLang->translateString( 'RSS_NEWESTARTICLES_TITLE', $iLang ) );
00368 }
00369
00377 public function getNewestArticlesUrl()
00378 {
00379 return $this->_prepareUrl("cl=rss&fnc=newarts", $this->getNewestArticlesTitle());
00380 }
00381
00389 public function loadNewestArticles()
00390 {
00391 if ( ( $this->_aChannel = $this->_loadFromCache(self::RSS_NEWARTS ) ) ) {
00392 return;
00393 }
00394 $oArtList = oxNew( 'oxarticlelist' );
00395 $oArtList->loadNewestArticles( $this->getConfig()->getConfigParam( 'iRssItemsCount' ) );
00396
00397 $oLang = oxLang::getInstance();
00398 $this->_loadData(
00399 self::RSS_NEWARTS,
00400 $this->getNewestArticlesTitle( ),
00401 $oLang->translateString( 'RSS_NEWESTARTICLES_DESCRIPTION', $oLang->getBaseLanguage() ),
00402 $this->_getArticleItems($oArtList),
00403 $this->getNewestArticlesUrl()
00404 );
00405 }
00406
00407
00417 public function getCategoryArticlesTitle(oxCategory $oCat)
00418 {
00419 $oLang = oxLang::getInstance();
00420 $iLang = $oLang->getBaseLanguage();
00421 $sTitle = $this->_getCatPath($oCat);
00422 return $this->_prepareFeedName( $sTitle . $oLang->translateString( 'RSS_CATEGORYARTICLES_TITLE', $iLang ) );
00423 }
00424
00432 protected function _getCatPath( $oCat )
00433 {
00434 $sCatPathString = '';
00435 $sSep = '';
00436 while ( $oCat ) {
00437
00438 $sCatPathString = $oCat->oxcategories__oxtitle->value.$sSep.$sCatPathString ;
00439 $sSep = '/';
00440
00441 $oCat = $oCat->getParentCategory();
00442 }
00443 return $sCatPathString;
00444 }
00445
00455 public function getCategoryArticlesUrl(oxCategory $oCat)
00456 {
00457 $oLang = oxLang::getInstance();
00458 return $this->_prepareUrl("cl=rss&fnc=catarts&cat=".urlencode($oCat->getId()),
00459 sprintf($oLang->translateString( 'RSS_CATEGORYARTICLES_URL', $oLang->getBaseLanguage() ), $oCat->oxcategories__oxtitle->value));
00460 }
00461
00471 public function loadCategoryArticles( oxCategory $oCat )
00472 {
00473 $sId = $oCat->getId();
00474 if ( ( $this->_aChannel = $this->_loadFromCache( self::RSS_CATARTS.$sId ) ) ) {
00475 return;
00476 }
00477
00478 $oArtList = oxNew( 'oxarticlelist' );
00479 $oArtList->setCustomSorting('oc.oxtime desc');
00480 $oArtList->loadCategoryArticles($oCat->getId(), null, $this->getConfig()->getConfigParam( 'iRssItemsCount' ));
00481
00482 $oLang = oxLang::getInstance();
00483 $this->_loadData(
00484 self::RSS_CATARTS.$sId,
00485 $this->getCategoryArticlesTitle($oCat),
00486 sprintf($oLang->translateString( 'RSS_CATEGORYARTICLES_DESCRIPTION', $oLang->getBaseLanguage() ), $oCat->oxcategories__oxtitle->value),
00487 $this->_getArticleItems($oArtList),
00488 $this->getCategoryArticlesUrl($oCat),
00489 $oCat->getLink()
00490 );
00491 }
00492
00493
00506 public function getSearchArticlesTitle($sSearch, $sCatId, $sVendorId, $sManufacturerId)
00507 {
00508 return $this->_prepareFeedName( getStr()->htmlspecialchars($this->_getSearchParamsTranslation('RSS_SEARCHARTICLES_TITLE', $sSearch, $sCatId, $sVendorId, $sManufacturerId)) );
00509 }
00510
00523 protected function _getSearchParamsUrl($sSearch, $sCatId, $sVendorId, $sManufacturerId)
00524 {
00525 $sParams = "searchparam=".urlencode($sSearch);
00526 if ($sCatId) {
00527 $sParams .= "&searchcnid=".urlencode($sCatId);
00528 }
00529
00530 if ($sVendorId) {
00531 $sParams .= "&searchvendor=".urlencode($sVendorId);
00532 }
00533
00534 if ($sManufacturerId) {
00535 $sParams .= "&searchmanufacturer=".urlencode($sManufacturerId);
00536 }
00537
00538 return $sParams;
00539 }
00540
00551 protected function _getObjectField($sId, $sObject, $sField)
00552 {
00553 if (!$sId) {
00554 return '';
00555 }
00556 $oObj = oxNew($sObject);
00557 if ($oObj->load($sId)) {
00558 return $oObj->$sField->value;
00559 }
00560 return '';
00561 }
00562
00576 protected function _getSearchParamsTranslation($sId, $sSearch, $sCatId, $sVendorId, $sManufacturerId)
00577 {
00578 $oLang = oxLang::getInstance();
00579 $sCatTitle = '';
00580 if ($sTitle = $this->_getObjectField($sCatId, 'oxcategory', 'oxcategories__oxtitle')) {
00581 $sCatTitle = sprintf($oLang->translateString( 'RSS_SEARCHARTICLES_TAG_CATEGORY', $oLang->getBaseLanguage() ), $sTitle);
00582 }
00583 $sVendorTitle = '';
00584 if ($sTitle = $this->_getObjectField($sVendorId, 'oxvendor', 'oxvendor__oxtitle')) {
00585 $sVendorTitle = sprintf($oLang->translateString( 'RSS_SEARCHARTICLES_TAG_VENDOR', $oLang->getBaseLanguage() ), $sTitle);
00586 }
00587 $sManufacturerTitle = '';
00588 if ($sTitle = $this->_getObjectField($sManufacturerId, 'oxmanufacturer', 'oxmanufacturers__oxtitle')) {
00589 $sManufacturerTitle = sprintf($oLang->translateString( 'RSS_SEARCHARTICLES_TAG_MANUFACTURER', $oLang->getBaseLanguage() ), $sTitle);
00590 }
00591
00592 $sRet = sprintf($oLang->translateString( $sId, $oLang->getBaseLanguage() ), $sSearch);
00593
00594 $sRet = str_replace('<TAG_CATEGORY>', $sCatTitle, $sRet);
00595 $sRet = str_replace('<TAG_VENDOR>', $sVendorTitle, $sRet);
00596 $sRet = str_replace('<TAG_MANUFACTURER>', $sManufacturerTitle, $sRet);
00597
00598 return $sRet;
00599 }
00600
00613 public function getSearchArticlesUrl( $sSearch, $sCatId, $sVendorId, $sManufacturerId )
00614 {
00615 $oLang = oxLang::getInstance();
00616 $sUrl = $this->_prepareUrl("cl=rss&fnc=searcharts", $oLang->translateString( 'RSS_SEARCHARTICLES_URL', $oLang->getBaseLanguage()));
00617
00618 $sJoin = '?';
00619 if (strpos($sUrl, '?') !== false) {
00620 $sJoin = '&';
00621 }
00622 return $sUrl.$sJoin.$this->_getSearchParamsUrl($sSearch, $sCatId, $sVendorId, $sManufacturerId);
00623 }
00624
00637 public function loadSearchArticles( $sSearch, $sCatId, $sVendorId, $sManufacturerId )
00638 {
00639
00640
00641
00642
00643
00644 $oConfig = $this->getConfig();
00645 $oConfig->setConfigParam('iNrofCatArticles', $oConfig->getConfigParam( 'iRssItemsCount' ));
00646
00647 $oArtList = oxNew( 'oxsearch' )->getSearchArticles($sSearch, $sCatId, $sVendorId, $sManufacturerId, oxNew('oxarticle')->getViewName().'.oxtimestamp desc');
00648
00649 $this->_loadData(
00650
00651 null,
00652
00653 $this->getSearchArticlesTitle($sSearch, $sCatId, $sVendorId, $sManufacturerId),
00654 $this->_getSearchParamsTranslation('RSS_SEARCHARTICLES_DESCRIPTION', getStr()->htmlspecialchars( $sSearch ), $sCatId, $sVendorId, $sManufacturerId),
00655 $this->_getArticleItems($oArtList),
00656 $this->getSearchArticlesUrl($sSearch, $sCatId, $sVendorId, $sManufacturerId),
00657 $this->_getShopUrl()."cl=search&".$this->_getSearchParamsUrl($sSearch, $sCatId, $sVendorId, $sManufacturerId)
00658 );
00659 }
00660
00668 public function getRecommListsTitle(oxArticle $oArticle)
00669 {
00670 $oLang = oxLang::getInstance();
00671 $iLang = $oLang->getBaseLanguage();
00672 return $this->_prepareFeedName( sprintf($oLang->translateString( 'RSS_ARTRECOMMLISTS_TITLE', $iLang ), $oArticle->oxarticles__oxtitle->value) );
00673 }
00674
00682 public function getRecommListsUrl(oxArticle $oArticle)
00683 {
00684 $oLang = oxLang::getInstance();
00685 $iLang = $oLang->getBaseLanguage();
00686 return $this->_prepareUrl("cl=rss&fnc=recommlists&anid=".$oArticle->getId(),
00687 sprintf($oLang->translateString( 'RSS_ARTRECOMMLISTS_URL', $iLang ), $oArticle->oxarticles__oxtitle->value));
00688 }
00689
00697 protected function _getRecommListItems($oList)
00698 {
00699 $myUtilsUrl = oxUtilsUrl::getInstance();
00700 $aItems = array();
00701 foreach ($oList as $oRecommList) {
00702 $oItem = new oxStdClass();
00703 $oItem->title = $oRecommList->oxrecommlists__oxtitle->value;
00704 $oItem->guid = $oItem->link = $myUtilsUrl->prepareUrlForNoSession($oRecommList->getLink());
00705 $oItem->isGuidPermalink = true;
00706 $oItem->description = $oRecommList->oxrecommlists__oxdesc->value;
00707
00708 $aItems[] = $oItem;
00709 }
00710 return $aItems;
00711 }
00712
00720 public function loadRecommLists(oxArticle $oArticle)
00721 {
00722 if ( ( $this->_aChannel = $this->_loadFromCache( self::RSS_ARTRECOMMLISTS.$oArticle->getId() ) ) ) {
00723 return;
00724 }
00725
00726 $oConfig = $this->getConfig();
00727 $oConfig->setConfigParam('iNrofCrossellArticles', $oConfig->getConfigParam( 'iRssItemsCount' ));
00728
00729 $oList = oxNew( 'oxrecommlist' )->getRecommListsByIds(array($oArticle->getId()));
00730 if ($oList == null) {
00731 $oList = oxNew('oxlist');
00732 }
00733
00734 $oLang = oxLang::getInstance();
00735 $this->_loadData(
00736 self::RSS_ARTRECOMMLISTS.$oArticle->getId(),
00737 $this->getRecommListsTitle($oArticle),
00738 sprintf($oLang->translateString( 'RSS_ARTRECOMMLISTS_DESCRIPTION', $oLang->getBaseLanguage() ), $oArticle->oxarticles__oxtitle->value),
00739 $this->_getRecommListItems($oList),
00740 $this->getRecommListsUrl($oArticle),
00741 $oArticle->getLink()
00742 );
00743 }
00744
00752 public function getRecommListArticlesTitle(oxRecommList $oRecommList)
00753 {
00754 $oLang = oxLang::getInstance();
00755 $iLang = $oLang->getBaseLanguage();
00756 return $this->_prepareFeedName( sprintf($oLang->translateString( 'RSS_RECOMMLISTARTICLES_TITLE', $iLang ), $oRecommList->oxrecommlists__oxtitle->value) );
00757 }
00758
00766 public function getRecommListArticlesUrl(oxRecommList $oRecommList)
00767 {
00768 $oLang = oxLang::getInstance();
00769 $iLang = $oLang->getBaseLanguage();
00770 return $this->_prepareUrl("cl=rss&fnc=recommlistarts&recommid=".$oRecommList->getId(),
00771 sprintf($oLang->translateString( 'RSS_RECOMMLISTARTICLES_URL', $iLang ), $oRecommList->oxrecommlists__oxtitle->value));
00772 }
00773
00781 public function loadRecommListArticles(oxRecommList $oRecommList)
00782 {
00783 if ( ( $this->_aChannel = $this->_loadFromCache( self::RSS_RECOMMLISTARTS.$oRecommList->getId() ) ) ) {
00784 return;
00785 }
00786
00787 $oList = oxNew( 'oxarticlelist' );
00788 $oList->loadRecommArticles( $oRecommList->getId(), ' order by oxobject2list.oxtimestamp desc limit '. $this->getConfig()->getConfigParam( 'iRssItemsCount' ) );
00789
00790 $oLang = oxLang::getInstance();
00791 $this->_loadData(
00792 self::RSS_RECOMMLISTARTS.$oRecommList->getId(),
00793 $this->getRecommListArticlesTitle($oRecommList),
00794 sprintf($oLang->translateString( 'RSS_RECOMMLISTARTICLES_DESCRIPTION', $oLang->getBaseLanguage() ), $oRecommList->oxrecommlists__oxtitle->value),
00795 $this->_getArticleItems($oList),
00796 $this->getRecommListArticlesUrl($oRecommList),
00797 $oRecommList->getLink()
00798 );
00799 }
00800
00808 public function getBargainTitle()
00809 {
00810 $oLang = oxLang::getInstance();
00811 $iLang = $oLang->getBaseLanguage();
00812 return $this->_prepareFeedName( $oLang->translateString( 'RSS_BARGAIN_TITLE', $iLang ) );
00813 }
00814
00822 public function getBargainUrl()
00823 {
00824 return $this->_prepareUrl("cl=rss&fnc=bargain", $this->getBargainTitle());
00825 }
00826
00834 public function loadBargain()
00835 {
00836 if ( ( $this->_aChannel = $this->_loadFromCache( self::RSS_BARGAIN ) ) ) {
00837 return;
00838 }
00839
00840 $oArtList = oxNew( 'oxarticlelist' );
00841 $oArtList->loadAktionArticles( 'OXBARGAIN', $this->getConfig()->getConfigParam( 'iRssItemsCount' ) );
00842
00843 $oLang = oxLang::getInstance();
00844 $this->_loadData(
00845 self::RSS_BARGAIN,
00846 $this->getBargainTitle(),
00847 $oLang->translateString( 'RSS_BARGAIN_DESCRIPTION', $oLang->getBaseLanguage() ),
00848 $this->_getArticleItems($oArtList),
00849 $this->getBargainUrl()
00850 );
00851 }
00852
00853 }
00854