Skip to content

Commit 03eec2e

Browse files
authored
Use addon settings (#132)
1 parent d6af929 commit 03eec2e

File tree

8 files changed

+177
-41
lines changed

8 files changed

+177
-41
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"rlanvin/php-rrule": "^2.3.1",
1616
"spatie/calendar-links": "^1.0",
1717
"spatie/icalendar-generator": "^2.3.3",
18-
"statamic/cms": "6.0.0-alpha.4"
18+
"statamic/cms": "^6.0.0-alpha.14"
1919
},
2020
"require-dev": {
2121
"orchestra/testbench": "^9.2 || ^10.0",

resources/blueprints/config.yaml

Lines changed: 0 additions & 18 deletions
This file was deleted.

resources/blueprints/settings.yaml

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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+
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+
-
25+
handle: location_field
26+
field:
27+
type: text
28+
display: 'Location Field'
29+
width: 50
30+
default: location
31+
default:
32+
-
33+
collection: events
34+
location_field: location
35+
-
36+
handle: timezone
37+
field:
38+
mode: select
39+
max_items: 1
40+
type: timezones
41+
display: Timezone
42+
full_width_setting: true
43+
default: 'UTC'

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')->settings()->get($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: 27 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', [['collection' => 'events']]))
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,19 @@ private function publishConfig(): self
9794

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

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(
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<?php
2+
3+
namespace TransformStudios\Events\UpdateScripts;
4+
5+
use Illuminate\Support\Fluent;
6+
use Illuminate\Support\Str;
7+
use Statamic\Addons\Addon;
8+
use Statamic\Addons\Settings;
9+
use Statamic\Facades\Addon as AddonFacade;
10+
use Statamic\Support\Arr;
11+
use Statamic\UpdateScripts\UpdateScript;
12+
13+
class ConvertConfigToSettings extends UpdateScript
14+
{
15+
private Addon $addon;
16+
17+
public function shouldUpdate($newVersion, $oldVersion)
18+
{
19+
return $this->isUpdatingTo('6.0');
20+
}
21+
22+
public function update()
23+
{
24+
$this->addon = AddonFacade::get('transformstudios/events');
25+
26+
if (is_null($settings = $this->settingsFromConfig())) {
27+
return;
28+
}
29+
30+
if (! $settings->save()) {
31+
$this->console()->error('Failed to save events settings. Please check your logs for details.');
32+
33+
return;
34+
}
35+
36+
$this->console()->info('Converted events config to settings.');
37+
38+
$this->removeConfig();
39+
}
40+
41+
private function removeConfig(): void
42+
{
43+
if ($this->files->exists($configPath = config_path('events.php'))) {
44+
$this->files->delete($configPath);
45+
$this->console()->info('Removed old events config file.');
46+
}
47+
}
48+
49+
private function settingsFromConfig(): ?Settings
50+
{
51+
$config = Fluent::make($this->addon->config());
52+
53+
if ($config->isEmpty()) {
54+
return null;
55+
}
56+
57+
$collections = collect([$config->collection => null])
58+
->merge($config->collections)
59+
->map(function (array|string $collection, $handle) {
60+
if (is_string($collection)) {
61+
return [
62+
'id' => Str::random(8),
63+
'collection' => $collection,
64+
];
65+
}
66+
67+
$collectionSetting = [
68+
'id' => Str::random(8),
69+
'collection' => $handle,
70+
'location_field' => Arr::get($collection, 'location_field', 'location'),
71+
];
72+
73+
return Arr::removeNullValues($collectionSetting);
74+
})->reject(fn (array $collection) => $collection['collection'] == 'events' && is_null(Arr::get($collection, 'location_field')))
75+
->values()
76+
->all();
77+
78+
$timezone = $config->timezone;
79+
80+
return $this->addon
81+
->settings()
82+
->set(Arr::removeNullValues(compact('collections', 'timezone')));
83+
}
84+
}

0 commit comments

Comments
 (0)