1<?php 2 3/** 4 * webtrees: online genealogy 5 * Copyright (C) 2021 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 18declare(strict_types=1); 19 20namespace Fisharebest\Webtrees\Module; 21 22use Aura\Router\RouterContainer; 23use Fisharebest\Webtrees\Auth; 24use Fisharebest\Webtrees\Date\GregorianDate; 25use Fisharebest\Webtrees\Fact; 26use Fisharebest\Webtrees\Family; 27use Fisharebest\Webtrees\GedcomRecord; 28use Fisharebest\Webtrees\Http\Exceptions\HttpNotFoundException; 29use Fisharebest\Webtrees\I18N; 30use Fisharebest\Webtrees\Individual; 31use Fisharebest\Webtrees\Registry; 32use Fisharebest\Webtrees\Validator; 33use Illuminate\Support\Collection; 34use Psr\Http\Message\ResponseInterface; 35use Psr\Http\Message\ServerRequestInterface; 36use Psr\Http\Server\RequestHandlerInterface; 37use Sabre\VObject\Component\VCalendar; 38 39use function app; 40use function assert; 41use function response; 42use function route; 43use function strip_tags; 44use function view; 45 46/** 47 * Class ShareAnniversaryModule 48 */ 49class ShareAnniversaryModule extends AbstractModule implements ModuleShareInterface, RequestHandlerInterface 50{ 51 use ModuleShareTrait; 52 53 protected const INDIVIDUAL_EVENTS = ['BIRT', 'DEAT']; 54 protected const FAMILY_EVENTS = ['MARR']; 55 56 protected const ROUTE_URL = '/tree/{tree}/anniversary-ics/{xref}/{fact_id}'; 57 58 /** 59 * Initialization. 60 * 61 * @return void 62 */ 63 public function boot(): void 64 { 65 Registry::routeFactory()->routeMap() 66 ->get(static::class, static::ROUTE_URL, $this); 67 } 68 69 /** 70 * How should this module be identified in the control panel, etc.? 71 * 72 * @return string 73 */ 74 public function title(): string 75 { 76 return I18N::translate('Share the anniversary of an event'); 77 } 78 79 /** 80 * A sentence describing what this module does. 81 * 82 * @return string 83 */ 84 public function description(): string 85 { 86 return I18N::translate('Download a .ICS file containing an anniversary'); 87 } 88 89 /** 90 * HTML to include in the share links page. 91 * 92 * @param GedcomRecord $record 93 * 94 * @return string 95 */ 96 public function share(GedcomRecord $record): string 97 { 98 if ($record instanceof Individual) { 99 $facts = $record->facts(static::INDIVIDUAL_EVENTS, true) 100 ->merge($record->spouseFamilies()->map(fn (Family $family): Collection => $family->facts(static::FAMILY_EVENTS, true))); 101 } elseif ($record instanceof Family) { 102 $facts = $record->facts(static::FAMILY_EVENTS, true); 103 } else { 104 return ''; 105 } 106 107 // iCalendar only supports exact Gregorian dates. 108 $facts = $facts 109 ->flatten() 110 ->filter(fn (Fact $fact): bool => $fact->date()->isOK()) 111 ->filter(fn (Fact $fact): bool => $fact->date()->qual1 === '') 112 ->filter(fn (Fact $fact): bool => $fact->date()->minimumDate() instanceof GregorianDate) 113 ->filter(fn (Fact $fact): bool => $fact->date()->minimumJulianDay() === $fact->date()->maximumJulianDay()) 114 ->mapWithKeys(fn (Fact $fact): array => [ 115 route(static::class, ['tree' => $record->tree()->name(), 'xref' => $fact->record()->xref(), 'fact_id' => $fact->id()]) => 116 $fact->label() . ' — ' . $fact->date()->display(), 117 ]); 118 119 if ($facts->isNotEmpty()) { 120 $url = route(static::class, ['tree' => $record->tree()->name(), 'xref' => $record->xref()]); 121 122 return view('modules/share-anniversary/share', [ 123 'facts' => $facts, 124 'record' => $record, 125 'url' => $url, 126 ]); 127 } 128 129 return ''; 130 } 131 132 /** 133 * @param ServerRequestInterface $request 134 * 135 * @return ResponseInterface 136 */ 137 public function handle(ServerRequestInterface $request): ResponseInterface 138 { 139 $tree = Validator::attributes($request)->tree(); 140 $xref = Validator::attributes($request)->isXref()->string('xref'); 141 $fact_id = Validator::attributes($request)->string('fact_id'); 142 $record = Registry::gedcomRecordFactory()->make($xref, $tree); 143 $record = Auth::checkRecordAccess($record); 144 145 $fact = $record->facts() 146 ->filter(fn (Fact $fact): bool => $fact->id() === $fact_id) 147 ->first(); 148 149 if ($fact instanceof Fact) { 150 $date = $fact->date()->minimumDate()->format('%Y%m%d'); 151 $vcalendar = new VCalendar(); 152 $vevent = $vcalendar->add('VEVENT'); 153 $dtstart = $vevent->add('DTSTART', $date); 154 $dtstart['VALUE'] = 'DATE'; 155 $vevent->add('RRULE', 'FREQ=YEARLY'); 156 $vevent->add('SUMMARY', strip_tags($record->fullName()) . ' — ' . $fact->label()); 157 158 return response($vcalendar->serialize()) 159 ->withHeader('Content-Type', 'text/calendar') 160 ->withHeader('Content-Disposition', 'attachment; filename="' . $fact->id() . '.ics'); 161 } 162 163 throw new HttpNotFoundException(); 164 } 165} 166