FEATURE: add article list -- advanced options

This commit is contained in:
Michal Kváček
2016-09-07 02:21:30 +02:00
parent 44677aea78
commit f8127f3ed6
@@ -8,7 +8,10 @@ use App\Http\Requests\Request;
use App\Models\Content; use App\Models\Content;
use App\Models\User; use App\Models\User;
use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;
use Modules\Blog\Article\Handlers\Article; use Modules\Blog\Article\Handlers\Article;
use Modules\Categories\Entities\Category;
class ArticleList extends Handler class ArticleList extends Handler
{ {
@@ -39,13 +42,60 @@ class ArticleList extends Handler
{ {
$articles = Content::where('module_handler', Article::class); $articles = Content::where('module_handler', Article::class);
// selected category?
// selected articles from archive?
$year = Input::get('y');
$month = Input::get('m');
$day = Input::get('d');
if ($year) {
$articles = $articles->where(DB::raw('YEAR(created_at)'), $year);
}
if ($month) {
$articles = $articles->where(DB::raw('MONTH(created_at)'), $month);
}
if ($day) {
$articles = $articles->where(DB::raw('DAY(created_at)'), $day);
}
// is given any category?
$categoryUrl = Input::get('category');
if ($categoryUrl && \Module::get('Categories')
->enabled()
) {
$category = Category::where('url', $categoryUrl)
->first();
$subcategories = $category->getChildren()
->toArray();
$categories = [$category->id];
$categories = array_merge($categories, array_map(function ($cat) {
return $cat['id'];
}, $subcategories));
$articles = $articles->whereIn('category_id', $categories);
}
// show articles for not-logged users of even for logged onew?
if (!Auth::user()) { if (!Auth::user()) {
$articles = $articles->where('status', Content::STATUS_PUBLIC); $articles = $articles->where('status', Content::STATUS_PUBLIC);
} else { } else {
$articles = $articles->whereIn('status', [Content::STATUS_PUBLIC, Content::STATUS_PROTECTED]); $articles = $articles->whereIn('status', [Content::STATUS_PUBLIC, Content::STATUS_PROTECTED]);
} }
$articles = $articles->orderBy('created_at') // show articles only from specified user
$author = Input::get('author');
if ($author) {
$user = User::findOrFail($author);
$articles = $articles->where('user_id', $user->id);
}
$articles = $articles->orderBy('created_at', 'DESC')
->paginate(20); ->paginate(20);
return Controller::VisitorsView($content->template, ['page' => $content, 'articles' => $articles]); return Controller::VisitorsView($content->template, ['page' => $content, 'articles' => $articles]);