crispage/core/actions/Crispage/Actions/ArticleAction.php
2023-04-17 19:19:36 -04:00

79 lines
2.2 KiB
PHP

<?php
/*
Crispage CMS
crispycat <the@crispy.cat>
https://crispage.crispy.cat
https://github.com/crispy-cat/crispage
Crispage is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
*/
namespace Crispage\Actions;
defined("ROOT") or die();
use \Crispage\CrispageException;
use \Crispage\Auth\CorePermissions;
use \Crispage\Assets\Article;
class ArticleAction extends \Crispage\Response\Action {
public static function getExtensionInfo(): array {
return [
"id" => "crispage.actions.article"
];
}
public function __construct(\Crispage $app, array $data) {
parent::__construct($app, $data);
}
public function run(): void {
// Get article
$article = $this->app->assets->get($this->data["asset_id"] ?? 0);
// If article does not exist, error 404
if (!$article) {
$this->app->handleException(new CrispageException(
"Page Not Found", 404, null, false
));
return;
}
// If not published and user does not have unpublished perms, error 404
$show_unpublished = $this->app->auth->userHasPermission(
null, CorePermissions::VIEW_UNPUBLISHED
);
if ($article->state < Article::PUBLISHED && !$show_unpublished) {
$this->app->handleException(new CrispageException(
"Page Not Found", 404, null, false
));
return;
}
// Increment hit count
$article->hits++;
$this->app->assets->set($article);
// Set title and metas
$this->app->page->data["title"] = $article->title;
if (!empty($article->meta_desc))
$this->app->page->data["metas"]["description"]["content"] = $article->meta_desc;
if (!empty($article->meta_keys))
$this->app->page->data["metas"]["keywords"]["content"] = $article->meta_keys;
if (!empty($article->meta_robots))
$this->app->page->data["metas"]["robots"]["content"] = $article->meta_robots;
// Create and set component
$com = $this->app->page->createComponent(
"\\Crispage\\Components\\ArticleComponent",
array_merge($this->data, ["article" => $article])
);
$this->app->page->setMainComponent($com);
$this->app->page->actionFinished();
}
}
?>