isAdmin()) throw new NotAllowedException(); if (isset($_REQUEST['create']) && isset($_POST['tag'])) $this->handleTagCreation(); $options = [ 'columns' => [ 'id_post' => [ 'value' => 'id_tag', 'header' => 'ID', 'is_sortable' => true, ], 'tag' => [ 'header' => 'Tag', 'is_sortable' => true, 'parse' => [ 'link' => BASEURL . '/managetag/?id={ID_TAG}', 'data' => 'tag', ], ], 'slug' => [ 'header' => 'Slug', 'is_sortable' => true, 'parse' => [ 'link' => BASEURL . '/managetag/?id={ID_TAG}', 'data' => 'slug', ], ], 'count' => [ 'header' => 'Cardinality', 'is_sortable' => true, 'value' => 'count', ], ], 'start' => !empty($_GET['start']) ? (int) $_GET['start'] : 0, 'sort_order' => !empty($_GET['order']) ? $_GET['order'] : null, 'sort_direction' => !empty($_GET['dir']) ? $_GET['dir'] : null, 'title' => 'Manage tags', 'no_items_label' => 'No tags meet the requirements of the current filter.', 'items_per_page' => 25, 'base_url' => BASEURL . '/managetags/', 'get_data' => function($offset = 0, $limit = 15, $order = '', $direction = 'up') { if (!in_array($order, ['id_post', 'tag', 'slug', 'count'])) $order = 'tag'; if (!in_array($direction, ['up', 'down'])) $direction = 'up'; $data = Registry::get('db')->queryAssocs(' SELECT * FROM tags ORDER BY {raw:order} LIMIT {int:offset}, {int:limit}', [ 'order' => $order . ($direction == 'up' ? ' ASC' : ' DESC'), 'offset' => $offset, 'limit' => $limit, ]); return [ 'rows' => $data, 'order' => $order, 'direction' => ($direction == 'up' ? 'up' : 'down'), ]; }, 'get_count' => function() { return Registry::get('db')->queryValue(' SELECT COUNT(*) FROM tags'); } ]; $table = new GenericTable($options); parent::__construct('Tag management - Page ' . $table->getCurrentPage() .' - ' . SITE_TITLE); $this->page->adopt(new TabularData($table)); } private function handleTagCreation() { header('Content-Type: text/json; charset=utf-8'); // It better not already exist! if (Tag::exactMatch($_POST['tag'])) { echo '{"error":"Tag already exists!"}'; exit; } $label = htmlentities(trim($_POST['tag'])); $slug = strtr(strtolower($label), [' ' => '-']); $tag = Tag::createNew([ 'tag' => $label, 'slug' => $slug, ]); // Did we succeed? if (!$tag) { echo '{"error":"Could not create tag."}'; exit; } echo json_encode([ 'label' => $tag->tag, 'id_tag' => $tag->id_tag, ]); exit; } }