Skip to content

Commit a3f9d9e

Browse files
Fix v6 issues (#143)
Co-authored-by: Marco Rieser <marco@rieser.dev>
1 parent a97a961 commit a3f9d9e

20 files changed

Lines changed: 437 additions & 409 deletions

resources/blueprints/settings.yaml

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -7,29 +7,17 @@ tabs:
77
-
88
handle: collections
99
field:
10-
type: grid
10+
type: collections
1111
display: Collections
12-
sortable: false
13-
add_row: 'Add Collection'
14-
full_width_setting: true
15-
fields:
16-
-
17-
handle: collection
18-
field:
19-
type: collections
20-
display: Collection
21-
width: 50
22-
mode: select
23-
max_items: 1
24-
default:
25-
-
26-
collection: events
12+
width: 50
13+
mode: select
14+
default: events
2715
-
2816
handle: timezone
2917
field:
30-
mode: select
18+
dictionary: timezones
3119
max_items: 1
32-
type: timezones
20+
type: dictionary
3321
display: Timezone
34-
full_width_setting: true
35-
default: 'UTC'
22+
default: computed:default-events-timezone
23+
width: 50

resources/fieldsets/event.yaml

Lines changed: 45 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,18 @@ fields:
1111
monthly: Monthly
1212
every: Every
1313
multi_day: Multi-Day
14-
width: 33
14+
width: 50
1515
display: Recurrence
1616
default: none
1717
-
1818
handle: timezone
1919
field:
2020
dictionary: timezones
2121
max_items: 1
22-
default: UTC
2322
type: dictionary
2423
display: Timezone
25-
-
26-
handle: all_day
27-
field:
28-
type: toggle
29-
width: 33
30-
display: 'All Day?'
31-
unless:
32-
recurrence: 'equals multi_day'
24+
default: computed:default-event-timezone
25+
width: 50
3326
-
3427
handle: specific_days
3528
field:
@@ -75,6 +68,13 @@ fields:
7568
multi_day: 'equals true'
7669
recurrence: 'equals multi_day'
7770
format: Y-m-d
71+
-
72+
handle: end_date_spacer
73+
field:
74+
type: spacer
75+
width: 33
76+
if:
77+
recurrence: 'equals none'
7878
-
7979
handle: end_date
8080
field:
@@ -91,11 +91,44 @@ fields:
9191
if:
9292
recurrence: 'contains_any daily, weekly, monthly, every'
9393
format: Y-m-d
94+
-
95+
handle: exclude_dates
96+
field:
97+
type: grid
98+
fullscreen: false
99+
display: 'Exclude Days'
100+
add_row: 'Add Day'
101+
if_any:
102+
recurrence: 'contains_any monthly, daily, weekly, every'
103+
fields:
104+
-
105+
handle: date
106+
field:
107+
type: date
108+
allow_blank: false
109+
allow_time: false
110+
require_time: false
111+
input_format: YYYY/M/D/YYYY
112+
display: Date
113+
format: Y-m-d
114+
-
115+
handle: times_sections
116+
field:
117+
type: section
118+
display: Times
119+
-
120+
handle: all_day
121+
field:
122+
type: toggle
123+
width: 33
124+
display: 'All Day?'
125+
unless:
126+
recurrence: 'equals multi_day'
94127
-
95128
handle: start_time
96129
field:
97130
type: time
98-
width: 25
131+
width: 33
99132
display: 'Start Time'
100133
instructions: 'Input in [24-hour format](https://en.wikipedia.org/wiki/24-hour_clock)'
101134
unless_any:
@@ -106,7 +139,7 @@ fields:
106139
handle: end_time
107140
field:
108141
type: time
109-
width: 25
142+
width: 33
110143
display: 'End Time'
111144
instructions: 'Input in [24-hour format](https://en.wikipedia.org/wiki/24-hour_clock)'
112145
unless_any:
@@ -170,22 +203,3 @@ fields:
170203
field: 'events::event.all_day'
171204
config:
172205
width: 25
173-
-
174-
handle: exclude_dates
175-
field:
176-
type: grid
177-
display: 'Exclude Days'
178-
add_row: 'Add Day'
179-
if_any:
180-
recurrence: 'contains_any monthly, daily, weekly, every'
181-
fields:
182-
-
183-
handle: date
184-
field:
185-
type: date
186-
allow_blank: false
187-
allow_time: false
188-
require_time: false
189-
input_format: YYYY/M/D/YYYY
190-
display: Date
191-
format: Y-m-d

src/Events.php

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use Carbon\CarbonInterface;
66
use Exception;
7-
use Illuminate\Support\Collection;
87
use Illuminate\Support\Traits\Conditionable;
98
use Statamic\Entries\Entry;
109
use Statamic\Entries\EntryCollection;
@@ -45,6 +44,14 @@ class Events
4544

4645
private array $terms = [];
4746

47+
private ?string $timezone = null;
48+
49+
public static function defaultTimezone(): string
50+
{
51+
// return static::setting('timezone', config('statamic.system.display_timezone') ?? config('app.timezone', 'UTC'));
52+
return static::setting('timezone');
53+
}
54+
4855
public static function fromCollection(string $handle): self
4956
{
5057
return tap(new static)->collection($handle);
@@ -55,21 +62,11 @@ public static function fromEntry(string $id): self
5562
return tap(new static)->event($id);
5663
}
5764

58-
public static function collectionHandles(): Collection
59-
{
60-
return collect(static::setting('collections', ['events']))->keys();
61-
}
62-
6365
public static function setting(string $key, $default = null): mixed
6466
{
6567
return Addon::get('transformstudios/events')->settings()->get($key, $default);
6668
}
6769

68-
public static function timezone(): string
69-
{
70-
return static::setting('timezone', config('app.timezone'));
71-
}
72-
7370
private function __construct() {}
7471

7572
public function collapseMultiDays(): self
@@ -145,6 +142,13 @@ public function terms(string|array $terms): self
145142
return $this;
146143
}
147144

145+
public function timezone(string $timezone): self
146+
{
147+
$this->timezone = $timezone;
148+
149+
return $this;
150+
}
151+
148152
public function between(string|CarbonInterface $from, string|CarbonInterface $to): EntryCollection|LengthAwarePaginator
149153
{
150154
return $this->output(
@@ -163,6 +167,18 @@ private function output(callable $type): EntryCollection|LengthAwarePaginator
163167
{
164168
$occurrences = $this->entries()->occurrences(generator: $type);
165169

170+
if (! is_null($this->timezone)) {
171+
$occurrences->transform(function (Entry $occurrence) {
172+
$start = $occurrence->start->setTimezone($this->timezone);
173+
$end = $occurrence->end->setTimezone($this->timezone);
174+
175+
return $occurrence
176+
->setSupplement('start', $start)
177+
->setSupplement('end', $end)
178+
->setSupplement('spansDay', ! $start->isSameDay($end));
179+
});
180+
}
181+
166182
if ($this->offset) {
167183
$occurrences = $occurrences->slice(offset: $this->offset);
168184
}
@@ -200,7 +216,7 @@ private function isMultiDay(Entry $occurrence): bool
200216
private function occurrences(callable $generator): EntryCollection
201217
{
202218
return $this->entries
203-
->filter(fn (Entry $occurrence) => $this->hasStartDate($occurrence))
219+
->filter(fn (Entry $event) => $this->hasStartDate($event))
204220
// take each event and generate the occurrences
205221
->flatMap(callback: $generator)
206222
->reject(fn (Entry $occurrence) => collect($occurrence->exclude_dates)

src/Modifiers/IsEndOfWeek.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,26 @@
33
namespace TransformStudios\Events\Modifiers;
44

55
use Carbon\CarbonImmutable;
6+
use Statamic\Facades\Site;
67
use Statamic\Modifiers\Modifier;
78

89
class IsEndOfWeek extends Modifier
910
{
1011
public function index($value, $params, $context)
1112
{
13+
/*
14+
have to do this because Statamic sets the Carbon locale
15+
to the `lang` of the site, instead of the `locale`
16+
*/
17+
$currentLocale = CarbonImmutable::getLocale();
18+
CarbonImmutable::setLocale(Site::current()->locale());
19+
1220
$date = CarbonImmutable::parse($value);
1321

14-
$date->isSameDay($date->locale(CarbonImmutable::getLocale())->startOfWeek());
22+
$isStartOfWeek = $date->dayOfWeek == now()->endOfWeek()->dayOfWeek;
23+
24+
CarbonImmutable::setLocale($currentLocale);
1525

16-
return $date->dayOfWeek == now()->endOfWeek()->dayOfWeek;
26+
return $isStartOfWeek;
1727
}
1828
}

src/Modifiers/IsStartOfWeek.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,26 @@
33
namespace TransformStudios\Events\Modifiers;
44

55
use Carbon\CarbonImmutable;
6+
use Statamic\Facades\Site;
67
use Statamic\Modifiers\Modifier;
78

89
class IsStartOfWeek extends Modifier
910
{
1011
public function index($value, $params, $context)
1112
{
13+
/*
14+
have to do this because Statamic sets the Carbon locale
15+
to the `lang` of the site, instead of the `locale`
16+
*/
17+
$currentLocale = CarbonImmutable::getLocale();
18+
CarbonImmutable::setLocale(Site::current()->locale());
19+
1220
$date = CarbonImmutable::parse($value);
1321

14-
$date->isSameDay($date->locale(CarbonImmutable::getLocale())->startOfWeek());
22+
$isStartOfWeek = $date->dayOfWeek == now()->startOfWeek()->dayOfWeek;
23+
24+
CarbonImmutable::setLocale($currentLocale);
1525

16-
return $date->dayOfWeek == now()->startOfWeek()->dayOfWeek;
26+
return $isStartOfWeek;
1727
}
1828
}

src/ServiceProvider.php

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,28 @@
22

33
namespace TransformStudios\Events;
44

5-
use Statamic\Entries\Entry;
65
use Statamic\Facades\Collection;
7-
use Statamic\Fields\Field;
8-
use Statamic\Fields\Value;
9-
use Statamic\Fieldtypes\Dictionary;
6+
use Statamic\Facades\Field;
107
use Statamic\Providers\AddonServiceProvider;
8+
use Statamic\Statamic;
119

1210
class ServiceProvider extends AddonServiceProvider
1311
{
1412
public function bootAddon()
1513
{
16-
collect(Events::setting('collections', [['collection' => 'events']]))
17-
->each(fn (array $collection) => Collection::computed(
18-
$collection['collection'],
19-
'timezone',
20-
$this->timezone(...)
21-
));
22-
}
23-
24-
private function timezone(Entry $entry, $value): string|Value
25-
{
26-
$value ??= Events::timezone();
27-
28-
if ($entry->blueprint()->fields()->get('timezone')?->fieldtype() instanceof Dictionary) {
29-
return $value;
30-
}
14+
Field::computedDefault('default-events-timezone', fn () => Statamic::displayTimezone());
15+
Field::computedDefault('default-event-timezone', fn () => Events::defaultTimezone());
3116

32-
return (new Field('timezone', ['type' => 'timezones', 'max_items' => 1]))
33-
->setValue($value)
34-
->setParent($entry)
35-
->augment()
36-
->value();
17+
collect(Events::setting('collections', ['events']))
18+
->each(function (string $collection) {
19+
Collection::findByHandle($collection)?->entryBlueprint()->ensureField(
20+
'timezone',
21+
[
22+
'dictionary' => 'timezones',
23+
'max_items' => '1',
24+
'type' => 'dictionary',
25+
'default' => 'computed:default-event-timezone',
26+
]);
27+
});
3728
}
3829
}

0 commit comments

Comments
 (0)