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 <?php
/**
* @link https://www.humhub.org/
* @copyright Copyright (c) HumHub GmbH & Co. KG
* @license https://www.humhub.com/licences
*/
namespace humhub\modules\content\search; namespace humhub\modules\content\search;
use DateTime;
use humhub\modules\content\models\ContentType; use humhub\modules\content\models\ContentType;
use humhub\modules\user\models\User; use humhub\modules\user\models\User;
use Yii; use Yii;
use yii\base\Model; use yii\base\Model;
use yii\db\Query; use yii\helpers\FormatConverter;
use yii\web\IdentityInterface;
class SearchRequest extends Model class SearchRequest extends Model
{ {
public const ORDER_BY_CREATION_DATE = 'content.created_at'; public const ORDER_BY_CREATION_DATE = 'content.created_at';
public const ORDER_BY_SCORE = 'score'; public const ORDER_BY_SCORE = 'score';
public const DATE_FORMAT = 'short';
public ?User $user = null; public ?User $user = null;
@ -24,6 +30,9 @@ class SearchRequest extends Model
public $contentType = ''; public $contentType = '';
public ?string $dateFrom = null;
public ?string $dateTo = null;
public $contentContainer = []; public $contentContainer = [];
public $orderBy = 'content.created_at'; public $orderBy = 'content.created_at';
@ -45,6 +54,7 @@ class SearchRequest extends Model
[['keyword'], 'safe'], [['keyword'], 'safe'],
[['keyword'], 'required'], [['keyword'], 'required'],
[['contentType'], 'in', 'range' => array_keys(static::getContentTypes())], [['contentType'], 'in', 'range' => array_keys(static::getContentTypes())],
[['dateFrom', 'dateTo'], 'date', 'format' => 'php:' . FormatConverter::convertDateIcuToPhp(self::DATE_FORMAT)],
//[['page'], 'numeric'], //[['page'], 'numeric'],
//[['pageSize'], 'numeric'], //[['pageSize'], 'numeric'],
[['orderBy'], 'in', 'range' => [static::ORDER_BY_SCORE, static::ORDER_BY_CREATION_DATE]], [['orderBy'], 'in', 'range' => [static::ORDER_BY_SCORE, static::ORDER_BY_CREATION_DATE]],
@ -66,4 +76,26 @@ class SearchRequest extends Model
return $result; 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 <?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; namespace humhub\modules\content\search\driver;
@ -68,6 +73,13 @@ class MysqlDriver extends AbstractDriver
$query->andWhere(['content.object_model' => $request->contentType]); $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) { if ($request->author) {
$query->andWhere(['content.created_by' => $request->author->id]); $query->andWhere(['content.created_by' => $request->author->id]);
} }

View File

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

View File

@ -9,6 +9,7 @@ namespace humhub\modules\ui\widgets;
use humhub\components\Widget; use humhub\components\Widget;
use humhub\libs\Html; use humhub\libs\Html;
use humhub\modules\ui\form\widgets\DatePicker;
use humhub\widgets\Button; use humhub\widgets\Button;
use Yii; use Yii;
use yii\helpers\ArrayHelper; use yii\helpers\ArrayHelper;
@ -142,6 +143,17 @@ abstract class DirectoryFilters extends Widget
$inputOptions['data-action-change'] = 'cards.applyFilters'; $inputOptions['data-action-change'] = 'cards.applyFilters';
$inputHtml = $data['widget']::widget(array_merge(['name' => $filter, 'options' => $inputOptions], $data['widgetOptions'])); $inputHtml = $data['widget']::widget(array_merge(['name' => $filter, 'options' => $inputOptions], $data['widgetOptions']));
break; 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 'input':
case 'text': case 'text':
default: default: