OXID eShop CE  4.8.12
 All Classes Files Functions Variables Pages
oxrssfeed.php
Go to the documentation of this file.
1 <?php
2 
8 class oxRssFeed extends oxSuperCfg
9 {
13  const CACHE_TTL = 10800;
14 
18  const RSS_TOPSHOP = 'RSS_TopShop';
19  const RSS_NEWARTS = 'RSS_NewArts';
20  const RSS_CATARTS = 'RSS_CatArts';
21  const RSS_ARTRECOMMLISTS = 'RSS_ARTRECOMMLISTS';
22  const RSS_RECOMMLISTARTS = 'RSS_RECOMMLISTARTS';
23  const RSS_BARGAIN = 'RSS_Bargain';
24 
31  protected $_aChannel = array();
32 
39  public function getChannel()
40  {
41  return $this->_aChannel;
42  }
43 
50  protected function _loadBaseChannel()
51  {
52  $oShop = $this->getConfig()->getActiveShop();
53  $this->_aChannel['title'] = $oShop->oxshops__oxname->value;
54  $this->_aChannel['link'] = oxRegistry::get("oxUtilsUrl")->prepareUrlForNoSession($this->getConfig()->getShopUrl());
55  $this->_aChannel['description'] = '';
56  $oLang = oxRegistry::getLang();
57  $aLangIds = $oLang->getLanguageIds();
58  $this->_aChannel['language'] = $aLangIds[$oLang->getBaseLanguage()];
59  $this->_aChannel['copyright'] = $oShop->oxshops__oxname->value;
60  $this->_aChannel['selflink'] = '';
61  if ( oxRegistry::getUtils()->isValidEmail( $oShop->oxshops__oxinfoemail->value )) {
62  $this->_aChannel['managingEditor'] = $oShop->oxshops__oxinfoemail->value;
63  if ( $oShop->oxshops__oxfname ) {
64  $this->_aChannel['managingEditor'] .= " ({$oShop->oxshops__oxfname} {$oShop->oxshops__oxlname})";
65  }
66  }
67  //$this->_aChannel['webMaster'] = '';
68 
69  $this->_aChannel['generator'] = $oShop->oxshops__oxname->value;
70  $this->_aChannel['image']['url'] = $this->getConfig()->getImageUrl().'logo.png';
71 
72 
73  $this->_aChannel['image']['title'] = $this->_aChannel['title'];
74  $this->_aChannel['image']['link'] = $this->_aChannel['link'];
75  }
76 
85  protected function _getCacheId($name)
86  {
87  $oConfig = $this->getConfig();
88  return $name.'_'.$oConfig->getShopId().'_'.oxRegistry::getLang()->getBaseLanguage().'_'.(int) $oConfig->getShopCurrency();
89  }
90 
99  protected function _loadFromCache($name)
100  {
101  if ($aRes = oxRegistry::getUtils()->fromFileCache($this->_getCacheId($name))) {
102  if ($aRes['timestamp'] > time() - self::CACHE_TTL) {
103  return $aRes['content'];
104  }
105  }
106  return false;
107  }
108 
109 
120  protected function _getLastBuildDate($name, $aData)
121  {
122  if ($aData2 = oxRegistry::getUtils()->fromFileCache($this->_getCacheId($name))) {
123  $sLastBuildDate = $aData2['content']['lastBuildDate'];
124  $aData2['content']['lastBuildDate'] = '';
125  $aData['lastBuildDate'] = '';
126  if (!strcmp(serialize($aData), serialize($aData2['content']))) {
127  return $sLastBuildDate;
128  }
129  }
130  return date('D, d M Y H:i:s O');
131  }
132 
145  protected function _saveToCache($name, $aContent)
146  {
147  $aData = array( 'timestamp' => time(), 'content' => $aContent );
148  return oxRegistry::getUtils()->toFileCache($this->_getCacheId($name), $aData);
149  }
150 
151 
160  protected function _getArticleItems(oxArticleList $oList)
161  {
162  $myUtilsUrl = oxRegistry::get("oxUtilsUrl");
163  $aItems = array();
164  $oLang = oxRegistry::getLang();
165  $oStr = getStr();
166 
167  foreach ($oList as $oArticle) {
168  $oItem = new stdClass();
169  $oActCur = $this->getConfig()->getActShopCurrencyObject();
170  $sPrice = '';
171  if ( $oPrice = $oArticle->getPrice() ) {
172  $sPrice = " " . $oArticle->getPriceFromPrefix().$oLang->formatCurrency( $oPrice->getBruttoPrice(), $oActCur ) . " ". $oActCur->sign;
173  }
174  $oItem->title = strip_tags($oArticle->oxarticles__oxtitle->value . $sPrice);
175  $oItem->guid = $oItem->link = $myUtilsUrl->prepareUrlForNoSession($oArticle->getLink());
176  $oItem->isGuidPermalink = true;
177  // $oItem->description = $oArticle->getLongDescription()->value; //oxarticles__oxshortdesc->value;
178  //#4038: Smarty not parsed in RSS, although smarty parsing activated for longdescriptions
179  if ( oxRegistry::getConfig()->getConfigParam( 'bl_perfParseLongDescinSmarty' ) ) {
180  $oItem->description = $oArticle->getLongDesc();
181  } else {
182  $oItem->description = $oArticle->getLongDescription()->value;
183  }
184 
185  if (trim(str_replace('&nbsp;', '', (strip_tags($oItem->description)))) == '') {
186  $oItem->description = $oArticle->oxarticles__oxshortdesc->value;
187  }
188 
189  $oItem->description = trim($oItem->description);
190  if ( $sThumb = $oArticle->getThumbnailUrl() ) {
191  $oItem->description = "<img src='$sThumb' border=0 align='left' hspace=5>".$oItem->description;
192  }
193  $oItem->description = $oStr->htmlspecialchars( $oItem->description );
194 
195  if ( $oArticle->oxarticles__oxtimestamp->value ) {
196  list($date, $time) = explode(' ', $oArticle->oxarticles__oxtimestamp->value);
197  $date = explode('-', $date);
198  $time = explode(':', $time);
199  $oItem->date = date( 'D, d M Y H:i:s O', mktime($time[0], $time[1], $time[2], $date[1], $date[2], $date[0]) );
200  } else {
201  $oItem->date = date( 'D, d M Y H:i:s O', time() );
202  }
203 
204  $aItems[] = $oItem;
205  }
206  return $aItems;
207  }
208 
219  protected function _prepareUrl($sUri, $sTitle)
220  {
221  $iLang = oxRegistry::getLang()->getBaseLanguage();
222  $sUrl = $this->_getShopUrl();
223  $sUrl .= $sUri.'&amp;lang='.$iLang;
224 
225  if ( oxRegistry::getUtils()->seoIsActive() ) {
226  $oEncoder = oxRegistry::get("oxSeoEncoder");
227  $sUrl = $oEncoder->getDynamicUrl( $sUrl, "rss/{$sTitle}/", $iLang );
228  }
229 
230  return oxRegistry::get("oxUtilsUrl")->prepareUrlForNoSession( $sUrl );
231  }
232 
242  protected function _prepareFeedName($sTitle)
243  {
244  $oShop = $this->getConfig()->getActiveShop();
245 
246  return $oShop->oxshops__oxname->value . "/" . $sTitle;
247  }
248 
255  protected function _getShopUrl()
256  {
257  $sUrl = $this->getConfig()->getShopUrl();
258  $oStr = getStr();
259  if ( $oStr->strpos($sUrl, '?') !== false ) {
260  if ( !$oStr->preg_match('/[?&](amp;)?$/i', $sUrl)) {
261  $sUrl .= '&amp;';
262  }
263  } else {
264  $sUrl .= '?';
265  }
266  return $sUrl;
267  }
268 
282  protected function _loadData($sTag, $sTitle, $sDesc, $aItems, $sRssUrl, $sTargetUrl = null)
283  {
284  $this->_loadBaseChannel();
285 
286  $this->_aChannel['selflink'] = $sRssUrl;
287 
288  if ($sTargetUrl) {
289  $this->_aChannel['link'] = $this->_aChannel['image']['link'] = $sTargetUrl;
290  }
291 
292  $this->_aChannel['image']['title'] = $this->_aChannel['title'] = $sTitle;
293  $this->_aChannel['image']['description'] = $this->_aChannel['description'] = $sDesc;
294 
295  $this->_aChannel['items'] = $aItems;
296 
297  if ($sTag) {
298  $this->_aChannel['lastBuildDate'] = $this->_getLastBuildDate($sTag, $this->_aChannel);
299  $this->_saveToCache($sTag, $this->_aChannel);
300  } else {
301  $this->_aChannel['lastBuildDate'] = date( 'D, d M Y H:i:s O', oxRegistry::get("oxUtilsDate")->getTime() );
302  }
303  }
304 
312  public function getTopInShopTitle()
313  {
314  $oLang = oxRegistry::getLang();
315  $iLang = $oLang->getBaseLanguage();
316  return $this->_prepareFeedName( $oLang->translateString( 'TOP_OF_THE_SHOP', $iLang ) );
317  }
318 
326  public function getTopInShopUrl()
327  {
328  return $this->_prepareUrl("cl=rss&amp;fnc=topshop", $this->getTopInShopTitle());
329  }
330 
338  public function loadTopInShop()
339  {
340  if ( ( $this->_aChannel = $this->_loadFromCache( self::RSS_TOPSHOP ) ) ) {
341  return;
342  }
343 
344  $oArtList = oxNew( 'oxarticlelist' );
345  $oArtList->loadTop5Articles( $this->getConfig()->getConfigParam( 'iRssItemsCount' ) );
346 
347  $oLang = oxRegistry::getLang();
348  $this->_loadData(
349  self::RSS_TOPSHOP,
350  $this->getTopInShopTitle(),
351  $oLang->translateString( 'TOP_SHOP_PRODUCTS', $oLang->getBaseLanguage() ),
352  $this->_getArticleItems($oArtList),
353  $this->getTopInShopUrl()
354  );
355  }
356 
357 
358 
366  public function getNewestArticlesTitle()
367  {
368  $oLang = oxRegistry::getLang();
369  $iLang = $oLang->getBaseLanguage();
370  return $this->_prepareFeedName( $oLang->translateString( 'NEWEST_SHOP_PRODUCTS', $iLang ) );
371  }
372 
380  public function getNewestArticlesUrl()
381  {
382  return $this->_prepareUrl("cl=rss&amp;fnc=newarts", $this->getNewestArticlesTitle());
383  }
384 
392  public function loadNewestArticles()
393  {
394  if ( ( $this->_aChannel = $this->_loadFromCache(self::RSS_NEWARTS ) ) ) {
395  return;
396  }
397  $oArtList = oxNew( 'oxarticlelist' );
398  $oArtList->loadNewestArticles( $this->getConfig()->getConfigParam( 'iRssItemsCount' ) );
399 
400  $oLang = oxRegistry::getLang();
401  $this->_loadData(
402  self::RSS_NEWARTS,
403  $this->getNewestArticlesTitle( ),
404  $oLang->translateString( 'NEWEST_SHOP_PRODUCTS', $oLang->getBaseLanguage() ),
405  $this->_getArticleItems($oArtList),
406  $this->getNewestArticlesUrl()
407  );
408  }
409 
410 
420  public function getCategoryArticlesTitle(oxCategory $oCat)
421  {
422  $oLang = oxRegistry::getLang();
423  $iLang = $oLang->getBaseLanguage();
424  $sTitle = $this->_getCatPath($oCat);
425  return $this->_prepareFeedName( $sTitle . $oLang->translateString( 'PRODUCTS', $iLang ) );
426  }
427 
435  protected function _getCatPath( $oCat )
436  {
437  $sCatPathString = '';
438  $sSep = '';
439  while ( $oCat ) {
440  // prepare oCat title part
441  $sCatPathString = $oCat->oxcategories__oxtitle->value.$sSep.$sCatPathString ;
442  $sSep = '/';
443  // load parent
444  $oCat = $oCat->getParentCategory();
445  }
446  return $sCatPathString;
447  }
448 
458  public function getCategoryArticlesUrl(oxCategory $oCat)
459  {
460  $oLang = oxRegistry::getLang();
461  return $this->_prepareUrl("cl=rss&amp;fnc=catarts&amp;cat=".urlencode($oCat->getId()),
462  sprintf($oLang->translateString( 'CATEGORY_PRODUCTS_S', $oLang->getBaseLanguage() ), $oCat->oxcategories__oxtitle->value));
463  }
464 
474  public function loadCategoryArticles( oxCategory $oCat )
475  {
476  $sId = $oCat->getId();
477  if ( ( $this->_aChannel = $this->_loadFromCache( self::RSS_CATARTS.$sId ) ) ) {
478  return;
479  }
480 
481  $oArtList = oxNew( 'oxarticlelist' );
482  $oArtList->setCustomSorting('oc.oxtime desc');
483  $oArtList->loadCategoryArticles($oCat->getId(), null, $this->getConfig()->getConfigParam( 'iRssItemsCount' ));
484 
485  $oLang = oxRegistry::getLang();
486  $this->_loadData(
487  self::RSS_CATARTS.$sId,
488  $this->getCategoryArticlesTitle($oCat),
489  sprintf($oLang->translateString( 'S_CATEGORY_PRODUCTS', $oLang->getBaseLanguage() ), $oCat->oxcategories__oxtitle->value),
490  $this->_getArticleItems($oArtList),
491  $this->getCategoryArticlesUrl($oCat),
492  $oCat->getLink()
493  );
494  }
495 
496 
509  public function getSearchArticlesTitle($sSearch, $sCatId, $sVendorId, $sManufacturerId)
510  {
511  return $this->_prepareFeedName( getStr()->htmlspecialchars($this->_getSearchParamsTranslation('SEARCH_FOR_PRODUCTS_CATEGORY_VENDOR_MANUFACTURER', $sSearch, $sCatId, $sVendorId, $sManufacturerId)) );
512  }
513 
526  protected function _getSearchParamsUrl($sSearch, $sCatId, $sVendorId, $sManufacturerId)
527  {
528  $sParams = "searchparam=".urlencode($sSearch);
529  if ($sCatId) {
530  $sParams .= "&amp;searchcnid=".urlencode($sCatId);
531  }
532 
533  if ($sVendorId) {
534  $sParams .= "&amp;searchvendor=".urlencode($sVendorId);
535  }
536 
537  if ($sManufacturerId) {
538  $sParams .= "&amp;searchmanufacturer=".urlencode($sManufacturerId);
539  }
540 
541  return $sParams;
542  }
543 
554  protected function _getObjectField($sId, $sObject, $sField)
555  {
556  if (!$sId) {
557  return '';
558  }
559  $oObj = oxNew($sObject);
560  if ($oObj->load($sId)) {
561  return $oObj->$sField->value;
562  }
563  return '';
564  }
565 
579  protected function _getSearchParamsTranslation($sSearch, $sId, $sCatId, $sVendorId, $sManufacturerId)
580  {
581  $oLang = oxRegistry::getLang();
582  $sCatTitle = '';
583  if ($sTitle = $this->_getObjectField($sCatId, 'oxcategory', 'oxcategories__oxtitle')) {
584  $sCatTitle = sprintf($oLang->translateString( 'CATEGORY_S', $oLang->getBaseLanguage() ), $sTitle);
585  }
586  $sVendorTitle = '';
587  if ($sTitle = $this->_getObjectField($sVendorId, 'oxvendor', 'oxvendor__oxtitle')) {
588  $sVendorTitle = sprintf($oLang->translateString( 'VENDOR_S', $oLang->getBaseLanguage() ), $sTitle);
589  }
590  $sManufacturerTitle = '';
591  if ($sTitle = $this->_getObjectField($sManufacturerId, 'oxmanufacturer', 'oxmanufacturers__oxtitle')) {
592  $sManufacturerTitle = sprintf($oLang->translateString( 'MANUFACTURER_S', $oLang->getBaseLanguage() ), $sTitle);
593  }
594 
595  $sRet = sprintf($oLang->translateString( $sSearch, $oLang->getBaseLanguage() ), $sId);
596 
597  $sRet = str_replace('<TAG_CATEGORY>', $sCatTitle, $sRet);
598  $sRet = str_replace('<TAG_VENDOR>', $sVendorTitle, $sRet);
599  $sRet = str_replace('<TAG_MANUFACTURER>', $sManufacturerTitle, $sRet);
600 
601  return $sRet;
602  }
603 
616  public function getSearchArticlesUrl( $sSearch, $sCatId, $sVendorId, $sManufacturerId )
617  {
618  $oLang = oxRegistry::getLang();
619  $sUrl = $this->_prepareUrl("cl=rss&amp;fnc=searcharts", $oLang->translateString( 'SEARCH', $oLang->getBaseLanguage()));
620 
621  $sJoin = '?';
622  if (strpos($sUrl, '?') !== false) {
623  $sJoin = '&amp;';
624  }
625  return $sUrl.$sJoin.$this->_getSearchParamsUrl($sSearch, $sCatId, $sVendorId, $sManufacturerId);
626  }
627 
640  public function loadSearchArticles( $sSearch, $sCatId, $sVendorId, $sManufacturerId )
641  {
642  // dont use cache for search
643  //if ($this->_aChannel = $this->_loadFromCache(self::RSS_SEARCHARTS.md5($sSearch.$sCatId.$sVendorId))) {
644  // return;
645  //}
646 
647  $oConfig = $this->getConfig();
648  $oConfig->setConfigParam('iNrofCatArticles', $oConfig->getConfigParam( 'iRssItemsCount' ));
649 
650  $oArtList = oxNew( 'oxsearch' )->getSearchArticles($sSearch, $sCatId, $sVendorId, $sManufacturerId, oxNew('oxarticle')->getViewName().'.oxtimestamp desc');
651 
652  $this->_loadData(
653  // dont use cache for search
654  null,
655  //self::RSS_SEARCHARTS.md5($sSearch.$sCatId.$sVendorId),
656  $this->getSearchArticlesTitle($sSearch, $sCatId, $sVendorId, $sManufacturerId),
657  $this->_getSearchParamsTranslation('SEARCH_FOR_PRODUCTS_CATEGORY_VENDOR_MANUFACTURER', getStr()->htmlspecialchars( $sSearch ), $sCatId, $sVendorId, $sManufacturerId),
658  $this->_getArticleItems($oArtList),
659  $this->getSearchArticlesUrl($sSearch, $sCatId, $sVendorId, $sManufacturerId),
660  $this->_getShopUrl()."cl=search&amp;".$this->_getSearchParamsUrl($sSearch, $sCatId, $sVendorId, $sManufacturerId)
661  );
662  }
663 
671  public function getRecommListsTitle(oxArticle $oArticle)
672  {
673  $oLang = oxRegistry::getLang();
674  $iLang = $oLang->getBaseLanguage();
675  return $this->_prepareFeedName( sprintf($oLang->translateString( 'LISTMANIA_LIST_FOR', $iLang ), $oArticle->oxarticles__oxtitle->value) );
676  }
677 
685  public function getRecommListsUrl(oxArticle $oArticle)
686  {
687  $oLang = oxRegistry::getLang();
688  $iLang = $oLang->getBaseLanguage();
689  return $this->_prepareUrl("cl=rss&amp;fnc=recommlists&amp;anid=".$oArticle->getId(),
690  $oLang->translateString( "LISTMANIA", $iLang ) . "/" . $oArticle->oxarticles__oxtitle->value );
691  }
692 
700  protected function _getRecommListItems($oList)
701  {
702  $myUtilsUrl = oxRegistry::get("oxUtilsUrl");
703  $aItems = array();
704  foreach ($oList as $oRecommList) {
705  $oItem = new stdClass();
706  $oItem->title = $oRecommList->oxrecommlists__oxtitle->value;
707  $oItem->guid = $oItem->link = $myUtilsUrl->prepareUrlForNoSession($oRecommList->getLink());
708  $oItem->isGuidPermalink = true;
709  $oItem->description = $oRecommList->oxrecommlists__oxdesc->value;
710 
711  $aItems[] = $oItem;
712  }
713  return $aItems;
714  }
715 
723  public function loadRecommLists(oxArticle $oArticle)
724  {
725  if ( ( $this->_aChannel = $this->_loadFromCache( self::RSS_ARTRECOMMLISTS.$oArticle->getId() ) ) ) {
726  return;
727  }
728 
729  $oConfig = $this->getConfig();
730  $oConfig->setConfigParam('iNrofCrossellArticles', $oConfig->getConfigParam( 'iRssItemsCount' ));
731 
732  $oList = oxNew( 'oxrecommlist' )->getRecommListsByIds(array($oArticle->getId()));
733  if ($oList == null) {
734  $oList = oxNew('oxlist');
735  }
736 
737  $oLang = oxRegistry::getLang();
738  $this->_loadData(
739  self::RSS_ARTRECOMMLISTS.$oArticle->getId(),
740  $this->getRecommListsTitle($oArticle),
741  sprintf($oLang->translateString( 'LISTMANIA_LIST_FOR', $oLang->getBaseLanguage() ), $oArticle->oxarticles__oxtitle->value),
742  $this->_getRecommListItems($oList),
743  $this->getRecommListsUrl($oArticle),
744  $oArticle->getLink()
745  );
746  }
747 
755  public function getRecommListArticlesTitle(oxRecommList $oRecommList)
756  {
757  $oLang = oxRegistry::getLang();
758  $iLang = $oLang->getBaseLanguage();
759  return $this->_prepareFeedName( sprintf($oLang->translateString( 'LISTMANIA_LIST_PRODUCTS', $iLang ), $oRecommList->oxrecommlists__oxtitle->value) );
760  }
761 
769  public function getRecommListArticlesUrl(oxRecommList $oRecommList)
770  {
771  $oLang = oxRegistry::getLang();
772  $iLang = $oLang->getBaseLanguage();
773  return $this->_prepareUrl("cl=rss&amp;fnc=recommlistarts&amp;recommid=".$oRecommList->getId(),
774  $oLang->translateString( "LISTMANIA", $iLang ) . "/" . $oRecommList->oxrecommlists__oxtitle->value );
775  }
776 
784  public function loadRecommListArticles(oxRecommList $oRecommList)
785  {
786  if ( ( $this->_aChannel = $this->_loadFromCache( self::RSS_RECOMMLISTARTS.$oRecommList->getId() ) ) ) {
787  return;
788  }
789 
790  $oList = oxNew( 'oxarticlelist' );
791  $oList->loadRecommArticles( $oRecommList->getId(), ' order by oxobject2list.oxtimestamp desc limit '. $this->getConfig()->getConfigParam( 'iRssItemsCount' ) );
792 
793  $oLang = oxRegistry::getLang();
794  $this->_loadData(
795  self::RSS_RECOMMLISTARTS.$oRecommList->getId(),
796  $this->getRecommListArticlesTitle($oRecommList),
797  sprintf($oLang->translateString( 'LISTMANIA_LIST_PRODUCTS', $oLang->getBaseLanguage() ), $oRecommList->oxrecommlists__oxtitle->value),
798  $this->_getArticleItems($oList),
799  $this->getRecommListArticlesUrl($oRecommList),
800  $oRecommList->getLink()
801  );
802  }
803 
811  public function getBargainTitle()
812  {
813  $oLang = oxRegistry::getLang();
814  $iLang = $oLang->getBaseLanguage();
815  return $this->_prepareFeedName( $oLang->translateString( 'BARGAIN', $iLang ) );
816  }
817 
825  public function getBargainUrl()
826  {
827  return $this->_prepareUrl("cl=rss&amp;fnc=bargain", $this->getBargainTitle());
828  }
829 
837  public function loadBargain()
838  {
839  if ( ( $this->_aChannel = $this->_loadFromCache( self::RSS_BARGAIN ) ) ) {
840  return;
841  }
842 
843  $oArtList = oxNew( 'oxarticlelist' );
844  $oArtList->loadActionArticles( 'OXBARGAIN', $this->getConfig()->getConfigParam( 'iRssItemsCount' ) );
845 
846  $oLang = oxRegistry::getLang();
847  $this->_loadData(
848  self::RSS_BARGAIN,
849  $this->getBargainTitle(),
850  $oLang->translateString( 'BARGAIN_PRODUCTS', $oLang->getBaseLanguage() ),
851  $this->_getArticleItems($oArtList),
852  $this->getBargainUrl()
853  );
854  }
855 
861  public function getCacheTtl()
862  {
863  return self::CACHE_TTL;
864  }
865 
866 }
867