|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace WebDevEtc\BlogEtc\Controllers; |
|
4
|
|
|
|
|
5
|
|
|
use App\Http\Controllers\Controller; |
|
6
|
|
|
use Auth; |
|
7
|
|
|
use Carbon\Carbon; |
|
8
|
|
|
use Laravelium\Feed\Feed; |
|
9
|
|
|
use WebDevEtc\BlogEtc\Models\Post; |
|
10
|
|
|
use WebDevEtc\BlogEtc\Requests\FeedRequest; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Class BlogEtcRssFeedController.php |
|
14
|
|
|
* All RSS feed viewing methods. |
|
15
|
|
|
*/ |
|
16
|
|
|
class FeedController extends Controller |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* RSS Feed |
|
20
|
|
|
* This is a long (but quite simple) method to show an RSS feed |
|
21
|
|
|
* It makes use of Laravelium\Feed\Feed. |
|
22
|
|
|
* |
|
23
|
|
|
* @return mixed |
|
24
|
|
|
*/ |
|
25
|
|
|
public function feed(FeedRequest $request, Feed $feed) |
|
26
|
|
|
{ |
|
27
|
|
|
// for different caching |
|
28
|
|
|
$user_or_guest = Auth::check() ? Auth::user()->id : 'guest'; |
|
29
|
|
|
|
|
30
|
|
|
$feed->setCache( |
|
31
|
|
|
config('blogetc.rssfeed.cache_in_minutes', 60), |
|
32
|
|
|
'blogetc-'.$request->getFeedType().$user_or_guest |
|
33
|
|
|
); |
|
34
|
|
|
|
|
35
|
|
|
if (!$feed->isCached()) { |
|
36
|
|
|
$this->makeFreshFeed($feed); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
return $feed->render($request->getFeedType()); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param $feed |
|
44
|
|
|
*/ |
|
45
|
|
|
protected function makeFreshFeed(Feed $feed) |
|
46
|
|
|
{ |
|
47
|
|
|
$posts = Post::orderBy('posted_at', 'desc') |
|
48
|
|
|
->limit(config('blogetc.rssfeed.posts_to_show_in_rss_feed', 10)) |
|
49
|
|
|
->with('author') |
|
50
|
|
|
->get(); |
|
51
|
|
|
|
|
52
|
|
|
$this->setupFeed($feed, $posts); |
|
53
|
|
|
|
|
54
|
|
|
/** @var Post $post */ |
|
55
|
|
|
foreach ($posts as $post) { |
|
56
|
|
|
$feed->add($post->title, |
|
57
|
|
|
$post->authorString(), |
|
58
|
|
|
$post->url(), |
|
59
|
|
|
$post->posted_at, |
|
60
|
|
|
$post->short_description, |
|
61
|
|
|
$post->generateIntroduction() |
|
62
|
|
|
); |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @param $posts |
|
68
|
|
|
* |
|
69
|
|
|
* @return mixed |
|
70
|
|
|
*/ |
|
71
|
|
|
protected function setupFeed(Feed $feed, $posts) |
|
72
|
|
|
{ |
|
73
|
|
|
$feed->title = config('app.name').' Blog'; |
|
74
|
|
|
$feed->description = config('blogetc.rssfeed.description', 'Our blog RSS feed'); |
|
75
|
|
|
$feed->link = route('blogetc.index'); |
|
76
|
|
|
$feed->setDateFormat('carbon'); |
|
77
|
|
|
$feed->pubdate = isset($posts[0]) ? $posts[0]->posted_at : Carbon::now()->subYear(); |
|
78
|
|
|
$feed->lang = config('blogetc.rssfeed.language', 'en'); |
|
79
|
|
|
$feed->setShortening(config('blogetc.rssfeed.should_shorten_text', true)); |
|
80
|
|
|
$feed->setTextLimit(config('blogetc.rssfeed.text_limit', 100)); |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
|