Skip to content

Commit

Permalink
Calendar: Show Birthdays
Browse files Browse the repository at this point in the history
  • Loading branch information
spitfire305 committed Nov 21, 2023
1 parent 5481e0f commit 4155990
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 1 deletion.
1 change: 1 addition & 0 deletions app/Http/Requests/StoreCalendar.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => [
Expand Down
2 changes: 2 additions & 0 deletions app/Models/Calendar.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
* @property int $calendar_id
* @property array $parameters
* @property bool $skip_year_zero
* @property bool $show_birthdays
*/
class Calendar extends MiscModel
{
Expand Down Expand Up @@ -66,6 +67,7 @@ class Calendar extends MiscModel
'reset',
'is_incrementing',
'format',
'show_birthdays',

// Leap year
'has_leap_year',
Expand Down
39 changes: 38 additions & 1 deletion app/Renderers/CalendarRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -54,6 +55,16 @@ class CalendarRenderer
*/
protected array $weeks = [];

/**
* Death events
*/
protected array $deaths = [];

/**
* Birthday events
*/
protected array $births = [];

/**
* Array of weirdly recurring events
*/
Expand Down Expand Up @@ -777,6 +788,7 @@ protected function events(): array
$reminders = $this->getReminders($this->calendar->calendar);
$this->parseReminders($reminders);
}
//dd($this->events);
return $this->events;
}

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('calendars', function (Blueprint $table) {
$table->boolean('show_birthdays')->default(true);
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('calendars', function (Blueprint $table) {
$table->dropColumn('show_birthdays');
});
}
};
2 changes: 2 additions & 0 deletions lang/en/calendars.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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.',
Expand Down
7 changes: 7 additions & 0 deletions resources/views/calendars/form/_calendar.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@
{!! Form::checkbox('is_incrementing', 1, FormCopy::field('is_incrementing')->string()) !!}
</x-checkbox>
</x-forms.field>

<x-forms.field field="birthdays" :label="__('calendars.fields.show_birthdays')">
{!! Form::hidden('show_birthdays', 0) !!}
<x-checkbox :text="__('calendars.hints.show_birthdays')">
{!! Form::checkbox('show_birthdays', 1, FormCopy::field('show_birthdays')->string()) !!}
</x-checkbox>
</x-forms.field>
</div>
<div class="flex gap-5 flex-col">
<x-forms.field field="years" :label=" __('calendars.panels.years')" :helper="__('calendars.hints.years')">
Expand Down

0 comments on commit 4155990

Please sign in to comment.