Skip to content

Commit

Permalink
Merge pull request #41 from MayMeow/dev/blogs-homepages
Browse files Browse the repository at this point in the history
Added blogs homepages
  • Loading branch information
MayMeow authored May 8, 2023
2 parents 5a58894 + 7bfa798 commit 5e42327
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/Controller/Admin/ArticlesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function index()
$this->Authorization->skipAuthorization();

$this->paginate = [
'contain' => ['Users'],
'contain' => ['Users', 'Blogs'],
];
$articles = $this->paginate($this->Articles);

Expand Down
4 changes: 3 additions & 1 deletion src/Controller/ArticlesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
Expand All @@ -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'));
}

/**
Expand Down
19 changes: 19 additions & 0 deletions src/Services/ArticlesManagerService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
}
9 changes: 9 additions & 0 deletions src/Services/ArticlesManagerServiceInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
19 changes: 14 additions & 5 deletions templates/Admin/Articles/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
<th>
<?= $this->Paginator->sort('title') ?>
</th>
<th><?= $this->Paginator->sort('slug') ?></th>
<th><?= $this->Paginator->sort('published') ?></th>
<th><?= $this->Paginator->sort('modified') ?></th>
<th class="actions"><?= __('Actions') ?></th>
Expand All @@ -27,13 +26,23 @@
<tbody>
<?php foreach ($articles as $article): ?>
<tr>
<td><td><?= $this->Number->format($article->id) ?></td></td>
<td><?= $this->Number->format($article->id) ?></td>
<td>
<?= h($article->title) ?>
<?= $article->blog->title ?> /
<?php if ($article->title == $article->blog->domain) : ?>
<span data-tooltip="<?= __('This Is Homepage of {0} blog', $article->blog->title) ?>">
<?= h($article->title) ?>
</span>
<?php else : ?>
<?= h($article->title) ?>
<?php endif; ?>
<mark><?= ArticleType::from($article->article_type)->name ?></mark>
</td>
<td><?= h($article->slug) ?></td>
<td><?= h($article->published) ?></td>
<td>
<span data-tooltip="<?= $article->slug ?>">
<?= $article->published ? __('Yes') : __('No') ?>
</span>
</td>
<td><?= h($article->modified) ?></td>
<td class="actions">
<?= $this->Html->link(__('View'), ['action' => 'view', $article->id]) ?>
Expand Down
4 changes: 3 additions & 1 deletion templates/Admin/Articles/view.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
</aside>
<div class="column-responsive column-80">
<div class="articles view content">
<h3><?= h($article->title) ?> <mark><?= ArticleType::from($article->article_type)->name ?></mark></h3>
<h3><?= h($article->title) ?>
<mark><?= $article->title == $article->blog->domain ? __('Home Page of {0}', $article->blog->title) : ArticleType::from($article->article_type)->name ?></mark>
</h3>
<table>
<tr>
<th><?= __('User') ?></th>
Expand Down
3 changes: 3 additions & 0 deletions templates/Articles/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
*/
?>
<div class="articles index content">

<?= $homePage ? $this->Markdown->parse($homePage) : '' ?>

<h3><?= __('Latest Articles') ?></h3>
<div style="margin-bottom: 1em;">
<?php foreach ($articles as $article): ?>
Expand Down

0 comments on commit 5e42327

Please sign in to comment.