fisharebest /
webtrees
| 1 | <?php |
||
| 2 | |||
| 3 | /** |
||
| 4 | * webtrees: online genealogy |
||
| 5 | * Copyright (C) 2025 webtrees development team |
||
| 6 | * This program is free software: you can redistribute it and/or modify |
||
| 7 | * it under the terms of the GNU General Public License as published by |
||
| 8 | * the Free Software Foundation, either version 3 of the License, or |
||
| 9 | * (at your option) any later version. |
||
| 10 | * This program is distributed in the hope that it will be useful, |
||
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
||
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||
| 13 | * GNU General Public License for more details. |
||
| 14 | * You should have received a copy of the GNU General Public License |
||
| 15 | * along with this program. If not, see <https://www.gnu.org/licenses/>. |
||
| 16 | */ |
||
| 17 | |||
| 18 | declare(strict_types=1); |
||
| 19 | |||
| 20 | namespace Fisharebest\Webtrees\Module; |
||
| 21 | |||
| 22 | use Fisharebest\Webtrees\Auth; |
||
| 23 | use Fisharebest\Webtrees\Date\GregorianDate; |
||
| 24 | use Fisharebest\Webtrees\Fact; |
||
| 25 | use Fisharebest\Webtrees\Family; |
||
| 26 | use Fisharebest\Webtrees\GedcomRecord; |
||
| 27 | use Fisharebest\Webtrees\Http\Exceptions\HttpNotFoundException; |
||
| 28 | use Fisharebest\Webtrees\I18N; |
||
| 29 | use Fisharebest\Webtrees\Individual; |
||
| 30 | use Fisharebest\Webtrees\Registry; |
||
| 31 | use Fisharebest\Webtrees\Validator; |
||
| 32 | use Illuminate\Support\Collection; |
||
| 33 | use Psr\Http\Message\ResponseInterface; |
||
| 34 | use Psr\Http\Message\ServerRequestInterface; |
||
| 35 | use Psr\Http\Server\RequestHandlerInterface; |
||
| 36 | use Sabre\VObject\Component\VCalendar; |
||
| 37 | |||
| 38 | use function response; |
||
| 39 | use function route; |
||
| 40 | use function strip_tags; |
||
| 41 | use function view; |
||
| 42 | |||
| 43 | class ShareAnniversaryModule extends AbstractModule implements ModuleShareInterface, RequestHandlerInterface |
||
| 44 | { |
||
| 45 | use ModuleShareTrait; |
||
| 46 | |||
| 47 | protected const array INDIVIDUAL_EVENTS = ['BIRT', 'DEAT']; |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 48 | protected const array FAMILY_EVENTS = ['MARR']; |
||
| 49 | |||
| 50 | protected const string ROUTE_URL = '/tree/{tree}/anniversary-ics/{xref}/{fact_id}'; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Initialization. |
||
| 54 | * |
||
| 55 | * @return void |
||
| 56 | */ |
||
| 57 | public function boot(): void |
||
| 58 | { |
||
| 59 | Registry::routeFactory()->routeMap() |
||
| 60 | ->get(static::class, static::ROUTE_URL, $this); |
||
| 61 | } |
||
| 62 | |||
| 63 | public function title(): string |
||
| 64 | { |
||
| 65 | return I18N::translate('Share the anniversary of an event'); |
||
| 66 | } |
||
| 67 | |||
| 68 | public function description(): string |
||
| 69 | { |
||
| 70 | return I18N::translate('Download a .ICS file containing an anniversary'); |
||
| 71 | } |
||
| 72 | |||
| 73 | /** |
||
| 74 | * HTML to include in the share links page. |
||
| 75 | * |
||
| 76 | * @param GedcomRecord $record |
||
| 77 | * |
||
| 78 | * @return string |
||
| 79 | */ |
||
| 80 | public function share(GedcomRecord $record): string |
||
| 81 | { |
||
| 82 | if ($record instanceof Individual) { |
||
| 83 | $facts = $record->facts(static::INDIVIDUAL_EVENTS, true) |
||
| 84 | ->merge($record->spouseFamilies()->map(fn (Family $family): Collection => $family->facts(static::FAMILY_EVENTS, true))); |
||
| 85 | } elseif ($record instanceof Family) { |
||
| 86 | $facts = $record->facts(static::FAMILY_EVENTS, true); |
||
| 87 | } else { |
||
| 88 | return ''; |
||
| 89 | } |
||
| 90 | |||
| 91 | // iCalendar only supports exact Gregorian dates. |
||
| 92 | $facts = $facts |
||
| 93 | ->flatten() |
||
| 94 | ->filter(fn (Fact $fact): bool => $fact->date()->isOK()) |
||
| 95 | ->filter(fn (Fact $fact): bool => $fact->date()->qual1 === '') |
||
| 96 | ->filter(fn (Fact $fact): bool => $fact->date()->minimumDate() instanceof GregorianDate) |
||
| 97 | ->filter(fn (Fact $fact): bool => $fact->date()->minimumJulianDay() === $fact->date()->maximumJulianDay()) |
||
| 98 | ->mapWithKeys(fn (Fact $fact): array => [ |
||
| 99 | route(static::class, ['tree' => $record->tree()->name(), 'xref' => $fact->record()->xref(), 'fact_id' => $fact->id()]) => |
||
| 100 | $fact->label() . ' — ' . $fact->date()->display(), |
||
| 101 | ]); |
||
| 102 | |||
| 103 | if ($facts->isNotEmpty()) { |
||
| 104 | return view('modules/share-anniversary/share', [ |
||
| 105 | 'facts' => $facts, |
||
| 106 | 'record' => $record, |
||
| 107 | ]); |
||
| 108 | } |
||
| 109 | |||
| 110 | return ''; |
||
| 111 | } |
||
| 112 | |||
| 113 | /** |
||
| 114 | * @param ServerRequestInterface $request |
||
| 115 | * |
||
| 116 | * @return ResponseInterface |
||
| 117 | */ |
||
| 118 | public function handle(ServerRequestInterface $request): ResponseInterface |
||
| 119 | { |
||
| 120 | $tree = Validator::attributes($request)->tree(); |
||
| 121 | $xref = Validator::attributes($request)->isXref()->string('xref'); |
||
| 122 | $fact_id = Validator::attributes($request)->string('fact_id'); |
||
| 123 | $record = Registry::gedcomRecordFactory()->make($xref, $tree); |
||
| 124 | $record = Auth::checkRecordAccess($record); |
||
| 125 | |||
| 126 | $fact = $record->facts()->first(fn (Fact $fact): bool => $fact->id() === $fact_id); |
||
| 127 | |||
| 128 | if ($fact instanceof Fact) { |
||
| 129 | $vcalendar = new VCalendar([ |
||
| 130 | 'VEVENT' => [ |
||
| 131 | 'DTSTART' => $fact->date()->minimumDate()->format('%Y%m%d'), |
||
| 132 | 'RRULE' => 'FREQ=YEARLY', |
||
| 133 | 'SUMMARY' => strip_tags($record->fullName()) . ' — ' . $fact->label(), |
||
| 134 | ], |
||
| 135 | ]); |
||
| 136 | |||
| 137 | return response($vcalendar->serialize()) |
||
| 138 | ->withHeader('content-type', 'text/calendar') |
||
| 139 | ->withHeader('content-disposition', 'attachment; filename="' . $fact->id() . '.ics'); |
||
| 140 | } |
||
| 141 | |||
| 142 | throw new HttpNotFoundException(); |
||
| 143 | } |
||
| 144 | } |
||
| 145 |