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'); } }