Skip to content

Commit

Permalink
Merge pull request #4908 from matsuoshi/feature/rssfeed
Browse files Browse the repository at this point in the history
新着商品・新着情報のRSSフィード機能
  • Loading branch information
Kiyotaka Oku committed Mar 12, 2021
2 parents 212ea0e + 625450e commit 9114846
Show file tree
Hide file tree
Showing 5 changed files with 193 additions and 0 deletions.
97 changes: 97 additions & 0 deletions src/Eccube/Controller/RssFeedController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?php

/*
* This file is part of EC-CUBE
*
* Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved.
*
* http://www.ec-cube.co.jp/
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Eccube\Controller;

use Eccube\Repository\NewsRepository;
use Eccube\Repository\ProductRepository;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

class RssFeedController extends AbstractController
{
/**
* @var ProductRepository
*/
protected $productRepository;

/**
* @var NewsRepository
*/
protected $newsRepository;

/**
* RssFeedController constructor.
*
* @param ProductRepository $productRepository
* @param NewsRepository $newsRepository
*/
public function __construct(ProductRepository $productRepository, NewsRepository $newsRepository)
{
$this->productRepository = $productRepository;
$this->newsRepository = $newsRepository;
}

/**
* @Route("/products_feed", name="rss_feed_for_products")
*/
public function products()
{
$products = $this->productRepository->findBy(
['Status' => 1],
[
'create_date' => 'DESC',
'id' => 'DESC',
],
20
);

$response = new Response();
$response->headers->set('Content-Type', 'application/xml; charset=UTF-8');

return $this->render(
'Feed/products.twig',
['products' => $products],
$response
);
}

/**
* @Route("/news_feed", name="rss_feed_for_news")
*/
public function news()
{
$builder = $this->newsRepository->createQueryBuilder('news');
$news = $builder
->where('news.visible = :visible')
->andWhere($builder->expr()->lte('news.publish_date', ':now'))
->setParameters([
'visible' => true,
'now' => new \DateTime(),
])
->orderBy('news.publish_date', 'DESC')
->addOrderBy('news.id', 'DESC')
->setMaxResults(20)
->getQuery()
->getResult();

$response = new Response();
$response->headers->set('Content-Type', 'application/xml; charset=UTF-8');

return $this->render(
'Feed/news.twig',
['news' => $news],
$response
);
}
}
26 changes: 26 additions & 0 deletions src/Eccube/Resource/template/default/Feed/news.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<title>{{ BaseInfo.shop_name }} 新着情報</title>
<description>{{ BaseInfo.shop_name }} 新着情報</description>
<link>{{ url('homepage') }}</link>
<lastBuildDate>{{ max( news | map(e => e.update_date) ) | date(constant('DATE_RFC2822')) }}</lastBuildDate>
<atom:link href="{{ url('rss_feed_for_news') }}" rel="self"/>

{% for article in news %}
<item>
<title>{{ article.title }}</title>
{% if article.url %}
<link>{{ article.url }}</link>
{% else %}
<link>{{ url('homepage') }}</link>
{% endif %}
<pubDate>{{ article.publish_date|date(constant('DATE_RFC2822')) }}</pubDate>
<guid isPermaLink="false">{{ url('homepage') }}rss/news/{{ article.id }}</guid>
<description><![CDATA[
{{ article.description|raw }}
]]></description>
</item>
{% endfor %}
</channel>
</rss>
23 changes: 23 additions & 0 deletions src/Eccube/Resource/template/default/Feed/products.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/">
<channel>
<title>{{ BaseInfo.shop_name }} 新着商品</title>
<description>{{ BaseInfo.shop_name }} 新着商品</description>
<link>{{ url('homepage') }}</link>
<lastBuildDate>{{ max( products | map(e => e.update_date) ) | date(constant('DATE_RFC2822')) }}</lastBuildDate>
<atom:link href="{{ url('rss_feed_for_products') }}" rel="self"/>

{% for product in products %}
<item>
<title>{{ product.name }}</title>
<link>{{ url('product_detail', {'id': product.id}) }}</link>
<pubDate>{{ product.create_date|date(constant('DATE_RFC2822')) }}</pubDate>
<guid isPermaLink="true">{{ url('product_detail', {'id': product.id}) }}</guid>
<description><![CDATA[
{{ product.description_detail }}
]]></description>
<media:thumbnail url="{{ absolute_url(asset(product.main_list_image|no_image_product, 'save_image')) }}"/>
</item>
{% endfor %}
</channel>
</rss>
2 changes: 2 additions & 0 deletions src/Eccube/Resource/template/default/default_frame.twig
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ file that was distributed with this source code.
{{ include(template_from_string(Page.meta_tags)) }}
{% endif %}
<link rel="icon" href="{{ asset('assets/img/common/favicon.ico', 'user_data') }}">
<link href="{{ url('rss_feed_for_products') }}" rel="alternate" type="application/rss+xml" title="{{ BaseInfo.shop_name }}"/>
<link href="{{ url('rss_feed_for_news') }}" rel="alternate" type="application/rss+xml" title="{{ BaseInfo.shop_name }}"/>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css" integrity="sha384-HSMxcRTRxnN+Bdg0JdbxYKrThecOKuH5zCYotlSAcp1+c8xmyTe9GYg1l9a69psu" crossorigin="anonymous">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.3.1/css/all.css" integrity="sha384-mzrmE5qonljUremFsqc01SB46JvROS7bZs3IO2EmfFsd15uHvIt+Y8vEf7N7fWAU" crossorigin="anonymous">
<link rel="stylesheet" href="//cdn.jsdelivr.net/jquery.slick/1.6.0/slick.css">
Expand Down
45 changes: 45 additions & 0 deletions tests/Eccube/Tests/Web/RssFeedControllerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

/*
* This file is part of EC-CUBE
*
* Copyright(c) EC-CUBE CO.,LTD. All Rights Reserved.
*
* http://www.ec-cube.co.jp/
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Eccube\Tests\Web;

class RssFeedControllerTest extends AbstractWebTestCase
{
/**
* 商品RSSフィードのテスト
*/
public function testRoutingRssFeedForProducts()
{
$client = $this->client;
$client->request('GET', $this->generateUrl('rss_feed_for_products'));
$content = simplexml_load_string($client->getResponse()->getContent());

$this->assertTrue($client->getResponse()->isSuccessful());
$this->assertCount(2, $content->channel->item);
$this->assertEquals('彩のジェラートCUBE', $content->channel->item[1]->title);
}

/**
* 新着情報RSSフィードのテスト
*/
public function testRoutingRssFeedForNews()
{
$client = $this->client;
$client->request('GET', $this->generateUrl('rss_feed_for_news'));
$content = simplexml_load_string($client->getResponse()->getContent());

$this->assertTrue($client->getResponse()->isSuccessful());
$this->assertCount(1, $content->channel->item);
$this->assertEquals('サイトオープンいたしました!', $content->channel->item[0]->title);
}
}

0 comments on commit 9114846

Please sign in to comment.