Cachet/app/Http/Controllers/RssController.php

72 lines
2.1 KiB
PHP
Raw Normal View History

2015-01-02 14:27:51 +00:00
<?php
/*
* This file is part of Cachet.
*
2015-07-06 17:37:01 +01:00
* (c) Alt Three Services Limited
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
2015-01-02 14:27:51 +00:00
namespace CachetHQ\Cachet\Http\Controllers;
2015-01-14 17:19:41 -06:00
use CachetHQ\Cachet\Facades\Setting;
2015-04-03 18:06:08 +01:00
use CachetHQ\Cachet\Models\ComponentGroup;
2015-01-02 14:27:51 +00:00
use CachetHQ\Cachet\Models\Incident;
use Illuminate\Routing\Controller;
2015-06-12 22:48:09 +01:00
use Illuminate\Support\Str;
2015-01-02 14:27:51 +00:00
use Roumen\Feed\Facades\Feed;
class RssController extends Controller
2015-01-02 14:27:51 +00:00
{
/**
2015-03-26 15:07:16 -06:00
* Generates an Atom feed of all incidents.
*
* @param \CachetHQ\Cachet\Models\ComponentGroup|null $group
2015-01-02 14:27:51 +00:00
*
* @return \Illuminate\Http\Response
*/
2015-03-26 15:07:16 -06:00
public function feedAction(ComponentGroup $group = null)
2015-01-02 14:27:51 +00:00
{
$feed = Feed::make();
$feed->title = Setting::get('app_name');
$feed->lang = Setting::get('app_locale');
$feed->description = trans('cachet.feed');
2015-06-12 22:48:09 +01:00
$feed->link = Str::canonicalize(Setting::get('app_domain'));
2015-01-02 14:27:51 +00:00
$feed->setDateFormat('datetime');
if ($group->exists) {
2015-03-26 15:07:16 -06:00
$group->components->map(function ($component) use ($feed) {
2015-06-12 08:56:17 +01:00
$component->incidents()->visible()->orderBy('created_at', 'desc')->get()->map(function ($incident) use ($feed) {
2015-03-26 15:07:16 -06:00
$this->feedAddItem($feed, $incident);
});
});
} else {
2015-06-12 08:56:17 +01:00
Incident::visible()->orderBy('created_at', 'desc')->get()->map(function ($incident) use ($feed) {
2015-03-26 15:07:16 -06:00
$this->feedAddItem($feed, $incident);
});
}
2015-01-02 14:27:51 +00:00
2015-03-20 18:30:45 -06:00
return $feed->render('rss');
2015-01-02 14:27:51 +00:00
}
2015-03-26 15:07:16 -06:00
/**
* Adds an item to the feed.
*
2015-06-12 22:49:28 +01:00
* @param \Roumen\Feed\Facades\Feed $feed
2015-03-26 15:07:16 -06:00
* @param \CachetHQ\Cachet\Models\Incident $incident
*/
2015-03-26 15:09:49 -06:00
private function feedAddItem(&$feed, $incident)
2015-03-26 15:07:16 -06:00
{
$feed->add(
$incident->name,
Setting::get('app_name'),
Str::canonicalize(route('incident', ['id' => $incident->id])),
2015-04-03 18:06:08 +01:00
$incident->created_at->toRssString(),
2015-03-26 15:07:16 -06:00
$incident->message
);
}
2015-01-02 14:27:51 +00:00
}