Skip to content

Commit

Permalink
Merge pull request #770 from owlchester/calendar-show-birthdays
Browse files Browse the repository at this point in the history
Calendar: Show Birthdays
  • Loading branch information
ilestis authored Nov 24, 2023
2 parents 5481e0f + 83f2cb1 commit 91f4f1e
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 5 deletions.
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
6 changes: 6 additions & 0 deletions app/Models/EntityEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
* @property int $elapsed
*
* @property Calendar|null $calendar
* @property EntityEvent|null $death
* @property EntityEventType|null $type
*/
class EntityEvent extends MiscModel
Expand Down Expand Up @@ -545,6 +546,11 @@ 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 [
Expand Down
36 changes: 34 additions & 2 deletions 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 All @@ -787,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
Expand All @@ -807,6 +819,15 @@ protected function getReminders(Calendar $calendar)
->where('is_recurring', true);
}
})
->orWhere(function ($sub) {
if ($this->calendar->show_birthdays) {
$sub->where('year', '<=', $this->getYear())
->whereIn('type_id', [EntityEventType::BIRTH, EntityEventType::DEATH]);
if (!$this->isYearlyLayout()) {
$sub->where('month', $this->getMonth());
}
}
})
// Events from previous year or month that spill over
->orWhere(function ($sub) {
$previousYear = $this->getYear(-1);
Expand Down Expand Up @@ -842,11 +863,14 @@ 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
// done in the query, but it didn't work on all systems.
if ($event->is_recurring) {
if ($event->is_recurring || $event->isBirth()) {
if ($event->year > $this->getYear()) {
continue;
}
Expand All @@ -856,6 +880,7 @@ protected function parseReminders(Collection $reminders): void
}
$date = $this->getYear() . '-' . $event->month . '-' . $event->day;
}

if (!isset($this->events[$date])) {
$this->events[$date] = [];
}
Expand Down Expand Up @@ -883,6 +908,13 @@ protected function parseReminders(Collection $reminders): void
$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
1 change: 0 additions & 1 deletion app/Services/Campaign/ExportService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?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(false);
});
}

/**
* 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 91f4f1e

Please sign in to comment.