Skip to content

Commit

Permalink
Merge pull request #19 from bluefyn-international/feature/improve-fie…
Browse files Browse the repository at this point in the history
…ld-and-filter-timezone-support

Add initial metadata support, Improve support for client timezones
  • Loading branch information
solflare committed Jul 14, 2022
2 parents 8c39d41 + a11b65c commit 883e67a
Show file tree
Hide file tree
Showing 11 changed files with 142 additions and 23 deletions.
26 changes: 24 additions & 2 deletions resources/views/base-web.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,19 @@ function popupConfirm(row, title, body, routeTemplate, routeReplacements, routeH
})
}
function addMetaDataToUrl(url)
{
let metaData = {
timezone: Intl.DateTimeFormat().resolvedOptions().timeZone,
}
let encodedMetaData = btoa(JSON.stringify(metaData))
url.searchParams.append('metaData', encodedMetaData)
return url
}
(() => {
$('#filterContainer .custom-select').change()
Expand Down Expand Up @@ -177,8 +190,17 @@ function popupConfirm(row, title, body, routeTemplate, routeReplacements, routeH
}
})
history.replaceState(null, '', endpoint.replace('.json', '') + '?' + filterParams.toString());
table.setData(endpoint + '?' + filterParams.toString());
let viewReportUrl = endpoint.replace('.json', '') + '?' + filterParams.toString()
let dataReportUrl = endpoint + '?' + filterParams.toString()
viewReportUrlObject = new URL(viewReportUrl)
dataReportUrlObject = new URL(dataReportUrl)
viewReportUrlObject = addMetaDataToUrl(viewReportUrlObject)
dataReportUrlObject = addMetaDataToUrl(dataReportUrlObject)
history.replaceState(null, '', viewReportUrlObject.toString());
table.setData(dataReportUrlObject.toString());
});
@if($autoloadInitialData) {
Expand Down
9 changes: 8 additions & 1 deletion src/BaseFeatures/Data/Types/DateTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ class DateTime extends BaseType

protected string $outputFormat = 'L';

protected ?string $formatter = 'datetime';

public function __construct(
string|null $outputFormat = null,
string|null $placeholder = null,
Expand Down Expand Up @@ -55,6 +57,11 @@ public function setInputFormat(string|null $format) : self
return $this;
}

public function getOutputTimezone() : ?string
{
return $this->outputTzName;
}

public function setOutputTimezone(string|null $timezone) : self
{
$this->outputTzName = $timezone;
Expand All @@ -64,7 +71,7 @@ public function setOutputTimezone(string|null $timezone) : self

public function formatter(): string
{
return 'datetime';
return $this->formatter;
}

public function formatterParams(): array
Expand Down
6 changes: 3 additions & 3 deletions src/BaseFeatures/Filters/BaseFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ abstract class BaseFilter implements Arrayable
/**
* BaseFilter constructor.
*
* @param Column $column
* @param null $value
* @param Column $column
* @param mixed|null $value
*/
public function __construct(Column $column, $value = null)
public function __construct(Column $column, mixed $value = null)
{
$this->setColumn($column);
$this->setValue($value);
Expand Down
25 changes: 25 additions & 0 deletions src/BaseFeatures/Filters/DoesNotEqualFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace BluefynInternational\ReportEngine\BaseFeatures\Filters;

use Carbon\Carbon;
use Illuminate\Database\Query\Builder;
use Illuminate\Support\Arr;

class DoesNotEqualFilter extends BaseFilter
{
Expand Down Expand Up @@ -45,4 +47,27 @@ public static function key(): string
{
return 'does_not_equal';
}

/**
* @return null|string|Carbon
*/
public function getValue(array $options = [])
{
if ($this->valueIsDate()) {
/**
* @var Carbon $value
*/
$value = parent::getValue();
$timeZoneString = $this->getColumn()->type()->getOutputTimezone()
?? Arr::get($options, 'timezone');

if ($timeZoneString) {
$value->shiftTimezone($timeZoneString);
}

return $value->utc();
}

return parent::getValue();
}
}
4 changes: 2 additions & 2 deletions src/BaseFeatures/Filters/EqualsFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ public function apply(Builder $builder, array $options = []) : Builder
if ($this->valueIsDate()) {
$greaterThanEqual = new GreaterThanOrEqualFilter($this->getColumn(), $this->getValue());
$lessThanEqual = new LessThanOrEqualFilter($this->getColumn(), $this->getValue());
$builder = $greaterThanEqual->apply($builder);
$builder = $greaterThanEqual->apply($builder, $options);

return $lessThanEqual->apply($builder);
return $lessThanEqual->apply($builder, $options);
}

$action = $this->getAction();
Expand Down
20 changes: 17 additions & 3 deletions src/BaseFeatures/Filters/GreaterThanFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace BluefynInternational\ReportEngine\BaseFeatures\Filters;

use Carbon\Carbon;
use Illuminate\Database\Query\Builder;
use Illuminate\Support\Arr;

class GreaterThanFilter extends BaseFilter
{
Expand All @@ -15,17 +17,29 @@ class GreaterThanFilter extends BaseFilter
public function apply(Builder $builder, array $options = []) : Builder
{
$action = $this->getAction();
$value = $this->getValue($options);

return $builder->$action($this->getField(), '>', $this->getValue());
return $builder->$action($this->getField(), '>', $value);
}

/**
* @return null|string|mixed
*/
public function getValue()
public function getValue(array $options = [])
{
if ($this->valueIsDate()) {
return parent::getValue()->endOfDay()->toDateTimeString();
/**
* @var Carbon $value
*/
$value = parent::getValue();
$timeZoneString = $this->getColumn()->type()->getOutputTimezone()
?? Arr::get($options, 'timezone');

if ($timeZoneString) {
$value->shiftTimezone($timeZoneString);
}

return $value->endOfDay()->utc()->toDateTimeString();
}

return parent::getValue();
Expand Down
20 changes: 17 additions & 3 deletions src/BaseFeatures/Filters/GreaterThanOrEqualFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace BluefynInternational\ReportEngine\BaseFeatures\Filters;

use Carbon\Carbon;
use Illuminate\Database\Query\Builder;
use Illuminate\Support\Arr;

class GreaterThanOrEqualFilter extends BaseFilter
{
Expand All @@ -15,17 +17,29 @@ class GreaterThanOrEqualFilter extends BaseFilter
public function apply(Builder $builder, array $options = []) : Builder
{
$action = $this->getAction();
$value = $this->getValue($options);

return $builder->$action($this->getField(), '>=', $this->getValue());
return $builder->$action($this->getField(), '>=', $value);
}

/**
* @return null|string
*/
public function getValue()
public function getValue(array $options = [])
{
if ($this->valueIsDate()) {
return parent::getValue()->startOfDay()->toDateTimeString();
/**
* @var Carbon $value
*/
$value = parent::getValue();
$timeZoneString = $this->getColumn()->type()->getOutputTimezone()
?? Arr::get($options, 'timezone');

if ($timeZoneString) {
$value->shiftTimezone($timeZoneString);
}

return $value->startOfDay()->utc()->toDateTimeString();
}

return parent::getValue();
Expand Down
20 changes: 17 additions & 3 deletions src/BaseFeatures/Filters/LessThanFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace BluefynInternational\ReportEngine\BaseFeatures\Filters;

use Carbon\Carbon;
use Illuminate\Database\Query\Builder;
use Illuminate\Support\Arr;

class LessThanFilter extends BaseFilter
{
Expand All @@ -15,17 +17,29 @@ class LessThanFilter extends BaseFilter
public function apply(Builder $builder, array $options = []) : Builder
{
$action = $this->getAction();
$value = $this->getValue($options);

return $builder->$action($this->getField(), '<', $this->getValue());
return $builder->$action($this->getField(), '<', $value);
}

/**
* @return null|string
*/
public function getValue()
public function getValue(array $options = [])
{
if ($this->valueIsDate()) {
return parent::getValue()->startOfDay()->toDateTimeString();
/**
* @var Carbon $value
*/
$value = parent::getValue();
$timeZoneString = $this->getColumn()->type()->getOutputTimezone()
?? Arr::get($options, 'timezone');

if ($timeZoneString) {
$value->shiftTimezone($timeZoneString);
}

return $value->startOfDay()->utc()->toDateTimeString();
}

return parent::getValue();
Expand Down
20 changes: 17 additions & 3 deletions src/BaseFeatures/Filters/LessThanOrEqualFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

namespace BluefynInternational\ReportEngine\BaseFeatures\Filters;

use Carbon\Carbon;
use Illuminate\Database\Query\Builder;
use Illuminate\Support\Arr;

class LessThanOrEqualFilter extends BaseFilter
{
Expand All @@ -15,17 +17,29 @@ class LessThanOrEqualFilter extends BaseFilter
public function apply(Builder $builder, array $options = []) : Builder
{
$action = $this->getAction();
$value = $this->getValue($options);

return $builder->$action($this->getField(), '<=', $this->getValue());
return $builder->$action($this->getField(), '<=', $value);
}

/**
* @return null|string
*/
public function getValue()
public function getValue(array $options = [])
{
if ($this->valueIsDate()) {
return parent::getValue()->endOfDay()->toDateTimeString();
/**
* @var Carbon $value
*/
$value = parent::getValue();
$timeZoneString = $this->getColumn()->type()->getOutputTimezone()
?? Arr::get($options, 'timezone');

if ($timeZoneString) {
$value->shiftTimezone($timeZoneString);
}

return $value->endOfDay()->utc()->toDateTimeString();
}

return parent::getValue();
Expand Down
8 changes: 5 additions & 3 deletions src/BaseFeatures/Traits/Filterable.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,12 @@ trait Filterable
*/
protected function applyFilters(array $params, Builder $builder) : Builder
{
$options = $this->getMetaData();

$this->getAppliedfilters($params)
->each(function ($fields) use (&$builder) {
$fields->each(function ($filter) use (&$builder) {
$builder = $this->filter($filter, $builder);
->each(function ($fields) use ($options, &$builder) {
$fields->each(function ($filter) use ($options, &$builder) {
$builder = $this->filter($filter, $builder, $options);
});
});

Expand Down
7 changes: 7 additions & 0 deletions src/ReportBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -481,4 +481,11 @@ public static function rendersFilters(Collection $filterColumns, int $columnsWid

return $output;
}

public function getMetaData() : array
{
$encodedData = $this->currentRequest->get('metaData', '');

return json_decode(json: base64_decode($encodedData), associative: true) ?? [];
}
}

0 comments on commit 883e67a

Please sign in to comment.