tags working

This commit is contained in:
Nick Yeoman 2025-12-06 01:09:39 -08:00
parent 208534b5fb
commit 08b8009dec
3 changed files with 101 additions and 31 deletions

View File

@ -1,26 +1,33 @@
<?php <?php
use Nickyeoman\Validation; use Nickyeoman\Validation;
use Novaconium\Services\TagManager;
$v = new Nickyeoman\Validation\Validate(); $v = new Nickyeoman\Validation\Validate();
use Novaconium\Services\TagManager; // Autoloads automatically
$url_error = '/novaconium/page/edit/' . $post->get('id'); // fallback for errors $url_error = '/novaconium/page/edit/' . $post->get('id'); // fallback for errors
// -------------------------
// Check login // Check login
// -------------------------
if (empty($session->get('username'))) { if (empty($session->get('username'))) {
$messages->error('You are not logged in'); $messages->error('You are not logged in');
$redirect->url('/novaconium/login'); $redirect->url('/novaconium/login');
makeitso(); makeitso();
} }
// Check token // -------------------------
// Check CSRF token
// -------------------------
if ($session->get('token') != $post->get('token')) { if ($session->get('token') != $post->get('token')) {
$messages->error('Invalid Token'); $messages->error('Invalid Token');
$redirect->url('/novaconium/pages'); $redirect->url('/novaconium/pages');
makeitso(); makeitso();
} }
// -------------------------
// Gather POST data // Gather POST data
// -------------------------
$id = $post->get('id'); $id = $post->get('id');
$title = $_POST['title'] ?? ''; $title = $_POST['title'] ?? '';
$heading = $_POST['heading'] ?? ''; $heading = $_POST['heading'] ?? '';
@ -35,9 +42,20 @@ $notes = $_POST['notes'] ?? '';
$draft = !empty($post->get('draft')) ? 1 : 0; $draft = !empty($post->get('draft')) ? 1 : 0;
$changefreq = $_POST['changefreq'] ?? 'monthly'; $changefreq = $_POST['changefreq'] ?? 'monthly';
$priority = $_POST['priority'] ?? 0.0; $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 // Validate required fields
// -------------------------
if (empty($title) || empty($slug) || empty($body)) { if (empty($title) || empty($slug) || empty($body)) {
$messages->error('Title, Slug, and Body are required.'); $messages->error('Title, Slug, and Body are required.');
$redirect->url($url_error); $redirect->url($url_error);
@ -45,8 +63,12 @@ if (empty($title) || empty($slug) || empty($body)) {
} }
try { try {
$tagManager = new TagManager();
if ($id == 'newpage') { if ($id == 'newpage') {
// -------------------------
// Create new page // Create new page
// -------------------------
$query = "INSERT INTO `pages` $query = "INSERT INTO `pages`
(`title`, `heading`, `description`, `keywords`, `author`, (`title`, `heading`, `description`, `keywords`, `author`,
`slug`, `path`, `intro`, `body`, `notes`, `slug`, `path`, `intro`, `body`, `notes`,
@ -58,28 +80,13 @@ try {
$draft, $changefreq, $priority $draft, $changefreq, $priority
]; ];
$db->query($query, $params); $db->query($query, $params);
$id = $db->lastid; // Get new page ID $id = $db->lastid;
$messages->notice('Page Created'); $messages->notice('Page Created');
$newpage = true;
} else { } 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 // Update existing page
// -------------------------
$query = "UPDATE `pages` SET $query = "UPDATE `pages` SET
`title` = ?, `heading` = ?, `description` = ?, `keywords` = ?, `author` = ?, `title` = ?, `heading` = ?, `description` = ?, `keywords` = ?, `author` = ?,
`slug` = ?, `path` = ?, `intro` = ?, `body` = ?, `notes` = ?, `slug` = ?, `path` = ?, `intro` = ?, `body` = ?, `notes` = ?,
@ -92,12 +99,18 @@ try {
]; ];
$db->query($query, $params); $db->query($query, $params);
$messages->notice('Page Updated'); $messages->notice('Page Updated');
} }
// -------------------------
// Save tags (for both new and existing pages)
// -------------------------
$tagManager->setTagsForPage($id, $tags);
} catch (Exception $e) { } catch (Exception $e) {
$messages->error($e->getMessage()); $messages->error($e->getMessage());
$redirect->url($url_error); $redirect->url($url_error);
makeitso(); makeitso();
} }
// Redirect to edit page // Redirect back to edit page
$redirect->url('/novaconium/page/edit/' . $id); $redirect->url('/novaconium/page/edit/' . $id);

18
docs/Dev-Fake_autoload.md Normal file
View File

@ -0,0 +1,18 @@
# Fake autoload for dev
put this in index.php
```
// --- Dev-only autoloader for manually cloned vendor copy ---
spl_autoload_register(function ($class) {
if (str_starts_with($class, 'Novaconium\\')) {
$baseDir = BASEPATH . '/vendor/4lt/novaconium/src/';
$relativeClass = substr($class, strlen('Novaconium\\'));
$file = $baseDir . str_replace('\\', '/', $relativeClass) . '.php';
if (file_exists($file)) {
require_once $file;
}
}
});
```

View File

@ -1,14 +1,53 @@
<?php <?php
namespace Novaconium\Services; namespace Novaconium\Services;
/**
* TagManager Class
* Handles tag preparation, insertion, and linking for pages.
* Cleans up controller by encapsulating tag logic.
*/
class TagManager class TagManager
{ {
protected $db;
public function __construct() public function __construct()
{ {
echo "class access"; global $db;
$this->db = $db;
} }
}
/**
* Assign tags to a page.
*
* This will delete old links and insert new ones.
*
* @param int $pageId
* @param array $tags Array of tag names
*/
public function setTagsForPage(int $pageId, array $tags): void
{
// Remove existing links
$this->db->query("DELETE FROM page_tags WHERE page_id = ?", [$pageId]);
foreach ($tags as $tagName) {
$tagName = trim($tagName);
if ($tagName === '') continue;
// Check if tag exists
$stmt = $this->db->query("SELECT id FROM tags WHERE name = ?", [$tagName]);
$row = $stmt->fetch_assoc(); // mysqli_result -> assoc array
if ($row) {
$tagId = $row['id'];
} else {
// Insert new tag
$this->db->query(
"INSERT INTO tags (name, created) VALUES (?, NOW())",
[$tagName]
);
$tagId = $this->db->lastid;
}
// Link page to tag
$this->db->query(
"INSERT INTO page_tags (page_id, tag_id) VALUES (?, ?)",
[$pageId, $tagId]
);
}
}
}