Files
coolcoth.com/app/Controllers/Admin/NewsController.php
T
2026-08-08 16:26:31 +08:00

80 lines
2.7 KiB
PHP

<?php
namespace App\Controllers\Admin;
use App\Models\News;
class NewsController extends AdminController
{
public function index()
{
return $this->view('admin/news', [
'news' => (new News())->all(),
'seg' => $this->seg(),
]);
}
public function create()
{
return $this->view('admin/news_form', ['n' => null]);
}
public function store()
{
if (!csrf_check()) { $this->redirect('admin/news'); }
$cover = $this->uploadFile('cover') ?? $this->post('cover_url', '');
$news = new News();
$id = $news->insert([
'title' => $this->post('title'),
'slug' => '',
'cover' => $cover,
'summary' => $this->post('summary'),
'content' => $this->post('content'),
'author' => $this->post('author', '酷冰甲'),
'published_at' => $this->post('published_at', date('Y-m-d')),
'status' => $this->post('status', 1) ? 1 : 0,
'views' => 0,
'layout' => $this->post('layout', ''),
]);
// URL 标识留空时按记录序号顺序生成(短、稳定)
$slug = $this->post('slug') ? slugify($this->post('slug')) : (string)$id;
$news->update($id, ['slug' => $slug]);
$this->redirect('admin/news');
}
public function edit($id)
{
$n = (new News())->find($id);
if (!$n) { $this->redirect('admin/news'); }
return $this->view('admin/news_form', ['n' => $n]);
}
public function update($id)
{
if (!csrf_check()) { $this->redirect('admin/news'); }
$news = new News();
$n = $news->find($id);
if (!$n) { $this->redirect('admin/news'); }
$cover = $this->uploadFile('cover');
if (!$cover && $this->post('cover_url')) $cover = $this->post('cover_url');
if (!$cover) $cover = $n['cover'] ?? '';
$news->update($id, [
'title' => $this->post('title'),
'slug' => $this->post('slug') ? slugify($this->post('slug')) : (string)$id,
'cover' => $cover,
'summary' => $this->post('summary'),
'content' => $this->post('content'),
'author' => $this->post('author', '酷冰甲'),
'published_at' => $this->post('published_at', date('Y-m-d')),
'status' => $this->post('status', 1) ? 1 : 0,
'layout' => $this->post('layout', ''),
]);
$this->redirect('admin/news');
}
public function delete($id)
{
if (csrf_check()) { (new News())->delete($id); }
$this->redirect('admin/news');
}
}