44
55use Carbon \CarbonInterface ;
66use Exception ;
7- use Illuminate \Support \Collection ;
87use Illuminate \Support \Traits \Conditionable ;
98use Statamic \Entries \Entry ;
109use 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 )
0 commit comments