tags working
This commit is contained in:
@@ -1,14 +1,53 @@
|
||||
<?php
|
||||
|
||||
namespace Novaconium\Services;
|
||||
/**
|
||||
* TagManager Class
|
||||
* Handles tag preparation, insertion, and linking for pages.
|
||||
* Cleans up controller by encapsulating tag logic.
|
||||
*/
|
||||
|
||||
class TagManager
|
||||
{
|
||||
protected $db;
|
||||
|
||||
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]
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user