Files
novaconium/controllers/savepage.php
T
2026-07-04 07:43:33 -07:00

117 lines
3.8 KiB
PHP

<?php
use Nickyeoman\Validation;
use Novaconium\Services\TagManager;
$v = new Nickyeoman\Validation\Validate();
$url_error = '/novaconium/page/edit/' . $ctx->post->get('id'); // fallback for errors
// -------------------------
// Check login
// -------------------------
if (empty($ctx->session->get('username'))) {
$ctx->messages->error('You are not logged in');
$ctx->redirect->url('/novaconium/login');
makeitso();
}
// -------------------------
// Check CSRF token
// -------------------------
if ($ctx->session->get('token') != $ctx->post->get('token')) {
$ctx->messages->error('Invalid Token');
$ctx->redirect->url('/novaconium/pages');
makeitso();
}
// -------------------------
// Gather POST data
// -------------------------
$id = $ctx->post->get('id');
$title = $ctx->post->get('title', '');
$heading = $ctx->post->get('heading', '');
$description = $ctx->post->get('description', '');
$keywords = $ctx->post->get('keywords', '');
$author = $ctx->post->get('author', '');
$slug = $ctx->post->get('slug', '');
$path = $ctx->post->get('path');
$intro = $ctx->post->get('intro', '');
$body = $ctx->post->get('body', '');
$notes = $ctx->post->get('notes', '');
$draft = !empty($ctx->post->get('draft')) ? 1 : 0;
$changefreq = $ctx->post->get('changefreq', 'monthly');
$priority = $ctx->post->get('priority', 0.0);
$tags_json = $ctx->post->get('tags_json', '[]');
// -------------------------
// Decode & sanitize tags
// -------------------------
$tags = json_decode($tags_json, true);
if (!is_array($tags)) $tags = [];
$tags = array_map('trim', $tags);
$tags = array_filter($tags, fn($t) => $t !== '');
$tags = array_unique($tags);
// -------------------------
// Validate required fields
// -------------------------
if (empty($title) || empty($slug) || empty($body)) {
$ctx->messages->error('Title, Slug, and Body are required.');
$ctx->redirect->url($url_error);
makeitso();
}
try {
$tagManager = new TagManager($ctx->db);
if ($id == 'newpage') {
// -------------------------
// Create new page
// -------------------------
$query = "INSERT INTO `pages`
(`title`, `heading`, `description`, `keywords`, `author`,
`slug`, `path`, `intro`, `body`, `notes`,
`draft`, `changefreq`, `priority`, `created`)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, NOW())";
$params = [
$title, $heading, $description, $keywords, $author,
$slug, $path, $intro, $body, $notes,
$draft, $changefreq, $priority
];
$ctx->db->query($query, $params);
$id = $ctx->db->lastid;
$ctx->messages->notice('Page Created');
} else {
// -------------------------
// Update existing page
// -------------------------
$query = "UPDATE `pages` SET
`title` = ?, `heading` = ?, `description` = ?, `keywords` = ?, `author` = ?,
`slug` = ?, `path` = ?, `intro` = ?, `body` = ?, `notes` = ?,
`draft` = ?, `changefreq` = ?, `priority` = ?, `updated` = NOW()
WHERE `id` = ?";
$params = [
$title, $heading, $description, $keywords, $author,
$slug, $path, $intro, $body, $notes,
$draft, $changefreq, $priority, $id
];
$ctx->db->query($query, $params);
$ctx->messages->notice('Page Updated');
}
// -------------------------
// Save tags (for both new and existing pages)
// -------------------------
$tagManager->setTagsForPage($id, $tags);
} catch (\Exception $e) {
$ctx->messages->error($e->getMessage());
$ctx->redirect->url($url_error);
makeitso();
}
// Redirect back to edit page
$ctx->redirect->url('/novaconium/page/edit/' . $id);