From 7bfa798f37080c8f1139ba3ca0cbb483e8fd67b2 Mon Sep 17 00:00:00 2001 From: May Date: Mon, 8 May 2023 23:18:50 +0200 Subject: [PATCH] Add blogs homepages --- src/Controller/Admin/ArticlesController.php | 2 +- src/Controller/ArticlesController.php | 4 +++- src/Services/ArticlesManagerService.php | 19 +++++++++++++++++++ .../ArticlesManagerServiceInterface.php | 9 +++++++++ templates/Admin/Articles/index.php | 19 ++++++++++++++----- templates/Admin/Articles/view.php | 4 +++- templates/Articles/index.php | 3 +++ 7 files changed, 52 insertions(+), 8 deletions(-) diff --git a/src/Controller/Admin/ArticlesController.php b/src/Controller/Admin/ArticlesController.php index 17ed468..6cfb1f9 100644 --- a/src/Controller/Admin/ArticlesController.php +++ b/src/Controller/Admin/ArticlesController.php @@ -26,7 +26,7 @@ public function index() $this->Authorization->skipAuthorization(); $this->paginate = [ - 'contain' => ['Users'], + 'contain' => ['Users', 'Blogs'], ]; $articles = $this->paginate($this->Articles); diff --git a/src/Controller/ArticlesController.php b/src/Controller/ArticlesController.php index 494bc60..66d10bb 100644 --- a/src/Controller/ArticlesController.php +++ b/src/Controller/ArticlesController.php @@ -45,6 +45,8 @@ public function index(ArticlesManagerServiceInterface $articlesManager, BlogsMan return $this->redirect($dr); } + $homePage = $articlesManager->getHomePageContent($this->request); + $this->paginate = [ 'contain' => ['Users', 'Blogs'], 'order' => ['Articles.created' => 'DESC'], @@ -53,7 +55,7 @@ public function index(ArticlesManagerServiceInterface $articlesManager, BlogsMan $articles = $articlesManager->getAll($this->request, $this); - $this->set(compact('articles')); + $this->set(compact('articles', 'homePage')); } /** diff --git a/src/Services/ArticlesManagerService.php b/src/Services/ArticlesManagerService.php index 22d246a..5472e3c 100644 --- a/src/Services/ArticlesManagerService.php +++ b/src/Services/ArticlesManagerService.php @@ -7,6 +7,7 @@ use Cake\ORM\Locator\LocatorAwareTrait; use Cake\ORM\Query; use Cake\ORM\Table; +use Cake\Utility\Text; use MeowBlog\Controller\AppController; use MeowBlog\Model\Entity\Article; use MeowBlog\Model\Entity\ArticleType; @@ -107,4 +108,22 @@ public function saveToDatabase(Article $article, ServerRequest $request): Articl return $savedArticle; } + + public function getHomePageContent(ServerRequest $request): ?string + { + /** @var \MeowBlog\Model\Table\ArticlesTable $articleTable */ + $articleTable = $this->articles; + + /** @var \Cake\ORM\Query $q */ + $q = $articleTable->findBySlug(Text::slug($request->getUri()->getHost()))->where([ + 'Blogs.domain' => $request->getUri()->getHost(), + 'Articles.article_type' => ArticleType::Page->value, + 'Articles.published' => 1, + ]); + + /** @var \MeowBlog\Model\Entity\Article $savedArticle */ + $article = $q->contain(['Blogs'])->first(); + + return $article ? $article->body : null; + } } diff --git a/src/Services/ArticlesManagerServiceInterface.php b/src/Services/ArticlesManagerServiceInterface.php index ae0b50c..96d4894 100644 --- a/src/Services/ArticlesManagerServiceInterface.php +++ b/src/Services/ArticlesManagerServiceInterface.php @@ -40,4 +40,13 @@ public function getAll(ServerRequest $request, AppController $controller, bool $ * @return \MeowBlog\Model\Entity\Article|false */ public function saveToDatabase(Article $article, ServerRequest $request): Article | false; + + /** + * Return content of the homepage articles + * Home-page articles are articles that have title matched with the blog domain + * + * @param ServerRequest $request + * @return string|null + */ + public function getHomePageContent(ServerRequest $request): ?string; } diff --git a/templates/Admin/Articles/index.php b/templates/Admin/Articles/index.php index 69d72a4..372baee 100644 --- a/templates/Admin/Articles/index.php +++ b/templates/Admin/Articles/index.php @@ -18,7 +18,6 @@ Paginator->sort('title') ?> - Paginator->sort('slug') ?> Paginator->sort('published') ?> Paginator->sort('modified') ?> @@ -27,13 +26,23 @@ - Number->format($article->id) ?> + Number->format($article->id) ?> - title) ?> + blog->title ?> / + title == $article->blog->domain) : ?> + + title) ?> + + + title) ?> + article_type)->name ?> - slug) ?> - published) ?> + + + published ? __('Yes') : __('No') ?> + + modified) ?> Html->link(__('View'), ['action' => 'view', $article->id]) ?> diff --git a/templates/Admin/Articles/view.php b/templates/Admin/Articles/view.php index b29bc29..09c1f1d 100644 --- a/templates/Admin/Articles/view.php +++ b/templates/Admin/Articles/view.php @@ -19,7 +19,9 @@
-

title) ?> article_type)->name ?>

+

title) ?> + title == $article->blog->domain ? __('Home Page of {0}', $article->blog->title) : ArticleType::from($article->article_type)->name ?> +

diff --git a/templates/Articles/index.php b/templates/Articles/index.php index 66f3d95..2dd3907 100644 --- a/templates/Articles/index.php +++ b/templates/Articles/index.php @@ -6,6 +6,9 @@ */ ?>
+ + Markdown->parse($homePage) : '' ?> +