aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorThomas Lange <code@nerdmind.de>2021-07-22 22:44:34 +0200
committerThomas Lange <code@nerdmind.de>2021-07-22 22:44:34 +0200
commit27e7a426b1c7ba7ec878204a937a0ecfff5214ed (patch)
treef6095b189b3d7d0b27eff41930d243f161870e91
parent8662f123d781a6df4eb5bd05b3556cca94fcf334 (diff)
downloadblog-27e7a426b1c7ba7ec878204a937a0ecfff5214ed.tar.gz
blog-27e7a426b1c7ba7ec878204a937a0ecfff5214ed.tar.xz
blog-27e7a426b1c7ba7ec878204a937a0ecfff5214ed.zip
Add user and category filter on post search
Add a user and category filter option for the post search in the administration area. Filter on default theme is followed later.
-rw-r--r--admin/post/search.php39
-rw-r--r--core/namespace/ORM/RepositorySearch.php10
-rw-r--r--theme/admin/html/post/search.php37
3 files changed, 84 insertions, 2 deletions
diff --git a/admin/post/search.php b/admin/post/search.php
index 8cbfafb..46ff60d 100644
--- a/admin/post/search.php
+++ b/admin/post/search.php
@@ -13,6 +13,7 @@ require '../../core/application.php';
#===============================================================================
# Get repositories
#===============================================================================
+$CategoryRepository = Application::getRepository('Category');
$PostRepository = Application::getRepository('Post');
$UserRepository = Application::getRepository('User');
@@ -31,7 +32,12 @@ $offset = ($currentSite-1) * $site_size;
#===============================================================================
if($search = HTTP::GET('q')) {
try {
- foreach ($PostRepository->search($search, [], $site_size, $offset) as $Post) {
+ $filter = [
+ 'user' => HTTP::GET('user'),
+ 'category' => HTTP::GET('category')
+ ];
+
+ foreach ($PostRepository->search($search, $filter, $site_size, $offset) as $Post) {
$User = $UserRepository->find($Post->get('user'));
$templates[] = generatePostItemTemplate($Post, $User);
}
@@ -56,13 +62,42 @@ if($count = $PostRepository->getLastSearchOverallCount()) {
}
#===============================================================================
+# Generate user list
+#===============================================================================
+foreach($UserRepository->getAll([], 'fullname ASC') as $User) {
+ $userList[] = [
+ 'ID' => $User->getID(),
+ 'FULLNAME' => $User->get('fullname'),
+ 'USERNAME' => $User->get('username'),
+ ];
+}
+
+#===============================================================================
+# Generate category list
+#===============================================================================
+foreach($CategoryRepository->getAll([], 'name ASC') as $Category) {
+ $categoryList[] = [
+ 'ID' => $Category->getID(),
+ 'NAME' => $Category->get('name'),
+ 'PARENT' => $Category->get('parent'),
+ ];
+}
+
+#===============================================================================
# Build document
#===============================================================================
$SearchTemplate = Template\Factory::build('post/search');
$SearchTemplate->set('QUERY', $search);
$SearchTemplate->set('POSTS', $templates ?? []);
$SearchTemplate->set('FORM', [
- 'INFO' => $messages ?? []
+ 'INFO' => $messages ?? [],
+ 'DATA' => [
+ 'USER' => HTTP::GET('user'),
+ 'CATEGORY' => HTTP::GET('category')
+ ],
+ 'USER_LIST' => $userList ?? [],
+ 'CATEGORY_LIST' => $categoryList ?? [],
+ 'CATEGORY_TREE' => generateCategoryDataTree($categoryList ?? [])
]);
$SearchTemplate->set('PAGINATION', $pagination_data ?? []);
diff --git a/core/namespace/ORM/RepositorySearch.php b/core/namespace/ORM/RepositorySearch.php
index 8a61998..aac46ef 100644
--- a/core/namespace/ORM/RepositorySearch.php
+++ b/core/namespace/ORM/RepositorySearch.php
@@ -24,6 +24,16 @@ trait RepositorySearch {
$params[] = $filter['day'];
}
+ if(is_numeric($filter['user'] ?? NULL)) {
+ $extend[] = 'user = ? AND';
+ $params[] = $filter['user'];
+ }
+
+ if(is_numeric($filter['category'] ?? NULL)) {
+ $extend[] = 'category = ? AND';
+ $params[] = $filter['category'];
+ }
+
if($limit) {
$limit = "LIMIT $offset,$limit";
}
diff --git a/theme/admin/html/post/search.php b/theme/admin/html/post/search.php
index 10aa43d..ff4cf59 100644
--- a/theme/admin/html/post/search.php
+++ b/theme/admin/html/post/search.php
@@ -1,3 +1,18 @@
+<?php
+function categorySelectList($category_tree, $selected = NULL, $prefix = '') {
+ foreach($category_tree as $category) {
+ $option = '<option value="%s"%s>%s%s [%d]</option>';
+ $select = ($category['ID'] == $selected) ? ' selected' : '';
+
+ printf($option, $category['ID'], $select, $prefix, escapeHTML($category['NAME']), $category['ID']);
+
+ if(isset($category['CHILDS'])) {
+ # If there are children, call self and pass children array.
+ (__FUNCTION__)($category['CHILDS'], $selected, $prefix.'– ');
+ }
+ }
+}
+?>
<main id="main-content" <?=!$POSTS ?: 'class="wide"'?>>
<h1><i class="fa fa-search"></i><?=$Language->text('title_post_search')?></h1>
<p><?=$Language->text('search_post_desc')?></p>
@@ -20,6 +35,28 @@
<div class="form-grid-item first">
<input id="form_query" type="search" name="q" placeholder="<?=$Language->text('placeholder_search')?>" value="<?=escapeHTML($QUERY)?>" />
</div>
+
+ <label for="form_category">
+ <i class="fa fa-tag"></i><?=$Language->text('label_category')?></label>
+
+ <div class="form-grid-item">
+ <select id="form_category" name="category">
+ <option value="">[ –– <?=$Language->text('label_category')?> –– ]</option>
+ <?=categorySelectList($FORM['CATEGORY_TREE'], $FORM['DATA']['CATEGORY']);?>
+ </select>
+ </div>
+
+ <label for="form_user">
+ <i class="fa fa-user"></i><?=$Language->text('label_user')?></label>
+
+ <div class="form-grid-item">
+ <select id="form_user" name="user">
+ <option value="">[ –– <?=$Language->text('label_user')?> –– ]</option>
+ <?php foreach($FORM['USER_LIST'] as $user): ?>
+ <option value="<?=$user['ID']?>"<?=($FORM['DATA']['USER'] == $user['ID']) ? ' selected' : '' ?>><?=escapeHTML($user['FULLNAME'])?> [<?=$user['USERNAME']?>]</option>
+ <?php endforeach; ?>
+ </select>
+ </div>
</div>
<div class="form-border-box background padding">
<input id="update-button" type="submit" value="<?=$Language->text('search')?>" />