Skip to content

Commit 883e67a

Browse files
authored
Merge pull request #19 from bluefyn-international/feature/improve-field-and-filter-timezone-support
Add initial metadata support, Improve support for client timezones
2 parents 8c39d41 + a11b65c commit 883e67a

File tree

11 files changed

+142
-23
lines changed

11 files changed

+142
-23
lines changed

resources/views/base-web.blade.php

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,19 @@ function popupConfirm(row, title, body, routeTemplate, routeReplacements, routeH
105105
})
106106
}
107107
108+
function addMetaDataToUrl(url)
109+
{
110+
let metaData = {
111+
timezone: Intl.DateTimeFormat().resolvedOptions().timeZone,
112+
}
113+
114+
let encodedMetaData = btoa(JSON.stringify(metaData))
115+
116+
url.searchParams.append('metaData', encodedMetaData)
117+
118+
return url
119+
}
120+
108121
(() => {
109122
$('#filterContainer .custom-select').change()
110123
@@ -177,8 +190,17 @@ function popupConfirm(row, title, body, routeTemplate, routeReplacements, routeH
177190
}
178191
})
179192
180-
history.replaceState(null, '', endpoint.replace('.json', '') + '?' + filterParams.toString());
181-
table.setData(endpoint + '?' + filterParams.toString());
193+
let viewReportUrl = endpoint.replace('.json', '') + '?' + filterParams.toString()
194+
let dataReportUrl = endpoint + '?' + filterParams.toString()
195+
196+
viewReportUrlObject = new URL(viewReportUrl)
197+
dataReportUrlObject = new URL(dataReportUrl)
198+
199+
viewReportUrlObject = addMetaDataToUrl(viewReportUrlObject)
200+
dataReportUrlObject = addMetaDataToUrl(dataReportUrlObject)
201+
202+
history.replaceState(null, '', viewReportUrlObject.toString());
203+
table.setData(dataReportUrlObject.toString());
182204
});
183205
184206
@if($autoloadInitialData) {

src/BaseFeatures/Data/Types/DateTime.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ class DateTime extends BaseType
2323

2424
protected string $outputFormat = 'L';
2525

26+
protected ?string $formatter = 'datetime';
27+
2628
public function __construct(
2729
string|null $outputFormat = null,
2830
string|null $placeholder = null,
@@ -55,6 +57,11 @@ public function setInputFormat(string|null $format) : self
5557
return $this;
5658
}
5759

60+
public function getOutputTimezone() : ?string
61+
{
62+
return $this->outputTzName;
63+
}
64+
5865
public function setOutputTimezone(string|null $timezone) : self
5966
{
6067
$this->outputTzName = $timezone;
@@ -64,7 +71,7 @@ public function setOutputTimezone(string|null $timezone) : self
6471

6572
public function formatter(): string
6673
{
67-
return 'datetime';
74+
return $this->formatter;
6875
}
6976

7077
public function formatterParams(): array

src/BaseFeatures/Filters/BaseFilter.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ abstract class BaseFilter implements Arrayable
2424
/**
2525
* BaseFilter constructor.
2626
*
27-
* @param Column $column
28-
* @param null $value
27+
* @param Column $column
28+
* @param mixed|null $value
2929
*/
30-
public function __construct(Column $column, $value = null)
30+
public function __construct(Column $column, mixed $value = null)
3131
{
3232
$this->setColumn($column);
3333
$this->setValue($value);

src/BaseFeatures/Filters/DoesNotEqualFilter.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
namespace BluefynInternational\ReportEngine\BaseFeatures\Filters;
44

5+
use Carbon\Carbon;
56
use Illuminate\Database\Query\Builder;
7+
use Illuminate\Support\Arr;
68

79
class DoesNotEqualFilter extends BaseFilter
810
{
@@ -45,4 +47,27 @@ public static function key(): string
4547
{
4648
return 'does_not_equal';
4749
}
50+
51+
/**
52+
* @return null|string|Carbon
53+
*/
54+
public function getValue(array $options = [])
55+
{
56+
if ($this->valueIsDate()) {
57+
/**
58+
* @var Carbon $value
59+
*/
60+
$value = parent::getValue();
61+
$timeZoneString = $this->getColumn()->type()->getOutputTimezone()
62+
?? Arr::get($options, 'timezone');
63+
64+
if ($timeZoneString) {
65+
$value->shiftTimezone($timeZoneString);
66+
}
67+
68+
return $value->utc();
69+
}
70+
71+
return parent::getValue();
72+
}
4873
}

src/BaseFeatures/Filters/EqualsFilter.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ public function apply(Builder $builder, array $options = []) : Builder
1717
if ($this->valueIsDate()) {
1818
$greaterThanEqual = new GreaterThanOrEqualFilter($this->getColumn(), $this->getValue());
1919
$lessThanEqual = new LessThanOrEqualFilter($this->getColumn(), $this->getValue());
20-
$builder = $greaterThanEqual->apply($builder);
20+
$builder = $greaterThanEqual->apply($builder, $options);
2121

22-
return $lessThanEqual->apply($builder);
22+
return $lessThanEqual->apply($builder, $options);
2323
}
2424

2525
$action = $this->getAction();

src/BaseFeatures/Filters/GreaterThanFilter.php

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
namespace BluefynInternational\ReportEngine\BaseFeatures\Filters;
44

5+
use Carbon\Carbon;
56
use Illuminate\Database\Query\Builder;
7+
use Illuminate\Support\Arr;
68

79
class GreaterThanFilter extends BaseFilter
810
{
@@ -15,17 +17,29 @@ class GreaterThanFilter extends BaseFilter
1517
public function apply(Builder $builder, array $options = []) : Builder
1618
{
1719
$action = $this->getAction();
20+
$value = $this->getValue($options);
1821

19-
return $builder->$action($this->getField(), '>', $this->getValue());
22+
return $builder->$action($this->getField(), '>', $value);
2023
}
2124

2225
/**
2326
* @return null|string|mixed
2427
*/
25-
public function getValue()
28+
public function getValue(array $options = [])
2629
{
2730
if ($this->valueIsDate()) {
28-
return parent::getValue()->endOfDay()->toDateTimeString();
31+
/**
32+
* @var Carbon $value
33+
*/
34+
$value = parent::getValue();
35+
$timeZoneString = $this->getColumn()->type()->getOutputTimezone()
36+
?? Arr::get($options, 'timezone');
37+
38+
if ($timeZoneString) {
39+
$value->shiftTimezone($timeZoneString);
40+
}
41+
42+
return $value->endOfDay()->utc()->toDateTimeString();
2943
}
3044

3145
return parent::getValue();

src/BaseFeatures/Filters/GreaterThanOrEqualFilter.php

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
namespace BluefynInternational\ReportEngine\BaseFeatures\Filters;
44

5+
use Carbon\Carbon;
56
use Illuminate\Database\Query\Builder;
7+
use Illuminate\Support\Arr;
68

79
class GreaterThanOrEqualFilter extends BaseFilter
810
{
@@ -15,17 +17,29 @@ class GreaterThanOrEqualFilter extends BaseFilter
1517
public function apply(Builder $builder, array $options = []) : Builder
1618
{
1719
$action = $this->getAction();
20+
$value = $this->getValue($options);
1821

19-
return $builder->$action($this->getField(), '>=', $this->getValue());
22+
return $builder->$action($this->getField(), '>=', $value);
2023
}
2124

2225
/**
2326
* @return null|string
2427
*/
25-
public function getValue()
28+
public function getValue(array $options = [])
2629
{
2730
if ($this->valueIsDate()) {
28-
return parent::getValue()->startOfDay()->toDateTimeString();
31+
/**
32+
* @var Carbon $value
33+
*/
34+
$value = parent::getValue();
35+
$timeZoneString = $this->getColumn()->type()->getOutputTimezone()
36+
?? Arr::get($options, 'timezone');
37+
38+
if ($timeZoneString) {
39+
$value->shiftTimezone($timeZoneString);
40+
}
41+
42+
return $value->startOfDay()->utc()->toDateTimeString();
2943
}
3044

3145
return parent::getValue();

src/BaseFeatures/Filters/LessThanFilter.php

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
namespace BluefynInternational\ReportEngine\BaseFeatures\Filters;
44

5+
use Carbon\Carbon;
56
use Illuminate\Database\Query\Builder;
7+
use Illuminate\Support\Arr;
68

79
class LessThanFilter extends BaseFilter
810
{
@@ -15,17 +17,29 @@ class LessThanFilter extends BaseFilter
1517
public function apply(Builder $builder, array $options = []) : Builder
1618
{
1719
$action = $this->getAction();
20+
$value = $this->getValue($options);
1821

19-
return $builder->$action($this->getField(), '<', $this->getValue());
22+
return $builder->$action($this->getField(), '<', $value);
2023
}
2124

2225
/**
2326
* @return null|string
2427
*/
25-
public function getValue()
28+
public function getValue(array $options = [])
2629
{
2730
if ($this->valueIsDate()) {
28-
return parent::getValue()->startOfDay()->toDateTimeString();
31+
/**
32+
* @var Carbon $value
33+
*/
34+
$value = parent::getValue();
35+
$timeZoneString = $this->getColumn()->type()->getOutputTimezone()
36+
?? Arr::get($options, 'timezone');
37+
38+
if ($timeZoneString) {
39+
$value->shiftTimezone($timeZoneString);
40+
}
41+
42+
return $value->startOfDay()->utc()->toDateTimeString();
2943
}
3044

3145
return parent::getValue();

src/BaseFeatures/Filters/LessThanOrEqualFilter.php

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
namespace BluefynInternational\ReportEngine\BaseFeatures\Filters;
44

5+
use Carbon\Carbon;
56
use Illuminate\Database\Query\Builder;
7+
use Illuminate\Support\Arr;
68

79
class LessThanOrEqualFilter extends BaseFilter
810
{
@@ -15,17 +17,29 @@ class LessThanOrEqualFilter extends BaseFilter
1517
public function apply(Builder $builder, array $options = []) : Builder
1618
{
1719
$action = $this->getAction();
20+
$value = $this->getValue($options);
1821

19-
return $builder->$action($this->getField(), '<=', $this->getValue());
22+
return $builder->$action($this->getField(), '<=', $value);
2023
}
2124

2225
/**
2326
* @return null|string
2427
*/
25-
public function getValue()
28+
public function getValue(array $options = [])
2629
{
2730
if ($this->valueIsDate()) {
28-
return parent::getValue()->endOfDay()->toDateTimeString();
31+
/**
32+
* @var Carbon $value
33+
*/
34+
$value = parent::getValue();
35+
$timeZoneString = $this->getColumn()->type()->getOutputTimezone()
36+
?? Arr::get($options, 'timezone');
37+
38+
if ($timeZoneString) {
39+
$value->shiftTimezone($timeZoneString);
40+
}
41+
42+
return $value->endOfDay()->utc()->toDateTimeString();
2943
}
3044

3145
return parent::getValue();

src/BaseFeatures/Traits/Filterable.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,12 @@ trait Filterable
2121
*/
2222
protected function applyFilters(array $params, Builder $builder) : Builder
2323
{
24+
$options = $this->getMetaData();
25+
2426
$this->getAppliedfilters($params)
25-
->each(function ($fields) use (&$builder) {
26-
$fields->each(function ($filter) use (&$builder) {
27-
$builder = $this->filter($filter, $builder);
27+
->each(function ($fields) use ($options, &$builder) {
28+
$fields->each(function ($filter) use ($options, &$builder) {
29+
$builder = $this->filter($filter, $builder, $options);
2830
});
2931
});
3032

0 commit comments

Comments
 (0)