Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

新着商品・新着情報のRSSフィード機能 #4908

Merged
merged 5 commits into from
Mar 12, 2021
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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.xml", name="rss_feed_for_products")
kiy0taka marked this conversation as resolved.
Show resolved Hide resolved
*/
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.xml.twig',
['products' => $products],
$response
);
}

/**
* @Route("/news_feed.xml", 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.xml.twig',
['news' => $news],
$response
);
}
}
26 changes: 26 additions & 0 deletions src/Eccube/Resource/template/default/Feed/news.xml.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>{{ "now" | date(constant('DATE_RFC822')) }}</lastBuildDate>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lastBuildDate | The last time the content of the channel changed.
https://validator.w3.org/feed/docs/rss2.html#optionalChannelElements

なので、お知らせの最終更新日時がいいかと思います。(商品情報も同様)

kiy0taka marked this conversation as resolved.
Show resolved Hide resolved
<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_RFC822')) }}</pubDate>
<guid>{{ url('homepage') }}rss/news/{{ article.id }}</guid>
kiy0taka marked this conversation as resolved.
Show resolved Hide resolved
<description><![CDATA[
{{ article.description|raw }}
]]></description>
</item>
{% endfor %}
</channel>
</rss>
23 changes: 23 additions & 0 deletions src/Eccube/Resource/template/default/Feed/products.xml.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>{{ "now" | date(constant('DATE_RFC822')) }}</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_RFC822')) }}</pubDate>
<guid>{{ url('homepage') }}rss/products/{{ 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);
}
}