tags working
This commit is contained in:
@@ -1,26 +1,33 @@
|
||||
<?php
|
||||
|
||||
use Nickyeoman\Validation;
|
||||
use Novaconium\Services\TagManager;
|
||||
|
||||
$v = new Nickyeoman\Validation\Validate();
|
||||
use Novaconium\Services\TagManager; // Autoloads automatically
|
||||
|
||||
$url_error = '/novaconium/page/edit/' . $post->get('id'); // fallback for errors
|
||||
|
||||
// -------------------------
|
||||
// Check login
|
||||
// -------------------------
|
||||
if (empty($session->get('username'))) {
|
||||
$messages->error('You are not logged in');
|
||||
$redirect->url('/novaconium/login');
|
||||
makeitso();
|
||||
}
|
||||
|
||||
// Check token
|
||||
// -------------------------
|
||||
// Check CSRF token
|
||||
// -------------------------
|
||||
if ($session->get('token') != $post->get('token')) {
|
||||
$messages->error('Invalid Token');
|
||||
$redirect->url('/novaconium/pages');
|
||||
makeitso();
|
||||
}
|
||||
|
||||
// -------------------------
|
||||
// Gather POST data
|
||||
// -------------------------
|
||||
$id = $post->get('id');
|
||||
$title = $_POST['title'] ?? '';
|
||||
$heading = $_POST['heading'] ?? '';
|
||||
@@ -35,9 +42,20 @@ $notes = $_POST['notes'] ?? '';
|
||||
$draft = !empty($post->get('draft')) ? 1 : 0;
|
||||
$changefreq = $_POST['changefreq'] ?? 'monthly';
|
||||
$priority = $_POST['priority'] ?? 0.0;
|
||||
$tags_json = $_POST['tags_json'] ?? '[]';
|
||||
$tags_json = $_POST['tags_json'] ?? '[]';
|
||||
|
||||
// -------------------------
|
||||
// Decode & sanitize tags
|
||||
// -------------------------
|
||||
$tags = json_decode($tags_json, true);
|
||||
if (!is_array($tags)) $tags = [];
|
||||
$tags = array_map('trim', $tags);
|
||||
$tags = array_filter($tags, fn($t) => $t !== '');
|
||||
$tags = array_unique($tags);
|
||||
|
||||
// -------------------------
|
||||
// Validate required fields
|
||||
// -------------------------
|
||||
if (empty($title) || empty($slug) || empty($body)) {
|
||||
$messages->error('Title, Slug, and Body are required.');
|
||||
$redirect->url($url_error);
|
||||
@@ -45,8 +63,12 @@ if (empty($title) || empty($slug) || empty($body)) {
|
||||
}
|
||||
|
||||
try {
|
||||
$tagManager = new TagManager();
|
||||
|
||||
if ($id == 'newpage') {
|
||||
// -------------------------
|
||||
// Create new page
|
||||
// -------------------------
|
||||
$query = "INSERT INTO `pages`
|
||||
(`title`, `heading`, `description`, `keywords`, `author`,
|
||||
`slug`, `path`, `intro`, `body`, `notes`,
|
||||
@@ -58,28 +80,13 @@ try {
|
||||
$draft, $changefreq, $priority
|
||||
];
|
||||
$db->query($query, $params);
|
||||
$id = $db->lastid; // Get new page ID
|
||||
$id = $db->lastid;
|
||||
$messages->notice('Page Created');
|
||||
$newpage = true;
|
||||
|
||||
} else {
|
||||
$newpage = false;
|
||||
}
|
||||
|
||||
if (!empty($id) && !$newpage) {
|
||||
|
||||
/** Work in Progress
|
||||
// Delete old tag links for this page (cleanup)
|
||||
$deleteQuery = <<<EOSQL
|
||||
DELETE FROM page_tags WHERE page_id = ?
|
||||
EOSQL;
|
||||
$db->query($deleteQuery, [$id]);
|
||||
|
||||
$tagManager = new TagManager();
|
||||
|
||||
dd($tags_json);
|
||||
**/
|
||||
|
||||
// -------------------------
|
||||
// Update existing page
|
||||
// -------------------------
|
||||
$query = "UPDATE `pages` SET
|
||||
`title` = ?, `heading` = ?, `description` = ?, `keywords` = ?, `author` = ?,
|
||||
`slug` = ?, `path` = ?, `intro` = ?, `body` = ?, `notes` = ?,
|
||||
@@ -92,12 +99,18 @@ try {
|
||||
];
|
||||
$db->query($query, $params);
|
||||
$messages->notice('Page Updated');
|
||||
}
|
||||
}
|
||||
|
||||
// -------------------------
|
||||
// Save tags (for both new and existing pages)
|
||||
// -------------------------
|
||||
$tagManager->setTagsForPage($id, $tags);
|
||||
|
||||
} catch (Exception $e) {
|
||||
$messages->error($e->getMessage());
|
||||
$redirect->url($url_error);
|
||||
makeitso();
|
||||
}
|
||||
|
||||
// Redirect to edit page
|
||||
// Redirect back to edit page
|
||||
$redirect->url('/novaconium/page/edit/' . $id);
|
||||
|
||||
Reference in New Issue
Block a user