Skip to content
Open
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
6 changes: 3 additions & 3 deletions resources/blueprints/settings.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ tabs:
-
handle: timezone
field:
mode: select
dictionary: timezones
max_items: 1
type: timezones
default: UTC
type: dictionary
display: Timezone
full_width_setting: true
default: 'UTC'
74 changes: 44 additions & 30 deletions resources/fieldsets/event.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ fields:
monthly: Monthly
every: Every
multi_day: Multi-Day
width: 33
width: 50
display: Recurrence
default: none
-
Expand All @@ -22,14 +22,7 @@ fields:
default: UTC
type: dictionary
display: Timezone
-
handle: all_day
field:
type: toggle
width: 33
display: 'All Day?'
unless:
recurrence: 'equals multi_day'
width: 50
-
handle: specific_days
field:
Expand Down Expand Up @@ -75,6 +68,13 @@ fields:
multi_day: 'equals true'
recurrence: 'equals multi_day'
format: Y-m-d
-
handle: end_date_spacer
field:
type: spacer
width: 33
if:
recurrence: 'equals none'
-
handle: end_date
field:
Expand All @@ -91,11 +91,44 @@ fields:
if:
recurrence: 'contains_any daily, weekly, monthly, every'
format: Y-m-d
-
handle: exclude_dates
field:
type: grid
fullscreen: false
display: 'Exclude Days'
add_row: 'Add Day'
if_any:
recurrence: 'contains_any monthly, daily, weekly, every'
fields:
-
handle: date
field:
type: date
allow_blank: false
allow_time: false
require_time: false
input_format: YYYY/M/D/YYYY
display: Date
format: Y-m-d
-
handle: times_sections
field:
type: section
display: Times
-
handle: all_day
field:
type: toggle
width: 33
display: 'All Day?'
unless:
recurrence: 'equals multi_day'
-
handle: start_time
field:
type: time
width: 25
width: 33
display: 'Start Time'
instructions: 'Input in [24-hour format](https://en.wikipedia.org/wiki/24-hour_clock)'
unless_any:
Expand All @@ -106,7 +139,7 @@ fields:
handle: end_time
field:
type: time
width: 25
width: 33
display: 'End Time'
instructions: 'Input in [24-hour format](https://en.wikipedia.org/wiki/24-hour_clock)'
unless_any:
Expand Down Expand Up @@ -170,22 +203,3 @@ fields:
field: 'events::event.all_day'
config:
width: 25
-
handle: exclude_dates
field:
type: grid
display: 'Exclude Days'
add_row: 'Add Day'
if_any:
recurrence: 'contains_any monthly, daily, weekly, every'
fields:
-
handle: date
field:
type: date
allow_blank: false
allow_time: false
require_time: false
input_format: YYYY/M/D/YYYY
display: Date
format: Y-m-d
4 changes: 2 additions & 2 deletions src/Events.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public static function setting(string $key, $default = null): mixed

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

