Edit page working

This commit is contained in:
Nick Yeoman 2025-08-12 23:08:20 -07:00
parent 4aebef12c8
commit 2f76c1ae35
15 changed files with 272 additions and 7 deletions

View File

@ -13,6 +13,15 @@ $framework_routes = [
'/novaconium/dashboard' => [
'get' => 'NOVACONIUM/dashboard'
],
'/novaconium/pages' => [
'get' => 'NOVACONIUM/pages'
],
'/novaconium/page/edit/{id}' => [
'get' => 'NOVACONIUM/editpage'
],
'/novaconium/savePage' => [
'post' => 'NOVACONIUM/savepage'
],
'/novaconium/logout' => [
'post' => 'NOVACONIUM/logout',
'get' => 'NOVACONIUM/logout'

30
controllers/editpage.php Normal file
View File

@ -0,0 +1,30 @@
<?php
$data['title'] = 'Novaconium Edit Page';
if ( empty($session->get('username'))) {
$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'
EOSQL;
$data['rows'] = $db->getRow($query);
$data = array_merge($data, [
'tinymce' => true,
'pageid' => 'admin-edit-page'
]);
view('@novacore/editpage', $data);

View File

@ -88,7 +88,7 @@ $result = $db->query($query);
if ($result->num_rows === 0) {
$query = <<<EOSQL
CREATE TABLE `pages` (
`id` int(11) NOT NULL,
`id` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(255) NOT NULL,
`heading` varchar(255) NOT NULL,
`description` varchar(255) NOT NULL,

16
controllers/pages.php Normal file
View File

@ -0,0 +1,16 @@
<?php
$data['title'] = 'Novaconium Pages';
if ( empty($session->get('username'))) {
$redirect->url('/novaconium/login');
$messages->error('You are not loggedin');
makeitso();
}
// Get the pages
$query = "SELECT id, title, created, updated, draft FROM pages";
$matched = $db->getRows($query);
$data['pages'] = $matched;
view('@novacore/pages', $data);

55
controllers/savepage.php Normal file
View File

@ -0,0 +1,55 @@
<?php
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
if ( empty($session->get('username'))) {
$redirect->url('/novaconium/login');
$messages->error('You are not loggedin');
makeitso();
}
// Check Token
if ($session->get('token') != $post->get('token')) {
$redirect->url('/novaconium/pages');
$messages->error('Invalid Token');
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
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);
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');
} catch (Exception $e) {
$messages->notice($e->getMessage());
}
$redirect->url('/novaconium/page/edit/' . $id);

7
docs/StyleSheets-sass.md Normal file
View File

@ -0,0 +1,7 @@
# Style Sheets
The idea is to use sass to generate only what you need for style sheets.
```bash
sudo docker run --rm -v $(pwd):/usr/src/app sass-container sass sass/project.sass public/css/main.css
```

View File

@ -11,8 +11,8 @@
<h2>Documentation</h2>
<ul>
<li>Style Sheets</li>
<li><a href="https://git.4lt.ca/4lt/novaconium/src/branch/master/docs/twig-overrides.md">Twig overrides</a></li>
<li><a href="https://git.4lt.ca/4lt/novaconium/src/branch/master/docs/StyleSheets-sass.md">Style Sheets</a></li>
<li><a href="https://git.4lt.ca/4lt/novaconium/src/branch/master/docs/Twig-Views.md">Twig overrides</a></li>
</ul>
<h2>Repository</h2>

View File

@ -4,14 +4,55 @@ body {
font-family: 'Fira Code', 'Source Code Pro', monospace;
}
#page .container {
display: flex;
padding: 0;
justify-content: center;
align-items: flex-start;
margin-top: 20px;
}
article {
max-width: 900px;
width: 900px;
flex-shrink: 0;
}
#leftnav {
width: 320px;
flex-shrink: 0;
margin: 0;
margin-right: 50px;
}
article, #leftnav {
border: 1px solid #3b444c;
background-color: #14171a;
color: #fff;
margin: 0 auto;
padding: 20px;
margin-top: 50px;
}
ul#leftnav {
list-style: none;
padding: 0;
}
#leftnav li {
border-bottom: 1px solid #3b444c;
}
#leftnav a {
display: block;
padding: 10px 15px;
text-decoration: none;
color: #fff;
}
#leftnav a:hover {
background: #333;
}
#leftnav li:last-child {
border-bottom: none;
}
code {
@ -41,7 +82,33 @@ div.error, div#debug {
width: 900px;
}
div.notice {
border: 1px solid rgb(31, 119, 13);
padding: 30px;
background-color: rgb(169, 218, 163);
color: rgb(20, 56, 13);
margin: 0 auto;
width: 900px;
}
div#debug {
margin-top: 100px;
margin-bottom: 100px;
}
.pages-table {
width: 100%;
border-collapse: collapse;
border: 1px solid #333;
}
.pages-table th,
.pages-table td {
padding: 10px;
border: 1px solid #ddd;
text-align: left;
}
.pages-table th {
font-weight: bold;
}

