forked from EC-CUBE/ec-cube
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request EC-CUBE#4908 from matsuoshi/feature/rssfeed
新着商品・新着情報のRSSフィード機能
- Loading branch information
Showing
5 changed files
with
193 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |