Add Filter Date Range (From, To)

This commit is contained in:
Yuriy Bakhtin 2024-02-09 12:01:31 +01:00
parent 121886940d
commit 717bfff4f9
4 changed files with 62 additions and 6 deletions

View File

@ -1,18 +1,24 @@
<?php
/**
* @link https://www.humhub.org/
* @copyright Copyright (c) HumHub GmbH & Co. KG
* @license https://www.humhub.com/licences
*/
namespace humhub\modules\content\search;
use DateTime;
use humhub\modules\content\models\ContentType;
use humhub\modules\user\models\User;
use Yii;
use yii\base\Model;
use yii\db\Query;
use yii\web\IdentityInterface;
use yii\helpers\FormatConverter;
class SearchRequest extends Model
{
public const ORDER_BY_CREATION_DATE = 'content.created_at';
public const ORDER_BY_SCORE = 'score';
public const DATE_FORMAT = 'short';
public ?User $user = null;
@ -24,6 +30,9 @@ class SearchRequest extends Model
public $contentType = '';
public ?string $dateFrom = null;
public ?string $dateTo = null;
public $contentContainer = [];
public $orderBy = 'content.created_at';
@ -45,6 +54,7 @@ class SearchRequest extends Model
[['keyword'], 'safe'],
[['keyword'], 'required'],
[['contentType'], 'in', 'range' => array_keys(static::getContentTypes())],
[['dateFrom', 'dateTo'], 'date', 'format' => 'php:' . FormatConverter::convertDateIcuToPhp(self::DATE_FORMAT)],
//[['page'], 'numeric'],
//[['pageSize'], 'numeric'],
[['orderBy'], 'in', 'range' => [static::ORDER_BY_SCORE, static::ORDER_BY_CREATION_DATE]],
@ -66,4 +76,26 @@ class SearchRequest extends Model
return $result;
}
/**
* @inheritdoc
*/
public function afterValidate()
{
parent::afterValidate();
$this->normalizeDate('dateFrom');
$this->normalizeDate('dateTo');
}
protected function normalizeDate(string $dateFieldName)
{
if ($this->hasErrors($dateFieldName) || empty($this->$dateFieldName)) {
return;
}
$format = FormatConverter::convertDateIcuToPhp(self::DATE_FORMAT, 'date', Yii::$app->formatter->locale);
$this->$dateFieldName = DateTime::createFromFormat($format, $this->$dateFieldName)->format('Y-m-d');
}
}

View File

@ -1,4 +1,9 @@
<?php
/**
* @link https://www.humhub.org/
* @copyright Copyright (c) HumHub GmbH & Co. KG
* @license https://www.humhub.com/licences
*/
namespace humhub\modules\content\search\driver;
@ -68,6 +73,13 @@ class MysqlDriver extends AbstractDriver
$query->andWhere(['content.object_model' => $request->contentType]);
}
if (!empty($request->dateFrom)) {
$query->andWhere(['>=', 'content.created_at', $request->dateFrom . ' 00:00:00']);
}
if (!empty($request->dateTo)) {
$query->andWhere(['<=', 'content.created_at', $request->dateTo . ' 23:59:59']);
}
if ($request->author) {
$query->andWhere(['content.created_by' => $request->author->id]);
}

View File

@ -53,19 +53,19 @@ class SearchFilters extends DirectoryFilters
'sortOrder' => 300,
]);
/*
$this->addFilter('dateForm', [
$this->addFilter('dateFrom', [
'title' => Yii::t('SpaceModule.base', 'Date From'),
'type' => 'input',
'type' => 'date',
'sortOrder' => 400,
]);
$this->addFilter('dateTo', [
'title' => Yii::t('SpaceModule.base', 'Date To'),
'type' => 'input',
'type' => 'date',
'sortOrder' => 420,
]);
/*
$this->addFilter('topic', [
'title' => Yii::t('SpaceModule.base', 'Topic'),
'type' => 'input',

View File

@ -9,6 +9,7 @@ namespace humhub\modules\ui\widgets;
use humhub\components\Widget;
use humhub\libs\Html;
use humhub\modules\ui\form\widgets\DatePicker;
use humhub\widgets\Button;
use Yii;
use yii\helpers\ArrayHelper;
@ -142,6 +143,17 @@ abstract class DirectoryFilters extends Widget
$inputOptions['data-action-change'] = 'cards.applyFilters';
$inputHtml = $data['widget']::widget(array_merge(['name' => $filter, 'options' => $inputOptions], $data['widgetOptions']));
break;
case 'date':
$format = $data['format'] ?? 'short';
$value = self::getValue($filter);
$inputHtml = DatePicker::widget([
'name' => $filter,
'value' => empty($value) ? '' : Yii::$app->formatter->asDate($value, $format),
'dateFormat' => $format
]);
break;
case 'input':
case 'text':
default: