1f0a11419SGreg Roach<?php 23976b470SGreg Roach 3f0a11419SGreg Roach/** 4f0a11419SGreg Roach * webtrees: online genealogy 5*d11be702SGreg 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; 31f0a11419SGreg Roachuse Fisharebest\Webtrees\Fact; 32f0a11419SGreg Roachuse Fisharebest\Webtrees\Family; 33f0a11419SGreg Roachuse Fisharebest\Webtrees\GedcomRecord; 34f0a11419SGreg Roachuse Fisharebest\Webtrees\Individual; 35f0c88a96SGreg Roachuse Fisharebest\Webtrees\Registry; 36f0a11419SGreg Roachuse Fisharebest\Webtrees\Tree; 379e87c1a2SGreg Roachuse Illuminate\Database\Capsule\Manager as DB; 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': 239cd51dbdfSGreg Roach $facts = $facts->sort(static function (Fact $x, Fact $y): int { 2406f97ab23SGreg Roach return $x->jd <=> $y->jd ?: $x->date()->minimumJulianDay() <=> $y->date()->minimumJulianDay(); 2416f97ab23SGreg Roach }); 2426f97ab23SGreg Roach break; 2436f97ab23SGreg Roach 2446f97ab23SGreg Roach case 'anniv_desc': 2456f97ab23SGreg Roach $facts = $facts->sort(static function (Fact $x, Fact $y): int { 2466f97ab23SGreg Roach return $x->jd <=> $y->jd ?: $y->date()->minimumJulianDay() <=> $x->date()->minimumJulianDay(); 247cd51dbdfSGreg Roach }); 248f0a11419SGreg Roach break; 249580a4d11SGreg Roach 250f0a11419SGreg Roach case 'alpha': 251cd51dbdfSGreg Roach $facts = $facts->sort(static function (Fact $x, Fact $y): int { 252c156e8f5SGreg Roach return GedcomRecord::nameComparator()($x->record(), $y->record()); 253f0a11419SGreg Roach }); 254f0a11419SGreg Roach break; 255f0a11419SGreg Roach } 256f0a11419SGreg Roach 257e24053e5SGreg Roach return $facts->values(); 258f0a11419SGreg Roach } 259f0a11419SGreg Roach 260f0a11419SGreg Roach /** 261fceda430SGreg Roach * Get a list of events whose anniversary occurred on a given julian day. 262f0a11419SGreg Roach * Used on the on-this-day/upcoming blocks and the day/month calendar views. 263f0a11419SGreg Roach * 264f0a11419SGreg Roach * @param int $jd the julian day 265f0a11419SGreg Roach * @param string $facts restrict the search to just these facts or leave blank for all 266f0a11419SGreg Roach * @param Tree $tree the tree to search 267b00cb080SGreg Roach * @param string $filterof filter by living/recent 268b00cb080SGreg Roach * @param string $filtersx filter by sex 269f0a11419SGreg Roach * 27009482a55SGreg Roach * @return array<Fact> 271f0a11419SGreg Roach */ 27224f2a3afSGreg Roach public function getAnniversaryEvents(int $jd, string $facts, Tree $tree, string $filterof = '', string $filtersx = ''): array 273f0a11419SGreg Roach { 274f0a11419SGreg Roach $found_facts = []; 275f0a11419SGreg Roach 276f0a11419SGreg Roach $anniversaries = [ 277f0a11419SGreg Roach new GregorianDate($jd), 278f0a11419SGreg Roach new JulianDate($jd), 279f0a11419SGreg Roach new FrenchDate($jd), 280f0a11419SGreg Roach new JewishDate($jd), 281f0a11419SGreg Roach new HijriDate($jd), 282f0a11419SGreg Roach ]; 283f0a11419SGreg Roach 2841bd3bc77SGreg Roach // There is a bug in the Persian Calendar that gives zero months for invalid dates 2854a9ac934SGreg Roach if ($jd > (new PersianCalendar())->jdStart()) { 2864a9ac934SGreg Roach $anniversaries[] = new JalaliDate($jd); 2874a9ac934SGreg Roach } 2884a9ac934SGreg Roach 289f0a11419SGreg Roach foreach ($anniversaries as $anniv) { 29025d4b688SGreg Roach // Build a query to match anniversaries in the appropriate calendar. 29125d4b688SGreg Roach $query = DB::table('dates') 29225d4b688SGreg Roach ->distinct() 29325d4b688SGreg Roach ->where('d_file', '=', $tree->id()) 29425d4b688SGreg Roach ->where('d_type', '=', $anniv->format('%@')); 295f0a11419SGreg Roach 296f0a11419SGreg Roach // SIMPLE CASES: 297f0a11419SGreg Roach // a) Non-hebrew anniversaries 298f0a11419SGreg Roach // b) Hebrew months TVT, SHV, IYR, SVN, TMZ, AAV, ELL 299e364afe4SGreg Roach if (!$anniv instanceof JewishDate || in_array($anniv->month, [1, 5, 6, 9, 10, 11, 12, 13], true)) { 30025d4b688SGreg Roach $this->defaultAnniversaries($query, $anniv); 301f0a11419SGreg Roach } else { 302f0a11419SGreg Roach // SPECIAL CASES: 3034a83f5d7SGreg Roach switch ($anniv->month) { 304f0a11419SGreg Roach case 2: 30525d4b688SGreg Roach $this->cheshvanAnniversaries($query, $anniv); 306f0a11419SGreg Roach break; 307f0a11419SGreg Roach case 3: 30825d4b688SGreg Roach $this->kislevAnniversaries($query, $anniv); 309f0a11419SGreg Roach break; 310f0a11419SGreg Roach case 4: 31125d4b688SGreg Roach $this->tevetAnniversaries($query, $anniv); 312f0a11419SGreg Roach break; 31325d4b688SGreg Roach case 7: 31425d4b688SGreg Roach $this->adarIIAnniversaries($query, $anniv); 315f0a11419SGreg Roach break; 31625d4b688SGreg Roach case 8: 31725d4b688SGreg Roach $this->nisanAnniversaries($query, $anniv); 318f0a11419SGreg Roach break; 319f0a11419SGreg Roach } 320f0a11419SGreg Roach } 321f0a11419SGreg Roach // Only events in the past (includes dates without a year) 32225d4b688SGreg Roach $query->where('d_year', '<=', $anniv->year()); 323f0a11419SGreg Roach 3240a614d28SGreg Roach if ($facts === '') { 3250a614d28SGreg Roach // If no facts specified, get all except these 3260a614d28SGreg Roach $query->whereNotIn('d_fact', self::SKIP_FACTS); 3270a614d28SGreg Roach } else { 3280a614d28SGreg Roach // Restrict to certain types of fact 329f0a11419SGreg Roach preg_match_all('/([_A-Z]+)/', $facts, $matches); 33025d4b688SGreg Roach 33125d4b688SGreg Roach $query->whereIn('d_fact', $matches[1]); 332f0a11419SGreg Roach } 333f0a11419SGreg Roach 334b00cb080SGreg Roach if ($filterof === 'recent') { 335d97083feSGreg Roach $query->where('d_julianday1', '>=', Registry::timestampFactory()->now()->subtractYears(100)->julianDay()); 336b00cb080SGreg Roach } 337b00cb080SGreg Roach 33825d4b688SGreg Roach $query 33994969abbSRico Sonntag ->orderBy('d_day') 3400195b0ddSGreg Roach ->orderBy('d_year', 'DESC'); 34125d4b688SGreg Roach 34225d4b688SGreg Roach $ind_query = (clone $query) 3430b5fd0a6SGreg Roach ->join('individuals', static function (JoinClause $join): void { 34425d4b688SGreg Roach $join->on('d_gid', '=', 'i_id')->on('d_file', '=', 'i_file'); 34525d4b688SGreg Roach }) 34625d4b688SGreg Roach ->select(['i_id AS xref', 'i_gedcom AS gedcom', 'd_type', 'd_day', 'd_month', 'd_year', 'd_fact']); 34725d4b688SGreg Roach 348b00cb080SGreg Roach $queries = ['INDI' => $ind_query]; 349b00cb080SGreg Roach 350b00cb080SGreg Roach if ($filtersx === '') { 35125d4b688SGreg Roach $fam_query = (clone $query) 3520b5fd0a6SGreg Roach ->join('families', static function (JoinClause $join): void { 35325d4b688SGreg Roach $join->on('d_gid', '=', 'f_id')->on('d_file', '=', 'f_file'); 35425d4b688SGreg Roach }) 35525d4b688SGreg Roach ->select(['f_id AS xref', 'f_gedcom AS gedcom', 'd_type', 'd_day', 'd_month', 'd_year', 'd_fact']); 356f0a11419SGreg Roach 357b00cb080SGreg Roach $queries['FAM'] = $fam_query; 358b00cb080SGreg Roach } else { 359b00cb080SGreg Roach $queries['INDI']->where('i_sex', '=', $filtersx); 360b00cb080SGreg Roach } 361b00cb080SGreg Roach 362f0a11419SGreg Roach // Now fetch these anniversaries 363b00cb080SGreg Roach foreach ($queries as $type => $record_query) { 36461b2e6eeSGreg Roach foreach ($record_query->get() as $row) { 365f0a11419SGreg Roach if ($type === 'INDI') { 3666b9cb339SGreg Roach $record = Registry::individualFactory()->make($row->xref, $tree, $row->gedcom); 367b00cb080SGreg Roach assert($record instanceof Individual); 368b00cb080SGreg Roach 369b00cb080SGreg Roach if ($filterof === 'living' && $record->isDead()) { 370b00cb080SGreg Roach continue; 371b00cb080SGreg Roach } 372f0a11419SGreg Roach } else { 3736b9cb339SGreg Roach $record = Registry::familyFactory()->make($row->xref, $tree, $row->gedcom); 374b00cb080SGreg Roach assert($record instanceof Family); 375b00cb080SGreg Roach $husb = $record->husband(); 376b00cb080SGreg Roach $wife = $record->wife(); 377b00cb080SGreg Roach 378b00cb080SGreg Roach if ($filterof === 'living' && ($husb && $husb->isDead() || $wife && $wife->isDead())) { 379b00cb080SGreg Roach continue; 380b00cb080SGreg Roach } 381f0a11419SGreg Roach } 38225d4b688SGreg Roach 383f0a11419SGreg Roach $anniv_date = new Date($row->d_type . ' ' . $row->d_day . ' ' . $row->d_month . ' ' . $row->d_year); 38425d4b688SGreg Roach 385543ab5f6SGreg Roach // The record may have multiple facts of this type. 386543ab5f6SGreg Roach // Find the ones that match the date. 387543ab5f6SGreg Roach foreach ($record->facts([$row->d_fact]) as $fact) { 388543ab5f6SGreg Roach $min_date = $fact->date()->minimumDate(); 389543ab5f6SGreg Roach $max_date = $fact->date()->maximumDate(); 390543ab5f6SGreg Roach 391543ab5f6SGreg 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) { 3924a83f5d7SGreg Roach $fact->anniv = $row->d_year === '0' ? 0 : $anniv->year - $row->d_year; 393f0a11419SGreg Roach $fact->jd = $jd; 394f0a11419SGreg Roach $found_facts[] = $fact; 395f0a11419SGreg Roach } 396f0a11419SGreg Roach } 397f0a11419SGreg Roach } 398f0a11419SGreg Roach } 399f0a11419SGreg Roach } 400f0a11419SGreg Roach 401f0a11419SGreg Roach return $found_facts; 402f0a11419SGreg Roach } 40325d4b688SGreg Roach 40425d4b688SGreg Roach /** 40525d4b688SGreg Roach * By default, missing days have anniversaries on the first of the month, 40625d4b688SGreg Roach * and invalid days have anniversaries on the last day of the month. 40725d4b688SGreg Roach * 40825d4b688SGreg Roach * @param Builder $query 40925d4b688SGreg Roach * @param AbstractCalendarDate $anniv 41025d4b688SGreg Roach */ 41125d4b688SGreg Roach private function defaultAnniversaries(Builder $query, AbstractCalendarDate $anniv): void 41225d4b688SGreg Roach { 41325d4b688SGreg Roach if ($anniv->day() === 1) { 41425d4b688SGreg Roach $query->where('d_day', '<=', 1); 41525d4b688SGreg Roach } elseif ($anniv->day() === $anniv->daysInMonth()) { 41625d4b688SGreg Roach $query->where('d_day', '>=', $anniv->daysInMonth()); 41725d4b688SGreg Roach } else { 41825d4b688SGreg Roach $query->where('d_day', '=', $anniv->day()); 41925d4b688SGreg Roach } 42025d4b688SGreg Roach 42125d4b688SGreg Roach $query->where('d_mon', '=', $anniv->month()); 42225d4b688SGreg Roach } 42325d4b688SGreg Roach 42425d4b688SGreg Roach /** 42525d4b688SGreg Roach * 29 CSH does not include 30 CSH (but would include an invalid 31 CSH if there were no 30 CSH). 42625d4b688SGreg Roach * 42725d4b688SGreg Roach * @param Builder $query 42825d4b688SGreg Roach * @param JewishDate $anniv 42925d4b688SGreg Roach */ 43025d4b688SGreg Roach private function cheshvanAnniversaries(Builder $query, JewishDate $anniv): void 43125d4b688SGreg Roach { 43225d4b688SGreg Roach if ($anniv->day === 29 && $anniv->daysInMonth() === 29) { 43325d4b688SGreg Roach $query 43425d4b688SGreg Roach ->where('d_mon', '=', 2) 43525d4b688SGreg Roach ->where('d_day', '>=', 29) 43625d4b688SGreg Roach ->where('d_day', '<>', 30); 43725d4b688SGreg Roach } else { 43825d4b688SGreg Roach $this->defaultAnniversaries($query, $anniv); 43925d4b688SGreg Roach } 44025d4b688SGreg Roach } 44125d4b688SGreg Roach 44225d4b688SGreg Roach /** 44325d4b688SGreg Roach * 1 KSL includes 30 CSH (if this year didn’t have 30 CSH). 44425d4b688SGreg Roach * 29 KSL does not include 30 KSL (but would include an invalid 31 KSL if there were no 30 KSL). 44525d4b688SGreg Roach * 44625d4b688SGreg Roach * @param Builder $query 44725d4b688SGreg Roach * @param JewishDate $anniv 44825d4b688SGreg Roach */ 44925d4b688SGreg Roach private function kislevAnniversaries(Builder $query, JewishDate $anniv): void 45025d4b688SGreg Roach { 45125d4b688SGreg Roach $tmp = new JewishDate([(string) $anniv->year, 'CSH', '1']); 45225d4b688SGreg Roach 45325d4b688SGreg Roach if ($anniv->day() === 1 && $tmp->daysInMonth() === 29) { 4540b5fd0a6SGreg Roach $query->where(static function (Builder $query): void { 4550b5fd0a6SGreg Roach $query->where(static function (Builder $query): void { 45625d4b688SGreg Roach $query->where('d_day', '<=', 1)->where('d_mon', '=', 3); 4570b5fd0a6SGreg Roach })->orWhere(static function (Builder $query): void { 45825d4b688SGreg Roach $query->where('d_day', '=', 30)->where('d_mon', '=', 2); 45925d4b688SGreg Roach }); 46025d4b688SGreg Roach }); 46125d4b688SGreg Roach } elseif ($anniv->day === 29 && $anniv->daysInMonth() === 29) { 46225d4b688SGreg Roach $query 46325d4b688SGreg Roach ->where('d_mon', '=', 3) 46425d4b688SGreg Roach ->where('d_day', '>=', 29) 46525d4b688SGreg Roach ->where('d_day', '<>', 30); 46625d4b688SGreg Roach } else { 46725d4b688SGreg Roach $this->defaultAnniversaries($query, $anniv); 46825d4b688SGreg Roach } 46925d4b688SGreg Roach } 47025d4b688SGreg Roach 47125d4b688SGreg Roach /** 47225d4b688SGreg Roach * 1 TVT includes 30 KSL (if this year didn’t have 30 KSL). 47325d4b688SGreg Roach * 47425d4b688SGreg Roach * @param Builder $query 47525d4b688SGreg Roach * @param JewishDate $anniv 47625d4b688SGreg Roach */ 47725d4b688SGreg Roach private function tevetAnniversaries(Builder $query, JewishDate $anniv): void 47825d4b688SGreg Roach { 47925d4b688SGreg Roach $tmp = new JewishDate([(string) $anniv->year, 'KSL', '1']); 48025d4b688SGreg Roach 48125d4b688SGreg Roach if ($anniv->day === 1 && $tmp->daysInMonth() === 29) { 4820b5fd0a6SGreg Roach $query->where(static function (Builder $query): void { 4830b5fd0a6SGreg Roach $query->where(static function (Builder $query): void { 48425d4b688SGreg Roach $query->where('d_day', '<=', 1)->where('d_mon', '=', 4); 4850b5fd0a6SGreg Roach })->orWhere(static function (Builder $query): void { 48625d4b688SGreg Roach $query->where('d_day', '=', 30)->where('d_mon', '=', 3); 48725d4b688SGreg Roach }); 48825d4b688SGreg Roach }); 48925d4b688SGreg Roach } else { 49025d4b688SGreg Roach $this->defaultAnniversaries($query, $anniv); 49125d4b688SGreg Roach } 49225d4b688SGreg Roach } 49325d4b688SGreg Roach 49425d4b688SGreg Roach /** 49525d4b688SGreg Roach * ADS includes non-leap ADR. 49625d4b688SGreg Roach * 49725d4b688SGreg Roach * @param Builder $query 49825d4b688SGreg Roach * @param JewishDate $anniv 49925d4b688SGreg Roach */ 50025d4b688SGreg Roach private function adarIIAnniversaries(Builder $query, JewishDate $anniv): void 50125d4b688SGreg Roach { 50225d4b688SGreg Roach if ($anniv->day() === 1) { 50325d4b688SGreg Roach $query->where('d_day', '<=', 1); 50425d4b688SGreg Roach } elseif ($anniv->day() === $anniv->daysInMonth()) { 50525d4b688SGreg Roach $query->where('d_day', '>=', $anniv->daysInMonth()); 506569a8ff0SGreg Roach if ($anniv->daysInMonth() === 29) { 507569a8ff0SGreg Roach // On short months, 30th Adar shown on 1st Nissan 508569a8ff0SGreg Roach $query->where('d_day', '<>', 30); 509569a8ff0SGreg Roach } 51025d4b688SGreg Roach } else { 5110bab1c4aSGreg Roach $query->where('d_day', '=', $anniv->day()); 51225d4b688SGreg Roach } 51325d4b688SGreg Roach 514082dc12eSGreg Roach if ($anniv->isLeapYear()) { 515082dc12eSGreg Roach $query->where('d_mon', '=', 7); 516082dc12eSGreg Roach } else { 517082dc12eSGreg Roach $query->whereIn('d_mon', [6, 7]); 518082dc12eSGreg Roach } 51925d4b688SGreg Roach } 52025d4b688SGreg Roach 52125d4b688SGreg Roach /** 52225d4b688SGreg Roach * 1 NSN includes 30 ADR, if this year is non-leap. 52325d4b688SGreg Roach * 52425d4b688SGreg Roach * @param Builder $query 52525d4b688SGreg Roach * @param JewishDate $anniv 52625d4b688SGreg Roach */ 52725d4b688SGreg Roach private function nisanAnniversaries(Builder $query, JewishDate $anniv): void 52825d4b688SGreg Roach { 52925d4b688SGreg Roach if ($anniv->day === 1 && !$anniv->isLeapYear()) { 5300b5fd0a6SGreg Roach $query->where(static function (Builder $query): void { 5310b5fd0a6SGreg Roach $query->where(static function (Builder $query): void { 53225d4b688SGreg Roach $query->where('d_day', '<=', 1)->where('d_mon', '=', 8); 5330b5fd0a6SGreg Roach })->orWhere(static function (Builder $query): void { 53425d4b688SGreg Roach $query->where('d_day', '=', 30)->where('d_mon', '=', 6); 53525d4b688SGreg Roach }); 53625d4b688SGreg Roach }); 53725d4b688SGreg Roach } else { 53825d4b688SGreg Roach $this->defaultAnniversaries($query, $anniv); 53925d4b688SGreg Roach } 54025d4b688SGreg Roach } 541f0a11419SGreg Roach} 542