View File

@ -30,6 +30,10 @@ class MessageHandler {
$this->addMessage('error', $message);
}
public function notice($message){
$this->addMessage('notice', $message);
}
// Get all messages of a specific type
public function getMessages($type) {
return $this->messages[$type] ?? [];

View File

@ -25,6 +25,7 @@ require_once(FRAMEWORKPATH . '/src/twig.php');
require_once(FRAMEWORKPATH . '/src/Session.php');
$session = new Session();
$data['token'] = $session->get('token');
$data['username'] = $session->get('username');
if ($config['loglevel'] == 'DEBUG') {
$data['debug'] = nl2br(print_r($session->debug(), true));
}

10
twig/left.html.twig Normal file
View File

@ -0,0 +1,10 @@
{% if username is not empty %}
<div class="left">
<ul id="leftnav">
<li><a href="/">Home</a></li>
<li><a href="/novaconium/dashboard">Dashboard</a></li>
<li><a href="/novaconium/pages">Pages</a></li>
<li><a href="/novaconium/logout">Logout</a></li>
</ul>
</div>
{% endif %}

View File

@ -20,6 +20,8 @@
<div id="page">
<div class="container">
{% include ['@override/left.html.twig','@novaconium/left.html.twig'] %}
<div class="middle">
{% if error|default is not empty %}
{% for key, val in error %}
@ -36,6 +38,8 @@
<article>
{% block content %}{% endblock %}
</article>
{% include ['@override/right.html.twig','@novaconium/right.html.twig'] %}
</div>
</div>

1
twig/right.html.twig Normal file
View File

@ -0,0 +1 @@
<!-- Right Col -->

32
views/editpage.html.twig Normal file
View File

@ -0,0 +1,32 @@
{% extends '@novaconium/master.html.twig' %}
{% block content %}
<h2>Edit Page - {{ title }}</h2>
<form method="post" action="/novaconium/savePage">
<input type="hidden" name="id" value="{{ rows.id }}">
<input type="hidden" name="token" value="{{ token }}">
<label for="title">Title:</label>
<input type="text" id="title" name="title" value="{{ rows.title }}" required>
<label for="slug">Slug: (<a href="/page/{{ rows.slug }}" target="_new">/page/{{ rows.slug }}</a>)</label>
<input type="text" id="slug" name="slug" value="{{ rows.slug }}" required>
<label for="body">Body:</label>
<textarea id="body" name="body" rows="10" required>{{ rows.body }}</textarea>
<label for="intro">Intro:</label>
<textarea id="intro" name="intro" rows="10" required>{{ rows.intro }}</textarea>
<label for="draft">
<input type="checkbox" id="draft" name="draft" value="1" {% if rows.draft %}checked{% endif %}>
Save as Draft
</label>
<p><strong>Created:</strong> {{ rows.created|date("Y-m-d H:i:s") }}</p>
<p><strong>Last Updated:</strong> {{ rows.updated|date("Y-m-d H:i:s") }}</p>
<button type="submit">Save Changes</button>
</form>
{% endblock %}

29
views/pages.html.twig Normal file
View File

@ -0,0 +1,29 @@
{% extends '@novaconium/master.html.twig' %}
{% block content %}
<h1>{{title}}</h1>
<table class="pages-table">
<thead>
<tr>
<th>Title</th>
<th>Created</th>
<th>Updated</th>
<th>Draft</th>
</tr>
</thead>
<tbody>
{% for page in pages %}
<tr>
<td><a href="/novaconium/page/edit/{{ page.id }}">{{ page.title }}</a></td>
<td>{{ page.created }}</td>
<td>{{ page.updated|default('Not updated') }}</td>
<td>{{ page.draft ? 'Draft' : 'Published' }}</td>
</tr>
{% else %}
<tr>
<td colspan="6">No pages found.</td>
</tr>
{% endfor %}
</tbody>
</table>
{% endblock %}