xref: /webtrees/app/Services/CalendarService.php (revision 449b311ecf65f677a2595e1e29f712d11ef22f34)
1f0a11419SGreg Roach<?php
23976b470SGreg Roach
3f0a11419SGreg Roach/**
4f0a11419SGreg Roach * webtrees: online genealogy
5d11be702SGreg Roach * Copyright (C) 2023 webtrees development team
6f0a11419SGreg Roach * This program is free software: you can redistribute it and/or modify
7f0a11419SGreg Roach * it under the terms of the GNU General Public License as published by
8f0a11419SGreg Roach * the Free Software Foundation, either version 3 of the License, or
9f0a11419SGreg Roach * (at your option) any later version.
10f0a11419SGreg Roach * This program is distributed in the hope that it will be useful,
11f0a11419SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12f0a11419SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13f0a11419SGreg Roach * GNU General Public License for more details.
14f0a11419SGreg Roach * You should have received a copy of the GNU General Public License
1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
16f0a11419SGreg Roach */
17fcfa147eSGreg Roach
18e7f56f2aSGreg Roachdeclare(strict_types=1);
19e7f56f2aSGreg Roach
20f0a11419SGreg Roachnamespace Fisharebest\Webtrees\Services;
21f0a11419SGreg Roach
224a9ac934SGreg Roachuse Fisharebest\ExtCalendar\PersianCalendar;
23f0a11419SGreg Roachuse Fisharebest\Webtrees\Date;
2425d4b688SGreg Roachuse Fisharebest\Webtrees\Date\AbstractCalendarDate;
25f0a11419SGreg Roachuse Fisharebest\Webtrees\Date\FrenchDate;
26f0a11419SGreg Roachuse Fisharebest\Webtrees\Date\GregorianDate;
27f0a11419SGreg Roachuse Fisharebest\Webtrees\Date\HijriDate;
28f0a11419SGreg Roachuse Fisharebest\Webtrees\Date\JalaliDate;
29f0a11419SGreg Roachuse Fisharebest\Webtrees\Date\JewishDate;
30f0a11419SGreg Roachuse Fisharebest\Webtrees\Date\JulianDate;
316f4ec3caSGreg Roachuse Fisharebest\Webtrees\DB;
32f0a11419SGreg Roachuse Fisharebest\Webtrees\Fact;
33f0a11419SGreg Roachuse Fisharebest\Webtrees\Family;
34f0a11419SGreg Roachuse Fisharebest\Webtrees\GedcomRecord;
35f0a11419SGreg Roachuse Fisharebest\Webtrees\Individual;
36f0c88a96SGreg Roachuse Fisharebest\Webtrees\Registry;
37f0a11419SGreg Roachuse Fisharebest\Webtrees\Tree;
389e87c1a2SGreg Roachuse Illuminate\Database\Query\Builder;
399e87c1a2SGreg Roachuse Illuminate\Database\Query\JoinClause;
408af3e5c1SGreg Roachuse Illuminate\Support\Collection;
413976b470SGreg Roach
420195b0ddSGreg Roachuse function array_merge;
4310e06497SGreg Roachuse function assert;
440195b0ddSGreg Roachuse function in_array;
450a614d28SGreg Roachuse function preg_match_all;
460a614d28SGreg Roachuse function range;
47f0a11419SGreg Roach
48f0a11419SGreg Roach/**
49f0a11419SGreg Roach * Calculate anniversaries, etc.
50f0a11419SGreg Roach */
51f0a11419SGreg Roachclass CalendarService
52f0a11419SGreg Roach{
530a614d28SGreg Roach    // If no facts specified, get all except these
540a614d28SGreg Roach    protected const SKIP_FACTS = ['CHAN', 'BAPL', 'SLGC', 'SLGS', 'ENDL', 'CENS', 'RESI', 'NOTE', 'ADDR', 'OBJE', 'SOUR', '_TODO'];
550a614d28SGreg Roach
56f0a11419SGreg Roach    /**
5770d8159dSGreg Roach     * List all the months in a given year.
5870d8159dSGreg Roach     *
5970d8159dSGreg Roach     * @param string $calendar
6070d8159dSGreg Roach     * @param int    $year
6170d8159dSGreg Roach     *
6224f2a3afSGreg Roach     * @return array<string>
6370d8159dSGreg Roach     */
6470d8159dSGreg Roach    public function calendarMonthsInYear(string $calendar, int $year): array
6570d8159dSGreg Roach    {
6670d8159dSGreg Roach        $date          = new Date($calendar . ' ' . $year);
6770d8159dSGreg Roach        $calendar_date = $date->minimumDate();
6870d8159dSGreg Roach        $month_numbers = range(1, $calendar_date->monthsInYear());
6970d8159dSGreg Roach        $month_names   = [];
7070d8159dSGreg Roach
7170d8159dSGreg Roach        foreach ($month_numbers as $month_number) {
724a83f5d7SGreg Roach            $calendar_date->day   = 1;
734a83f5d7SGreg Roach            $calendar_date->month = $month_number;
7470d8159dSGreg Roach            $calendar_date->setJdFromYmd();
7570d8159dSGreg Roach
7670d8159dSGreg Roach            if ($month_number === 6 && $calendar_date instanceof JewishDate && !$calendar_date->isLeapYear()) {
7770d8159dSGreg Roach                // No month 6 in Jewish non-leap years.
7870d8159dSGreg Roach                continue;
7970d8159dSGreg Roach            }
8070d8159dSGreg Roach
8170d8159dSGreg Roach            if ($month_number === 7 && $calendar_date instanceof JewishDate && !$calendar_date->isLeapYear()) {
8270d8159dSGreg Roach                // Month 7 is ADR in Jewish non-leap years (and ADS in others).
8370d8159dSGreg Roach                $mon = 'ADR';
8470d8159dSGreg Roach            } else {
8570d8159dSGreg Roach                $mon = $calendar_date->format('%O');
8670d8159dSGreg Roach            }
8770d8159dSGreg Roach
8870d8159dSGreg Roach            $month_names[$mon] = $calendar_date->format('%F');
8970d8159dSGreg Roach        }
9070d8159dSGreg Roach
9170d8159dSGreg Roach        return $month_names;
9270d8159dSGreg Roach    }
9370d8159dSGreg Roach
9470d8159dSGreg Roach    /**
95fceda430SGreg Roach     * Get a list of events which occurred during a given date range.
96f0a11419SGreg Roach     *
97f0a11419SGreg Roach     * @param int    $jd1      the start range of julian day
98f0a11419SGreg Roach     * @param int    $jd2      the end range of julian day
990a614d28SGreg Roach     * @param string $facts    restrict the search to just these facts or leave blank for all
100f0a11419SGreg Roach     * @param Tree   $tree     the tree to search
101b00cb080SGreg Roach     * @param string $filterof filter by living/recent
102b00cb080SGreg Roach     * @param string $filtersx filter by sex
103f0a11419SGreg Roach     *
10409482a55SGreg Roach     * @return array<Fact>
105f0a11419SGreg Roach     */
106b00cb080SGreg Roach    public function getCalendarEvents(int $jd1, int $jd2, string $facts, Tree $tree, string $filterof = '', string $filtersx = ''): array
107f0a11419SGreg Roach    {
108f0a11419SGreg Roach        // Events that start or end during the period
10925d4b688SGreg Roach        $query = DB::table('dates')
1109e87c1a2SGreg Roach            ->where('d_file', '=', $tree->id())
1110b5fd0a6SGreg Roach            ->where(static function (Builder $query) use ($jd1, $jd2): void {
1120b5fd0a6SGreg Roach                $query->where(static function (Builder $query) use ($jd1, $jd2): void {
1139e87c1a2SGreg Roach                    $query
1149e87c1a2SGreg Roach                        ->where('d_julianday1', '>=', $jd1)
1159e87c1a2SGreg Roach                        ->where('d_julianday1', '<=', $jd2);
1160b5fd0a6SGreg Roach                })->orWhere(static function (Builder $query) use ($jd1, $jd2): void {
1179e87c1a2SGreg Roach                    $query
1189e87c1a2SGreg Roach                        ->where('d_julianday2', '>=', $jd1)
1199e87c1a2SGreg Roach                        ->where('d_julianday2', '<=', $jd2);
1209e87c1a2SGreg Roach                });
1219e87c1a2SGreg Roach            });
122f0a11419SGreg Roach
123f0a11419SGreg Roach        // Restrict to certain types of fact
1240a614d28SGreg Roach        if ($facts === '') {
1250a614d28SGreg Roach            $query->whereNotIn('d_fact', self::SKIP_FACTS);
126f0a11419SGreg Roach        } else {
1270a614d28SGreg Roach            preg_match_all('/([_A-Z]+)/', $facts, $matches);
1280a614d28SGreg Roach
1290a614d28SGreg Roach            $query->whereIn('d_fact', $matches[1]);
130f0a11419SGreg Roach        }
131f0a11419SGreg Roach
132b00cb080SGreg Roach        if ($filterof === 'recent') {
133d97083feSGreg Roach            $query->where('d_julianday1', '>=', Registry::timestampFactory()->now()->subtractYears(100)->julianDay());
134b00cb080SGreg Roach        }
135b00cb080SGreg Roach
13625d4b688SGreg Roach        $ind_query = (clone $query)
1370b5fd0a6SGreg Roach            ->join('individuals', static function (JoinClause $join): void {
13825d4b688SGreg Roach                $join->on('d_gid', '=', 'i_id')->on('d_file', '=', 'i_file');
13925d4b688SGreg Roach            })
14025d4b688SGreg Roach            ->select(['i_id AS xref', 'i_gedcom AS gedcom', 'd_type', 'd_day', 'd_month', 'd_year', 'd_fact', 'd_type']);
141f0a11419SGreg Roach
142b00cb080SGreg Roach        $queries = ['INDI' => $ind_query];
143b00cb080SGreg Roach
144b00cb080SGreg Roach        if ($filtersx === '') {
14525d4b688SGreg Roach            $fam_query = (clone $query)
1460b5fd0a6SGreg Roach                ->join('families', static function (JoinClause $join): void {
14725d4b688SGreg Roach                    $join->on('d_gid', '=', 'f_id')->on('d_file', '=', 'f_file');
14825d4b688SGreg Roach                })
14925d4b688SGreg Roach                ->select(['f_id AS xref', 'f_gedcom AS gedcom', 'd_type', 'd_day', 'd_month', 'd_year', 'd_fact', 'd_type']);
15025d4b688SGreg Roach
151b00cb080SGreg Roach            $queries['FAM'] = $fam_query;
152b00cb080SGreg Roach        } else {
153b00cb080SGreg Roach            $queries['INDI']->where('i_sex', '=', $filtersx);
154b00cb080SGreg Roach        }
155b00cb080SGreg Roach
15625d4b688SGreg Roach        // Now fetch these events
15725d4b688SGreg Roach        $found_facts = [];
15825d4b688SGreg Roach
159b00cb080SGreg Roach        foreach ($queries as $type => $record_query) {
16061b2e6eeSGreg Roach            foreach ($record_query->get() as $row) {
161f0a11419SGreg Roach                if ($type === 'INDI') {
1626b9cb339SGreg Roach                    $record = Registry::individualFactory()->make($row->xref, $tree, $row->gedcom);
163b00cb080SGreg Roach                    assert($record instanceof Individual);
164b00cb080SGreg Roach
165b00cb080SGreg Roach                    if ($filterof === 'living' && $record->isDead()) {
166b00cb080SGreg Roach                        continue;
167b00cb080SGreg Roach                    }
168f0a11419SGreg Roach                } else {
1696b9cb339SGreg Roach                    $record = Registry::familyFactory()->make($row->xref, $tree, $row->gedcom);
170b00cb080SGreg Roach                    assert($record instanceof Family);
171b00cb080SGreg Roach                    $husb = $record->husband();
172b00cb080SGreg Roach                    $wife = $record->wife();
173b00cb080SGreg Roach
174b00cb080SGreg Roach                    if ($filterof === 'living' && ($husb && $husb->isDead() || $wife && $wife->isDead())) {
175b00cb080SGreg Roach                        continue;
176b00cb080SGreg Roach                    }
177f0a11419SGreg Roach                }
17825d4b688SGreg Roach
179f0a11419SGreg Roach                $anniv_date = new Date($row->d_type . ' ' . $row->d_day . ' ' . $row->d_month . ' ' . $row->d_year);
18025d4b688SGreg Roach
181b00cb080SGreg Roach                foreach ($record->facts([$row->d_fact]) as $fact) {
182f0a11419SGreg Roach                    // For date ranges, we need a match on either the start/end.
183dd71ff6bSGreg Roach                    if ($fact->date()->minimumJulianDay() === $anniv_date->minimumJulianDay() || $fact->date()->maximumJulianDay() === $anniv_date->maximumJulianDay()) {
184f0a11419SGreg Roach                        $fact->anniv   = 0;
185f0a11419SGreg Roach                        $found_facts[] = $fact;
186f0a11419SGreg Roach                    }
187f0a11419SGreg Roach                }
188f0a11419SGreg Roach            }
189f0a11419SGreg Roach        }
190f0a11419SGreg Roach
191f0a11419SGreg Roach        return $found_facts;
192f0a11419SGreg Roach    }
193f0a11419SGreg Roach
194f0a11419SGreg Roach    /**
195f0a11419SGreg Roach     * Get the list of current and upcoming events, sorted by anniversary date
196f0a11419SGreg Roach     *
197f0a11419SGreg Roach     * @param int    $jd1
198f0a11419SGreg Roach     * @param int    $jd2
199f0a11419SGreg Roach     * @param string $events
200c7ff4153SGreg Roach     * @param bool   $only_living
201f0a11419SGreg Roach     * @param string $sort_by
202f0a11419SGreg Roach     * @param Tree   $tree
203f0a11419SGreg Roach     *
20436779af1SGreg Roach     * @return Collection<int,Fact>
205f0a11419SGreg Roach     */
206cd51dbdfSGreg Roach    public function getEventsList(int $jd1, int $jd2, string $events, bool $only_living, string $sort_by, Tree $tree): Collection
207f0a11419SGreg Roach    {
208f0a11419SGreg Roach        $found_facts = [];
209cd51dbdfSGreg Roach        $facts       = new Collection();
210f0a11419SGreg Roach
211f0a11419SGreg Roach        foreach (range($jd1, $jd2) as $jd) {
212f0a11419SGreg Roach            $found_facts = array_merge($found_facts, $this->getAnniversaryEvents($jd, $events, $tree));
213f0a11419SGreg Roach        }
214f0a11419SGreg Roach
215f0a11419SGreg Roach        foreach ($found_facts as $fact) {
216e7766c08SGreg Roach            $record = $fact->record();
217f0a11419SGreg Roach            // only living people ?
218f0a11419SGreg Roach            if ($only_living) {
219f0a11419SGreg Roach                if ($record instanceof Individual && $record->isDead()) {
220f0a11419SGreg Roach                    continue;
221f0a11419SGreg Roach                }
222f0a11419SGreg Roach                if ($record instanceof Family) {
22339ca88baSGreg Roach                    $husb = $record->husband();
2248f038c36SRico Sonntag                    if ($husb === null || $husb->isDead()) {
225f0a11419SGreg Roach                        continue;
226f0a11419SGreg Roach                    }
22739ca88baSGreg Roach                    $wife = $record->wife();
2288f038c36SRico Sonntag                    if ($wife === null || $wife->isDead()) {
229f0a11419SGreg Roach                        continue;
230f0a11419SGreg Roach                    }
231f0a11419SGreg Roach                }
232f0a11419SGreg Roach            }
233cd51dbdfSGreg Roach            $facts->push($fact);
234f0a11419SGreg Roach        }
235f0a11419SGreg Roach
236f0a11419SGreg Roach        switch ($sort_by) {
237f0a11419SGreg Roach            case 'anniv':
2386f97ab23SGreg Roach            case 'anniv_asc':
239*f25fc0f9SGreg Roach                $facts = $facts->sort(static fn (Fact $x, Fact $y): int => $x->jd <=> $y->jd ?: $x->date()->minimumJulianDay() <=> $y->date()->minimumJulianDay());
2406f97ab23SGreg Roach                break;
2416f97ab23SGreg Roach
2426f97ab23SGreg Roach            case 'anniv_desc':
243*f25fc0f9SGreg Roach                $facts = $facts->sort(static fn (Fact $x, Fact $y): int => $x->jd <=> $y->jd ?: $y->date()->minimumJulianDay() <=> $x->date()->minimumJulianDay());
244f0a11419SGreg Roach                break;
245580a4d11SGreg Roach
246f0a11419SGreg Roach            case 'alpha':
247*f25fc0f9SGreg Roach                $facts = $facts->sort(static fn (Fact $x, Fact $y): int => GedcomRecord::nameComparator()($x->record(), $y->record()));
248f0a11419SGreg Roach                break;
249f0a11419SGreg Roach        }
250f0a11419SGreg Roach
251e24053e5SGreg Roach        return $facts->values();
252f0a11419SGreg Roach    }
253f0a11419SGreg Roach
254f0a11419SGreg Roach    /**
255fceda430SGreg Roach     * Get a list of events whose anniversary occurred on a given julian day.
256f0a11419SGreg Roach     * Used on the on-this-day/upcoming blocks and the day/month calendar views.
257f0a11419SGreg Roach     *
258f0a11419SGreg Roach     * @param int    $jd       the julian day
259f0a11419SGreg Roach     * @param string $facts    restrict the search to just these facts or leave blank for all
260f0a11419SGreg Roach     * @param Tree   $tree     the tree to search
261b00cb080SGreg Roach     * @param string $filterof filter by living/recent
262b00cb080SGreg Roach     * @param string $filtersx filter by sex
263f0a11419SGreg Roach     *
26409482a55SGreg Roach     * @return array<Fact>
265f0a11419SGreg Roach     */
26624f2a3afSGreg Roach    public function getAnniversaryEvents(int $jd, string $facts, Tree $tree, string $filterof = '', string $filtersx = ''): array
267f0a11419SGreg Roach    {
268f0a11419SGreg Roach        $found_facts = [];
269f0a11419SGreg Roach
270f0a11419SGreg Roach        $anniversaries = [
271f0a11419SGreg Roach            new GregorianDate($jd),
272f0a11419SGreg Roach            new JulianDate($jd),
273f0a11419SGreg Roach            new FrenchDate($jd),
274f0a11419SGreg Roach            new JewishDate($jd),
275f0a11419SGreg Roach            new HijriDate($jd),
276f0a11419SGreg Roach        ];
277f0a11419SGreg Roach
2781bd3bc77SGreg Roach        // There is a bug in the Persian Calendar that gives zero months for invalid dates
2794a9ac934SGreg Roach        if ($jd > (new PersianCalendar())->jdStart()) {
2804a9ac934SGreg Roach            $anniversaries[] = new JalaliDate($jd);
2814a9ac934SGreg Roach        }
2824a9ac934SGreg Roach
283f0a11419SGreg Roach        foreach ($anniversaries as $anniv) {
28425d4b688SGreg Roach            // Build a query to match anniversaries in the appropriate calendar.
28525d4b688SGreg Roach            $query = DB::table('dates')
28625d4b688SGreg Roach                ->distinct()
28725d4b688SGreg Roach                ->where('d_file', '=', $tree->id())
28825d4b688SGreg Roach                ->where('d_type', '=', $anniv->format('%@'));
289f0a11419SGreg Roach
290f0a11419SGreg Roach            // SIMPLE CASES:
291f0a11419SGreg Roach            // a) Non-hebrew anniversaries
292f0a11419SGreg Roach            // b) Hebrew months TVT, SHV, IYR, SVN, TMZ, AAV, ELL
293e364afe4SGreg Roach            if (!$anniv instanceof JewishDate || in_array($anniv->month, [1, 5, 6, 9, 10, 11, 12, 13], true)) {
29425d4b688SGreg Roach                $this->defaultAnniversaries($query, $anniv);
295f0a11419SGreg Roach            } else {
296f0a11419SGreg Roach                // SPECIAL CASES:
2974a83f5d7SGreg Roach                switch ($anniv->month) {
298f0a11419SGreg Roach                    case 2:
29925d4b688SGreg Roach                        $this->cheshvanAnniversaries($query, $anniv);
300f0a11419SGreg Roach                        break;
301f0a11419SGreg Roach                    case 3:
30225d4b688SGreg Roach                        $this->kislevAnniversaries($query, $anniv);
303f0a11419SGreg Roach                        break;
304f0a11419SGreg Roach                    case 4:
30525d4b688SGreg Roach                        $this->tevetAnniversaries($query, $anniv);
306f0a11419SGreg Roach                        break;
30725d4b688SGreg Roach                    case 7:
30825d4b688SGreg Roach                        $this->adarIIAnniversaries($query, $anniv);
309f0a11419SGreg Roach                        break;
31025d4b688SGreg Roach                    case 8:
31125d4b688SGreg Roach                        $this->nisanAnniversaries($query, $anniv);
312f0a11419SGreg Roach                        break;
313f0a11419SGreg Roach                }
314f0a11419SGreg Roach            }
315f0a11419SGreg Roach            // Only events in the past (includes dates without a year)
31625d4b688SGreg Roach            $query->where('d_year', '<=', $anniv->year());
317f0a11419SGreg Roach
3180a614d28SGreg Roach            if ($facts === '') {
3190a614d28SGreg Roach                // If no facts specified, get all except these
3200a614d28SGreg Roach                $query->whereNotIn('d_fact', self::SKIP_FACTS);
3210a614d28SGreg Roach            } else {
3220a614d28SGreg Roach                // Restrict to certain types of fact
323f0a11419SGreg Roach                preg_match_all('/([_A-Z]+)/', $facts, $matches);
32425d4b688SGreg Roach
32525d4b688SGreg Roach                $query->whereIn('d_fact', $matches[1]);
326f0a11419SGreg Roach            }
327f0a11419SGreg Roach
328b00cb080SGreg Roach            if ($filterof === 'recent') {
329d97083feSGreg Roach                $query->where('d_julianday1', '>=', Registry::timestampFactory()->now()->subtractYears(100)->julianDay());
330b00cb080SGreg Roach            }
331b00cb080SGreg Roach
33225d4b688SGreg Roach            $query
33394969abbSRico Sonntag                ->orderBy('d_day')
3340195b0ddSGreg Roach                ->orderBy('d_year', 'DESC');
33525d4b688SGreg Roach
33625d4b688SGreg Roach            $ind_query = (clone $query)
3370b5fd0a6SGreg Roach                ->join('individuals', static function (JoinClause $join): void {
33825d4b688SGreg Roach                    $join->on('d_gid', '=', 'i_id')->on('d_file', '=', 'i_file');
33925d4b688SGreg Roach                })
34025d4b688SGreg Roach                ->select(['i_id AS xref', 'i_gedcom AS gedcom', 'd_type', 'd_day', 'd_month', 'd_year', 'd_fact']);
34125d4b688SGreg Roach
342b00cb080SGreg Roach            $queries = ['INDI' => $ind_query];
343b00cb080SGreg Roach
344b00cb080SGreg Roach            if ($filtersx === '') {
34525d4b688SGreg Roach                $fam_query = (clone $query)
3460b5fd0a6SGreg Roach                    ->join('families', static function (JoinClause $join): void {
34725d4b688SGreg Roach                        $join->on('d_gid', '=', 'f_id')->on('d_file', '=', 'f_file');
34825d4b688SGreg Roach                    })
34925d4b688SGreg Roach                    ->select(['f_id AS xref', 'f_gedcom AS gedcom', 'd_type', 'd_day', 'd_month', 'd_year', 'd_fact']);
350f0a11419SGreg Roach
351b00cb080SGreg Roach                $queries['FAM'] = $fam_query;
352b00cb080SGreg Roach            } else {
353b00cb080SGreg Roach                $queries['INDI']->where('i_sex', '=', $filtersx);
354b00cb080SGreg Roach            }
355b00cb080SGreg Roach
356f0a11419SGreg Roach            // Now fetch these anniversaries
357b00cb080SGreg Roach            foreach ($queries as $type => $record_query) {
35861b2e6eeSGreg Roach                foreach ($record_query->get() as $row) {
359f0a11419SGreg Roach                    if ($type === 'INDI') {
3606b9cb339SGreg Roach                        $record = Registry::individualFactory()->make($row->xref, $tree, $row->gedcom);
361b00cb080SGreg Roach                        assert($record instanceof Individual);
362b00cb080SGreg Roach
363b00cb080SGreg Roach                        if ($filterof === 'living' && $record->isDead()) {
364b00cb080SGreg Roach                            continue;
365b00cb080SGreg Roach                        }
366f0a11419SGreg Roach                    } else {
3676b9cb339SGreg Roach                        $record = Registry::familyFactory()->make($row->xref, $tree, $row->gedcom);
368b00cb080SGreg Roach                        assert($record instanceof Family);
369b00cb080SGreg Roach                        $husb = $record->husband();
370b00cb080SGreg Roach                        $wife = $record->wife();
371b00cb080SGreg Roach
372b00cb080SGreg Roach                        if ($filterof === 'living' && ($husb && $husb->isDead() || $wife && $wife->isDead())) {
373b00cb080SGreg Roach                            continue;
374b00cb080SGreg Roach                        }
375f0a11419SGreg Roach                    }
37625d4b688SGreg Roach
377f0a11419SGreg Roach                    $anniv_date = new Date($row->d_type . ' ' . $row->d_day . ' ' . $row->d_month . ' ' . $row->d_year);
37825d4b688SGreg Roach
379543ab5f6SGreg Roach                    // The record may have multiple facts of this type.
380543ab5f6SGreg Roach                    // Find the ones that match the date.
381543ab5f6SGreg Roach                    foreach ($record->facts([$row->d_fact]) as $fact) {
382543ab5f6SGreg Roach                        $min_date = $fact->date()->minimumDate();
383543ab5f6SGreg Roach                        $max_date = $fact->date()->maximumDate();
384543ab5f6SGreg Roach
385543ab5f6SGreg Roach                        if ($min_date->minimumJulianDay() === $anniv_date->minimumJulianDay() && $min_date::ESCAPE === $row->d_type || $max_date->maximumJulianDay() === $anniv_date->maximumJulianDay() && $max_date::ESCAPE === $row->d_type) {
3864a83f5d7SGreg Roach                            $fact->anniv   = $row->d_year === '0' ? 0 : $anniv->year - $row->d_year;
387f0a11419SGreg Roach                            $fact->jd      = $jd;
388f0a11419SGreg Roach                            $found_facts[] = $fact;
389f0a11419SGreg Roach                        }
390f0a11419SGreg Roach                    }
391f0a11419SGreg Roach                }
392f0a11419SGreg Roach            }
393f0a11419SGreg Roach        }
394f0a11419SGreg Roach
395f0a11419SGreg Roach        return $found_facts;
396f0a11419SGreg Roach    }
39725d4b688SGreg Roach
39825d4b688SGreg Roach    /**
39925d4b688SGreg Roach     * By default, missing days have anniversaries on the first of the month,
40025d4b688SGreg Roach     * and invalid days have anniversaries on the last day of the month.
40125d4b688SGreg Roach     *
40225d4b688SGreg Roach     * @param Builder              $query
40325d4b688SGreg Roach     * @param AbstractCalendarDate $anniv
40425d4b688SGreg Roach     */
40525d4b688SGreg Roach    private function defaultAnniversaries(Builder $query, AbstractCalendarDate $anniv): void
40625d4b688SGreg Roach    {
40725d4b688SGreg Roach        if ($anniv->day() === 1) {
40825d4b688SGreg Roach            $query->where('d_day', '<=', 1);
40925d4b688SGreg Roach        } elseif ($anniv->day() === $anniv->daysInMonth()) {
41025d4b688SGreg Roach            $query->where('d_day', '>=', $anniv->daysInMonth());
41125d4b688SGreg Roach        } else {
41225d4b688SGreg Roach            $query->where('d_day', '=', $anniv->day());
41325d4b688SGreg Roach        }
41425d4b688SGreg Roach
41525d4b688SGreg Roach        $query->where('d_mon', '=', $anniv->month());
41625d4b688SGreg Roach    }
41725d4b688SGreg Roach
41825d4b688SGreg Roach    /**
41925d4b688SGreg Roach     * 29 CSH does not include 30 CSH (but would include an invalid 31 CSH if there were no 30 CSH).
42025d4b688SGreg Roach     *
42125d4b688SGreg Roach     * @param Builder    $query
42225d4b688SGreg Roach     * @param JewishDate $anniv
42325d4b688SGreg Roach     */
42425d4b688SGreg Roach    private function cheshvanAnniversaries(Builder $query, JewishDate $anniv): void
42525d4b688SGreg Roach    {
42625d4b688SGreg Roach        if ($anniv->day === 29 && $anniv->daysInMonth() === 29) {
42725d4b688SGreg Roach            $query
42825d4b688SGreg Roach                ->where('d_mon', '=', 2)
42925d4b688SGreg Roach                ->where('d_day', '>=', 29)
43025d4b688SGreg Roach                ->where('d_day', '<>', 30);
43125d4b688SGreg Roach        } else {
43225d4b688SGreg Roach            $this->defaultAnniversaries($query, $anniv);
43325d4b688SGreg Roach        }
43425d4b688SGreg Roach    }
43525d4b688SGreg Roach
43625d4b688SGreg Roach    /**
43725d4b688SGreg Roach     * 1 KSL includes 30 CSH (if this year didn’t have 30 CSH).
43825d4b688SGreg Roach     * 29 KSL does not include 30 KSL (but would include an invalid 31 KSL if there were no 30 KSL).
43925d4b688SGreg Roach     *
44025d4b688SGreg Roach     * @param Builder    $query
44125d4b688SGreg Roach     * @param JewishDate $anniv
44225d4b688SGreg Roach     */
44325d4b688SGreg Roach    private function kislevAnniversaries(Builder $query, JewishDate $anniv): void
44425d4b688SGreg Roach    {
44525d4b688SGreg Roach        $tmp = new JewishDate([(string) $anniv->year, 'CSH', '1']);
44625d4b688SGreg Roach
44725d4b688SGreg Roach        if ($anniv->day() === 1 && $tmp->daysInMonth() === 29) {
4480b5fd0a6SGreg Roach            $query->where(static function (Builder $query): void {
4490b5fd0a6SGreg Roach                $query->where(static function (Builder $query): void {
45025d4b688SGreg Roach                    $query->where('d_day', '<=', 1)->where('d_mon', '=', 3);
4510b5fd0a6SGreg Roach                })->orWhere(static function (Builder $query): void {
45225d4b688SGreg Roach                    $query->where('d_day', '=', 30)->where('d_mon', '=', 2);
45325d4b688SGreg Roach                });
45425d4b688SGreg Roach            });
45525d4b688SGreg Roach        } elseif ($anniv->day === 29 && $anniv->daysInMonth() === 29) {
45625d4b688SGreg Roach            $query
45725d4b688SGreg Roach                ->where('d_mon', '=', 3)
45825d4b688SGreg Roach                ->where('d_day', '>=', 29)
45925d4b688SGreg Roach                ->where('d_day', '<>', 30);
46025d4b688SGreg Roach        } else {
46125d4b688SGreg Roach            $this->defaultAnniversaries($query, $anniv);
46225d4b688SGreg Roach        }
46325d4b688SGreg Roach    }
46425d4b688SGreg Roach
46525d4b688SGreg Roach    /**
46625d4b688SGreg Roach     * 1 TVT includes 30 KSL (if this year didn’t have 30 KSL).
46725d4b688SGreg Roach     *
46825d4b688SGreg Roach     * @param Builder    $query
46925d4b688SGreg Roach     * @param JewishDate $anniv
47025d4b688SGreg Roach     */
47125d4b688SGreg Roach    private function tevetAnniversaries(Builder $query, JewishDate $anniv): void
47225d4b688SGreg Roach    {
47325d4b688SGreg Roach        $tmp = new JewishDate([(string) $anniv->year, 'KSL', '1']);
47425d4b688SGreg Roach
47525d4b688SGreg Roach        if ($anniv->day === 1 && $tmp->daysInMonth() === 29) {
4760b5fd0a6SGreg Roach            $query->where(static function (Builder $query): void {
4770b5fd0a6SGreg Roach                $query->where(static function (Builder $query): void {
47825d4b688SGreg Roach                    $query->where('d_day', '<=', 1)->where('d_mon', '=', 4);
4790b5fd0a6SGreg Roach                })->orWhere(static function (Builder $query): void {
48025d4b688SGreg Roach                    $query->where('d_day', '=', 30)->where('d_mon', '=', 3);
48125d4b688SGreg Roach                });
48225d4b688SGreg Roach            });
48325d4b688SGreg Roach        } else {
48425d4b688SGreg Roach            $this->defaultAnniversaries($query, $anniv);
48525d4b688SGreg Roach        }
48625d4b688SGreg Roach    }
48725d4b688SGreg Roach
48825d4b688SGreg Roach    /**
48925d4b688SGreg Roach     * ADS includes non-leap ADR.
49025d4b688SGreg Roach     *
49125d4b688SGreg Roach     * @param Builder    $query
49225d4b688SGreg Roach     * @param JewishDate $anniv
49325d4b688SGreg Roach     */
49425d4b688SGreg Roach    private function adarIIAnniversaries(Builder $query, JewishDate $anniv): void
49525d4b688SGreg Roach    {
49625d4b688SGreg Roach        if ($anniv->day() === 1) {
49725d4b688SGreg Roach            $query->where('d_day', '<=', 1);
49825d4b688SGreg Roach        } elseif ($anniv->day() === $anniv->daysInMonth()) {
49925d4b688SGreg Roach            $query->where('d_day', '>=', $anniv->daysInMonth());
500569a8ff0SGreg Roach            if ($anniv->daysInMonth() === 29) {
501569a8ff0SGreg Roach                // On short months, 30th Adar shown on 1st Nissan
502569a8ff0SGreg Roach                $query->where('d_day', '<>', 30);
503569a8ff0SGreg Roach            }
50425d4b688SGreg Roach        } else {
5050bab1c4aSGreg Roach            $query->where('d_day', '=', $anniv->day());
50625d4b688SGreg Roach        }
50725d4b688SGreg Roach
508082dc12eSGreg Roach        if ($anniv->isLeapYear()) {
509082dc12eSGreg Roach            $query->where('d_mon', '=', 7);
510082dc12eSGreg Roach        } else {
511082dc12eSGreg Roach            $query->whereIn('d_mon', [6, 7]);
512082dc12eSGreg Roach        }
51325d4b688SGreg Roach    }
51425d4b688SGreg Roach
51525d4b688SGreg Roach    /**
51625d4b688SGreg Roach     * 1 NSN includes 30 ADR, if this year is non-leap.
51725d4b688SGreg Roach     *
51825d4b688SGreg Roach     * @param Builder    $query
51925d4b688SGreg Roach     * @param JewishDate $anniv
52025d4b688SGreg Roach     */
52125d4b688SGreg Roach    private function nisanAnniversaries(Builder $query, JewishDate $anniv): void
52225d4b688SGreg Roach    {
52325d4b688SGreg Roach        if ($anniv->day === 1 && !$anniv->isLeapYear()) {
5240b5fd0a6SGreg Roach            $query->where(static function (Builder $query): void {
5250b5fd0a6SGreg Roach                $query->where(static function (Builder $query): void {
52625d4b688SGreg Roach                    $query->where('d_day', '<=', 1)->where('d_mon', '=', 8);
5270b5fd0a6SGreg Roach                })->orWhere(static function (Builder $query): void {
52825d4b688SGreg Roach                    $query->where('d_day', '=', 30)->where('d_mon', '=', 6);
52925d4b688SGreg Roach                });
53025d4b688SGreg Roach            });
53125d4b688SGreg Roach        } else {
53225d4b688SGreg Roach            $this->defaultAnniversaries($query, $anniv);
53325d4b688SGreg Roach        }
53425d4b688SGreg Roach    }
535f0a11419SGreg Roach}
536