summaryrefslogtreecommitdiff
path: root/app
diff options
context:
space:
mode:
Diffstat (limited to 'app')
-rw-r--r--app/Http/Controllers/FeedController.php67
-rw-r--r--app/Libraries/Helper.php73
2 files changed, 140 insertions, 0 deletions
diff --git a/app/Http/Controllers/FeedController.php b/app/Http/Controllers/FeedController.php
new file mode 100644
index 0000000..9fd73a5
--- /dev/null
+++ b/app/Http/Controllers/FeedController.php
@@ -0,0 +1,67 @@
+<?php
+
+namespace App\Http\Controllers;
+
+use Illuminate\Http\Request;
+use Illuminate\Support\Facades\DB;
+use App\Article;
+use App\Discussion;
+use App\Category;
+use App\Libraries\Helper;
+
+class FeedController extends Controller
+{
+
+ public function new()
+ {
+ // creating rss feed with our most recent 20 posts
+ $articles = Article::orderBy('created_at', 'desc')->take(20)->get();
+ return Helper::makeFeed($articles, "new");
+ }
+
+ public function popular()
+ {
+
+ $articles = new Article;
+ $articles = $articles->setTable('view_popular');
+ $articles = $articles->take(20)->get();
+ return Helper::makeFeed($articles, "popular");
+ }
+
+ public function search(Request $request) {
+ $search_unsafe = $request->input("q");
+
+ if ( "" == $search_unsafe ) {
+ $search_unsafe = "";
+ }
+
+ $search_unsafe = explode(",", $request->input("q"));
+
+ $articles = new Article;
+
+ if ( "on" == $request->input("onlypopular") ) {
+ $articles = $articles->setTable('view_popular');
+ }
+
+ foreach($search_unsafe as $q) {
+ $q = Helper::escapeLike($q);
+ $q = "%".$q."%";
+ $articles = $articles->where(function ($query) use ($q) {
+ $query->whereHas('getCategories', function ($query) use ($q){
+ $query->where('name', 'like', $q);
+ })
+ ->orWhere('title', 'like', $q)
+ ->orWhere('url', 'like', $q)
+ ->orWhere('excerpt_html', 'like', $q);
+ });
+ }
+ $count = $articles->count();
+
+ if ( "on" == $request->input("onlypopular") ) {
+ $articles = $articles->orderBy('impact', 'desc');
+ }
+ $articles = $articles->orderBy('created_at', 'desc');
+
+ return Helper::makeFeed($articles->take(20)->get(), $request->input("q"));
+ }
+}
diff --git a/app/Libraries/Helper.php b/app/Libraries/Helper.php
index d85d216..69e7de6 100644
--- a/app/Libraries/Helper.php
+++ b/app/Libraries/Helper.php
@@ -15,4 +15,77 @@ class Helper {
public static function formatTimestamp($timestamp) {
return Carbon::createFromTimestamp($timestamp)->format("Y-m-d");
}
+
+ public static function makeFeed($model, $title, $cache = 60) {
+
+ switch($title) {
+ case("new"):
+ $feed_title = 'Newest Articles';
+ $feed_description = 'Newest interesting articles from Wikipedia. Keep exploring.';
+ break;
+ case( "popular"):
+ $feed_title = 'Popular Articles';
+ $feed_description = 'The most popular articles. Keep exploring.';
+ break;
+ default:
+ $feed_title = 'Search for: ' . $title;
+ $feed_description = 'All articles for "' . $title . '".';
+ break;
+ }
+ // create new feed
+ $feed = \App::make("feed");
+
+ // multiple feeds are supported
+ // if you are using caching you should set different cache keys for your feeds
+
+ // cache the feed for $cache minutes (second parameter is optional)
+ $feed->setCache($cache, 'feed_' . $title);
+
+ // check if there is cached feed and build new only if is not
+ if (!$feed->isCached())
+ {
+
+ // set your feed's title, description, link, pubdate and language
+ $feed->title = $feed_title;
+ $feed->description = $feed_description;
+ #$feed->logo = 'http://yoursite.tld/logo.jpg';
+ $feed->link = url('feed/' . $title);
+ $feed->setDateFormat('datetime'); // 'datetime', 'timestamp' or 'carbon'
+ $feed->pubdate = $model[0]->created_at;
+ $feed->lang = 'en';
+ $feed->setShortening(true); // true or false
+ $feed->setTextLimit(100); // maximum length of description text
+
+ foreach ($model as $post)
+ {
+
+ $desc = ($post->excerpt_html);
+
+ $categories = "<br>";
+ foreach( $post->getCategories()->get() as $cat ) {
+ $categories .= "<a href='". \URL::to('/topic/' . $cat->name) ."'>". $cat->name . "</a> | ";
+ }
+ $categories = rtrim($categories, " | ");
+ $desc .= "<br>Topics:";
+ $desc .= $categories;
+
+ $discussions = "<br>";
+ foreach( $post->getDiscussions()->orderBy('comments', 'desc')->get() as $dis ) {
+ $discussions .= "<a href='" . $dis->source_url . "'>" . $dis->title . "</a> ";
+ $discussions .= $dis->upvotes . " Upvotes | " . $dis->comments . " Comments<br>";
+ }
+ $desc .= "<br><br>Discussions:";
+ $desc .= $discussions;
+
+ // set item's title, author, url, pubdate, description, content, enclosure (optional)*
+ $feed->add($post->title, env('APP_NAME'), \URL::to($post->url), $post->created_at, $desc, $desc);
+ }
+
+ }
+
+ // first param is the feed format
+ // optional: second param is cache duration (value of 0 turns off caching)
+ // optional: you can set custom cache key with 3rd param as string
+ return $feed->render('atom', $cache, 'feed_' . $title);
+ }
}