Add Filter for Topic

This commit is contained in:
Yuriy Bakhtin 2024-02-09 17:51:15 +01:00
parent 717bfff4f9
commit 19ce6f6737
5 changed files with 50 additions and 21 deletions

View File

@ -1,6 +1,7 @@
<?php
use yii\db\Migration;
use humhub\components\Migration;
/**
* Class m240203_112155_search
@ -12,7 +13,7 @@ class m240203_112155_search extends Migration
*/
public function safeUp()
{
$this->createTable('content_fulltext', [
$this->safeCreateTable('content_fulltext', [
'content_id' => $this->integer(),
'contents' => $this->text(),
'comments' => $this->text(),

View File

@ -33,6 +33,8 @@ class SearchRequest extends Model
public ?string $dateFrom = null;
public ?string $dateTo = null;
public array $topic = [];
public $contentContainer = [];
public $orderBy = 'content.created_at';
@ -51,7 +53,7 @@ class SearchRequest extends Model
public function rules()
{
return [
[['keyword'], 'safe'],
[['keyword', 'topic'], 'safe'],
[['keyword'], 'required'],
[['contentType'], 'in', 'range' => array_keys(static::getContentTypes())],
[['dateFrom', 'dateTo'], 'date', 'format' => 'php:' . FormatConverter::convertDateIcuToPhp(self::DATE_FORMAT)],

View File

@ -80,6 +80,11 @@ class MysqlDriver extends AbstractDriver
$query->andWhere(['<=', 'content.created_at', $request->dateTo . ' 23:59:59']);
}
if (!empty($request->topic)) {
$query->leftJoin('content_tag_relation', 'content_tag_relation.content_id = content.id')
->andWhere(['IN', 'content_tag_relation.tag_id', $request->topic]);
}
if ($request->author) {
$query->andWhere(['content.created_by' => $request->author->id]);
}

View File

@ -9,11 +9,13 @@ namespace humhub\modules\content\widgets;
use humhub\libs\Html;
use humhub\modules\content\search\SearchRequest;
use humhub\modules\topic\models\Topic;
use humhub\modules\topic\widgets\TopicPicker;
use humhub\modules\ui\widgets\DirectoryFilters;
use Yii;
/**
* SpaceDirectoryFilters displays the filters on the directory spaces page
* SearchFilters displays the filters on the content searching page
*
* @since 1.9
* @author Luke
@ -29,7 +31,7 @@ class SearchFilters extends DirectoryFilters
{
$this->addFilter('keyword', [
'title' => Yii::t('ContentModule.search', 'Find Content based on keywords'),
'placeholder' => Yii::t('SpaceModule.base', 'Search...'),
'placeholder' => Yii::t('ContentModule.search', 'Search...'),
'type' => 'input',
'wrapperClass' => 'col-md-6 form-search-filter-keyword',
'afterInput' => Html::submitButton('<span class="fa fa-search"></span>', ['class' => 'form-button-search']),
@ -37,43 +39,47 @@ class SearchFilters extends DirectoryFilters
]);
$this->addFilter('orderBy', [
'title' => Yii::t('SpaceModule.base', 'Sorting'),
'title' => Yii::t('ContentModule.search', 'Sorting'),
'type' => 'dropdown',
'options' => [
SearchRequest::ORDER_BY_SCORE => Yii::t('SpaceModule.base', 'Best'),
SearchRequest::ORDER_BY_CREATION_DATE => Yii::t('SpaceModule.base', 'Newest first'),
SearchRequest::ORDER_BY_SCORE => Yii::t('ContentModule.search', 'Best'),
SearchRequest::ORDER_BY_CREATION_DATE => Yii::t('ContentModule.search', 'Newest first'),
],
'sortOrder' => 200,
]);
$this->addFilter('contentType', [
'title' => Yii::t('SpaceModule.base', 'Content type'),
'title' => Yii::t('ContentModule.search', 'Content type'),
'type' => 'dropdown',
'options' => array_merge(['' => Yii::t('SpaceModule.base', 'Any')], SearchRequest::getContentTypes()),
'options' => array_merge(['' => Yii::t('ContentModule.search', 'Any')], SearchRequest::getContentTypes()),
'sortOrder' => 300,
]);
$this->addFilter('dateFrom', [
'title' => Yii::t('SpaceModule.base', 'Date From'),
'title' => Yii::t('ContentModule.search', 'Date From'),
'type' => 'date',
'sortOrder' => 400,
]);
$this->addFilter('dateTo', [
'title' => Yii::t('SpaceModule.base', 'Date To'),
'title' => Yii::t('ContentModule.search', 'Date To'),
'type' => 'date',
'sortOrder' => 420,
]);
/*
$this->addFilter('topic', [
'title' => Yii::t('SpaceModule.base', 'Topic'),
'type' => 'input',
'sortOrder' => 420,
'title' => Yii::t('ContentModule.search', 'Topic'),
'type' => 'widget',
'widget' => TopicPicker::class,
'widgetOptions' => [
'selection' => $this->getTopicsFromRequest()
],
'sortOrder' => 430,
]);
/*
$this->addFilter('author', [
'title' => Yii::t('SpaceModule.base', 'Author'),
'title' => Yii::t('ContentModule.search', 'Author'),
'type' => 'input',
'sortOrder' => 500,
]);
@ -81,7 +87,7 @@ class SearchFilters extends DirectoryFilters
/*
$this->addFilter('status', [
'title' => Yii::t('SpaceModule.base', 'Status'),
'title' => Yii::t('ContentModule.search', 'Status'),
'type' => 'dropdown',
'options' => [
'' => 'Any',
@ -90,7 +96,7 @@ class SearchFilters extends DirectoryFilters
'sortOrder' => 500,
]);
$this->addFilter('space', [
'title' => Yii::t('SpaceModule.base', 'Space'),
'title' => Yii::t('ContentModule.search', 'Space'),
'type' => 'input',
'sortOrder' => 500,
]);
@ -98,7 +104,7 @@ class SearchFilters extends DirectoryFilters
/*
$this->addFilter('profile', [
'title' => Yii::t('SpaceModule.base', 'Profile'),
'title' => Yii::t('ContentModule.search', 'Profile'),
'type' => 'input',
'sortOrder' => 500,
]);
@ -114,4 +120,14 @@ class SearchFilters extends DirectoryFilters
return parent::getDefaultValue($filter);
}
protected function getTopicsFromRequest(): array
{
$topics = Yii::$app->request->get('topic');
if (!is_array($topics) || empty($topics)) {
return [];
}
return Topic::find()->where(['IN', 'id', $topics])->all();
}
}

View File

@ -139,9 +139,14 @@ abstract class DirectoryFilters extends Widget
case 'info':
$inputHtml = $data['info'];
break;
case 'widget':
$inputOptions['data-action-change'] = 'cards.applyFilters';
$inputHtml = $data['widget']::widget(array_merge(['name' => $filter, 'options' => $inputOptions], $data['widgetOptions']));
$options = ['name' => $filter, 'options' => $inputOptions];
if (isset($data['widgetOptions']) && is_array($data['widgetOptions'])) {
$options = array_merge($options, $data['widgetOptions']);
}
$inputHtml = $data['widget']::widget($options);
break;
case 'date':