FEATURE #2350 @1h: move beforeRender methods into pipeline

This commit is contained in:
Michal Kváček
2017-03-09 00:09:26 +01:00
parent 03ce64a731
commit 55f9c0a679
9 changed files with 188 additions and 162 deletions
@@ -1,18 +1,8 @@
<?php
namespace Modules\Blog\ArticleList\Handlers;
use App\CMS\Contracts\Repositories\Content as IContent;
use App\CMS\Handlers\Handler;
use App\Http\Controllers\Controller;
use App\Models\Content\Content;
use App\Models\User;
use Carbon\Carbon;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;
use Modules\Blog\Article\Entities\Article as ArticleModel;
use Modules\Blog\Article\Handlers\Article;
use Modules\Categories\Entities\Category;
class ArticleList extends Handler
{
@@ -28,114 +18,7 @@ class ArticleList extends Handler
}, 20, ['icon' => 'fa fa-fw fa-book']);
}
$blogSubmenu->route('content.new', trans('articlelist::module.name'), ['id' => 'blog-articlelist'], 100, ['icon' => 'fa fa-fw fa-list-alt']);
}
/**
* Filter articles on article list using archive
*
* @param $articles
*/
protected static 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 static 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);
}
}
/**
* Render given module into visitor area.
*
* @param Content $content DB model from table contents
* @param array $parameters parameters used for modifications/custom settings of given model
*
* @return mixed
*/
public function render(Content $content, $parameters)
{
$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?
self::archive($articles);
// is given any category?
self::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);
}
$articles = $articles->paginate(setting('articlesPerPage', 20));
return Controller::VisitorsView($content->template, [
'page' => $content,
'articles' => $articles,
'extra' => $parameters
]);
$blogSubmenu->route('content.new', trans('articlelist::module.name'), ['id' => config('articlelist.alias')], 100, ['icon' => 'fa fa-fw fa-list-alt']);
}
public static function supportCategories()
@@ -0,0 +1,120 @@
<?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->handler != 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);
}
}
}
@@ -1,6 +1,7 @@
<?php namespace Modules\Blog\ArticleList\Providers;
use App\Providers\ModuleServiceProvider;
use Modules\Blog\ArticleList\Pipeline\DisplayContent\LoadArticlesForList;
class ArticleListServiceProvider extends ModuleServiceProvider
{
@@ -13,4 +14,11 @@ class ArticleListServiceProvider extends ModuleServiceProvider
{
$this->path = __DIR__;
}
public function boot()
{
parent::boot();
app('displayPipeline')->add(LoadArticlesForList::class);
}
}