Skip to content

Commit 1f96aa8

Browse files
committed
wip
1 parent d6af929 commit 1f96aa8

5 files changed

Lines changed: 86 additions & 22 deletions

File tree

resources/blueprints/settings.yaml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
tabs:
2+
main:
3+
display: Main
4+
sections:
5+
-
6+
fields:
7+
-
8+
handle: collections
9+
field:
10+
type: grid
11+
display: Collections
12+
sortable: false
13+
add_row: 'Add Collection'
14+
fields:
15+
-
16+
handle: collection
17+
field:
18+
type: collections
19+
display: Collection
20+
width: 50
21+
mode: select
22+
max_items: 1
23+
-
24+
handle: location_field
25+
field:
26+
type: text
27+
display: 'Location Field'
28+
width: 50
29+
default: location
30+
-
31+
handle: timezone
32+
field:
33+
mode: select
34+
max_items: 1
35+
type: timezones
36+
display: Timezone

src/Events.php

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,12 @@
44

55
use Carbon\CarbonInterface;
66
use Exception;
7+
use Illuminate\Support\Collection;
78
use Illuminate\Support\Traits\Conditionable;
89
use Statamic\Entries\Entry;
910
use Statamic\Entries\EntryCollection;
1011
use Statamic\Extensions\Pagination\LengthAwarePaginator;
12+
use Statamic\Facades\Addon;
1113
use Statamic\Facades\Cascade;
1214
use Statamic\Facades\Entry as EntryFacade;
1315
use Statamic\Facades\Site;
@@ -45,18 +47,31 @@ class Events
4547

4648
public static function fromCollection(string $handle): self
4749
{
48-
return tap(new static())->collection($handle);
50+
return tap(new static)->collection($handle);
4951
}
5052

5153
public static function fromEntry(string $id): self
5254
{
53-
return tap(new static())->event($id);
55+
return tap(new static)->event($id);
5456
}
5557

56-
private function __construct()
58+
public static function collectionHandles(): Collection
5759
{
60+
return collect(static::setting('collections', ['events']))->keys();
5861
}
5962

63+
public static function setting(string $key, $default = null): mixed
64+
{
65+
return Addon::get('transformstudios/events')->setting($key, $default);
66+
}
67+
68+
public static function timezone(): string
69+
{
70+
return static::setting('timezone', config('app.timezone'));
71+
}
72+
73+
private function __construct() {}
74+
6075
public function collapseMultiDays(): self
6176
{
6277
$this->collapseMultiDays = true;

src/Http/Controllers/IcsController.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class IcsController extends Controller
2525

2626
public function __invoke(Request $request)
2727
{
28-
$handle = $request->get('collection', config('events.collection', 'events'));
28+
$handle = $request->get('collection', 'events');
2929
$date = $request->has('date') ? CarbonImmutable::parse($request->get('date')) : null;
3030
$eventId = $request->get('event');
3131
$entry = null;

src/ServiceProvider.php

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,18 @@
55
use Composer\InstalledVersions;
66
use Illuminate\Support\Carbon;
77
use Illuminate\Support\Facades\Artisan;
8+
use Statamic\Entries\Entry;
89
use Statamic\Facades\Collection;
910
use Statamic\Facades\Site;
1011
use Statamic\Fields\Field;
12+
use Statamic\Fields\Value;
1113
use Statamic\Providers\AddonServiceProvider;
1214
use Statamic\Statamic;
1315
use TransformStudios\Events\Fieldtypes\Timezones;
1416
use TransformStudios\Events\Modifiers\InMonth;
1517
use TransformStudios\Events\Modifiers\IsEndOfWeek;
1618
use TransformStudios\Events\Modifiers\IsStartOfWeek;
17-
use TransformStudios\Events\Tags\Events;
19+
use TransformStudios\Events\Tags\Events as EventsTag;
1820

1921
class ServiceProvider extends AddonServiceProvider
2022
{
@@ -33,7 +35,7 @@ class ServiceProvider extends AddonServiceProvider
3335
];
3436

3537
protected $tags = [
36-
Events::class,
38+
EventsTag::class,
3739
];
3840

3941
public function bootAddon()
@@ -72,23 +74,18 @@ private function bootCarbon(): self
7274

7375
private function bootFields(): self
7476
{
75-
Collection::computed(config('events.collection', 'events'), 'timezone', function ($entry, $value) {
76-
$value ??= config('events.timezone', config('app.timezone'));
77-
78-
if ($entry->blueprint()->fields()->get('timezone')?->fieldtype() instanceof Timezones) {
79-
return $value;
80-
}
81-
82-
return (new Field('timezone', ['type' => 'timezones', 'max_items' => 1]))
83-
->setValue($value)
84-
->setParent($entry)
85-
->augment()
86-
->value();
87-
});
77+
collect(Events::setting('collections'))
78+
->each(fn (array $collection) => $this
79+
->defineComputedTimezoneField($collection['collection']));
8880

8981
return $this;
9082
}
9183

84+
private function defineComputedTimezoneField(string $handle): void
85+
{
86+
Collection::computed($handle, 'timezone', $this->timezone(...));
87+
}
88+
9289
private function publishConfig(): self
9390
{
9491
Statamic::afterInstalled(function ($command) {
@@ -97,4 +94,20 @@ private function publishConfig(): self
9794

9895
return $this;
9996
}
97+
98+
private function timezone(Entry $entry, $value): string|Value
99+
{
100+
ray('hi');
101+
$value ??= Events::timezone();
102+
103+
if ($entry->blueprint()->fields()->get('timezone')?->fieldtype() instanceof Timezones) {
104+
return $value;
105+
}
106+
107+
return (new Field('timezone', ['type' => 'timezones', 'max_items' => 1]))
108+
->setValue($value)
109+
->setParent($entry)
110+
->augment()
111+
->value();
112+
}
100113
}

src/Tags/Events.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public function downloadLink(): string
5151
return route(
5252
'statamic.events.ics.show',
5353
Arr::removeNullValues([
54-
'collection' => $this->params->get('collection', config('events.collection', 'events')),
54+
'collection' => $this->params->get('collection', 'events'),
5555
'date' => $this->params->has('date') ? Carbon::parse($this->params->get('date'))->toDateString() : null,
5656
'event' => $this->params->get('event'),
5757
])
@@ -129,7 +129,7 @@ private function generator(): Generator
129129
{
130130
$generator = $this->params->has('event') ?
131131
Generator::fromEntry($this->params->get('event')) :
132-
Generator::fromCollection($this->params->get('collection', config('events.collection', 'events')));
132+
Generator::fromCollection($this->params->get('collection', 'events'));
133133

134134
return $generator
135135
->site($this->params->get('site'))
@@ -140,7 +140,7 @@ private function generator(): Generator
140140
)->when(
141141
value: $this->parseFilters(),
142142
callback: fn (Generator $generator, array $filters) => $generator->filters(filters: $filters)
143-
)-> when(
143+
)->when(
144144
value: $this->params->int('offset'),
145145
callback: fn (Generator $generator, int $offset) => $generator->offset(offset: $offset)
146146
)->when(

0 commit comments

Comments
 (0)