private function __construct() {}
Expand Down Expand Up @@ -200,7 +200,7 @@ private function isMultiDay(Entry $occurrence): bool
private function occurrences(callable $generator): EntryCollection
{
return $this->entries
->filter(fn (Entry $occurrence) => $this->hasStartDate($occurrence))
->filter(fn (Entry $event) => $this->hasStartDate($event))
// take each event and generate the occurrences
->flatMap(callback: $generator)
->reject(fn (Entry $occurrence) => collect($occurrence->exclude_dates)
Expand Down
14 changes: 12 additions & 2 deletions src/Modifiers/IsEndOfWeek.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,26 @@
namespace TransformStudios\Events\Modifiers;

use Carbon\CarbonImmutable;
use Statamic\Facades\Site;
use Statamic\Modifiers\Modifier;

class IsEndOfWeek extends Modifier
{
public function index($value, $params, $context)
{
/*
have to do this because Statamic sets the Carbon locale
to the `lang` of the site, instead of the `locale`
*/
$currentLocale = CarbonImmutable::getLocale();
CarbonImmutable::setLocale(Site::current()->locale());

$date = CarbonImmutable::parse($value);

$date->isSameDay($date->locale(CarbonImmutable::getLocale())->startOfWeek());
$isStartOfWeek = $date->dayOfWeek == now()->endOfWeek()->dayOfWeek;

CarbonImmutable::setLocale($currentLocale);

return $date->dayOfWeek == now()->endOfWeek()->dayOfWeek;
return $isStartOfWeek;
}
}
14 changes: 12 additions & 2 deletions src/Modifiers/IsStartOfWeek.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,26 @@
namespace TransformStudios\Events\Modifiers;

use Carbon\CarbonImmutable;
use Statamic\Facades\Site;
use Statamic\Modifiers\Modifier;

class IsStartOfWeek extends Modifier
{
public function index($value, $params, $context)
{
/*
have to do this because Statamic sets the Carbon locale
to the `lang` of the site, instead of the `locale`
*/
$currentLocale = CarbonImmutable::getLocale();
CarbonImmutable::setLocale(Site::current()->locale());

$date = CarbonImmutable::parse($value);

$date->isSameDay($date->locale(CarbonImmutable::getLocale())->startOfWeek());
$isStartOfWeek = $date->dayOfWeek == now()->startOfWeek()->dayOfWeek;

CarbonImmutable::setLocale($currentLocale);

return $date->dayOfWeek == now()->startOfWeek()->dayOfWeek;
return $isStartOfWeek;
}
}
3 changes: 3 additions & 0 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,17 @@
use Statamic\Entries\Entry;
use Statamic\Facades\Collection;
use Statamic\Fields\Field;
use Statamic\Fields\Fields;
use Statamic\Fields\Value;
use Statamic\Fieldtypes\Dictionary;
use Statamic\Providers\AddonServiceProvider;
use Statamic\Statamic;

class ServiceProvider extends AddonServiceProvider
{
public function bootAddon()
{
// Fields::default('events_timezone', fn () => Statamic::displayTimezone());
collect(Events::setting('collections', [['collection' => 'events']]))
->each(fn (array $collection) => Collection::computed(
$collection['collection'],
Expand Down
45 changes: 40 additions & 5 deletions src/Tags/Events.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
use Carbon\Carbon;
use Carbon\CarbonImmutable;
use Carbon\CarbonInterface;
use Carbon\CarbonPeriod;
use Illuminate\Pagination\Paginator;
use Illuminate\Support\Collection;
use Statamic\Contracts\Query\Builder;
use Statamic\Contracts\Taxonomies\Term;
use Statamic\Entries\Entry;
use Statamic\Entries\EntryCollection;
use Statamic\Facades\Compare;
use Statamic\Facades\Site;
use Statamic\Support\Arr;
use Statamic\Support\Str;
use Statamic\Tags\Concerns\OutputsItems;
Expand All @@ -32,19 +34,52 @@ public function between(): EntryCollection|array

public function calendar(): Collection
{
$currentLocale = CarbonImmutable::getLocale();
CarbonImmutable::setLocale(Site::current()->locale());

$month = $this->params->get('month', now()->englishMonth);
$year = $this->params->get('year', now()->year);

$from = parse_date($month.' '.$year)->startOfMonth()->startOfWeek();
$to = parse_date($month.' '.$year)->endOfMonth()->endOfWeek();
$from = parse_date($month . ' ' . $year)->startOfMonth()->startOfWeek();
$to = parse_date($month . ' ' . $year)->endOfMonth()->endOfWeek();

$occurrences = $this
->generator()
->between(from: $from, to: $to)
->groupBy(fn (Entry $occurrence) => $occurrence->start->toDateString())
->map(fn (EntryCollection $occurrences, string $date) => $this->day(date: $date, occurrences: $occurrences));
->groupBy(function (Entry $occurrence) {
$start = $occurrence->start->setTimezone($this->params->get('timezone') ?? Generator::timezone());
$end = $occurrence->end->setTimezone($this->params->get('timezone') ?? Generator::timezone());

return $start->isSameDay($end) ? $start->toDateString() : [$start->toDateString(), $end->toDateString()];
})
->map(fn(EntryCollection $occurrences, string $date) => $this->day(date: $date, occurrences: $occurrences));

$days = $this->output($this->makeEmptyDates(from: $from, to: $to)->merge($occurrences)->values());

CarbonImmutable::setLocale($currentLocale);

return $days;
}

public function daysOfWeek(): Collection
{
/*
have to do this because Statamic sets the Carbon locale
to the `lang` of the site, instead of the `locale`
*/
$currentLocale = Carbon::getLocale();
Carbon::setLocale(Site::current()->locale());

$days = collect(CarbonPeriod::dates(now()->startOfWeek(), now()->endOfWeek()))
->map(fn (Carbon $date) => [
'short' => $date->format('D')[0],
'medium' => $date->format('D'),
'long' => $date->format('l'),
]);

Carbon::setLocale($currentLocale);

return $this->output($this->makeEmptyDates(from: $from, to: $to)->merge($occurrences)->values());
return $days;
}

public function downloadLink(): string
Expand Down
4 changes: 2 additions & 2 deletions src/Types/Event.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

abstract class Event
{
abstract protected function rule(): RRuleInterface;
abstract protected function rule(bool $useEnd = false): RRuleInterface;

public function __construct(protected Entry $event) {}

Expand Down Expand Up @@ -76,7 +76,7 @@ public function occursOnDate(string|CarbonInterface $date): bool

public function nextOccurrences(int $limit = 1): Collection
{
return $this->collect($this->rule()->getOccurrencesAfter(date: now(), inclusive: true, limit: $limit));
return $this->collect($this->rule(true)->getOccurrencesAfter(date: now(), inclusive: true, limit: $limit));
}

public function startTime(): string
Expand Down
11 changes: 1 addition & 10 deletions src/Types/MultiDayEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,17 +93,8 @@ public function toICalendarEvents(): array
->all();
}

protected function rule(bool $collapseDays = false): RRuleInterface
protected function rule(bool $useEnd = false): RRuleInterface
{
// if we're collapsing, then return an rrule instead of rset and use start of first day to end of last day
if ($this->collapseMultiDays) {
return new RRule([
'count' => 1,
'dtstart' => $this->end(),
'freq' => RRule::DAILY,
]);
}

return tap(
new RSet,
fn (RSet $rset) => $this->days->each(fn (Day $day) => $rset->addRRule([
Expand Down
2 changes: 1 addition & 1 deletion src/Types/RecurringEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function toICalendarEvents(): array
return [$iCalEvent];
}

protected function rule(): RRuleInterface
protected function rule(bool $useEnd = false): RRuleInterface
{
$rule = [
'dtstart' => $this->end(),
Expand Down
15 changes: 12 additions & 3 deletions src/Types/SingleDayEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,20 @@

class SingleDayEvent extends Event
{
protected function rule(): RRuleInterface
protected function rule(bool $useEnd = false): RRuleInterface
{
if ($useEnd) {
return new RRule([
'count' => 1,
'dtstart' => $this->end(),
'freq' => RRule::DAILY,
]);
}

return new RRule([
'count' => 1,
'dtstart' => $this->start()->setTimeFromTimeString($this->endTime()),
// 'count' => 1,
'dtstart' => $this->start(),
'until' => $this->end(),
'freq' => RRule::DAILY,
]);
}
Expand Down
Loading
Loading