FEATURE #2350 @1h: move beforeRender methods into pipeline
This commit is contained in:
@@ -16,28 +16,6 @@ use Modules\Blog\Article\Entities\Comment;
|
||||
|
||||
class Article extends Handler implements ContentTitles, AdvancedContentModifier, InstallableModule
|
||||
{
|
||||
/**
|
||||
* 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)
|
||||
{
|
||||
$content->comments = new Collection();
|
||||
if (isset($content->moduleSettings['allowComments']) && $content->moduleSettings['allowComments']) {
|
||||
$content->comments = Comment::where('content_id', $content->id)
|
||||
->with('author')
|
||||
->where('approved', 1)
|
||||
->orderBy('created_at')
|
||||
->get();
|
||||
}
|
||||
|
||||
return Controller::VisitorsView($content->template, ['page' => $content, 'extra' => $parameters]);
|
||||
}
|
||||
|
||||
public static function getExtendedFormInputsCreate()
|
||||
{
|
||||
$sections = parent::getExtendedFormInputsCreate();
|
||||
@@ -198,17 +176,6 @@ class Article extends Handler implements ContentTitles, AdvancedContentModifier,
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Load some extra data for content management form?
|
||||
*
|
||||
* @param Content $content
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public static function loadExtraData(Content $content)
|
||||
{
|
||||
}
|
||||
|
||||
/**
|
||||
* Does this module support categories?
|
||||
*
|
||||
|
||||
@@ -0,0 +1,33 @@
|
||||
<?php
|
||||
namespace Modules\Blog\Article\Pipeline\DisplayContent;
|
||||
|
||||
use App\CMS\Contracts\Content\DisplayPipeline\Process;
|
||||
use App\Models\Content\Content;
|
||||
use Illuminate\Support\Collection;
|
||||
use Modules\Blog\Article\Entities\Comment;
|
||||
use Modules\Blog\Article\Handlers\Article;
|
||||
|
||||
class LoadCommentsForBlogpost implements Process
|
||||
{
|
||||
/**
|
||||
* Process and modify given page
|
||||
*
|
||||
* @param Content $content
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
function handle(Content & $content)
|
||||
{
|
||||
if ($content->handler != Article::class)
|
||||
return;
|
||||
|
||||
$content->comments = new Collection();
|
||||
if (isset($content->moduleSettings['allowComments']) && $content->moduleSettings['allowComments']) {
|
||||
$content->comments = Comment::where('content_id', $content->id)
|
||||
->with('author')
|
||||
->where('approved', 1)
|
||||
->orderBy('created_at')
|
||||
->get();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
<?php namespace Modules\Blog\Article\Providers;
|
||||
|
||||
use App\Providers\ModuleServiceProvider;
|
||||
use Modules\Blog\Article\Pipeline\DisplayContent\LoadCommentsForBlogpost;
|
||||
|
||||
class ArticleServiceProvider extends ModuleServiceProvider
|
||||
{
|
||||
@@ -14,6 +15,10 @@ class ArticleServiceProvider extends ModuleServiceProvider
|
||||
parent::boot();
|
||||
|
||||
$this->app->register(ArticleEventServiceProvider::class);
|
||||
|
||||
// load comments for blogpost
|
||||
$pipeline = app('displayPipeline');
|
||||
$pipeline->add(LoadCommentsForBlogpost::class);
|
||||
}
|
||||
|
||||
protected function setModuleAlias()
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user