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

58 lines
1.5 KiB
PHP

<?php
use Nickyeoman\Validation;
$v = new Nickyeoman\Validation\Validate();
$url_success = '/novaconium/messages';
$url_error = '/novaconium/messages/edit/' . $ctx->post->get('id'); // Redirect back to the message edit form on error
// Check if logged in
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($url_success);
makeitso();
}
// Get POST data
$id = $ctx->post->get('id');
$name = $ctx->post->get('name');
$email = $ctx->post->get('email');
$message = $ctx->post->get('message');
$unread = !empty($ctx->post->get('unread')) ? 1 : 0;
// Validate required fields
if (empty($id) || empty($message) || empty($email)) {
$ctx->messages->error('One of the required fields was empty.');
$ctx->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];
$ctx->db->query($query, $params);
$ctx->messages->notice('Message updated successfully');
} catch (\Exception $e) {
$ctx->messages->error('Error updating message: ' . $e->getMessage());
$ctx->redirect->url($url_error);
makeitso();
}
// Redirect to success page
$ctx->redirect->url($url_success);