From 55f9c0a679a67edda51e928bada4c68aaaf0b85d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Kv=C3=A1=C4=8Dek?= Date: Thu, 9 Mar 2017 00:09:26 +0100 Subject: [PATCH] FEATURE #2350 @1h: move beforeRender methods into pipeline --- modules/Blog/Article/Handlers/Article.php | 33 ----- .../LoadCommentsForBlogpost.php | 33 +++++ .../Providers/ArticleServiceProvider.php | 5 + .../Blog/ArticleList/Handlers/ArticleList.php | 119 +---------------- .../DisplayContent/LoadArticlesForList.php | 120 ++++++++++++++++++ .../Providers/ArticleListServiceProvider.php | 8 ++ modules/ContactForm/Handlers/ContactForm.php | 11 -- .../PipelineProcess/RenderPage.php | 20 +++ .../Providers/StaticPagesServiceProvider.php | 1 + 9 files changed, 188 insertions(+), 162 deletions(-) create mode 100644 modules/Blog/Article/Pipeline/DisplayContent/LoadCommentsForBlogpost.php create mode 100644 modules/Blog/ArticleList/Pipeline/DisplayContent/LoadArticlesForList.php create mode 100644 modules/StaticPages/PipelineProcess/RenderPage.php diff --git a/modules/Blog/Article/Handlers/Article.php b/modules/Blog/Article/Handlers/Article.php index f8234344..1db036e1 100644 --- a/modules/Blog/Article/Handlers/Article.php +++ b/modules/Blog/Article/Handlers/Article.php @@ -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? * diff --git a/modules/Blog/Article/Pipeline/DisplayContent/LoadCommentsForBlogpost.php b/modules/Blog/Article/Pipeline/DisplayContent/LoadCommentsForBlogpost.php new file mode 100644 index 00000000..3f7b606a --- /dev/null +++ b/modules/Blog/Article/Pipeline/DisplayContent/LoadCommentsForBlogpost.php @@ -0,0 +1,33 @@ +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(); + } + } +} \ No newline at end of file diff --git a/modules/Blog/Article/Providers/ArticleServiceProvider.php b/modules/Blog/Article/Providers/ArticleServiceProvider.php index ccfe2120..6ba07271 100644 --- a/modules/Blog/Article/Providers/ArticleServiceProvider.php +++ b/modules/Blog/Article/Providers/ArticleServiceProvider.php @@ -1,6 +1,7 @@ app->register(ArticleEventServiceProvider::class); + + // load comments for blogpost + $pipeline = app('displayPipeline'); + $pipeline->add(LoadCommentsForBlogpost::class); } protected function setModuleAlias() diff --git a/modules/Blog/ArticleList/Handlers/ArticleList.php b/modules/Blog/ArticleList/Handlers/ArticleList.php index fc8c86cf..2f67d703 100644 --- a/modules/Blog/ArticleList/Handlers/ArticleList.php +++ b/modules/Blog/ArticleList/Handlers/ArticleList.php @@ -1,18 +1,8 @@ '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() diff --git a/modules/Blog/ArticleList/Pipeline/DisplayContent/LoadArticlesForList.php b/modules/Blog/ArticleList/Pipeline/DisplayContent/LoadArticlesForList.php new file mode 100644 index 00000000..7e7b06c1 --- /dev/null +++ b/modules/Blog/ArticleList/Pipeline/DisplayContent/LoadArticlesForList.php @@ -0,0 +1,120 @@ +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); + } + } +} \ No newline at end of file diff --git a/modules/Blog/ArticleList/Providers/ArticleListServiceProvider.php b/modules/Blog/ArticleList/Providers/ArticleListServiceProvider.php index 280c1f84..75fea4ae 100644 --- a/modules/Blog/ArticleList/Providers/ArticleListServiceProvider.php +++ b/modules/Blog/ArticleList/Providers/ArticleListServiceProvider.php @@ -1,6 +1,7 @@ path = __DIR__; } + + public function boot() + { + parent::boot(); + + app('displayPipeline')->add(LoadArticlesForList::class); + } } diff --git a/modules/ContactForm/Handlers/ContactForm.php b/modules/ContactForm/Handlers/ContactForm.php index 76718dbf..cf53c414 100644 --- a/modules/ContactForm/Handlers/ContactForm.php +++ b/modules/ContactForm/Handlers/ContactForm.php @@ -190,15 +190,4 @@ class ContactForm extends Handler implements ContentTitles, AdvancedContentModif public static function delete(Content $content) { } - - /** - * Load some extra data for content management form? - * - * @param Content $content - * - * @return mixed - */ - public static function loadExtraData(Content $content) - { - } } \ No newline at end of file diff --git a/modules/StaticPages/PipelineProcess/RenderPage.php b/modules/StaticPages/PipelineProcess/RenderPage.php new file mode 100644 index 00000000..ee8f63aa --- /dev/null +++ b/modules/StaticPages/PipelineProcess/RenderPage.php @@ -0,0 +1,20 @@ +