From 4155990afa86985355753166428360c05fb0afa2 Mon Sep 17 00:00:00 2001 From: Spitfire Date: Tue, 21 Nov 2023 16:22:39 -0600 Subject: [PATCH 1/6] Calendar: Show Birthdays --- app/Http/Requests/StoreCalendar.php | 1 + app/Models/Calendar.php | 2 + app/Renderers/CalendarRenderer.php | 39 ++++++++++++++++++- ...34_update_calendars_add_show_birthdays.php | 28 +++++++++++++ lang/en/calendars.php | 2 + .../views/calendars/form/_calendar.blade.php | 7 ++++ 6 files changed, 78 insertions(+), 1 deletion(-) create mode 100644 database/migrations/2023_11_21_215234_update_calendars_add_show_birthdays.php diff --git a/app/Http/Requests/StoreCalendar.php b/app/Http/Requests/StoreCalendar.php index 6d7ea3a6e4..b454a6b212 100644 --- a/app/Http/Requests/StoreCalendar.php +++ b/app/Http/Requests/StoreCalendar.php @@ -44,6 +44,7 @@ public function rules() 'moon_name' => 'nullable|array', 'epoch_name' => 'nullable|array', 'season_name' => 'nullable|array', + 'show_birthdays' => 'boolean', 'template_id' => 'nullable', 'format' => ['nullable', new CalendarFormat(), 'string', 'max:20'], 'moon_offset' => [ diff --git a/app/Models/Calendar.php b/app/Models/Calendar.php index caad6a5750..20256e1ac8 100644 --- a/app/Models/Calendar.php +++ b/app/Models/Calendar.php @@ -32,6 +32,7 @@ * @property int $calendar_id * @property array $parameters * @property bool $skip_year_zero + * @property bool $show_birthdays */ class Calendar extends MiscModel { @@ -66,6 +67,7 @@ class Calendar extends MiscModel 'reset', 'is_incrementing', 'format', + 'show_birthdays', // Leap year 'has_leap_year', diff --git a/app/Renderers/CalendarRenderer.php b/app/Renderers/CalendarRenderer.php index d8c1b465cc..031b512d04 100644 --- a/app/Renderers/CalendarRenderer.php +++ b/app/Renderers/CalendarRenderer.php @@ -5,6 +5,7 @@ use App\Models\Calendar; use App\Models\CalendarWeather; use App\Models\EntityEvent; +use App\Models\EntityEventType; use App\Traits\CampaignAware; use Illuminate\Support\Arr; use Illuminate\Support\Collection; @@ -54,6 +55,16 @@ class CalendarRenderer */ protected array $weeks = []; + /** + * Death events + */ + protected array $deaths = []; + + /** + * Birthday events + */ + protected array $births = []; + /** * Array of weirdly recurring events */ @@ -777,6 +788,7 @@ protected function events(): array $reminders = $this->getReminders($this->calendar->calendar); $this->parseReminders($reminders); } + //dd($this->events); return $this->events; } @@ -807,6 +819,18 @@ protected function getReminders(Calendar $calendar) ->where('is_recurring', true); } }) + ->orWhere(function ($sub) { + if ($this->calendar->show_birthdays) { + $sub->where('year', '<=', $this->getYear()); + + if ($this->isYearlyLayout()) { + $sub->whereIn('type_id', [EntityEventType::BIRTH, EntityEventType::DEATH]); + } else { + $sub->where('month', $this->getMonth()) + ->whereIn('type_id', [EntityEventType::BIRTH, EntityEventType::DEATH]); + } + } + }) // Events from previous year or month that spill over ->orWhere(function ($sub) { $previousYear = $this->getYear(-1); @@ -846,7 +870,7 @@ protected function parseReminders(Collection $reminders): void // If the event is recurring, get the year to make sure it should start showing. This was previously // done in the query, but it didn't work on all systems. - if ($event->is_recurring) { + if ($event->is_recurring || $event->type_id == EntityEventType::BIRTH) { if ($event->year > $this->getYear()) { continue; } @@ -878,11 +902,24 @@ protected function parseReminders(Collection $reminders): void $this->recurring[$event->recurring_periodicity][] = $event; } } else { + if ($event->type_id == EntityEventType::DEATH) { + $this->deaths[$event->entity_id] = $event; + } elseif ($event->type_id == EntityEventType::BIRTH) { + $this->births[$event->entity_id] = $event; + continue; + } // Only add it once $this->events[$date][] = $event; $this->addMultidayEvent($event, $date); } } + + foreach ($this->births as $key => $birth) { + if (!isset($this->deaths[$key]) || ($this->deaths[$key]->month > $birth->month || ($this->deaths[$key]->month == $birth->month && $this->deaths[$key]->day > $birth->day))) { + $date = $this->getYear() . '-' . $birth->month . '-' . $birth->day; + $this->events[$date][] = $birth; + } + } //should end the first day of the month } diff --git a/database/migrations/2023_11_21_215234_update_calendars_add_show_birthdays.php b/database/migrations/2023_11_21_215234_update_calendars_add_show_birthdays.php new file mode 100644 index 0000000000..2bf231048b --- /dev/null +++ b/database/migrations/2023_11_21_215234_update_calendars_add_show_birthdays.php @@ -0,0 +1,28 @@ +boolean('show_birthdays')->default(true); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::table('calendars', function (Blueprint $table) { + $table->dropColumn('show_birthdays'); + }); + } +}; diff --git a/lang/en/calendars.php b/lang/en/calendars.php index acb0ced23b..1ff857ab79 100644 --- a/lang/en/calendars.php +++ b/lang/en/calendars.php @@ -90,6 +90,7 @@ 'recurring_until' => 'Recurring Until Year', 'reset' => 'Weekly Reset', 'seasons' => 'Seasons', + 'show_birthdays' => 'Show Birthdays', 'skip_year_zero' => 'Skip Year Zero', 'start_offset' => 'Start Offset', 'suffix' => 'Suffix', @@ -117,6 +118,7 @@ 'reset' => 'Always start the beginning of the month or year on the first week day.', 'seasons' => 'Create seasons for your calendar by providing when each of them start. Kanka will take care of the rest.', 'skip_year_zero' => 'By default, the calendar\'s first year is year zero. Enable this option to skip year zero.', + 'show_birthdays' => 'Show the yearly birthdays of characters that have a birthday reminder on this calendar up to their death date.', 'weekdays' => 'Set your weekday names. At least 2 weekdays are required.', 'weeks' => 'Define some names for the more important weeks of your calendar.', 'years' => 'Some years are so important that they have their own name.', diff --git a/resources/views/calendars/form/_calendar.blade.php b/resources/views/calendars/form/_calendar.blade.php index 5d23b3eb34..98b5a0371c 100644 --- a/resources/views/calendars/form/_calendar.blade.php +++ b/resources/views/calendars/form/_calendar.blade.php @@ -44,6 +44,13 @@ {!! Form::checkbox('is_incrementing', 1, FormCopy::field('is_incrementing')->string()) !!} + + + {!! Form::hidden('show_birthdays', 0) !!} + + {!! Form::checkbox('show_birthdays', 1, FormCopy::field('show_birthdays')->string()) !!} + +
From 8b8e27782718b95821c59823518c1e34a888d2ae Mon Sep 17 00:00:00 2001 From: spitfire305 Date: Tue, 21 Nov 2023 22:24:12 +0000 Subject: [PATCH 2/6] Fix styling --- app/Services/Campaign/ExportService.php | 1 - .../2023_11_19_121322_update_campaign_exports_add_progress.php | 3 +-- .../2023_11_21_215234_update_calendars_add_show_birthdays.php | 3 +-- 3 files changed, 2 insertions(+), 5 deletions(-) diff --git a/app/Services/Campaign/ExportService.php b/app/Services/Campaign/ExportService.php index 9ddf78d8de..e3e821519d 100644 --- a/app/Services/Campaign/ExportService.php +++ b/app/Services/Campaign/ExportService.php @@ -346,7 +346,6 @@ public function fail(): self /** * Each time an element is added to the zip, there is a chance that the progress is increased - * @return void */ protected function progress(): void { diff --git a/database/migrations/2023_11_19_121322_update_campaign_exports_add_progress.php b/database/migrations/2023_11_19_121322_update_campaign_exports_add_progress.php index b84a31791f..9487f06a7b 100644 --- a/database/migrations/2023_11_19_121322_update_campaign_exports_add_progress.php +++ b/database/migrations/2023_11_19_121322_update_campaign_exports_add_progress.php @@ -4,8 +4,7 @@ use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; -return new class extends Migration -{ +return new class () extends Migration { /** * Run the migrations. */ diff --git a/database/migrations/2023_11_21_215234_update_calendars_add_show_birthdays.php b/database/migrations/2023_11_21_215234_update_calendars_add_show_birthdays.php index 2bf231048b..d1afed39d0 100644 --- a/database/migrations/2023_11_21_215234_update_calendars_add_show_birthdays.php +++ b/database/migrations/2023_11_21_215234_update_calendars_add_show_birthdays.php @@ -4,8 +4,7 @@ use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; -return new class extends Migration -{ +return new class () extends Migration { /** * Run the migrations. */ From d9694e2dea38cc6199c18c77fca84bf3d23ca444 Mon Sep 17 00:00:00 2001 From: Spitfire Date: Wed, 22 Nov 2023 16:51:28 -0600 Subject: [PATCH 3/6] Implemented requested changes. --- app/Models/EntityEvent.php | 4 ++++ app/Renderers/CalendarRenderer.php | 20 +++++++------------ ...34_update_calendars_add_show_birthdays.php | 2 +- 3 files changed, 12 insertions(+), 14 deletions(-) diff --git a/app/Models/EntityEvent.php b/app/Models/EntityEvent.php index 6e8994a4db..c68d23a9b4 100644 --- a/app/Models/EntityEvent.php +++ b/app/Models/EntityEvent.php @@ -545,6 +545,10 @@ protected function previousMonth(int $month, int $min): int return $month--; } + public function death() { + return $this->hasOne(EntityEvent::class, 'entity_id', 'entity_id')->whereColumn('calendar_id', 'entity_events.calendar_id')->where('type_id', EntityEventType::DEATH); + } + public function exportFields(): array { return [ diff --git a/app/Renderers/CalendarRenderer.php b/app/Renderers/CalendarRenderer.php index 031b512d04..c4dd1193c3 100644 --- a/app/Renderers/CalendarRenderer.php +++ b/app/Renderers/CalendarRenderer.php @@ -799,7 +799,7 @@ protected function getReminders(Calendar $calendar) { return $calendar->calendarEvents() ->has('entity') - ->with(['entity', 'entity.tags', 'entity.image']) + ->with(['entity', 'entity.tags', 'entity.image', 'death']) ->where(function ($query) { $query // Where it's the current year , or current year and current month @@ -821,13 +821,10 @@ protected function getReminders(Calendar $calendar) }) ->orWhere(function ($sub) { if ($this->calendar->show_birthdays) { - $sub->where('year', '<=', $this->getYear()); - - if ($this->isYearlyLayout()) { - $sub->whereIn('type_id', [EntityEventType::BIRTH, EntityEventType::DEATH]); - } else { - $sub->where('month', $this->getMonth()) - ->whereIn('type_id', [EntityEventType::BIRTH, EntityEventType::DEATH]); + $sub->where('year', '<=', $this->getYear()) + ->whereIn('type_id', [EntityEventType::BIRTH, EntityEventType::DEATH]); + if (!$this->isYearlyLayout()) { + $sub->where('month', $this->getMonth()); } } }) @@ -870,7 +867,7 @@ protected function parseReminders(Collection $reminders): void // If the event is recurring, get the year to make sure it should start showing. This was previously // done in the query, but it didn't work on all systems. - if ($event->is_recurring || $event->type_id == EntityEventType::BIRTH) { + if ($event->is_recurring || $event->isBirth()) { if ($event->year > $this->getYear()) { continue; } @@ -902,10 +899,7 @@ protected function parseReminders(Collection $reminders): void $this->recurring[$event->recurring_periodicity][] = $event; } } else { - if ($event->type_id == EntityEventType::DEATH) { - $this->deaths[$event->entity_id] = $event; - } elseif ($event->type_id == EntityEventType::BIRTH) { - $this->births[$event->entity_id] = $event; + if ($event->isBirth() && $event->death && $event->death->isPastDate($this->getYear(), $event->month, $event->day)) { continue; } // Only add it once diff --git a/database/migrations/2023_11_21_215234_update_calendars_add_show_birthdays.php b/database/migrations/2023_11_21_215234_update_calendars_add_show_birthdays.php index 2bf231048b..a3ac25f85d 100644 --- a/database/migrations/2023_11_21_215234_update_calendars_add_show_birthdays.php +++ b/database/migrations/2023_11_21_215234_update_calendars_add_show_birthdays.php @@ -12,7 +12,7 @@ public function up(): void { Schema::table('calendars', function (Blueprint $table) { - $table->boolean('show_birthdays')->default(true); + $table->boolean('show_birthdays')->default(false); }); } From 5439624faccc50c9d35ef7641c9e58a47f2eb1a2 Mon Sep 17 00:00:00 2001 From: spitfire305 Date: Wed, 22 Nov 2023 22:52:36 +0000 Subject: [PATCH 4/6] Fix styling --- app/Models/EntityEvent.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/Models/EntityEvent.php b/app/Models/EntityEvent.php index c68d23a9b4..9ec75b09cc 100644 --- a/app/Models/EntityEvent.php +++ b/app/Models/EntityEvent.php @@ -545,7 +545,8 @@ protected function previousMonth(int $month, int $min): int return $month--; } - public function death() { + public function death() + { return $this->hasOne(EntityEvent::class, 'entity_id', 'entity_id')->whereColumn('calendar_id', 'entity_events.calendar_id')->where('type_id', EntityEventType::DEATH); } From cd8fa714517da43e0a59146a76fd4e9b9aa0a055 Mon Sep 17 00:00:00 2001 From: Spitfire Date: Thu, 23 Nov 2023 16:42:52 -0600 Subject: [PATCH 5/6] Implemented requested changes --- app/Renderers/CalendarRenderer.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/app/Renderers/CalendarRenderer.php b/app/Renderers/CalendarRenderer.php index c4dd1193c3..aa7b265c15 100644 --- a/app/Renderers/CalendarRenderer.php +++ b/app/Renderers/CalendarRenderer.php @@ -863,6 +863,9 @@ protected function parseReminders(Collection $reminders): void } /** @var EntityEvent $event */ foreach ($reminders as $event) { + if ($event->isBirth() && $event->death && $event->death->isPastDate($this->getYear(), $event->month, $event->day)) { + continue; + } $date = $event->year . '-' . $event->month . '-' . $event->day; // If the event is recurring, get the year to make sure it should start showing. This was previously @@ -877,6 +880,7 @@ protected function parseReminders(Collection $reminders): void } $date = $this->getYear() . '-' . $event->month . '-' . $event->day; } + if (!isset($this->events[$date])) { $this->events[$date] = []; } @@ -899,9 +903,6 @@ protected function parseReminders(Collection $reminders): void $this->recurring[$event->recurring_periodicity][] = $event; } } else { - if ($event->isBirth() && $event->death && $event->death->isPastDate($this->getYear(), $event->month, $event->day)) { - continue; - } // Only add it once $this->events[$date][] = $event; $this->addMultidayEvent($event, $date); From 83f2cb1c40c1b140cecde46ee773e5cd5a3ec8fe Mon Sep 17 00:00:00 2001 From: Spitfire Date: Thu, 23 Nov 2023 17:14:49 -0600 Subject: [PATCH 6/6] php stan fix --- app/Models/EntityEvent.php | 1 + 1 file changed, 1 insertion(+) diff --git a/app/Models/EntityEvent.php b/app/Models/EntityEvent.php index 9ec75b09cc..917a17453e 100644 --- a/app/Models/EntityEvent.php +++ b/app/Models/EntityEvent.php @@ -30,6 +30,7 @@ * @property int $elapsed * * @property Calendar|null $calendar + * @property EntityEvent|null $death * @property EntityEventType|null $type */ class EntityEvent extends MiscModel