Skip to content

Commit

Permalink
Merge pull request #42 from MayMeow/dev/add-now-pages
Browse files Browse the repository at this point in the history
Added now pages
  • Loading branch information
MayMeow authored May 9, 2023
2 parents 5e42327 + 448fdfd commit 7d559da
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 2 deletions.
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>

0 comments on commit 7d559da

Please sign in to comment.