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'] = oxRegistry::get("oxUtilsUrl")->prepareUrlForNoSession($this->getConfig()->getShopUrl());
00055 $this->_aChannel['description'] = '';
00056 $oLang = oxRegistry::getLang();
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 ( oxRegistry::getUtils()->isValidEmail( $oShop->oxshops__oxinfoemail->value )) {
00062 $this->_aChannel['managingEditor'] = $oShop->oxshops__oxinfoemail->value;
00063 if ( $oShop->oxshops__oxfname ) {
00064 $this->_aChannel['managingEditor'] .= " ({$oShop->oxshops__oxfname} {$oShop->oxshops__oxlname})";
00065 }
00066 }
00067
00068
00069 $this->_aChannel['generator'] = $oShop->oxshops__oxname->value;
00070 $this->_aChannel['image']['url'] = $this->getConfig()->getImageUrl().'logo.png';
00071
00072
00073 $this->_aChannel['image']['title'] = $this->_aChannel['title'];
00074 $this->_aChannel['image']['link'] = $this->_aChannel['link'];
00075 }
00076
00085 protected function _getCacheId($name)
00086 {
00087 $oConfig = $this->getConfig();
00088 return $name.'_'.$oConfig->getShopId().'_'.oxRegistry::getLang()->getBaseLanguage().'_'.(int) $oConfig->getShopCurrency();
00089 }
00090
00099 protected function _loadFromCache($name)
00100 {
00101 if ($aRes = oxRegistry::getUtils()->fromFileCache($this->_getCacheId($name))) {
00102 if ($aRes['timestamp'] > time() - self::CACHE_TTL) {
00103 return $aRes['content'];
00104 }
00105 }
00106 return false;
00107 }
00108
00109
00120 protected function _getLastBuildDate($name, $aData)
00121 {
00122 if ($aData2 = oxRegistry::getUtils()->fromFileCache($this->_getCacheId($name))) {
00123 $sLastBuildDate = $aData2['content']['lastBuildDate'];
00124 $aData2['content']['lastBuildDate'] = '';
00125 $aData['lastBuildDate'] = '';
00126 if (!strcmp(serialize($aData), serialize($aData2['content']))) {
00127 return $sLastBuildDate;
00128 }
00129 }
00130 return date('D, d M Y H:i:s O');
00131 }
00132
00145 protected function _saveToCache($name, $aContent)
00146 {
00147 $aData = array( 'timestamp' => time(), 'content' => $aContent );
00148 return oxRegistry::getUtils()->toFileCache($this->_getCacheId($name), $aData);
00149 }
00150
00151
00160 protected function _getArticleItems(oxArticleList $oList)
00161 {
00162 $myUtilsUrl = oxRegistry::get("oxUtilsUrl");
00163 $aItems = array();
00164 $oLang = oxRegistry::getLang();
00165 $oStr = getStr();
00166
00167 foreach ($oList as $oArticle) {
00168 $oItem = new stdClass();
00169 $oActCur = $this->getConfig()->getActShopCurrencyObject();
00170 $sPrice = '';
00171 if ( $oPrice = $oArticle->getPrice() ) {
00172 $sPrice = " " . $oArticle->getPriceFromPrefix().$oLang->formatCurrency( $oPrice->getBruttoPrice(), $oActCur ) . " ". $oActCur->sign;
00173 }
00174 $oItem->title = strip_tags($oArticle->oxarticles__oxtitle->value . $sPrice);
00175 $oItem->guid = $oItem->link = $myUtilsUrl->prepareUrlForNoSession($oArticle->getLink());
00176 $oItem->isGuidPermalink = true;
00177
00178
00179 if ( oxRegistry::getConfig()->getConfigParam( 'bl_perfParseLongDescinSmarty' ) ) {
00180 $oItem->description = $oArticle->getLongDesc();
00181 } else {
00182 $oItem->description = $oArticle->getLongDescription()->value;
00183 }
00184
00185 if (trim(str_replace(' ', '', (strip_tags($oItem->description)))) == '') {
00186 $oItem->description = $oArticle->oxarticles__oxshortdesc->value;
00187 }
00188
00189 $oItem->description = trim($oItem->description);
00190 if ( $sThumb = $oArticle->getThumbnailUrl() ) {
00191 $oItem->description = "<img src='$sThumb' border=0 align='left' hspace=5>".$oItem->description;
00192 }
00193 $oItem->description = $oStr->htmlspecialchars( $oItem->description );
00194
00195 if ( $oArticle->oxarticles__oxtimestamp->value ) {
00196 list($date, $time) = explode(' ', $oArticle->oxarticles__oxtimestamp->value);
00197 $date = explode('-', $date);
00198 $time = explode(':', $time);
00199 $oItem->date = date( 'D, d M Y H:i:s O', mktime($time[0], $time[1], $time[2], $date[1], $date[2], $date[0]) );
00200 } else {
00201 $oItem->date = date( 'D, d M Y H:i:s O', time() );
00202 }
00203
00204 $aItems[] = $oItem;
00205 }
00206 return $aItems;
00207 }
00208
00219 protected function _prepareUrl($sUri, $sTitle)
00220 {
00221 $iLang = oxRegistry::getLang()->getBaseLanguage();
00222 $sUrl = $this->_getShopUrl();
00223 $sUrl .= $sUri.'&lang='.$iLang;
00224
00225 if ( oxRegistry::getUtils()->seoIsActive() ) {
00226 $oEncoder = oxRegistry::get("oxSeoEncoder");
00227 $sUrl = $oEncoder->getDynamicUrl( $sUrl, "rss/{$sTitle}/", $iLang );
00228 }
00229
00230 return oxRegistry::get("oxUtilsUrl")->prepareUrlForNoSession( $sUrl );
00231 }
00232
00242 protected function _prepareFeedName($sTitle)
00243 {
00244 $oShop = $this->getConfig()->getActiveShop();
00245
00246 return $oShop->oxshops__oxname->value . "/" . $sTitle;
00247 }
00248
00255 protected function _getShopUrl()
00256 {
00257 $sUrl = $this->getConfig()->getShopUrl();
00258 $oStr = getStr();
00259 if ( $oStr->strpos($sUrl, '?') !== false ) {
00260 if ( !$oStr->preg_match('/[?&](amp;)?$/i', $sUrl)) {
00261 $sUrl .= '&';
00262 }
00263 } else {
00264 $sUrl .= '?';
00265 }
00266 return $sUrl;
00267 }
00268
00282 protected function _loadData($sTag, $sTitle, $sDesc, $aItems, $sRssUrl, $sTargetUrl = null)
00283 {
00284 $this->_loadBaseChannel();
00285
00286 $this->_aChannel['selflink'] = $sRssUrl;
00287
00288 if ($sTargetUrl) {
00289 $this->_aChannel['link'] = $this->_aChannel['image']['link'] = $sTargetUrl;
00290 }
00291
00292 $this->_aChannel['image']['title'] = $this->_aChannel['title'] = $sTitle;
00293 $this->_aChannel['image']['description'] = $this->_aChannel['description'] = $sDesc;
00294
00295 $this->_aChannel['items'] = $aItems;
00296
00297 if ($sTag) {
00298 $this->_aChannel['lastBuildDate'] = $this->_getLastBuildDate($sTag, $this->_aChannel);
00299 $this->_saveToCache($sTag, $this->_aChannel);
00300 } else {
00301 $this->_aChannel['lastBuildDate'] = date( 'D, d M Y H:i:s O', oxRegistry::get("oxUtilsDate")->getTime() );
00302 }
00303 }
00304
00312 public function getTopInShopTitle()
00313 {
00314 $oLang = oxRegistry::getLang();
00315 $iLang = $oLang->getBaseLanguage();
00316 return $this->_prepareFeedName( $oLang->translateString( 'TOP_OF_THE_SHOP', $iLang ) );
00317 }
00318
00326 public function getTopInShopUrl()
00327 {
00328 return $this->_prepareUrl("cl=rss&fnc=topshop", $this->getTopInShopTitle());
00329 }
00330
00338 public function loadTopInShop()
00339 {
00340 if ( ( $this->_aChannel = $this->_loadFromCache( self::RSS_TOPSHOP ) ) ) {
00341 return;
00342 }
00343
00344 $oArtList = oxNew( 'oxarticlelist' );
00345 $oArtList->loadTop5Articles( $this->getConfig()->getConfigParam( 'iRssItemsCount' ) );
00346
00347 $oLang = oxRegistry::getLang();
00348 $this->_loadData(
00349 self::RSS_TOPSHOP,
00350 $this->getTopInShopTitle(),
00351 $oLang->translateString( 'TOP_SHOP_PRODUCTS', $oLang->getBaseLanguage() ),
00352 $this->_getArticleItems($oArtList),
00353 $this->getTopInShopUrl()
00354 );
00355 }
00356
00357
00358
00366 public function getNewestArticlesTitle()
00367 {
00368 $oLang = oxRegistry::getLang();
00369 $iLang = $oLang->getBaseLanguage();
00370 return $this->_prepareFeedName( $oLang->translateString( 'NEWEST_SHOP_PRODUCTS', $iLang ) );
00371 }
00372
00380 public function getNewestArticlesUrl()
00381 {
00382 return $this->_prepareUrl("cl=rss&fnc=newarts", $this->getNewestArticlesTitle());
00383 }
00384
00392 public function loadNewestArticles()
00393 {
00394 if ( ( $this->_aChannel = $this->_loadFromCache(self::RSS_NEWARTS ) ) ) {
00395 return;
00396 }
00397 $oArtList = oxNew( 'oxarticlelist' );
00398 $oArtList->loadNewestArticles( $this->getConfig()->getConfigParam( 'iRssItemsCount' ) );
00399
00400 $oLang = oxRegistry::getLang();
00401 $this->_loadData(
00402 self::RSS_NEWARTS,
00403 $this->getNewestArticlesTitle( ),
00404 $oLang->translateString( 'NEWEST_SHOP_PRODUCTS', $oLang->getBaseLanguage() ),
00405 $this->_getArticleItems($oArtList),
00406 $this->getNewestArticlesUrl()
00407 );
00408 }
00409
00410
00420 public function getCategoryArticlesTitle(oxCategory $oCat)
00421 {
00422 $oLang = oxRegistry::getLang();
00423 $iLang = $oLang->getBaseLanguage();
00424 $sTitle = $this->_getCatPath($oCat);
00425 return $this->_prepareFeedName( $sTitle . $oLang->translateString( 'PRODUCTS', $iLang ) );
00426 }
00427
00435 protected function _getCatPath( $oCat )
00436 {
00437 $sCatPathString = '';
00438 $sSep = '';
00439 while ( $oCat ) {
00440
00441 $sCatPathString = $oCat->oxcategories__oxtitle->value.$sSep.$sCatPathString ;
00442 $sSep = '/';
00443
00444 $oCat = $oCat->getParentCategory();
00445 }
00446 return $sCatPathString;
00447 }
00448
00458 public function getCategoryArticlesUrl(oxCategory $oCat)
00459 {
00460 $oLang = oxRegistry::getLang();
00461 return $this->_prepareUrl("cl=rss&fnc=catarts&cat=".urlencode($oCat->getId()),
00462 sprintf($oLang->translateString( 'CATEGORY_PRODUCTS_S', $oLang->getBaseLanguage() ), $oCat->oxcategories__oxtitle->value));
00463 }
00464
00474 public function loadCategoryArticles( oxCategory $oCat )
00475 {
00476 $sId = $oCat->getId();
00477 if ( ( $this->_aChannel = $this->_loadFromCache( self::RSS_CATARTS.$sId ) ) ) {
00478 return;
00479 }
00480
00481 $oArtList = oxNew( 'oxarticlelist' );
00482 $oArtList->setCustomSorting('oc.oxtime desc');
00483 $oArtList->loadCategoryArticles($oCat->getId(), null, $this->getConfig()->getConfigParam( 'iRssItemsCount' ));
00484
00485 $oLang = oxRegistry::getLang();
00486 $this->_loadData(
00487 self::RSS_CATARTS.$sId,
00488 $this->getCategoryArticlesTitle($oCat),
00489 sprintf($oLang->translateString( 'S_CATEGORY_PRODUCTS', $oLang->getBaseLanguage() ), $oCat->oxcategories__oxtitle->value),
00490 $this->_getArticleItems($oArtList),
00491 $this->getCategoryArticlesUrl($oCat),
00492 $oCat->getLink()
00493 );
00494 }
00495
00496
00509 public function getSearchArticlesTitle($sSearch, $sCatId, $sVendorId, $sManufacturerId)
00510 {
00511 return $this->_prepareFeedName( getStr()->htmlspecialchars($this->_getSearchParamsTranslation('SEARCH_FOR_PRODUCTS_CATEGORY_VENDOR_MANUFACTURER', $sSearch, $sCatId, $sVendorId, $sManufacturerId)) );
00512 }
00513
00526 protected function _getSearchParamsUrl($sSearch, $sCatId, $sVendorId, $sManufacturerId)
00527 {
00528 $sParams = "searchparam=".urlencode($sSearch);
00529 if ($sCatId) {
00530 $sParams .= "&searchcnid=".urlencode($sCatId);
00531 }
00532
00533 if ($sVendorId) {
00534 $sParams .= "&searchvendor=".urlencode($sVendorId);
00535 }
00536
00537 if ($sManufacturerId) {
00538 $sParams .= "&searchmanufacturer=".urlencode($sManufacturerId);
00539 }
00540
00541 return $sParams;
00542 }
00543
00554 protected function _getObjectField($sId, $sObject, $sField)
00555 {
00556 if (!$sId) {
00557 return '';
00558 }
00559 $oObj = oxNew($sObject);
00560 if ($oObj->load($sId)) {
00561 return $oObj->$sField->value;
00562 }
00563 return '';
00564 }
00565
00579 protected function _getSearchParamsTranslation($sSearch, $sId, $sCatId, $sVendorId, $sManufacturerId)
00580 {
00581 $oLang = oxRegistry::getLang();
00582 $sCatTitle = '';
00583 if ($sTitle = $this->_getObjectField($sCatId, 'oxcategory', 'oxcategories__oxtitle')) {
00584 $sCatTitle = sprintf($oLang->translateString( 'CATEGORY_S', $oLang->getBaseLanguage() ), $sTitle);
00585 }
00586 $sVendorTitle = '';
00587 if ($sTitle = $this->_getObjectField($sVendorId, 'oxvendor', 'oxvendor__oxtitle')) {
00588 $sVendorTitle = sprintf($oLang->translateString( 'VENDOR_S', $oLang->getBaseLanguage() ), $sTitle);
00589 }
00590 $sManufacturerTitle = '';
00591 if ($sTitle = $this->_getObjectField($sManufacturerId, 'oxmanufacturer', 'oxmanufacturers__oxtitle')) {
00592 $sManufacturerTitle = sprintf($oLang->translateString( 'MANUFACTURER_S', $oLang->getBaseLanguage() ), $sTitle);
00593 }
00594
00595 $sRet = sprintf($oLang->translateString( $sSearch, $oLang->getBaseLanguage() ), $sId);
00596
00597 $sRet = str_replace('<TAG_CATEGORY>', $sCatTitle, $sRet);
00598 $sRet = str_replace('<TAG_VENDOR>', $sVendorTitle, $sRet);
00599 $sRet = str_replace('<TAG_MANUFACTURER>', $sManufacturerTitle, $sRet);
00600
00601 return $sRet;
00602 }
00603
00616 public function getSearchArticlesUrl( $sSearch, $sCatId, $sVendorId, $sManufacturerId )
00617 {
00618 $oLang = oxRegistry::getLang();
00619 $sUrl = $this->_prepareUrl("cl=rss&fnc=searcharts", $oLang->translateString( 'SEARCH', $oLang->getBaseLanguage()));
00620
00621 $sJoin = '?';
00622 if (strpos($sUrl, '?') !== false) {
00623 $sJoin = '&';
00624 }
00625 return $sUrl.$sJoin.$this->_getSearchParamsUrl($sSearch, $sCatId, $sVendorId, $sManufacturerId);
00626 }
00627
00640 public function loadSearchArticles( $sSearch, $sCatId, $sVendorId, $sManufacturerId )
00641 {
00642
00643
00644
00645
00646
00647 $oConfig = $this->getConfig();
00648 $oConfig->setConfigParam('iNrofCatArticles', $oConfig->getConfigParam( 'iRssItemsCount' ));
00649
00650 $oArtList = oxNew( 'oxsearch' )->getSearchArticles($sSearch, $sCatId, $sVendorId, $sManufacturerId, oxNew('oxarticle')->getViewName().'.oxtimestamp desc');
00651
00652 $this->_loadData(
00653
00654 null,
00655
00656 $this->getSearchArticlesTitle($sSearch, $sCatId, $sVendorId, $sManufacturerId),
00657 $this->_getSearchParamsTranslation('SEARCH_FOR_PRODUCTS_CATEGORY_VENDOR_MANUFACTURER', getStr()->htmlspecialchars( $sSearch ), $sCatId, $sVendorId, $sManufacturerId),
00658 $this->_getArticleItems($oArtList),
00659 $this->getSearchArticlesUrl($sSearch, $sCatId, $sVendorId, $sManufacturerId),
00660 $this->_getShopUrl()."cl=search&".$this->_getSearchParamsUrl($sSearch, $sCatId, $sVendorId, $sManufacturerId)
00661 );
00662 }
00663
00671 public function getRecommListsTitle(oxArticle $oArticle)
00672 {
00673 $oLang = oxRegistry::getLang();
00674 $iLang = $oLang->getBaseLanguage();
00675 return $this->_prepareFeedName( sprintf($oLang->translateString( 'LISTMANIA_LIST_FOR', $iLang ), $oArticle->oxarticles__oxtitle->value) );
00676 }
00677
00685 public function getRecommListsUrl(oxArticle $oArticle)
00686 {
00687 $oLang = oxRegistry::getLang();
00688 $iLang = $oLang->getBaseLanguage();
00689 return $this->_prepareUrl("cl=rss&fnc=recommlists&anid=".$oArticle->getId(),
00690 $oLang->translateString( "LISTMANIA", $iLang ) . "/" . $oArticle->oxarticles__oxtitle->value );
00691 }
00692
00700 protected function _getRecommListItems($oList)
00701 {
00702 $myUtilsUrl = oxRegistry::get("oxUtilsUrl");
00703 $aItems = array();
00704 foreach ($oList as $oRecommList) {
00705 $oItem = new stdClass();
00706 $oItem->title = $oRecommList->oxrecommlists__oxtitle->value;
00707 $oItem->guid = $oItem->link = $myUtilsUrl->prepareUrlForNoSession($oRecommList->getLink());
00708 $oItem->isGuidPermalink = true;
00709 $oItem->description = $oRecommList->oxrecommlists__oxdesc->value;
00710
00711 $aItems[] = $oItem;
00712 }
00713 return $aItems;
00714 }
00715
00723 public function loadRecommLists(oxArticle $oArticle)
00724 {
00725 if ( ( $this->_aChannel = $this->_loadFromCache( self::RSS_ARTRECOMMLISTS.$oArticle->getId() ) ) ) {
00726 return;
00727 }
00728
00729 $oConfig = $this->getConfig();
00730 $oConfig->setConfigParam('iNrofCrossellArticles', $oConfig->getConfigParam( 'iRssItemsCount' ));
00731
00732 $oList = oxNew( 'oxrecommlist' )->getRecommListsByIds(array($oArticle->getId()));
00733 if ($oList == null) {
00734 $oList = oxNew('oxlist');
00735 }
00736
00737 $oLang = oxRegistry::getLang();
00738 $this->_loadData(
00739 self::RSS_ARTRECOMMLISTS.$oArticle->getId(),
00740 $this->getRecommListsTitle($oArticle),
00741 sprintf($oLang->translateString( 'LISTMANIA_LIST_FOR', $oLang->getBaseLanguage() ), $oArticle->oxarticles__oxtitle->value),
00742 $this->_getRecommListItems($oList),
00743 $this->getRecommListsUrl($oArticle),
00744 $oArticle->getLink()
00745 );
00746 }
00747
00755 public function getRecommListArticlesTitle(oxRecommList $oRecommList)
00756 {
00757 $oLang = oxRegistry::getLang();
00758 $iLang = $oLang->getBaseLanguage();
00759 return $this->_prepareFeedName( sprintf($oLang->translateString( 'LISTMANIA_LIST_PRODUCTS', $iLang ), $oRecommList->oxrecommlists__oxtitle->value) );
00760 }
00761
00769 public function getRecommListArticlesUrl(oxRecommList $oRecommList)
00770 {
00771 $oLang = oxRegistry::getLang();
00772 $iLang = $oLang->getBaseLanguage();
00773 return $this->_prepareUrl("cl=rss&fnc=recommlistarts&recommid=".$oRecommList->getId(),
00774 $oLang->translateString( "LISTMANIA", $iLang ) . "/" . $oRecommList->oxrecommlists__oxtitle->value );
00775 }
00776
00784 public function loadRecommListArticles(oxRecommList $oRecommList)
00785 {
00786 if ( ( $this->_aChannel = $this->_loadFromCache( self::RSS_RECOMMLISTARTS.$oRecommList->getId() ) ) ) {
00787 return;
00788 }
00789
00790 $oList = oxNew( 'oxarticlelist' );
00791 $oList->loadRecommArticles( $oRecommList->getId(), ' order by oxobject2list.oxtimestamp desc limit '. $this->getConfig()->getConfigParam( 'iRssItemsCount' ) );
00792
00793 $oLang = oxRegistry::getLang();
00794 $this->_loadData(
00795 self::RSS_RECOMMLISTARTS.$oRecommList->getId(),
00796 $this->getRecommListArticlesTitle($oRecommList),
00797 sprintf($oLang->translateString( 'LISTMANIA_LIST_PRODUCTS', $oLang->getBaseLanguage() ), $oRecommList->oxrecommlists__oxtitle->value),
00798 $this->_getArticleItems($oList),
00799 $this->getRecommListArticlesUrl($oRecommList),
00800 $oRecommList->getLink()
00801 );
00802 }
00803
00811 public function getBargainTitle()
00812 {
00813 $oLang = oxRegistry::getLang();
00814 $iLang = $oLang->getBaseLanguage();
00815 return $this->_prepareFeedName( $oLang->translateString( 'BARGAIN', $iLang ) );
00816 }
00817
00825 public function getBargainUrl()
00826 {
00827 return $this->_prepareUrl("cl=rss&fnc=bargain", $this->getBargainTitle());
00828 }
00829
00837 public function loadBargain()
00838 {
00839 if ( ( $this->_aChannel = $this->_loadFromCache( self::RSS_BARGAIN ) ) ) {
00840 return;
00841 }
00842
00843 $oArtList = oxNew( 'oxarticlelist' );
00844 $oArtList->loadActionArticles( 'OXBARGAIN', $this->getConfig()->getConfigParam( 'iRssItemsCount' ) );
00845
00846 $oLang = oxRegistry::getLang();
00847 $this->_loadData(
00848 self::RSS_BARGAIN,
00849 $this->getBargainTitle(),
00850 $oLang->translateString( 'BARGAIN_PRODUCTS', $oLang->getBaseLanguage() ),
00851 $this->_getArticleItems($oArtList),
00852 $this->getBargainUrl()
00853 );
00854 }
00855
00861 public function getCacheTtl()
00862 {
00863 return self::CACHE_TTL;
00864 }
00865
00866 }
00867