Skip to content

Commit

Permalink
add numeric range field
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan DJ committed Aug 25, 2020
1 parent 2de41dc commit 912b968
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 20 deletions.
27 changes: 23 additions & 4 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ Filter record by date range of selected date field.
'fieldName' => 'Created',
'fieldType' => 'dateRange',
'options' => [
'beginTitle' => 'Create Date Begin',
'endTitle' => 'Create Date End'
'beginTitle' => 'Create Date From',
'endTitle' => 'Create Date To'
]
]
```
Expand All @@ -88,8 +88,27 @@ Filter record by date and time range of selected date field.
'fieldName' => 'Created',
'fieldType' => 'dateTimeRange',
'options' => [
'beginTitle' => 'Create Time Begin',
'endTitle' => 'Create Time End'
'beginTitle' => 'Create Time From',
'endTitle' => 'Create Time To'
]
]
```
Options:
- `beginTitle`: custom begin label
- `endTitle`: custom end label
### numericRange
Filter record by numeric range of selected date field.
```
[
'fieldName' => 'Weight',
'fieldType' => 'numericRange',
'options' => [
'beginTitle' => 'Weight From',
'endTitle' => 'Weight To'
]
]
```
Expand Down
17 changes: 13 additions & 4 deletions src/FilterExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,26 @@ public function updateSearchContext($context)
$endTitle = $field['options']['endTitle'] ?? '';
$dateRangeField = FilterType::getDateRangeFilter($fieldName, $beginTitle, $endTitle);

$context->getFields()->push($dateRangeField['beginDate']);
$context->getFields()->push($dateRangeField['endDate']);
$context->getFields()->push($dateRangeField['begin']);
$context->getFields()->push($dateRangeField['end']);

break;
case 'dateTimeRange':
$beginTitle = $field['options']['beginTitle'] ?? '';
$endTitle = $field['options']['endTitle'] ?? '';
$dateTimeRangeField = FilterType::getDateTimeRangeFilter($fieldName, $beginTitle, $endTitle);

$context->getFields()->push($dateTimeRangeField['beginDateTime']);
$context->getFields()->push($dateTimeRangeField['endDateTime']);
$context->getFields()->push($dateTimeRangeField['begin']);
$context->getFields()->push($dateTimeRangeField['end']);

break;
case 'numericRange':
$beginTitle = $field['options']['beginTitle'] ?? '';
$endTitle = $field['options']['endTitle'] ?? '';
$numericRangeField = FilterType::getNumericRangeFilter($fieldName, $beginTitle, $endTitle);

$context->getFields()->push($numericRangeField['begin']);
$context->getFields()->push($numericRangeField['end']);

break;
}
Expand Down
40 changes: 28 additions & 12 deletions src/FilterType.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use SilverStripe\Forms\DateField;
use SilverStripe\Forms\DatetimeField;
use SilverStripe\Forms\NumericField;
use SilverStripe\Forms\TextField;

/**
Expand All @@ -14,30 +15,45 @@ class FilterType
/**
* Add date range filter
*/
public static function getDateRangeFilter(string $dateField, string $beginTitle = '', string $endTitle = ''): array
public static function getDateRangeFilter(string $field, string $beginTitle = '', string $endTitle = ''): array
{
$dateTitle = self::getFieldLabel($dateField);
$beginTitle = !empty($beginTitle) ? $beginTitle : $dateTitle . ' Begin';
$endTitle = !empty($endTitle) ? $endTitle : $dateTitle . ' End';
$title = self::getFieldLabel($field);
$beginTitle = !empty($beginTitle) ? $beginTitle : $title . ' Begin';
$endTitle = !empty($endTitle) ? $endTitle : $title . ' End';

return [
'beginDate' => DateField::create($dateField . ':GreaterThanOrEqual', $beginTitle),
'endDate' => DateField::create($dateField . ':LessThanOrEqual', $endTitle)
'begin' => DateField::create($field . ':GreaterThanOrEqual', $beginTitle),
'end' => DateField::create($field . ':LessThanOrEqual', $endTitle)
];
}

/**
* Add date time range filter
*/
public static function getDateTimeRangeFilter(string $dateTimeField, string $beginTitle = '', string $endTitle = ''): array
public static function getDateTimeRangeFilter(string $field, string $beginTitle = '', string $endTitle = ''): array
{
$dateTimeTitle = self::getFieldLabel($dateTimeField);
$beginTitle = !empty($beginTitle) ? $beginTitle : $dateTimeTitle . ' Begin';
$endTitle = !empty($endTitle) ? $endTitle : $dateTimeTitle . ' End';
$title = self::getFieldLabel($field);
$beginTitle = !empty($beginTitle) ? $beginTitle : $title . ' Begin';
$endTitle = !empty($endTitle) ? $endTitle : $title . ' End';

return [
'beginDateTime' => DatetimeField::create($dateTimeField . ':GreaterThanOrEqual', $beginTitle),
'endDateTime' => DatetimeField::create($dateTimeField . ':LessThanOrEqual', $endTitle)
'begin' => DatetimeField::create($field . ':GreaterThanOrEqual', $beginTitle),
'end' => DatetimeField::create($field . ':LessThanOrEqual', $endTitle)
];
}

/**
* Add numeric range filter
*/
public static function getNumericRangeFilter(string $field, string $beginTitle = '', string $endTitle = ''): array
{
$title = self::getFieldLabel($field);
$beginTitle = !empty($beginTitle) ? $beginTitle : $title . ' Begin';
$endTitle = !empty($endTitle) ? $endTitle : $title . ' End';

return [
'begin' => NumericField::create($field . ':GreaterThanOrEqual', $beginTitle),
'end' => NumericField::create($field . ':LessThanOrEqual', $endTitle)
];
}

Expand Down

0 comments on commit 912b968

Please sign in to comment.