Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added now pages #42

Merged
merged 1 commit into from
May 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions config/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@
$builder->connect('/article/{slug}', ['controller' => 'Articles', 'action' => 'view'])->setPass(['slug']);
$builder->connect('/page/{slug}', ['controller' => 'Articles', 'action' => 'view'])->setPass(['slug']);

$builder->connect('/now', ['controller' => 'Articles', 'action' => 'now']);

$builder->scope("/articles", function (RouteBuilder $builder) {
$builder->connect('/tagged/*', ['controller' => 'Articles', 'action' => 'tags']);
$builder->connect('/v/{slug}', ['controller' => 'Articles', 'action' => 'view'])->setPass(['slug']);
Expand Down
16 changes: 15 additions & 1 deletion src/Controller/ArticlesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class ArticlesController extends AppController
public function beforeFilter(EventInterface $event)
{
parent::beforeFilter($event);
$this->Authentication->allowUnauthenticated(['index', 'tags', 'view']);
$this->Authentication->allowUnauthenticated(['index', 'tags', 'view', 'now']);
}

/**
Expand Down Expand Up @@ -173,4 +173,18 @@ public function tags(string ...$tags)
//'_serialize' => ['articles', 'tags']
]);
}

/**
* Returns latest article tagged with NOW
*
* @param ArticlesManagerServiceInterface $articlesManager
* @return void
*/
public function now(ArticlesManagerServiceInterface $articlesManager)
{
$content = $articlesManager->getLatestNowPageContent($this->request);
$this->Authorization->skipAuthorization();

$this->set(compact('content'));
}
}
20 changes: 20 additions & 0 deletions src/Services/ArticlesManagerService.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,24 @@ public function getHomePageContent(ServerRequest $request): ?string

return $article ? $article->body : null;
}

public function getLatestNowPageContent(ServerRequest $request): ?Article
{
/** @var \MeowBlog\Model\Table\ArticlesTable $articleTable */
$articleTable = $this->articles;

/** @var \Cake\ORM\Query $q */
$q = $articleTable->find('tagged', [
'tags' => 'now',
])->where([
'Blogs.Domain' => $request->getUri()->getHost(),
'Articles.article_type' => ArticleType::Article->value,
'Articles.published' => 1,
])->order(['Articles.created' => 'DESC']);

/** @var \MeowBlog\Model\Entity\Article $savedArticle */
$article = $q->contain(['Blogs', 'Tags'])->first();

return $article ? $article : null;
}
}
10 changes: 9 additions & 1 deletion src/Services/ArticlesManagerServiceInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,18 @@ public function saveToDatabase(Article $article, ServerRequest $request): Articl

/**
* Return content of the homepage articles
* Home-page articles are articles that have title matched with the blog domain
* Home-page articles are articles that have title matched with the blog domain type must be Page
*
* @param ServerRequest $request
* @return string|null
*/
public function getHomePageContent(ServerRequest $request): ?string;

/**
* Return content of latest article (type: Article) tagged with Now
*
* @param ServerRequest $request
* @return Article|null
*/
public function getLatestNowPageContent(ServerRequest $request): ?Article;
}
20 changes: 20 additions & 0 deletions templates/Articles/now.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php
/**
* @var \MeowBlog\View\AppView $this
* @var ?\MeowBlog\Model\Entity\Article $content
*/
?>
<div class="headings">
<h2><?= __('Now') ?></h2>
<h3><?= $content ? __('Last updated {0}', $content->created ) : '' ?></h3>
</div>
<div id="article">
<p><?= __('This is my {0} page, a summary of what im currently doing or enjoying.', $this->Html->link('/now', 'https://nownownow.com/about', ['target' => '_blank']))?></p>
<div>
<?php if (!$content) : ?>
<p><?= __('There is no update, come back later.') ?></p>
<?php else : ?>
<?= $this->Markdown->parse($content->body); ?>
<?php endif; ?>
</div>
</div>