Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"rlanvin/php-rrule": "^2.3.1",
"spatie/calendar-links": "^1.0",
"spatie/icalendar-generator": "^2.3.3",
"statamic/cms": "6.0.0-alpha.4"
"statamic/cms": "^6.0.0-alpha.14"
},
"require-dev": {
"orchestra/testbench": "^9.2 || ^10.0",
Expand Down
18 changes: 0 additions & 18 deletions resources/blueprints/config.yaml

This file was deleted.

43 changes: 43 additions & 0 deletions resources/blueprints/settings.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
tabs:
main:
display: Main
sections:
-
fields:
-
handle: collections
field:
type: grid
display: Collections
sortable: false
add_row: 'Add Collection'
full_width_setting: true
fields:
-
handle: collection
field:
type: collections
display: Collection
width: 50
mode: select
max_items: 1
-
handle: location_field
field:
type: text
display: 'Location Field'
width: 50
default: location
default:
-
collection: events
location_field: location
-
handle: timezone
field:
mode: select
max_items: 1
type: timezones
display: Timezone
full_width_setting: true
default: 'UTC'
21 changes: 18 additions & 3 deletions src/Events.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@

use Carbon\CarbonInterface;
use Exception;
use Illuminate\Support\Collection;
use Illuminate\Support\Traits\Conditionable;
use Statamic\Entries\Entry;
use Statamic\Entries\EntryCollection;
use Statamic\Extensions\Pagination\LengthAwarePaginator;
use Statamic\Facades\Addon;
use Statamic\Facades\Cascade;
use Statamic\Facades\Entry as EntryFacade;
use Statamic\Facades\Site;
Expand Down Expand Up @@ -45,18 +47,31 @@ class Events

public static function fromCollection(string $handle): self
{
return tap(new static())->collection($handle);
return tap(new static)->collection($handle);
}

public static function fromEntry(string $id): self
{
return tap(new static())->event($id);
return tap(new static)->event($id);
}

private function __construct()
public static function collectionHandles(): Collection
{
return collect(static::setting('collections', ['events']))->keys();
}

public static function setting(string $key, $default = null): mixed
{
return Addon::get('transformstudios/events')->settings()->get($key, $default);
}

public static function timezone(): string
{
return static::setting('timezone', config('app.timezone'));
}

private function __construct() {}

public function collapseMultiDays(): self
{
$this->collapseMultiDays = true;
Expand Down
2 changes: 1 addition & 1 deletion src/Http/Controllers/IcsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class IcsController extends Controller

public function __invoke(Request $request)
{
$handle = $request->get('collection', config('events.collection', 'events'));
$handle = $request->get('collection', 'events');
$date = $request->has('date') ? CarbonImmutable::parse($request->get('date')) : null;
$eventId = $request->get('event');
$entry = null;
Expand Down
42 changes: 27 additions & 15 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,18 @@
use Composer\InstalledVersions;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Artisan;
use Statamic\Entries\Entry;
use Statamic\Facades\Collection;
use Statamic\Facades\Site;
use Statamic\Fields\Field;
use Statamic\Fields\Value;
use Statamic\Providers\AddonServiceProvider;
use Statamic\Statamic;
use TransformStudios\Events\Fieldtypes\Timezones;
use TransformStudios\Events\Modifiers\InMonth;
use TransformStudios\Events\Modifiers\IsEndOfWeek;
use TransformStudios\Events\Modifiers\IsStartOfWeek;
use TransformStudios\Events\Tags\Events;
use TransformStudios\Events\Tags\Events as EventsTag;

class ServiceProvider extends AddonServiceProvider
{
Expand All @@ -33,7 +35,7 @@ class ServiceProvider extends AddonServiceProvider
];

protected $tags = [
Events::class,
EventsTag::class,
];

public function bootAddon()
Expand Down Expand Up @@ -72,23 +74,18 @@ private function bootCarbon(): self

private function bootFields(): self
{
Collection::computed(config('events.collection', 'events'), 'timezone', function ($entry, $value) {
$value ??= config('events.timezone', config('app.timezone'));

if ($entry->blueprint()->fields()->get('timezone')?->fieldtype() instanceof Timezones) {
return $value;
}

return (new Field('timezone', ['type' => 'timezones', 'max_items' => 1]))
->setValue($value)
->setParent($entry)
->augment()
->value();
});
collect(Events::setting('collections', [['collection' => 'events']]))
->each(fn (array $collection) => $this
->defineComputedTimezoneField($collection['collection']));

return $this;
}

private function defineComputedTimezoneField(string $handle): void
{
Collection::computed($handle, 'timezone', $this->timezone(...));
}

private function publishConfig(): self
{
Statamic::afterInstalled(function ($command) {
Expand All @@ -97,4 +94,19 @@ private function publishConfig(): self

return $this;
}

private function timezone(Entry $entry, $value): string|Value
{
$value ??= Events::timezone();

if ($entry->blueprint()->fields()->get('timezone')?->fieldtype() instanceof Timezones) {
return $value;
}

return (new Field('timezone', ['type' => 'timezones', 'max_items' => 1]))
->setValue($value)
->setParent($entry)
->augment()
->value();
}
}
6 changes: 3 additions & 3 deletions src/Tags/Events.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public function downloadLink(): string
return route(
'statamic.events.ics.show',
Arr::removeNullValues([
'collection' => $this->params->get('collection', config('events.collection', 'events')),
'collection' => $this->params->get('collection', 'events'),
'date' => $this->params->has('date') ? Carbon::parse($this->params->get('date'))->toDateString() : null,
'event' => $this->params->get('event'),
])
Expand Down Expand Up @@ -129,7 +129,7 @@ private function generator(): Generator
{
$generator = $this->params->has('event') ?
Generator::fromEntry($this->params->get('event')) :
Generator::fromCollection($this->params->get('collection', config('events.collection', 'events')));
Generator::fromCollection($this->params->get('collection', 'events'));

return $generator
->site($this->params->get('site'))
Expand All @@ -140,7 +140,7 @@ private function generator(): Generator
)->when(
value: $this->parseFilters(),
callback: fn (Generator $generator, array $filters) => $generator->filters(filters: $filters)
)-> when(
)->when(
value: $this->params->int('offset'),
callback: fn (Generator $generator, int $offset) => $generator->offset(offset: $offset)
)->when(
Expand Down
84 changes: 84 additions & 0 deletions src/UpdateScripts/ConvertConfigToSettings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php

namespace TransformStudios\Events\UpdateScripts;

use Illuminate\Support\Fluent;
use Illuminate\Support\Str;
use Statamic\Addons\Addon;
use Statamic\Addons\Settings;
use Statamic\Facades\Addon as AddonFacade;
use Statamic\Support\Arr;
use Statamic\UpdateScripts\UpdateScript;

class ConvertConfigToSettings extends UpdateScript
{
private Addon $addon;

public function shouldUpdate($newVersion, $oldVersion)
{
return $this->isUpdatingTo('6.0');
}

public function update()
{
$this->addon = AddonFacade::get('transformstudios/events');

if (is_null($settings = $this->settingsFromConfig())) {
return;
}

if (! $settings->save()) {
$this->console()->error('Failed to save events settings. Please check your logs for details.');

return;
}

$this->console()->info('Converted events config to settings.');

$this->removeConfig();
}

private function removeConfig(): void
{
if ($this->files->exists($configPath = config_path('events.php'))) {
$this->files->delete($configPath);
$this->console()->info('Removed old events config file.');
}
}

private function settingsFromConfig(): ?Settings
{
$config = Fluent::make($this->addon->config());

if ($config->isEmpty()) {
return null;
}

$collections = collect([$config->collection => null])
->merge($config->collections)
->map(function (array|string $collection, $handle) {
if (is_string($collection)) {
return [
'id' => Str::random(8),
'collection' => $collection,
];
}

$collectionSetting = [
'id' => Str::random(8),
'collection' => $handle,
'location_field' => Arr::get($collection, 'location_field', 'location'),
];

return Arr::removeNullValues($collectionSetting);
})->reject(fn (array $collection) => $collection['collection'] == 'events' && is_null(Arr::get($collection, 'location_field')))
->values()
->all();

$timezone = $config->timezone;

return $this->addon
->settings()
->set(Arr::removeNullValues(compact('collections', 'timezone')));
}
}