00001 <?php
00002
00008 class oxRssFeed extends oxSuperCfg
00009 {
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
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
00089 return $name . '_' . $oConfig->getShopId() . '_' . oxRegistry::getLang()->getBaseLanguage() . '_' . (int) $oConfig->getShopCurrency();
00090 }
00091
00100 protected function _loadFromCache($name)
00101 {
00102 if ($aRes = oxRegistry::getUtils()->fromFileCache($this->_getCacheId($name))) {
00103 if ($aRes['timestamp'] > time() - self::CACHE_TTL) {
00104 return $aRes['content'];
00105 }
00106 }
00107
00108 return false;
00109 }
00110
00111
00122 protected function _getLastBuildDate($name, $aData)
00123 {
00124 if ($aData2 = oxRegistry::getUtils()->fromFileCache($this->_getCacheId($name))) {
00125 $sLastBuildDate = $aData2['content']['lastBuildDate'];
00126 $aData2['content']['lastBuildDate'] = '';
00127 $aData['lastBuildDate'] = '';
00128 if (!strcmp(serialize($aData), serialize($aData2['content']))) {
00129 return $sLastBuildDate;
00130 }
00131 }
00132
00133 return date('D, d M Y H:i:s O');
00134 }
00135
00148 protected function _saveToCache($name, $aContent)
00149 {
00150 $aData = array('timestamp' => time(), 'content' => $aContent);
00151
00152 return oxRegistry::getUtils()->toFileCache($this->_getCacheId($name), $aData);
00153 }
00154
00155
00164 protected function _getArticleItems(oxArticleList $oList)
00165 {
00166 $myUtilsUrl = oxRegistry::get("oxUtilsUrl");
00167 $aItems = array();
00168 $oLang = oxRegistry::getLang();
00169 $oStr = getStr();
00170
00171 foreach ($oList as $oArticle) {
00172 $oItem = new stdClass();
00173 $oActCur = $this->getConfig()->getActShopCurrencyObject();
00174 $sPrice = '';
00175 if ($oPrice = $oArticle->getPrice()) {
00176 $sFrom = ($oArticle->isRangePrice()) ? oxRegistry::getLang()->translateString('PRICE_FROM') : '';
00177 $sPrice .= ' ' . $sFrom . $oLang->formatCurrency($oPrice->getBruttoPrice(), $oActCur) . " " . $oActCur->sign;
00178 }
00179 $oItem->title = strip_tags($oArticle->oxarticles__oxtitle->value . $sPrice);
00180 $oItem->guid = $oItem->link = $myUtilsUrl->prepareUrlForNoSession($oArticle->getLink());
00181 $oItem->isGuidPermalink = true;
00182
00183
00184 if (oxRegistry::getConfig()->getConfigParam('bl_perfParseLongDescinSmarty')) {
00185 $oItem->description = $oArticle->getLongDesc();
00186 } else {
00187 $oItem->description = $oArticle->getLongDescription()->value;
00188 }
00189
00190 if (trim(str_replace(' ', '', (strip_tags($oItem->description)))) == '') {
00191 $oItem->description = $oArticle->oxarticles__oxshortdesc->value;
00192 }
00193
00194 $oItem->description = trim($oItem->description);
00195 if ($sThumb = $oArticle->getThumbnailUrl()) {
00196 $oItem->description = "<img src='$sThumb' border=0 align='left' hspace=5>" . $oItem->description;
00197 }
00198 $oItem->description = $oStr->htmlspecialchars($oItem->description);
00199
00200 if ($oArticle->oxarticles__oxtimestamp->value) {
00201 list($date, $time) = explode(' ', $oArticle->oxarticles__oxtimestamp->value);
00202 $date = explode('-', $date);
00203 $time = explode(':', $time);
00204 $oItem->date = date('D, d M Y H:i:s O', mktime($time[0], $time[1], $time[2], $date[1], $date[2], $date[0]));
00205 } else {
00206 $oItem->date = date('D, d M Y H:i:s O', time());
00207 }
00208
00209 $aItems[] = $oItem;
00210 }
00211
00212 return $aItems;
00213 }
00214
00225 protected function _prepareUrl($sUri, $sTitle)
00226 {
00227 $iLang = oxRegistry::getLang()->getBaseLanguage();
00228 $sUrl = $this->_getShopUrl();
00229 $sUrl .= $sUri . '&lang=' . $iLang;
00230
00231 if (oxRegistry::getUtils()->seoIsActive()) {
00232 $oEncoder = oxRegistry::get("oxSeoEncoder");
00233 $sUrl = $oEncoder->getDynamicUrl($sUrl, "rss/{$sTitle}/", $iLang);
00234 }
00235
00236 return oxRegistry::get("oxUtilsUrl")->prepareUrlForNoSession($sUrl);
00237 }
00238
00248 protected function _prepareFeedName($sTitle)
00249 {
00250 $oShop = $this->getConfig()->getActiveShop();
00251
00252 return $oShop->oxshops__oxname->value . "/" . $sTitle;
00253 }
00254
00261 protected function _getShopUrl()
00262 {
00263 $sUrl = $this->getConfig()->getShopUrl();
00264 $oStr = getStr();
00265 if ($oStr->strpos($sUrl, '?') !== false) {
00266 if (!$oStr->preg_match('/[?&](amp;)?$/i', $sUrl)) {
00267 $sUrl .= '&';
00268 }
00269 } else {
00270 $sUrl .= '?';
00271 }
00272
00273 return $sUrl;
00274 }
00275
00288 protected function _loadData($sTag, $sTitle, $sDesc, $aItems, $sRssUrl, $sTargetUrl = null)
00289 {
00290 $this->_loadBaseChannel();
00291
00292 $this->_aChannel['selflink'] = $sRssUrl;
00293
00294 if ($sTargetUrl) {
00295 $this->_aChannel['link'] = $this->_aChannel['image']['link'] = $sTargetUrl;
00296 }
00297
00298 $this->_aChannel['image']['title'] = $this->_aChannel['title'] = $sTitle;
00299 $this->_aChannel['image']['description'] = $this->_aChannel['description'] = $sDesc;
00300
00301 $this->_aChannel['items'] = $aItems;
00302
00303 if ($sTag) {
00304 $this->_aChannel['lastBuildDate'] = $this->_getLastBuildDate($sTag, $this->_aChannel);
00305 $this->_saveToCache($sTag, $this->_aChannel);
00306 } else {
00307 $this->_aChannel['lastBuildDate'] = date('D, d M Y H:i:s O', oxRegistry::get("oxUtilsDate")->getTime());
00308 }
00309 }
00310
00318 public function getTopInShopTitle()
00319 {
00320 $oLang = oxRegistry::getLang();
00321 $iLang = $oLang->getBaseLanguage();
00322
00323 return $this->_prepareFeedName($oLang->translateString('TOP_OF_THE_SHOP', $iLang));
00324 }
00325
00333 public function getTopInShopUrl()
00334 {
00335 return $this->_prepareUrl("cl=rss&fnc=topshop", $this->getTopInShopTitle());
00336 }
00337
00345 public function loadTopInShop()
00346 {
00347 if (($this->_aChannel = $this->_loadFromCache(self::RSS_TOPSHOP))) {
00348 return;
00349 }
00350
00351 $oArtList = oxNew('oxarticlelist');
00352 $oArtList->loadTop5Articles($this->getConfig()->getConfigParam('iRssItemsCount'));
00353
00354 $oLang = oxRegistry::getLang();
00355 $this->_loadData(
00356 self::RSS_TOPSHOP,
00357 $this->getTopInShopTitle(),
00358 $oLang->translateString('TOP_SHOP_PRODUCTS', $oLang->getBaseLanguage()),
00359 $this->_getArticleItems($oArtList),
00360 $this->getTopInShopUrl()
00361 );
00362 }
00363
00364
00372 public function getNewestArticlesTitle()
00373 {
00374 $oLang = oxRegistry::getLang();
00375 $iLang = $oLang->getBaseLanguage();
00376
00377 return $this->_prepareFeedName($oLang->translateString('NEWEST_SHOP_PRODUCTS', $iLang));
00378 }
00379
00387 public function getNewestArticlesUrl()
00388 {
00389 return $this->_prepareUrl("cl=rss&fnc=newarts", $this->getNewestArticlesTitle());
00390 }
00391
00399 public function loadNewestArticles()
00400 {
00401 if (($this->_aChannel = $this->_loadFromCache(self::RSS_NEWARTS))) {
00402 return;
00403 }
00404 $oArtList = oxNew('oxarticlelist');
00405 $oArtList->loadNewestArticles($this->getConfig()->getConfigParam('iRssItemsCount'));
00406
00407 $oLang = oxRegistry::getLang();
00408 $this->_loadData(
00409 self::RSS_NEWARTS,
00410 $this->getNewestArticlesTitle(),
00411 $oLang->translateString('NEWEST_SHOP_PRODUCTS', $oLang->getBaseLanguage()),
00412 $this->_getArticleItems($oArtList),
00413 $this->getNewestArticlesUrl()
00414 );
00415 }
00416
00417
00427 public function getCategoryArticlesTitle(oxCategory $oCat)
00428 {
00429 $oLang = oxRegistry::getLang();
00430 $iLang = $oLang->getBaseLanguage();
00431 $sTitle = $this->_getCatPath($oCat);
00432
00433 return $this->_prepareFeedName($sTitle . $oLang->translateString('PRODUCTS', $iLang));
00434 }
00435
00443 protected function _getCatPath($oCat)
00444 {
00445 $sCatPathString = '';
00446 $sSep = '';
00447 while ($oCat) {
00448
00449 $sCatPathString = $oCat->oxcategories__oxtitle->value . $sSep . $sCatPathString;
00450 $sSep = '/';
00451
00452 $oCat = $oCat->getParentCategory();
00453 }
00454
00455 return $sCatPathString;
00456 }
00457
00467 public function getCategoryArticlesUrl(oxCategory $oCat)
00468 {
00469 $oLang = oxRegistry::getLang();
00470
00471 return $this->_prepareUrl(
00472 "cl=rss&fnc=catarts&cat=" . urlencode($oCat->getId()),
00473 sprintf($oLang->translateString('CATEGORY_PRODUCTS_S', $oLang->getBaseLanguage()), $oCat->oxcategories__oxtitle->value)
00474 );
00475 }
00476
00486 public function loadCategoryArticles(oxCategory $oCat)
00487 {
00488 $sId = $oCat->getId();
00489 if (($this->_aChannel = $this->_loadFromCache(self::RSS_CATARTS . $sId))) {
00490 return;
00491 }
00492
00493 $oArtList = oxNew('oxarticlelist');
00494 $oArtList->setCustomSorting('oc.oxtime desc');
00495 $oArtList->loadCategoryArticles($oCat->getId(), null, $this->getConfig()->getConfigParam('iRssItemsCount'));
00496
00497 $oLang = oxRegistry::getLang();
00498 $this->_loadData(
00499 self::RSS_CATARTS . $sId,
00500 $this->getCategoryArticlesTitle($oCat),
00501 sprintf($oLang->translateString('S_CATEGORY_PRODUCTS', $oLang->getBaseLanguage()), $oCat->oxcategories__oxtitle->value),
00502 $this->_getArticleItems($oArtList),
00503 $this->getCategoryArticlesUrl($oCat),
00504 $oCat->getLink()
00505 );
00506 }
00507
00508
00521 public function getSearchArticlesTitle($sSearch, $sCatId, $sVendorId, $sManufacturerId)
00522 {
00523 return $this->_prepareFeedName(getStr()->htmlspecialchars($this->_getSearchParamsTranslation('SEARCH_FOR_PRODUCTS_CATEGORY_VENDOR_MANUFACTURER', $sSearch, $sCatId, $sVendorId, $sManufacturerId)));
00524 }
00525
00538 protected function _getSearchParamsUrl($sSearch, $sCatId, $sVendorId, $sManufacturerId)
00539 {
00540 $sParams = "searchparam=" . urlencode($sSearch);
00541 if ($sCatId) {
00542 $sParams .= "&searchcnid=" . urlencode($sCatId);
00543 }
00544
00545 if ($sVendorId) {
00546 $sParams .= "&searchvendor=" . urlencode($sVendorId);
00547 }
00548
00549 if ($sManufacturerId) {
00550 $sParams .= "&searchmanufacturer=" . urlencode($sManufacturerId);
00551 }
00552
00553 return $sParams;
00554 }
00555
00566 protected function _getObjectField($sId, $sObject, $sField)
00567 {
00568 if (!$sId) {
00569 return '';
00570 }
00571 $oObj = oxNew($sObject);
00572 if ($oObj->load($sId)) {
00573 return $oObj->$sField->value;
00574 }
00575
00576 return '';
00577 }
00578
00592 protected function _getSearchParamsTranslation($sSearch, $sId, $sCatId, $sVendorId, $sManufacturerId)
00593 {
00594 $oLang = oxRegistry::getLang();
00595 $sCatTitle = '';
00596 if ($sTitle = $this->_getObjectField($sCatId, 'oxcategory', 'oxcategories__oxtitle')) {
00597 $sCatTitle = sprintf($oLang->translateString('CATEGORY_S', $oLang->getBaseLanguage()), $sTitle);
00598 }
00599 $sVendorTitle = '';
00600 if ($sTitle = $this->_getObjectField($sVendorId, 'oxvendor', 'oxvendor__oxtitle')) {
00601 $sVendorTitle = sprintf($oLang->translateString('VENDOR_S', $oLang->getBaseLanguage()), $sTitle);
00602 }
00603 $sManufacturerTitle = '';
00604 if ($sTitle = $this->_getObjectField($sManufacturerId, 'oxmanufacturer', 'oxmanufacturers__oxtitle')) {
00605 $sManufacturerTitle = sprintf($oLang->translateString('MANUFACTURER_S', $oLang->getBaseLanguage()), $sTitle);
00606 }
00607
00608 $sRet = sprintf($oLang->translateString($sSearch, $oLang->getBaseLanguage()), $sId);
00609
00610 $sRet = str_replace('<TAG_CATEGORY>', $sCatTitle, $sRet);
00611 $sRet = str_replace('<TAG_VENDOR>', $sVendorTitle, $sRet);
00612 $sRet = str_replace('<TAG_MANUFACTURER>', $sManufacturerTitle, $sRet);
00613
00614 return $sRet;
00615 }
00616
00629 public function getSearchArticlesUrl($sSearch, $sCatId, $sVendorId, $sManufacturerId)
00630 {
00631 $oLang = oxRegistry::getLang();
00632 $sUrl = $this->_prepareUrl("cl=rss&fnc=searcharts", $oLang->translateString('SEARCH', $oLang->getBaseLanguage()));
00633
00634 $sJoin = '?';
00635 if (strpos($sUrl, '?') !== false) {
00636 $sJoin = '&';
00637 }
00638
00639 return $sUrl . $sJoin . $this->_getSearchParamsUrl($sSearch, $sCatId, $sVendorId, $sManufacturerId);
00640 }
00641
00652 public function loadSearchArticles($sSearch, $sCatId, $sVendorId, $sManufacturerId)
00653 {
00654
00655
00656
00657
00658
00659 $oConfig = $this->getConfig();
00660 $oConfig->setConfigParam('iNrofCatArticles', $oConfig->getConfigParam('iRssItemsCount'));
00661
00662 $oArtList = oxNew('oxsearch')->getSearchArticles($sSearch, $sCatId, $sVendorId, $sManufacturerId, oxNew('oxarticle')->getViewName() . '.oxtimestamp desc');
00663
00664 $this->_loadData(
00665
00666 null,
00667
00668 $this->getSearchArticlesTitle($sSearch, $sCatId, $sVendorId, $sManufacturerId),
00669 $this->_getSearchParamsTranslation('SEARCH_FOR_PRODUCTS_CATEGORY_VENDOR_MANUFACTURER', getStr()->htmlspecialchars($sSearch), $sCatId, $sVendorId, $sManufacturerId),
00670 $this->_getArticleItems($oArtList),
00671 $this->getSearchArticlesUrl($sSearch, $sCatId, $sVendorId, $sManufacturerId),
00672 $this->_getShopUrl() . "cl=search&" . $this->_getSearchParamsUrl($sSearch, $sCatId, $sVendorId, $sManufacturerId)
00673 );
00674 }
00675
00683 public function getRecommListsTitle(oxArticle $oArticle)
00684 {
00685 $oLang = oxRegistry::getLang();
00686 $iLang = $oLang->getBaseLanguage();
00687
00688 return $this->_prepareFeedName(sprintf($oLang->translateString('LISTMANIA_LIST_FOR', $iLang), $oArticle->oxarticles__oxtitle->value));
00689 }
00690
00698 public function getRecommListsUrl(oxArticle $oArticle)
00699 {
00700 $oLang = oxRegistry::getLang();
00701 $iLang = $oLang->getBaseLanguage();
00702
00703 return $this->_prepareUrl(
00704 "cl=rss&fnc=recommlists&anid=" . $oArticle->getId(),
00705 $oLang->translateString("LISTMANIA", $iLang) . "/" . $oArticle->oxarticles__oxtitle->value
00706 );
00707 }
00708
00716 protected function _getRecommListItems($oList)
00717 {
00718 $myUtilsUrl = oxRegistry::get("oxUtilsUrl");
00719 $aItems = array();
00720 foreach ($oList as $oRecommList) {
00721 $oItem = new stdClass();
00722 $oItem->title = $oRecommList->oxrecommlists__oxtitle->value;
00723 $oItem->guid = $oItem->link = $myUtilsUrl->prepareUrlForNoSession($oRecommList->getLink());
00724 $oItem->isGuidPermalink = true;
00725 $oItem->description = $oRecommList->oxrecommlists__oxdesc->value;
00726
00727 $aItems[] = $oItem;
00728 }
00729
00730 return $aItems;
00731 }
00732
00740 public function loadRecommLists(oxArticle $oArticle)
00741 {
00742 if (($this->_aChannel = $this->_loadFromCache(self::RSS_ARTRECOMMLISTS . $oArticle->getId()))) {
00743 return;
00744 }
00745
00746 $oConfig = $this->getConfig();
00747 $oConfig->setConfigParam('iNrofCrossellArticles', $oConfig->getConfigParam('iRssItemsCount'));
00748
00749 $oList = oxNew('oxrecommlist')->getRecommListsByIds(array($oArticle->getId()));
00750 if ($oList == null) {
00751 $oList = oxNew('oxlist');
00752 }
00753
00754 $oLang = oxRegistry::getLang();
00755 $this->_loadData(
00756 self::RSS_ARTRECOMMLISTS . $oArticle->getId(),
00757 $this->getRecommListsTitle($oArticle),
00758 sprintf($oLang->translateString('LISTMANIA_LIST_FOR', $oLang->getBaseLanguage()), $oArticle->oxarticles__oxtitle->value),
00759 $this->_getRecommListItems($oList),
00760 $this->getRecommListsUrl($oArticle),
00761 $oArticle->getLink()
00762 );
00763 }
00764
00772 public function getRecommListArticlesTitle(oxRecommList $oRecommList)
00773 {
00774 $oLang = oxRegistry::getLang();
00775 $iLang = $oLang->getBaseLanguage();
00776
00777 return $this->_prepareFeedName(sprintf($oLang->translateString('LISTMANIA_LIST_PRODUCTS', $iLang), $oRecommList->oxrecommlists__oxtitle->value));
00778 }
00779
00787 public function getRecommListArticlesUrl(oxRecommList $oRecommList)
00788 {
00789 $oLang = oxRegistry::getLang();
00790 $iLang = $oLang->getBaseLanguage();
00791
00792 return $this->_prepareUrl(
00793 "cl=rss&fnc=recommlistarts&recommid=" . $oRecommList->getId(),
00794 $oLang->translateString("LISTMANIA", $iLang) . "/" . $oRecommList->oxrecommlists__oxtitle->value
00795 );
00796 }
00797
00805 public function loadRecommListArticles(oxRecommList $oRecommList)
00806 {
00807 if (($this->_aChannel = $this->_loadFromCache(self::RSS_RECOMMLISTARTS . $oRecommList->getId()))) {
00808 return;
00809 }
00810
00811 $oList = oxNew('oxarticlelist');
00812 $oList->loadRecommArticles($oRecommList->getId(), ' order by oxobject2list.oxtimestamp desc limit ' . $this->getConfig()->getConfigParam('iRssItemsCount'));
00813
00814 $oLang = oxRegistry::getLang();
00815 $this->_loadData(
00816 self::RSS_RECOMMLISTARTS . $oRecommList->getId(),
00817 $this->getRecommListArticlesTitle($oRecommList),
00818 sprintf($oLang->translateString('LISTMANIA_LIST_PRODUCTS', $oLang->getBaseLanguage()), $oRecommList->oxrecommlists__oxtitle->value),
00819 $this->_getArticleItems($oList),
00820 $this->getRecommListArticlesUrl($oRecommList),
00821 $oRecommList->getLink()
00822 );
00823 }
00824
00832 public function getBargainTitle()
00833 {
00834 $oLang = oxRegistry::getLang();
00835 $iLang = $oLang->getBaseLanguage();
00836
00837 return $this->_prepareFeedName($oLang->translateString('BARGAIN', $iLang));
00838 }
00839
00847 public function getBargainUrl()
00848 {
00849 return $this->_prepareUrl("cl=rss&fnc=bargain", $this->getBargainTitle());
00850 }
00851
00859 public function loadBargain()
00860 {
00861 if (($this->_aChannel = $this->_loadFromCache(self::RSS_BARGAIN))) {
00862 return;
00863 }
00864
00865 $oArtList = oxNew('oxarticlelist');
00866 $oArtList->loadActionArticles('OXBARGAIN', $this->getConfig()->getConfigParam('iRssItemsCount'));
00867
00868 $oLang = oxRegistry::getLang();
00869 $this->_loadData(
00870 self::RSS_BARGAIN,
00871 $this->getBargainTitle(),
00872 $oLang->translateString('BARGAIN_PRODUCTS', $oLang->getBaseLanguage()),
00873 $this->_getArticleItems($oArtList),
00874 $this->getBargainUrl()
00875 );
00876 }
00877
00883 public function getCacheTtl()
00884 {
00885 return self::CACHE_TTL;
00886 }
00887 }