From b2ff261e74a9998f905d9b3d3d614fe38e96d277 Mon Sep 17 00:00:00 2001 From: tp_dhu Date: Sun, 9 Feb 2025 21:23:06 +0000 Subject: [PATCH] start of KB --- README.md | 1 + app/controllers/KBController.php | 131 +++++++++++++++++++++++++++++++ public/index.php | 16 ++-- public/test.php | 6 ++ ui/templates/layout.html | 2 +- ui/views/kb/create.html | 16 ++++ ui/views/kb/index.html | 37 +++++++++ ui/views/kb/view.html | 18 +++++ 8 files changed, 220 insertions(+), 7 deletions(-) create mode 100644 app/controllers/KBController.php create mode 100644 public/test.php create mode 100644 ui/views/kb/create.html create mode 100644 ui/views/kb/index.html create mode 100644 ui/views/kb/view.html diff --git a/README.md b/README.md index cf25db8..950a63b 100644 --- a/README.md +++ b/README.md @@ -12,3 +12,4 @@ Used to keep track of ongoing projects/tasks, allow to view and search historic ## Milestones - Database created locally - .gitignore added +- added AuthController - login and logout process \ No newline at end of file diff --git a/app/controllers/KBController.php b/app/controllers/KBController.php new file mode 100644 index 0000000..7c8a600 --- /dev/null +++ b/app/controllers/KBController.php @@ -0,0 +1,131 @@ +exists('SESSION.user')){ + // $f3->set('SESSION.error', 'You don\'t have permission for this ticket.'); + $f3->reroute('/login'); + } + } + + public function index($f3){ + + $this->check_access($f3); + + $db = $f3->get('DB'); + $search_term = $f3->get('GET.search'); + $tag_param = $f3->get('GET.tag'); + + // base query + $sql = 'SELECT a.* FROM kb a'; + $args = []; + + if($tag_param){ + $sql .= ' + JOIN kb_tags AS at ON a.id = at.article_id + JOIN tags t ON at.tag_id = t.id + WHERE t.name = ? + '; + $args[] = $tag_param; + + if($search_term){ + $sql .= ' AND a.title LIKE ?'; + $args[] = '%' . $search_term . '%'; + } + } else if ($search_term){ + $sql .= ' WHERE a.title LIKE ?'; + $args[] = '%' . $search_term . '%'; + } + + $sql .= ' ORDER BY a.created_at DESC'; + + $articles = $db->exec($sql, $args); + + // render + $f3->set('articles', $articles); + $f3->set('content', '../ui/views/kb/index.html'); + echo \Template::instance()->render('../ui/templates/layout.html'); + $f3->clear('SESSION.error'); + + } + + /** + * Form to create new article + */ + public function createForm($f3){ + $this->check_access($f3); + + $db = $f3->get('DB'); + $all_tags = $db->exec('SELECT * FROM tags ORDER BY name ASC'); + $f3->set('all_tags', $all_tags); + + // render + $f3->set('content', '../ui/views/kb/create.html'); + echo \Template::instance()->render('../ui/templates/layout.html'); + $f3->clear('SESSION.error'); + + } + + // handle POST + public function create($f3){ + $this->check_access($f3); + + $title = $f3->get('POST.title'); + $content = $f3->get('POST.content'); + $created_by = $f3->get('SESSION.user.id'); + + $db = $f3->get('DB'); + + // insert + + $db->exec( + 'INSERT INTO kb (title, content, created_by, updated_by, created_at, updated_at) + VALUES (?,?,?,?, NOW(), NOW())', + [$title, $content, $created_by] + ); + + $article_id = $db->lastInsertId(); + + // TODO: tags + + $f3->reroute('/kb'); + } + + // view a single + public function view($f3){ + $this->check_access($f3); + $article_id = $f3->get('PARAMS.id'); + $db = $f3->get('DB'); + + $articles = $db->exec( + 'SELECT a.*, u.username AS created_by_name + FROM kb AS a + LEFT JOIN users AS u ON a.created_by = u.id + WHERE a.id = ? LIMIT 1', + [$article_id] + ); + + if(!$articles){ + $f3->set('SESSION.error', 'Article not found'); + $f3->reroute('/kb'); + } + + $article = $articles[0]; + $f3->set('article', $article); + + // TODO: tags + $tags = $db->exec( + 'SELECT t.* FROM tags AS t + JOIN kb_tags AS at ON t.id = at.tag_id + WHERE at.article_id = ?', + [$article_id] + ); + + // render + $f3->set('content', '../ui/views/kb/view.html'); + echo \Template::instance()->render('../ui/templates/layout.html'); + $f3->clear('SESSION.error'); + } + +} \ No newline at end of file diff --git a/public/index.php b/public/index.php index b36a849..0412c3a 100644 --- a/public/index.php +++ b/public/index.php @@ -43,16 +43,20 @@ $f3->route('GET /ticket/@id/edit', 'TicketController->editForm'); // edit ticket $f3->route('POST /ticket/@id/update', 'TicketController->update'); // // knowledgebase -$f3->route('GET /kb', 'KB->list'); -$f3->route('GET /kb/create', 'KB->create'); -$f3->route('GET /kb/@id', 'KB->read'); -$f3->route('GET /kb/@id/edit', 'KB->edit'); -$f3->route('POST /kb/@id/edit', 'KB->update'); +$f3->route('GET /kb', 'KBController->index'); +$f3->route('GET /kb/create', 'KBController->createForm'); +$f3->route('POST /kb/create', 'KBController->create'); +$f3->route('GET /kb/@id', 'KBController->view'); +$f3->route('GET /kb/@id/edit', 'KBController->editForm'); +$f3->route('POST /kb/@id/edit', 'KBControllerKB->edit'); // should this be update - "crud"? // tags -$f3->route('GET /tags', 'Tag->list'); +$f3->route('GET /tags', 'TagController->index'); +$f3->route('GET /tag/create', 'Tag->createForm'); $f3->route('POST /tag/create', 'Tag->create'); + +// dashboard $f3->route('GET /dashboard', 'DashboardController->index'); $f3->run(); \ No newline at end of file diff --git a/public/test.php b/public/test.php new file mode 100644 index 0000000..8a7e230 --- /dev/null +++ b/public/test.php @@ -0,0 +1,6 @@ +Dashboard Tickets Projects - Knowledge Base + Knowledge Base