Made pages edit better and added messages

This commit is contained in:
2025-08-14 18:14:59 -07:00
parent 2f76c1ae35
commit 892110703b
16 changed files with 385 additions and 77 deletions

View File

@@ -3,53 +3,81 @@
use Nickyeoman\Validation;
$v = new Nickyeoman\Validation\Validate();
$url_success = '/dashboard';
$url_error = '/novaconium/page/edit/' . $post->get('id'); // Redirect back to the page edit form on error
$url_error = '/novaconium/page/edit/' . $post->get('id'); // fallback for errors
if ( empty($session->get('username'))) {
// Check login
if (empty($session->get('username'))) {
$messages->error('You are not logged in');
$redirect->url('/novaconium/login');
$messages->error('You are not loggedin');
makeitso();
}
// Check Token
// Check token
if ($session->get('token') != $post->get('token')) {
$redirect->url('/novaconium/pages');
$messages->error('Invalid Token');
$redirect->url('/novaconium/pages');
makeitso();
}
$id = $post->get('id');
$slug = $post->get('slug');
$title = $_POST['title'];
$body = $_POST['body']; // We want it dirty
$intro = $_POST['intro']; // We want it dirty
// Gather POST data
$id = $post->get('id');
$title = $_POST['title'] ?? '';
$heading = $_POST['heading'] ?? '';
$description = $_POST['description'] ?? '';
$keywords = $_POST['keywords'] ?? '';
$author = $_POST['author'] ?? '';
$slug = $_POST['slug'] ?? '';
$path = $_POST['path'] ?? null;
$intro = $_POST['intro'] ?? '';
$body = $_POST['body'] ?? '';
$notes = $_POST['notes'] ?? '';
$draft = !empty($post->get('draft')) ? 1 : 0;
$changefreq = $_POST['changefreq'] ?? 'monthly';
$priority = $_POST['priority'] ?? 0.0;
if ( empty( $post->get('draft') ) ) {
$draft = 0;
} else {
$draft = 1;
}
if ( empty($id) || empty($slug) || empty($body) ) {
$messages->error('One of the fields was empty.');
$redirect->url($url_fail);
// Validate required fields
if (empty($title) || empty($slug) || empty($body)) {
$messages->error('Title, Slug, and Body are required.');
$redirect->url($url_error);
makeitso();
}
try {
$query = "UPDATE `pages` SET `title` = ?, `slug` = ?, `body` = ?, `intro` = ?, `draft` = ?, `updated` = NOW() WHERE `id` = ?";
$params = [$title, $slug, $body, $intro, $draft, $id];
$db->query($query, $params);
$messages->notice('Page Saved');
if (!empty($id)) {
// 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
];
$db->query($query, $params);
$messages->notice('Page Updated');
} else {
// 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
];
$db->query($query, $params);
$id = $db->lastid; // Get new page ID
$messages->notice('Page Created');
}
} catch (Exception $e) {
$messages->notice($e->getMessage());
$messages->error($e->getMessage());
$redirect->url($url_error);
makeitso();
}
// Redirect to edit page
$redirect->url('/novaconium/page/edit/' . $id);