xref: /webtrees/app/Module/ShareAnniversaryModule.php (revision 7413816e6dd2d50e569034fb804f3dce7471bb94)
1354a9dbaSGreg Roach<?php
2354a9dbaSGreg Roach
3354a9dbaSGreg Roach/**
4354a9dbaSGreg Roach * webtrees: online genealogy
5d11be702SGreg Roach * Copyright (C) 2023 webtrees development team
6354a9dbaSGreg Roach * This program is free software: you can redistribute it and/or modify
7354a9dbaSGreg Roach * it under the terms of the GNU General Public License as published by
8354a9dbaSGreg Roach * the Free Software Foundation, either version 3 of the License, or
9354a9dbaSGreg Roach * (at your option) any later version.
10354a9dbaSGreg Roach * This program is distributed in the hope that it will be useful,
11354a9dbaSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12354a9dbaSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13354a9dbaSGreg Roach * GNU General Public License for more details.
14354a9dbaSGreg Roach * You should have received a copy of the GNU General Public License
15354a9dbaSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
16354a9dbaSGreg Roach */
17354a9dbaSGreg Roach
18354a9dbaSGreg Roachdeclare(strict_types=1);
19354a9dbaSGreg Roach
20354a9dbaSGreg Roachnamespace Fisharebest\Webtrees\Module;
21354a9dbaSGreg Roach
22354a9dbaSGreg Roachuse Fisharebest\Webtrees\Auth;
23354a9dbaSGreg Roachuse Fisharebest\Webtrees\Date\GregorianDate;
24354a9dbaSGreg Roachuse Fisharebest\Webtrees\Fact;
25354a9dbaSGreg Roachuse Fisharebest\Webtrees\Family;
26354a9dbaSGreg Roachuse Fisharebest\Webtrees\GedcomRecord;
2781b729d3SGreg Roachuse Fisharebest\Webtrees\Http\Exceptions\HttpNotFoundException;
28354a9dbaSGreg Roachuse Fisharebest\Webtrees\I18N;
29354a9dbaSGreg Roachuse Fisharebest\Webtrees\Individual;
30354a9dbaSGreg Roachuse Fisharebest\Webtrees\Registry;
31b55cbc6bSGreg Roachuse Fisharebest\Webtrees\Validator;
32354a9dbaSGreg Roachuse Illuminate\Support\Collection;
33354a9dbaSGreg Roachuse Psr\Http\Message\ResponseInterface;
34354a9dbaSGreg Roachuse Psr\Http\Message\ServerRequestInterface;
35354a9dbaSGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
36354a9dbaSGreg Roachuse Sabre\VObject\Component\VCalendar;
37354a9dbaSGreg Roach
38354a9dbaSGreg Roachuse function response;
39354a9dbaSGreg Roachuse function route;
40354a9dbaSGreg Roachuse function strip_tags;
41354a9dbaSGreg Roachuse function view;
42354a9dbaSGreg Roach
43354a9dbaSGreg Roach/**
44354a9dbaSGreg Roach * Class ShareAnniversaryModule
45354a9dbaSGreg Roach */
46354a9dbaSGreg Roachclass ShareAnniversaryModule extends AbstractModule implements ModuleShareInterface, RequestHandlerInterface
47354a9dbaSGreg Roach{
48354a9dbaSGreg Roach    use ModuleShareTrait;
49354a9dbaSGreg Roach
50354a9dbaSGreg Roach    protected const INDIVIDUAL_EVENTS = ['BIRT', 'DEAT'];
51354a9dbaSGreg Roach    protected const FAMILY_EVENTS     = ['MARR'];
52354a9dbaSGreg Roach
53354a9dbaSGreg Roach    protected const ROUTE_URL = '/tree/{tree}/anniversary-ics/{xref}/{fact_id}';
54354a9dbaSGreg Roach
55354a9dbaSGreg Roach    /**
56354a9dbaSGreg Roach     * Initialization.
57354a9dbaSGreg Roach     *
58354a9dbaSGreg Roach     * @return void
59354a9dbaSGreg Roach     */
60354a9dbaSGreg Roach    public function boot(): void
61354a9dbaSGreg Roach    {
62158900c2SGreg Roach        Registry::routeFactory()->routeMap()
63354a9dbaSGreg Roach            ->get(static::class, static::ROUTE_URL, $this);
64354a9dbaSGreg Roach    }
65354a9dbaSGreg Roach
66354a9dbaSGreg Roach    /**
67354a9dbaSGreg Roach     * How should this module be identified in the control panel, etc.?
68354a9dbaSGreg Roach     *
69354a9dbaSGreg Roach     * @return string
70354a9dbaSGreg Roach     */
71354a9dbaSGreg Roach    public function title(): string
72354a9dbaSGreg Roach    {
73354a9dbaSGreg Roach        return I18N::translate('Share the anniversary of an event');
74354a9dbaSGreg Roach    }
75354a9dbaSGreg Roach
76354a9dbaSGreg Roach    public function description(): string
77354a9dbaSGreg Roach    {
78354a9dbaSGreg Roach        return I18N::translate('Download a .ICS file containing an anniversary');
79354a9dbaSGreg Roach    }
80354a9dbaSGreg Roach
81354a9dbaSGreg Roach    /**
82354a9dbaSGreg Roach     * HTML to include in the share links page.
83354a9dbaSGreg Roach     *
84354a9dbaSGreg Roach     * @param GedcomRecord $record
85354a9dbaSGreg Roach     *
86354a9dbaSGreg Roach     * @return string
87354a9dbaSGreg Roach     */
88354a9dbaSGreg Roach    public function share(GedcomRecord $record): string
89354a9dbaSGreg Roach    {
90354a9dbaSGreg Roach        if ($record instanceof Individual) {
91354a9dbaSGreg Roach            $facts = $record->facts(static::INDIVIDUAL_EVENTS, true)
92354a9dbaSGreg Roach                ->merge($record->spouseFamilies()->map(fn (Family $family): Collection => $family->facts(static::FAMILY_EVENTS, true)));
93354a9dbaSGreg Roach        } elseif ($record instanceof Family) {
94354a9dbaSGreg Roach            $facts = $record->facts(static::FAMILY_EVENTS, true);
95354a9dbaSGreg Roach        } else {
96354a9dbaSGreg Roach            return '';
97354a9dbaSGreg Roach        }
98354a9dbaSGreg Roach
99354a9dbaSGreg Roach        // iCalendar only supports exact Gregorian dates.
100354a9dbaSGreg Roach        $facts = $facts
101354a9dbaSGreg Roach            ->flatten()
102354a9dbaSGreg Roach            ->filter(fn (Fact $fact): bool => $fact->date()->isOK())
103354a9dbaSGreg Roach            ->filter(fn (Fact $fact): bool => $fact->date()->qual1 === '')
104354a9dbaSGreg Roach            ->filter(fn (Fact $fact): bool => $fact->date()->minimumDate() instanceof GregorianDate)
105138837acSGreg Roach            ->filter(fn (Fact $fact): bool => $fact->date()->minimumJulianDay() === $fact->date()->maximumJulianDay())
106354a9dbaSGreg Roach            ->mapWithKeys(fn (Fact $fact): array => [
107354a9dbaSGreg Roach                route(static::class, ['tree' => $record->tree()->name(), 'xref' => $fact->record()->xref(), 'fact_id' => $fact->id()]) =>
10866ecd017SGreg Roach                    $fact->label() . ' — ' . $fact->date()->display(),
109354a9dbaSGreg Roach            ]);
110354a9dbaSGreg Roach
111354a9dbaSGreg Roach        if ($facts->isNotEmpty()) {
112354a9dbaSGreg Roach            $url = route(static::class, ['tree' => $record->tree()->name(), 'xref' => $record->xref()]);
113354a9dbaSGreg Roach
114354a9dbaSGreg Roach            return view('modules/share-anniversary/share', [
115354a9dbaSGreg Roach                'facts'  => $facts,
116354a9dbaSGreg Roach                'record' => $record,
117354a9dbaSGreg Roach                'url'    => $url,
118354a9dbaSGreg Roach            ]);
119354a9dbaSGreg Roach        }
120354a9dbaSGreg Roach
121354a9dbaSGreg Roach        return '';
122354a9dbaSGreg Roach    }
123354a9dbaSGreg Roach
124354a9dbaSGreg Roach    /**
125354a9dbaSGreg Roach     * @param ServerRequestInterface $request
126354a9dbaSGreg Roach     *
127354a9dbaSGreg Roach     * @return ResponseInterface
128354a9dbaSGreg Roach     */
129354a9dbaSGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
130354a9dbaSGreg Roach    {
131b55cbc6bSGreg Roach        $tree    = Validator::attributes($request)->tree();
132b55cbc6bSGreg Roach        $xref    = Validator::attributes($request)->isXref()->string('xref');
133b55cbc6bSGreg Roach        $fact_id = Validator::attributes($request)->string('fact_id');
134354a9dbaSGreg Roach        $record  = Registry::gedcomRecordFactory()->make($xref, $tree);
135354a9dbaSGreg Roach        $record  = Auth::checkRecordAccess($record);
136354a9dbaSGreg Roach
13771e7c06eSGreg Roach        $fact = $record->facts()->first(fn (Fact $fact): bool => $fact->id() === $fact_id);
138354a9dbaSGreg Roach
139354a9dbaSGreg Roach        if ($fact instanceof Fact) {
140*6bd19c8cSGreg Roach            $vcalendar = new VCalendar([
141*6bd19c8cSGreg Roach                'VEVENT' => [
142*6bd19c8cSGreg Roach                    'DTSTART' => $fact->date()->minimumDate()->format('%Y%m%d'),
143*6bd19c8cSGreg Roach                    'RRULE'   => 'FREQ=YEARLY',
144*6bd19c8cSGreg Roach                    'SUMMARY' => strip_tags($record->fullName()) . ' — ' . $fact->label(),
145*6bd19c8cSGreg Roach                ],
146*6bd19c8cSGreg Roach            ]);
147354a9dbaSGreg Roach
148354a9dbaSGreg Roach            return response($vcalendar->serialize())
1496172e7f6SGreg Roach                ->withHeader('content-type', 'text/calendar')
1506172e7f6SGreg Roach                ->withHeader('content-disposition', 'attachment; filename="' . $fact->id() . '.ics');
151354a9dbaSGreg Roach        }
152354a9dbaSGreg Roach
153354a9dbaSGreg Roach        throw new HttpNotFoundException();
154354a9dbaSGreg Roach    }
155354a9dbaSGreg Roach}
156