OXID eShop CE  4.9.7
 All Classes Files Functions Variables Pages
oxrssfeed.php
Go to the documentation of this file.
1 <?php
2 
8 class oxRssFeed extends oxSuperCfg
9 {
10 
14  const CACHE_TTL = 10800;
15 
19  const RSS_TOPSHOP = 'RSS_TopShop';
20  const RSS_NEWARTS = 'RSS_NewArts';
21  const RSS_CATARTS = 'RSS_CatArts';
22  const RSS_ARTRECOMMLISTS = 'RSS_ARTRECOMMLISTS';
23  const RSS_RECOMMLISTARTS = 'RSS_RECOMMLISTARTS';
24  const RSS_BARGAIN = 'RSS_Bargain';
25 
32  protected $_aChannel = array();
33 
40  public function getChannel()
41  {
42  return $this->_aChannel;
43  }
44 
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 
89  return $name . '_' . $oConfig->getShopId() . '_' . oxRegistry::getLang()->getBaseLanguage() . '_' . (int) $oConfig->getShopCurrency();
90  }
91 
100  protected function _loadFromCache($name)
101  {
102  if ($aRes = oxRegistry::getUtils()->fromFileCache($this->_getCacheId($name))) {
103  if ($aRes['timestamp'] > time() - self::CACHE_TTL) {
104  return $aRes['content'];
105  }
106  }
107 
108  return false;
109  }
110 
111 
122  protected function _getLastBuildDate($name, $aData)
123  {
124  if ($aData2 = oxRegistry::getUtils()->fromFileCache($this->_getCacheId($name))) {
125  $sLastBuildDate = $aData2['content']['lastBuildDate'];
126  $aData2['content']['lastBuildDate'] = '';
127  $aData['lastBuildDate'] = '';
128  if (!strcmp(serialize($aData), serialize($aData2['content']))) {
129  return $sLastBuildDate;
130  }
131  }
132 
133  return date('D, d M Y H:i:s O');
134  }
135 
148  protected function _saveToCache($name, $aContent)
149  {
150  $aData = array('timestamp' => time(), 'content' => $aContent);
151 
152  return oxRegistry::getUtils()->toFileCache($this->_getCacheId($name), $aData);
153  }
154 
155 
164  protected function _getArticleItems(oxArticleList $oList)
165  {
166  $myUtilsUrl = oxRegistry::get("oxUtilsUrl");
167  $aItems = array();
168  $oLang = oxRegistry::getLang();
169  $oStr = getStr();
170 
171  foreach ($oList as $oArticle) {
172  $oItem = new stdClass();
173  $oActCur = $this->getConfig()->getActShopCurrencyObject();
174  $sPrice = '';
175  if ($oPrice = $oArticle->getPrice()) {
176  $sFrom = ($oArticle->isRangePrice()) ? oxRegistry::getLang()->translateString('PRICE_FROM') : '';
177  $sPrice .= ' ' . $sFrom . $oLang->formatCurrency($oPrice->getBruttoPrice(), $oActCur) . " " . $oActCur->sign;
178  }
179  $oItem->title = strip_tags($oArticle->oxarticles__oxtitle->value . $sPrice);
180  $oItem->guid = $oItem->link = $myUtilsUrl->prepareUrlForNoSession($oArticle->getLink());
181  $oItem->isGuidPermalink = true;
182  // $oItem->description = $oArticle->getLongDescription()->value; //oxarticles__oxshortdesc->value;
183  //#4038: Smarty not parsed in RSS, although smarty parsing activated for longdescriptions
184  if (oxRegistry::getConfig()->getConfigParam('bl_perfParseLongDescinSmarty')) {
185  $oItem->description = $oArticle->getLongDesc();
186  } else {
187  $oItem->description = $oArticle->getLongDescription()->value;
188  }
189 
190  if (trim(str_replace('&nbsp;', '', (strip_tags($oItem->description)))) == '') {
191  $oItem->description = $oArticle->oxarticles__oxshortdesc->value;
192  }
193 
194  $oItem->description = trim($oItem->description);
195  if ($sThumb = $oArticle->getThumbnailUrl()) {
196  $oItem->description = "<img src='$sThumb' border=0 align='left' hspace=5>" . $oItem->description;
197  }
198  $oItem->description = $oStr->htmlspecialchars($oItem->description);
199 
200  if ($oArticle->oxarticles__oxtimestamp->value) {
201  list($date, $time) = explode(' ', $oArticle->oxarticles__oxtimestamp->value);
202  $date = explode('-', $date);
203  $time = explode(':', $time);
204  $oItem->date = date('D, d M Y H:i:s O', mktime($time[0], $time[1], $time[2], $date[1], $date[2], $date[0]));
205  } else {
206  $oItem->date = date('D, d M Y H:i:s O', time());
207  }
208 
209  $aItems[] = $oItem;
210  }
211 
212  return $aItems;
213  }
214 
225  protected function _prepareUrl($sUri, $sTitle)
226  {
227  $iLang = oxRegistry::getLang()->getBaseLanguage();
228  $sUrl = $this->_getShopUrl();
229  $sUrl .= $sUri . '&amp;lang=' . $iLang;
230 
231  if (oxRegistry::getUtils()->seoIsActive()) {
232  $oEncoder = oxRegistry::get("oxSeoEncoder");
233  $sUrl = $oEncoder->getDynamicUrl($sUrl, "rss/{$sTitle}/", $iLang);
234  }
235 
236  return oxRegistry::get("oxUtilsUrl")->prepareUrlForNoSession($sUrl);
237  }
238 
248  protected function _prepareFeedName($sTitle)
249  {
250  $oShop = $this->getConfig()->getActiveShop();
251 
252  return $oShop->oxshops__oxname->value . "/" . $sTitle;
253  }
254 
261  protected function _getShopUrl()
262  {
263  $sUrl = $this->getConfig()->getShopUrl();
264  $oStr = getStr();
265  if ($oStr->strpos($sUrl, '?') !== false) {
266  if (!$oStr->preg_match('/[?&](amp;)?$/i', $sUrl)) {
267  $sUrl .= '&amp;';
268  }
269  } else {
270  $sUrl .= '?';
271  }
272 
273  return $sUrl;
274  }
275 
288  protected function _loadData($sTag, $sTitle, $sDesc, $aItems, $sRssUrl, $sTargetUrl = null)
289  {
290  $this->_loadBaseChannel();
291 
292  $this->_aChannel['selflink'] = $sRssUrl;
293 
294  if ($sTargetUrl) {
295  $this->_aChannel['link'] = $this->_aChannel['image']['link'] = $sTargetUrl;
296  }
297 
298  $this->_aChannel['image']['title'] = $this->_aChannel['title'] = $sTitle;
299  $this->_aChannel['image']['description'] = $this->_aChannel['description'] = $sDesc;
300 
301  $this->_aChannel['items'] = $aItems;
302 
303  if ($sTag) {
304  $this->_aChannel['lastBuildDate'] = $this->_getLastBuildDate($sTag, $this->_aChannel);
305  $this->_saveToCache($sTag, $this->_aChannel);
306  } else {
307  $this->_aChannel['lastBuildDate'] = date('D, d M Y H:i:s O', oxRegistry::get("oxUtilsDate")->getTime());
308  }
309  }
310 
318  public function getTopInShopTitle()
319  {
320  $oLang = oxRegistry::getLang();
321  $iLang = $oLang->getBaseLanguage();
322 
323  return $this->_prepareFeedName($oLang->translateString('TOP_OF_THE_SHOP', $iLang));
324  }
325 
333  public function getTopInShopUrl()
334  {
335  return $this->_prepareUrl("cl=rss&amp;fnc=topshop", $this->getTopInShopTitle());
336  }
337 
345  public function loadTopInShop()
346  {
347  if (($this->_aChannel = $this->_loadFromCache(self::RSS_TOPSHOP))) {
348  return;
349  }
350 
351  $oArtList = oxNew('oxarticlelist');
352  $oArtList->loadTop5Articles($this->getConfig()->getConfigParam('iRssItemsCount'));
353 
354  $oLang = oxRegistry::getLang();
355  $this->_loadData(
356  self::RSS_TOPSHOP,
357  $this->getTopInShopTitle(),
358  $oLang->translateString('TOP_SHOP_PRODUCTS', $oLang->getBaseLanguage()),
359  $this->_getArticleItems($oArtList),
360  $this->getTopInShopUrl()
361  );
362  }
363 
364 
372  public function getNewestArticlesTitle()
373  {
374  $oLang = oxRegistry::getLang();
375  $iLang = $oLang->getBaseLanguage();
376 
377  return $this->_prepareFeedName($oLang->translateString('NEWEST_SHOP_PRODUCTS', $iLang));
378  }
379 
387  public function getNewestArticlesUrl()
388  {
389  return $this->_prepareUrl("cl=rss&amp;fnc=newarts", $this->getNewestArticlesTitle());
390  }
391 
399  public function loadNewestArticles()
400  {
401  if (($this->_aChannel = $this->_loadFromCache(self::RSS_NEWARTS))) {
402  return;
403  }
404  $oArtList = oxNew('oxarticlelist');
405  $oArtList->loadNewestArticles($this->getConfig()->getConfigParam('iRssItemsCount'));
406 
407  $oLang = oxRegistry::getLang();
408  $this->_loadData(
409  self::RSS_NEWARTS,
410  $this->getNewestArticlesTitle(),
411  $oLang->translateString('NEWEST_SHOP_PRODUCTS', $oLang->getBaseLanguage()),
412  $this->_getArticleItems($oArtList),
413  $this->getNewestArticlesUrl()
414  );
415  }
416 
417 
427  public function getCategoryArticlesTitle(oxCategory $oCat)
428  {
429  $oLang = oxRegistry::getLang();
430  $iLang = $oLang->getBaseLanguage();
431  $sTitle = $this->_getCatPath($oCat);
432 
433  return $this->_prepareFeedName($sTitle . $oLang->translateString('PRODUCTS', $iLang));
434  }
435 
443  protected function _getCatPath($oCat)
444  {
445  $sCatPathString = '';
446  $sSep = '';
447  while ($oCat) {
448  // prepare oCat title part
449  $sCatPathString = $oCat->oxcategories__oxtitle->value . $sSep . $sCatPathString;
450  $sSep = '/';
451  // load parent
452  $oCat = $oCat->getParentCategory();
453  }
454 
455  return $sCatPathString;
456  }
457 
467  public function getCategoryArticlesUrl(oxCategory $oCat)
468  {
469  $oLang = oxRegistry::getLang();
470 
471  return $this->_prepareUrl(
472  "cl=rss&amp;fnc=catarts&amp;cat=" . urlencode($oCat->getId()),
473  sprintf($oLang->translateString('CATEGORY_PRODUCTS_S', $oLang->getBaseLanguage()), $oCat->oxcategories__oxtitle->value)
474  );
475  }
476 
486  public function loadCategoryArticles(oxCategory $oCat)
487  {
488  $sId = $oCat->getId();
489  if (($this->_aChannel = $this->_loadFromCache(self::RSS_CATARTS . $sId))) {
490  return;
491  }
492 
493  $oArtList = oxNew('oxarticlelist');
494  $oArtList->setCustomSorting('oc.oxtime desc');
495  $oArtList->loadCategoryArticles($oCat->getId(), null, $this->getConfig()->getConfigParam('iRssItemsCount'));
496 
497  $oLang = oxRegistry::getLang();
498  $this->_loadData(
499  self::RSS_CATARTS . $sId,
500  $this->getCategoryArticlesTitle($oCat),
501  sprintf($oLang->translateString('S_CATEGORY_PRODUCTS', $oLang->getBaseLanguage()), $oCat->oxcategories__oxtitle->value),
502  $this->_getArticleItems($oArtList),
503  $this->getCategoryArticlesUrl($oCat),
504  $oCat->getLink()
505  );
506  }
507 
508 
521  public function getSearchArticlesTitle($sSearch, $sCatId, $sVendorId, $sManufacturerId)
522  {
523  return $this->_prepareFeedName(getStr()->htmlspecialchars($this->_getSearchParamsTranslation('SEARCH_FOR_PRODUCTS_CATEGORY_VENDOR_MANUFACTURER', $sSearch, $sCatId, $sVendorId, $sManufacturerId)));
524  }
525 
538  protected function _getSearchParamsUrl($sSearch, $sCatId, $sVendorId, $sManufacturerId)
539  {
540  $sParams = "searchparam=" . urlencode($sSearch);
541  if ($sCatId) {
542  $sParams .= "&amp;searchcnid=" . urlencode($sCatId);
543  }
544 
545  if ($sVendorId) {
546  $sParams .= "&amp;searchvendor=" . urlencode($sVendorId);
547  }
548 
549  if ($sManufacturerId) {
550  $sParams .= "&amp;searchmanufacturer=" . urlencode($sManufacturerId);
551  }
552 
553  return $sParams;
554  }
555 
566  protected function _getObjectField($sId, $sObject, $sField)
567  {
568  if (!$sId) {
569  return '';
570  }
571  $oObj = oxNew($sObject);
572  if ($oObj->load($sId)) {
573  return $oObj->$sField->value;
574  }
575 
576  return '';
577  }
578 
592  protected function _getSearchParamsTranslation($sSearch, $sId, $sCatId, $sVendorId, $sManufacturerId)
593  {
594  $oLang = oxRegistry::getLang();
595  $sCatTitle = '';
596  if ($sTitle = $this->_getObjectField($sCatId, 'oxcategory', 'oxcategories__oxtitle')) {
597  $sCatTitle = sprintf($oLang->translateString('CATEGORY_S', $oLang->getBaseLanguage()), $sTitle);
598  }
599  $sVendorTitle = '';
600  if ($sTitle = $this->_getObjectField($sVendorId, 'oxvendor', 'oxvendor__oxtitle')) {
601  $sVendorTitle = sprintf($oLang->translateString('VENDOR_S', $oLang->getBaseLanguage()), $sTitle);
602  }
603  $sManufacturerTitle = '';
604  if ($sTitle = $this->_getObjectField($sManufacturerId, 'oxmanufacturer', 'oxmanufacturers__oxtitle')) {
605  $sManufacturerTitle = sprintf($oLang->translateString('MANUFACTURER_S', $oLang->getBaseLanguage()), $sTitle);
606  }
607 
608  $sRet = sprintf($oLang->translateString($sSearch, $oLang->getBaseLanguage()), $sId);
609 
610  $sRet = str_replace('<TAG_CATEGORY>', $sCatTitle, $sRet);
611  $sRet = str_replace('<TAG_VENDOR>', $sVendorTitle, $sRet);
612  $sRet = str_replace('<TAG_MANUFACTURER>', $sManufacturerTitle, $sRet);
613 
614  return $sRet;
615  }
616 
629  public function getSearchArticlesUrl($sSearch, $sCatId, $sVendorId, $sManufacturerId)
630  {
631  $oLang = oxRegistry::getLang();
632  $sUrl = $this->_prepareUrl("cl=rss&amp;fnc=searcharts", $oLang->translateString('SEARCH', $oLang->getBaseLanguage()));
633 
634  $sJoin = '?';
635  if (strpos($sUrl, '?') !== false) {
636  $sJoin = '&amp;';
637  }
638 
639  return $sUrl . $sJoin . $this->_getSearchParamsUrl($sSearch, $sCatId, $sVendorId, $sManufacturerId);
640  }
641 
652  public function loadSearchArticles($sSearch, $sCatId, $sVendorId, $sManufacturerId)
653  {
654  // dont use cache for search
655  //if ($this->_aChannel = $this->_loadFromCache(self::RSS_SEARCHARTS.md5($sSearch.$sCatId.$sVendorId))) {
656  // return;
657  //}
658 
659  $oConfig = $this->getConfig();
660  $oConfig->setConfigParam('iNrofCatArticles', $oConfig->getConfigParam('iRssItemsCount'));
661 
662  $oArtList = oxNew('oxsearch')->getSearchArticles($sSearch, $sCatId, $sVendorId, $sManufacturerId, oxNew('oxarticle')->getViewName() . '.oxtimestamp desc');
663 
664  $this->_loadData(
665  // dont use cache for search
666  null,
667  //self::RSS_SEARCHARTS.md5($sSearch.$sCatId.$sVendorId),
668  $this->getSearchArticlesTitle($sSearch, $sCatId, $sVendorId, $sManufacturerId),
669  $this->_getSearchParamsTranslation('SEARCH_FOR_PRODUCTS_CATEGORY_VENDOR_MANUFACTURER', getStr()->htmlspecialchars($sSearch), $sCatId, $sVendorId, $sManufacturerId),
670  $this->_getArticleItems($oArtList),
671  $this->getSearchArticlesUrl($sSearch, $sCatId, $sVendorId, $sManufacturerId),
672  $this->_getShopUrl() . "cl=search&amp;" . $this->_getSearchParamsUrl($sSearch, $sCatId, $sVendorId, $sManufacturerId)
673  );
674  }
675 
683  public function getRecommListsTitle(oxArticle $oArticle)
684  {
685  $oLang = oxRegistry::getLang();
686  $iLang = $oLang->getBaseLanguage();
687 
688  return $this->_prepareFeedName(sprintf($oLang->translateString('LISTMANIA_LIST_FOR', $iLang), $oArticle->oxarticles__oxtitle->value));
689  }
690 
698  public function getRecommListsUrl(oxArticle $oArticle)
699  {
700  $oLang = oxRegistry::getLang();
701  $iLang = $oLang->getBaseLanguage();
702 
703  return $this->_prepareUrl(
704  "cl=rss&amp;fnc=recommlists&amp;anid=" . $oArticle->getId(),
705  $oLang->translateString("LISTMANIA", $iLang) . "/" . $oArticle->oxarticles__oxtitle->value
706  );
707  }
708 
716  protected function _getRecommListItems($oList)
717  {
718  $myUtilsUrl = oxRegistry::get("oxUtilsUrl");
719  $aItems = array();
720  foreach ($oList as $oRecommList) {
721  $oItem = new stdClass();
722  $oItem->title = $oRecommList->oxrecommlists__oxtitle->value;
723  $oItem->guid = $oItem->link = $myUtilsUrl->prepareUrlForNoSession($oRecommList->getLink());
724  $oItem->isGuidPermalink = true;
725  $oItem->description = $oRecommList->oxrecommlists__oxdesc->value;
726 
727  $aItems[] = $oItem;
728  }
729 
730  return $aItems;
731  }
732 
740  public function loadRecommLists(oxArticle $oArticle)
741  {
742  if (($this->_aChannel = $this->_loadFromCache(self::RSS_ARTRECOMMLISTS . $oArticle->getId()))) {
743  return;
744  }
745 
746  $oConfig = $this->getConfig();
747  $oConfig->setConfigParam('iNrofCrossellArticles', $oConfig->getConfigParam('iRssItemsCount'));
748 
749  $oList = oxNew('oxrecommlist')->getRecommListsByIds(array($oArticle->getId()));
750  if ($oList == null) {
751  $oList = oxNew('oxlist');
752  }
753 
754  $oLang = oxRegistry::getLang();
755  $this->_loadData(
756  self::RSS_ARTRECOMMLISTS . $oArticle->getId(),
757  $this->getRecommListsTitle($oArticle),
758  sprintf($oLang->translateString('LISTMANIA_LIST_FOR', $oLang->getBaseLanguage()), $oArticle->oxarticles__oxtitle->value),
759  $this->_getRecommListItems($oList),
760  $this->getRecommListsUrl($oArticle),
761  $oArticle->getLink()
762  );
763  }
764 
772  public function getRecommListArticlesTitle(oxRecommList $oRecommList)
773  {
774  $oLang = oxRegistry::getLang();
775  $iLang = $oLang->getBaseLanguage();
776 
777  return $this->_prepareFeedName(sprintf($oLang->translateString('LISTMANIA_LIST_PRODUCTS', $iLang), $oRecommList->oxrecommlists__oxtitle->value));
778  }
779 
787  public function getRecommListArticlesUrl(oxRecommList $oRecommList)
788  {
789  $oLang = oxRegistry::getLang();
790  $iLang = $oLang->getBaseLanguage();
791 
792  return $this->_prepareUrl(
793  "cl=rss&amp;fnc=recommlistarts&amp;recommid=" . $oRecommList->getId(),
794  $oLang->translateString("LISTMANIA", $iLang) . "/" . $oRecommList->oxrecommlists__oxtitle->value
795  );
796  }
797 
805  public function loadRecommListArticles(oxRecommList $oRecommList)
806  {
807  if (($this->_aChannel = $this->_loadFromCache(self::RSS_RECOMMLISTARTS . $oRecommList->getId()))) {
808  return;
809  }
810 
811  $oList = oxNew('oxarticlelist');
812  $oList->loadRecommArticles($oRecommList->getId(), ' order by oxobject2list.oxtimestamp desc limit ' . $this->getConfig()->getConfigParam('iRssItemsCount'));
813 
814  $oLang = oxRegistry::getLang();
815  $this->_loadData(
816  self::RSS_RECOMMLISTARTS . $oRecommList->getId(),
817  $this->getRecommListArticlesTitle($oRecommList),
818  sprintf($oLang->translateString('LISTMANIA_LIST_PRODUCTS', $oLang->getBaseLanguage()), $oRecommList->oxrecommlists__oxtitle->value),
819  $this->_getArticleItems($oList),
820  $this->getRecommListArticlesUrl($oRecommList),
821  $oRecommList->getLink()
822  );
823  }
824 
832  public function getBargainTitle()
833  {
834  $oLang = oxRegistry::getLang();
835  $iLang = $oLang->getBaseLanguage();
836 
837  return $this->_prepareFeedName($oLang->translateString('BARGAIN', $iLang));
838  }
839 
847  public function getBargainUrl()
848  {
849  return $this->_prepareUrl("cl=rss&amp;fnc=bargain", $this->getBargainTitle());
850  }
851 
859  public function loadBargain()
860  {
861  if (($this->_aChannel = $this->_loadFromCache(self::RSS_BARGAIN))) {
862  return;
863  }
864 
865  $oArtList = oxNew('oxarticlelist');
866  $oArtList->loadActionArticles('OXBARGAIN', $this->getConfig()->getConfigParam('iRssItemsCount'));
867 
868  $oLang = oxRegistry::getLang();
869  $this->_loadData(
870  self::RSS_BARGAIN,
871  $this->getBargainTitle(),
872  $oLang->translateString('BARGAIN_PRODUCTS', $oLang->getBaseLanguage()),
873  $this->_getArticleItems($oArtList),
874  $this->getBargainUrl()
875  );
876  }
877 
883  public function getCacheTtl()
884  {
885  return self::CACHE_TTL;
886  }
887 }