Made pages edit better and added messages
This commit is contained in:
@@ -1,5 +1,8 @@
|
||||
<?php
|
||||
$data['title'] = 'Novaconium Dashboard Page';
|
||||
$data = array_merge($data, [
|
||||
'title' => 'Novaconium Dashboard Page',
|
||||
'pageclass' => 'novaconium'
|
||||
]);
|
||||
|
||||
if ( empty($session->get('username'))) {
|
||||
$redirect->url('/novaconium/login');
|
||||
|
||||
@@ -1,30 +1,73 @@
|
||||
<?php
|
||||
$data['title'] = 'Novaconium Edit Page';
|
||||
|
||||
if ( empty($session->get('username'))) {
|
||||
$data = array_merge($data, [
|
||||
'title' => 'Novaconium Edit Page',
|
||||
'pageclass' => 'novaconium'
|
||||
]);
|
||||
|
||||
// Check if logged in
|
||||
if (empty($session->get('username'))) {
|
||||
$messages->error('You are not logged in');
|
||||
$redirect->url('/novaconium/login');
|
||||
$messages->error('You are not loggedin');
|
||||
makeitso();
|
||||
}
|
||||
|
||||
$pageid = $router->parameters['id'];
|
||||
$query=<<<EOSQL
|
||||
SELECT
|
||||
id,
|
||||
title,
|
||||
intro,
|
||||
slug,
|
||||
body,
|
||||
draft,
|
||||
created,
|
||||
updated
|
||||
FROM pages
|
||||
WHERE id = '$pageid'
|
||||
// Get page ID from router parameters
|
||||
$pageid = $router->parameters['id'] ?? null;
|
||||
|
||||
if (!empty($pageid)) {
|
||||
// Existing page: fetch from database
|
||||
$query = <<<EOSQL
|
||||
SELECT
|
||||
id,
|
||||
title,
|
||||
heading,
|
||||
description,
|
||||
keywords,
|
||||
author,
|
||||
slug,
|
||||
path,
|
||||
intro,
|
||||
body,
|
||||
notes,
|
||||
draft,
|
||||
changefreq,
|
||||
priority,
|
||||
created,
|
||||
updated
|
||||
FROM pages
|
||||
WHERE id = ?
|
||||
EOSQL;
|
||||
|
||||
$data['rows'] = $db->getRow($query);
|
||||
$data = array_merge($data, [
|
||||
'tinymce' => true,
|
||||
'pageid' => 'admin-edit-page'
|
||||
]);
|
||||
view('@novacore/editpage', $data);
|
||||
$data['rows'] = $db->getRow($query, [$pageid]);
|
||||
|
||||
// If no row is found, treat as new page
|
||||
if (!$data['rows']) {
|
||||
$pageid = null;
|
||||
}
|
||||
}
|
||||
|
||||
if (empty($pageid)) {
|
||||
// New page: set default values for all fields
|
||||
$data['rows'] = [
|
||||
'id' => '',
|
||||
'title' => '',
|
||||
'heading' => '',
|
||||
'description' => '',
|
||||
'keywords' => '',
|
||||
'author' => $session->get('username') ?? '',
|
||||
'slug' => '',
|
||||
'path' => '',
|
||||
'intro' => '',
|
||||
'body' => '',
|
||||
'notes' => '',
|
||||
'draft' => 0,
|
||||
'changefreq' => 'monthly',
|
||||
'priority' => 0.0,
|
||||
'created' => date('Y-m-d H:i:s'),
|
||||
'updated' => date('Y-m-d H:i:s')
|
||||
];
|
||||
}
|
||||
|
||||
// Render the edit page view
|
||||
view('@novacore/editpage', $data);
|
||||
|
||||
15
controllers/message_delete.php
Normal file
15
controllers/message_delete.php
Normal file
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
if ( empty($session->get('username'))) {
|
||||
$redirect->url('/novaconium/login');
|
||||
$messages->error('You are not loggedin');
|
||||
makeitso();
|
||||
}
|
||||
|
||||
$messageid = $router->parameters['id'];
|
||||
$query="DELETE FROM contactForm WHERE `contactForm`.`id` = ?";
|
||||
$db->query($query, [$messageid]);
|
||||
|
||||
$redirect->url('/novaconium/messages');
|
||||
$messages->notice("Removed Message $messageid");
|
||||
makeitso();
|
||||
19
controllers/message_edit.php
Normal file
19
controllers/message_edit.php
Normal file
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
$data = array_merge($data, [
|
||||
'title' => 'Novaconium Message Page',
|
||||
'pageclass' => 'novaconium'
|
||||
]);
|
||||
|
||||
if ( empty($session->get('username'))) {
|
||||
$redirect->url('/novaconium/login');
|
||||
$messages->error('You are not loggedin');
|
||||
makeitso();
|
||||
}
|
||||
|
||||
$messageid = $router->parameters['id'];
|
||||
$query = "SELECT id, name, email, message, created, unread FROM contactForm WHERE id = '$messageid'";
|
||||
|
||||
$data['themessage'] = $db->getRow($query);
|
||||
|
||||
view('@novacore/editmessage', $data);
|
||||
57
controllers/message_save.php
Normal file
57
controllers/message_save.php
Normal file
@@ -0,0 +1,57 @@
|
||||
<?php
|
||||
|
||||
use Nickyeoman\Validation;
|
||||
|
||||
$v = new Nickyeoman\Validation\Validate();
|
||||
|
||||
$url_success = '/novaconium/messages';
|
||||
$url_error = '/novaconium/messages/edit/' . $post->get('id'); // Redirect back to the message edit form on error
|
||||
|
||||
// Check if logged in
|
||||
if (empty($session->get('username'))) {
|
||||
$messages->error('You are not logged in');
|
||||
$redirect->url('/novaconium/login');
|
||||
makeitso();
|
||||
}
|
||||
|
||||
// Check CSRF token
|
||||
if ($session->get('token') != $post->get('token')) {
|
||||
$messages->error('Invalid token');
|
||||
$redirect->url($url_success);
|
||||
makeitso();
|
||||
}
|
||||
|
||||
// Get POST data
|
||||
$id = $post->get('id');
|
||||
$name = $post->get('name');
|
||||
$email = $post->get('email');
|
||||
$message = $post->get('message');
|
||||
$unread = !empty($post->get('unread')) ? 1 : 0;
|
||||
|
||||
// Validate required fields
|
||||
if (empty($id) || empty($message) || empty($email)) {
|
||||
$messages->error('One of the required fields was empty.');
|
||||
$redirect->url($url_error);
|
||||
makeitso();
|
||||
}
|
||||
|
||||
try {
|
||||
// Prepare update query
|
||||
$query = "UPDATE `contactForm`
|
||||
SET `name` = ?, `email` = ?, `message` = ?, `unread` = ?
|
||||
WHERE `id` = ?";
|
||||
|
||||
$params = [$name, $email, $message, $unread, $id];
|
||||
|
||||
$db->query($query, $params);
|
||||
|
||||
$messages->notice('Message updated successfully');
|
||||
|
||||
} catch (Exception $e) {
|
||||
$messages->error('Error updating message: ' . $e->getMessage());
|
||||
$redirect->url($url_error);
|
||||
makeitso();
|
||||
}
|
||||
|
||||
// Redirect to success page
|
||||
$redirect->url($url_success);
|
||||
21
controllers/messages.php
Normal file
21
controllers/messages.php
Normal file
@@ -0,0 +1,21 @@
|
||||
<?php
|
||||
|
||||
$data = array_merge($data, [
|
||||
'title' => 'Novaconium Messages',
|
||||
'pageclass' => 'novaconium'
|
||||
]);
|
||||
|
||||
if ( empty($session->get('username'))) {
|
||||
$redirect->url('/novaconium/login');
|
||||
$messages->error('You are not loggedin');
|
||||
makeitso();
|
||||
}
|
||||
|
||||
// Get the pages
|
||||
$query = "SELECT id, name, email, LEFT(message, 40) AS message, created, unread FROM contactForm";
|
||||
|
||||
$matched = $db->getRows($query);
|
||||
|
||||
$data['messages'] = $matched;
|
||||
|
||||
view('@novacore/messages', $data);
|
||||
@@ -1,5 +1,9 @@
|
||||
<?php
|
||||
$data['title'] = 'Novaconium Pages';
|
||||
|
||||
$data = array_merge($data, [
|
||||
'title' => 'Novaconium Pages',
|
||||
'pageclass' => 'novaconium'
|
||||
]);
|
||||
|
||||
if ( empty($session->get('username'))) {
|
||||
$redirect->url('/novaconium/login');
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user