120 lines
3.8 KiB
PHP
Executable File
120 lines
3.8 KiB
PHP
Executable File
<?php
|
|
namespace Modules\Blog\ArticleList\Pipeline\DisplayContent;
|
|
|
|
use App\CMS\Contracts\Content\DisplayPipeline\Process;
|
|
use App\CMS\Contracts\Repositories\Content as IContent;
|
|
use App\Models\Content\Content;
|
|
use App\Models\User;
|
|
use Carbon\Carbon;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Support\Facades\Input;
|
|
use Modules\Blog\Article\Entities\Article as ArticleModel;
|
|
use Modules\Blog\Article\Handlers\Article;
|
|
use Modules\Blog\ArticleList\Handlers\ArticleList;
|
|
|
|
class LoadArticlesForList implements Process
|
|
{
|
|
/**
|
|
* Process and modify given page
|
|
*
|
|
* @param Content $page
|
|
*
|
|
* @return mixed
|
|
*/
|
|
function handle(Content & $page)
|
|
{
|
|
// load articles only for article list
|
|
if ($page->moduleHandler != ArticleList::class)
|
|
return;
|
|
|
|
$articles = ArticleModel::where('moduleHandler', Article::class)
|
|
->with('comments')
|
|
->with('category')
|
|
->where(function ($query) {
|
|
$query->where('publishedFrom', '<', Carbon::now())
|
|
->orWhereNull('publishedFrom');
|
|
})
|
|
->where(function ($query) {
|
|
$query->where('publishedTo', '>', Carbon::now())
|
|
->orWhereNull('publishedTo');
|
|
})
|
|
->orderBy('publishedFrom', 'DESC')
|
|
->orderBy('created_at', 'DESC');
|
|
|
|
// show articles for not-logged users of even for logged onew?
|
|
$articles = !Auth::user() ? $articles->where('status', IContent::STATUS_PUBLIC) : $articles->whereIn('status', [
|
|
IContent::STATUS_PUBLIC,
|
|
IContent::STATUS_PROTECTED
|
|
]);
|
|
|
|
// selected articles from archive?
|
|
$this->archive($articles);
|
|
|
|
// is given any category?
|
|
$this->category($articles);
|
|
|
|
// show articles only from specified user
|
|
$author = Input::get('author');
|
|
if ($author) {
|
|
$user = User::findOrFail($author);
|
|
|
|
$articles = $articles->where('user_id', $user->id);
|
|
}
|
|
|
|
$page->articles = $articles->paginate(setting('articlesPerPage', 20));
|
|
}
|
|
|
|
/**
|
|
* Filter articles on article list using archive
|
|
*
|
|
* @param $articles
|
|
*/
|
|
protected function archive(&$articles)
|
|
{
|
|
$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);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Filter article list to some specified category
|
|
*
|
|
* Category URL is specified as GET attribute "category"
|
|
*
|
|
* @param $articles
|
|
*/
|
|
protected function category(&$articles)
|
|
{
|
|
$categoryUrl = Input::get('category');
|
|
|
|
$categoriesEnabled = \Module::get('Categories')
|
|
->enabled();
|
|
|
|
if ($categoryUrl && $categoriesEnabled) {
|
|
$category = Category::where('url', $categoryUrl)
|
|
->firstOrFail();
|
|
|
|
$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);
|
|
}
|
|
}
|
|
} |