From 892110703b2df91c21755963d5bc78f943ab06c8 Mon Sep 17 00:00:00 2001 From: Nick Yeoman Date: Thu, 14 Aug 2025 18:14:59 -0700 Subject: [PATCH] Made pages edit better and added messages --- config/routes.php | 15 ++++++ controllers/dashboard.php | 5 +- controllers/editpage.php | 87 +++++++++++++++++++++++--------- controllers/message_delete.php | 15 ++++++ controllers/message_edit.php | 19 +++++++ controllers/message_save.php | 57 +++++++++++++++++++++ controllers/messages.php | 21 ++++++++ controllers/pages.php | 6 ++- controllers/savepage.php | 90 ++++++++++++++++++++++------------ twig/left.html.twig | 1 + twig/master.html.twig | 2 +- views/dashboard.html.twig | 1 - views/editmessage.html.twig | 30 ++++++++++++ views/editpage.html.twig | 69 ++++++++++++++++++-------- views/messages.html.twig | 43 ++++++++++++++++ views/pages.html.twig | 1 + 16 files changed, 385 insertions(+), 77 deletions(-) create mode 100644 controllers/message_delete.php create mode 100644 controllers/message_edit.php create mode 100644 controllers/message_save.php create mode 100644 controllers/messages.php create mode 100644 views/editmessage.html.twig create mode 100644 views/messages.html.twig diff --git a/config/routes.php b/config/routes.php index edf20bd..43b8428 100644 --- a/config/routes.php +++ b/config/routes.php @@ -19,9 +19,24 @@ $framework_routes = [ '/novaconium/page/edit/{id}' => [ 'get' => 'NOVACONIUM/editpage' ], + '/novaconium/page/create' => [ + 'get' => 'NOVACONIUM/editpage' + ], '/novaconium/savePage' => [ 'post' => 'NOVACONIUM/savepage' ], + '/novaconium/messages' => [ + 'get' => 'NOVACONIUM/messages' + ], + '/novaconium/messages/delete/{id}' => [ + 'get' => 'NOVACONIUM/message_delete' + ], + '/novaconium/messages/edit/{id}' => [ + 'get' => 'NOVACONIUM/message_edit' + ], + '/novaconium/message_save' => [ + 'post' => 'NOVACONIUM/message_save' + ], '/novaconium/logout' => [ 'post' => 'NOVACONIUM/logout', 'get' => 'NOVACONIUM/logout' diff --git a/controllers/dashboard.php b/controllers/dashboard.php index affb905..8c667b2 100644 --- a/controllers/dashboard.php +++ b/controllers/dashboard.php @@ -1,5 +1,8 @@ 'Novaconium Dashboard Page', + 'pageclass' => 'novaconium' +]); if ( empty($session->get('username'))) { $redirect->url('/novaconium/login'); diff --git a/controllers/editpage.php b/controllers/editpage.php index b653a6e..29bf262 100644 --- a/controllers/editpage.php +++ b/controllers/editpage.php @@ -1,30 +1,73 @@ 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=<<parameters['id'] ?? null; + +if (!empty($pageid)) { + // Existing page: fetch from database + $query = <<getRow($query); -$data = array_merge($data, [ - 'tinymce' => true, - 'pageid' => 'admin-edit-page' -]); -view('@novacore/editpage', $data); \ No newline at end of file + $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); diff --git a/controllers/message_delete.php b/controllers/message_delete.php new file mode 100644 index 0000000..c3143f9 --- /dev/null +++ b/controllers/message_delete.php @@ -0,0 +1,15 @@ +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(); \ No newline at end of file diff --git a/controllers/message_edit.php b/controllers/message_edit.php new file mode 100644 index 0000000..015f8fe --- /dev/null +++ b/controllers/message_edit.php @@ -0,0 +1,19 @@ + '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); \ No newline at end of file diff --git a/controllers/message_save.php b/controllers/message_save.php new file mode 100644 index 0000000..e037063 --- /dev/null +++ b/controllers/message_save.php @@ -0,0 +1,57 @@ +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); diff --git a/controllers/messages.php b/controllers/messages.php new file mode 100644 index 0000000..b2c9e7d --- /dev/null +++ b/controllers/messages.php @@ -0,0 +1,21 @@ + '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); \ No newline at end of file diff --git a/controllers/pages.php b/controllers/pages.php index 47fcb85..0462fe7 100644 --- a/controllers/pages.php +++ b/controllers/pages.php @@ -1,5 +1,9 @@ 'Novaconium Pages', + 'pageclass' => 'novaconium' +]); if ( empty($session->get('username'))) { $redirect->url('/novaconium/login'); diff --git a/controllers/savepage.php b/controllers/savepage.php index 9e279e1..2a32921 100644 --- a/controllers/savepage.php +++ b/controllers/savepage.php @@ -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); diff --git a/twig/left.html.twig b/twig/left.html.twig index 4c2bc14..9b8ae2b 100644 --- a/twig/left.html.twig +++ b/twig/left.html.twig @@ -4,6 +4,7 @@
  • Home
  • Dashboard
  • Pages
  • +
  • Messages
  • Logout
  • diff --git a/twig/master.html.twig b/twig/master.html.twig index da9e08c..cd4a60c 100644 --- a/twig/master.html.twig +++ b/twig/master.html.twig @@ -7,7 +7,7 @@ {% include ['@override/head.html.twig', '@novaconium/head.html.twig'] %} - + {# Page Header #}
    diff --git a/views/dashboard.html.twig b/views/dashboard.html.twig index 89b2f2d..988a615 100644 --- a/views/dashboard.html.twig +++ b/views/dashboard.html.twig @@ -5,5 +5,4 @@

    Dashboard page

    Homepage

    logout

    - {% endblock %} diff --git a/views/editmessage.html.twig b/views/editmessage.html.twig new file mode 100644 index 0000000..208978f --- /dev/null +++ b/views/editmessage.html.twig @@ -0,0 +1,30 @@ +{% extends '@novaconium/master.html.twig' %} + +{% block content %} +

    Edit Message - {{ title }}

    + +

    Delete

    + +
    + + + + + + + + + + + + + + +

    Created: {{ themessage.created|date("Y-m-d H:i:s") }}

    + + +
    +{% endblock %} diff --git a/views/editpage.html.twig b/views/editpage.html.twig index f3e0e83..499c339 100644 --- a/views/editpage.html.twig +++ b/views/editpage.html.twig @@ -1,32 +1,61 @@ {% extends '@novaconium/master.html.twig' %} {% block content %} -

    Edit Page - {{ title }}

    +

    Edit Page - {{ title }}

    -
    - - + + + - - + + - - + + - - + + - - + + - + + -

    Created: {{ rows.created|date("Y-m-d H:i:s") }}

    -

    Last Updated: {{ rows.updated|date("Y-m-d H:i:s") }}

    + + - -
    + + + + + + + + + + + + + + + + + + + + +

    Created: {{ rows.created|date("Y-m-d H:i:s") }}

    +

    Last Updated: {{ rows.updated|date("Y-m-d H:i:s") }}

    + + + {% endblock %} diff --git a/views/messages.html.twig b/views/messages.html.twig new file mode 100644 index 0000000..eb06b7a --- /dev/null +++ b/views/messages.html.twig @@ -0,0 +1,43 @@ +{% extends '@novaconium/master.html.twig' %} + +{% block content %} +

    {{title}}

    + + + + + + + + + + + + + {% for msg in messages %} + + + + + + + + + {% else %} + + + + {% endfor %} + +
    EmailNameMessage PreviewCreatedStatusActions
    {{ msg.email }}{{ msg.name }}{{ msg.message }}{{ msg.created|date('Y-m-d H:i') }} + {% if msg.unread %} + Unread + {% else %} + Read + {% endif %} + + Edit + Delete +
    No messages found
    + +{% endblock %} diff --git a/views/pages.html.twig b/views/pages.html.twig index 90d094b..d0d1b76 100644 --- a/views/pages.html.twig +++ b/views/pages.html.twig @@ -2,6 +2,7 @@ {% block content %}

    {{title}}

    +

    Create Page