1<?php 2/** 3 * webtrees: online genealogy 4 * Copyright (C) 2018 webtrees development team 5 * This program is free software: you can redistribute it and/or modify 6 * it under the terms of the GNU General Public License as published by 7 * the Free Software Foundation, either version 3 of the License, or 8 * (at your option) any later version. 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * You should have received a copy of the GNU General Public License 14 * along with this program. If not, see <http://www.gnu.org/licenses/>. 15 */ 16declare(strict_types=1); 17 18namespace Fisharebest\Webtrees\Services; 19 20use Fisharebest\Webtrees\Database; 21use Fisharebest\Webtrees\Date; 22use Fisharebest\Webtrees\Date\FrenchDate; 23use Fisharebest\Webtrees\Date\GregorianDate; 24use Fisharebest\Webtrees\Date\HijriDate; 25use Fisharebest\Webtrees\Date\JalaliDate; 26use Fisharebest\Webtrees\Date\JewishDate; 27use Fisharebest\Webtrees\Date\JulianDate; 28use Fisharebest\Webtrees\Fact; 29use Fisharebest\Webtrees\Family; 30use Fisharebest\Webtrees\GedcomRecord; 31use Fisharebest\Webtrees\Individual; 32use Fisharebest\Webtrees\Tree; 33 34/** 35 * Calculate anniversaries, etc. 36 */ 37class CalendarService 38{ 39 /** 40 * List all the months in a given year. 41 * 42 * @param string $calendar 43 * @param int $year 44 * 45 * @return string[] 46 */ 47 public function calendarMonthsInYear(string $calendar, int $year): array 48 { 49 $date = new Date($calendar . ' ' . $year); 50 $calendar_date = $date->minimumDate(); 51 $month_numbers = range(1, $calendar_date->monthsInYear()); 52 $month_names = []; 53 54 foreach ($month_numbers as $month_number) { 55 $calendar_date->day = 1; 56 $calendar_date->month = $month_number; 57 $calendar_date->setJdFromYmd(); 58 59 if ($month_number === 6 && $calendar_date instanceof JewishDate && !$calendar_date->isLeapYear()) { 60 // No month 6 in Jewish non-leap years. 61 continue; 62 } 63 64 if ($month_number === 7 && $calendar_date instanceof JewishDate && !$calendar_date->isLeapYear()) { 65 // Month 7 is ADR in Jewish non-leap years (and ADS in others). 66 $mon = 'ADR'; 67 } else { 68 $mon = $calendar_date->format('%O'); 69 } 70 71 72 $month_names[$mon] = $calendar_date->format('%F'); 73 } 74 75 return $month_names; 76 } 77 78 /** 79 * Get a list of events which occured during a given date range. 80 * 81 * @param int $jd1 the start range of julian day 82 * @param int $jd2 the end range of julian day 83 * @param string $facts restrict the search to just these facts or leave blank for all 84 * @param Tree $tree the tree to search 85 * 86 * @return Fact[] 87 */ 88 public function getCalendarEvents(int $jd1, int $jd2, string $facts, Tree $tree): array 89 { 90 // If no facts specified, get all except these 91 $skipfacts = 'CHAN,BAPL,SLGC,SLGS,ENDL,CENS,RESI,NOTE,ADDR,OBJE,SOUR'; 92 93 $found_facts = []; 94 95 // Events that start or end during the period 96 $where = "WHERE (d_julianday1>={$jd1} AND d_julianday1<={$jd2} OR d_julianday2>={$jd1} AND d_julianday2<={$jd2})"; 97 98 // Restrict to certain types of fact 99 if (empty($facts)) { 100 $excl_facts = "'" . preg_replace('/\W+/', "','", $skipfacts) . "'"; 101 $where .= " AND d_fact NOT IN ({$excl_facts})"; 102 } else { 103 $incl_facts = "'" . preg_replace('/\W+/', "','", $facts) . "'"; 104 $where .= " AND d_fact IN ({$incl_facts})"; 105 } 106 // Only get events from the current gedcom 107 $where .= " AND d_file=" . $tree->id(); 108 109 // Now fetch these events 110 $ind_sql = "SELECT d_gid AS xref, i_gedcom AS gedcom, d_type, d_day, d_month, d_year, d_fact, d_type FROM `##dates`, `##individuals` {$where} AND d_gid=i_id AND d_file=i_file ORDER BY d_julianday1"; 111 $fam_sql = "SELECT d_gid AS xref, f_gedcom AS gedcom, d_type, d_day, d_month, d_year, d_fact, d_type FROM `##dates`, `##families` {$where} AND d_gid=f_id AND d_file=f_file ORDER BY d_julianday1"; 112 113 foreach (['INDI' => $ind_sql, 'FAM' => $fam_sql] as $type => $sql) { 114 $rows = Database::prepare($sql)->fetchAll(); 115 116 foreach ($rows as $row) { 117 if ($type === 'INDI') { 118 $record = Individual::getInstance($row->xref, $tree, $row->gedcom); 119 } else { 120 $record = Family::getInstance($row->xref, $tree, $row->gedcom); 121 } 122 $anniv_date = new Date($row->d_type . ' ' . $row->d_day . ' ' . $row->d_month . ' ' . $row->d_year); 123 foreach ($record->facts() as $fact) { 124 // For date ranges, we need a match on either the start/end. 125 if (($fact->date()->minimumJulianDay() === $anniv_date->minimumJulianDay() || $fact->date()->maximumJulianDay() == $anniv_date->maximumJulianDay()) && $fact->getTag() === $row->d_fact) { 126 $fact->anniv = 0; 127 $found_facts[] = $fact; 128 } 129 } 130 } 131 } 132 133 return $found_facts; 134 } 135 136 /** 137 * Get the list of current and upcoming events, sorted by anniversary date 138 * 139 * @param int $jd1 140 * @param int $jd2 141 * @param string $events 142 * @param bool $only_living 143 * @param string $sort_by 144 * @param Tree $tree 145 * 146 * @return Fact[] 147 */ 148 public function getEventsList(int $jd1, int $jd2, string $events, bool $only_living, string $sort_by, Tree $tree): array 149 { 150 $found_facts = []; 151 $facts = []; 152 153 foreach (range($jd1, $jd2) as $jd) { 154 $found_facts = array_merge($found_facts, $this->getAnniversaryEvents($jd, $events, $tree)); 155 } 156 157 foreach ($found_facts as $fact) { 158 $record = $fact->record(); 159 // only living people ? 160 if ($only_living) { 161 if ($record instanceof Individual && $record->isDead()) { 162 continue; 163 } 164 if ($record instanceof Family) { 165 $husb = $record->getHusband(); 166 if ($husb === null || $husb->isDead()) { 167 continue; 168 } 169 $wife = $record->getWife(); 170 if ($wife === null || $wife->isDead()) { 171 continue; 172 } 173 } 174 } 175 $facts[] = $fact; 176 } 177 178 switch ($sort_by) { 179 case 'anniv': 180 uasort($facts, function (Fact $x, Fact $y): int { 181 return Fact::compareDate($y, $x); 182 }); 183 break; 184 case 'alpha': 185 uasort($facts, function (Fact $x, Fact $y): int { 186 return GedcomRecord::compare($x->record(), $y->record()); 187 }); 188 break; 189 } 190 191 return $facts; 192 } 193 194 /** 195 * Get a list of events whose anniversary occured on a given julian day. 196 * Used on the on-this-day/upcoming blocks and the day/month calendar views. 197 * 198 * @param int $jd the julian day 199 * @param string $facts restrict the search to just these facts or leave blank for all 200 * @param Tree $tree the tree to search 201 * 202 * @return Fact[] 203 */ 204 public function getAnniversaryEvents($jd, $facts, Tree $tree): array 205 { 206 $found_facts = []; 207 208 $anniversaries = [ 209 new GregorianDate($jd), 210 new JulianDate($jd), 211 new FrenchDate($jd), 212 new JewishDate($jd), 213 new HijriDate($jd), 214 new JalaliDate($jd), 215 ]; 216 217 foreach ($anniversaries as $anniv) { 218 // Build a SQL where clause to match anniversaries in the appropriate calendar. 219 $ind_sql = 220 "SELECT DISTINCT i_id AS xref, i_gedcom AS gedcom, d_type, d_day, d_month, d_year, d_fact" . 221 " FROM `##dates` JOIN `##individuals` ON d_gid = i_id AND d_file = i_file" . 222 " WHERE d_type = :type AND d_file = :tree_id"; 223 $fam_sql = 224 "SELECT DISTINCT f_id AS xref, f_gedcom AS gedcom, d_type, d_day, d_month, d_year, d_fact" . 225 " FROM `##dates` JOIN `##families` ON d_gid = f_id AND d_file = f_file" . 226 " WHERE d_type = :type AND d_file = :tree_id"; 227 $args = [ 228 'type' => $anniv->format('%@'), 229 'tree_id' => $tree->id(), 230 ]; 231 232 $where = ""; 233 // SIMPLE CASES: 234 // a) Non-hebrew anniversaries 235 // b) Hebrew months TVT, SHV, IYR, SVN, TMZ, AAV, ELL 236 if (!$anniv instanceof JewishDate || in_array($anniv->month, [ 237 1, 238 5, 239 6, 240 9, 241 10, 242 11, 243 12, 244 13, 245 ])) { 246 // Dates without days go on the first day of the month 247 // Dates with invalid days go on the last day of the month 248 if ($anniv->day === 1) { 249 $where .= " AND d_day <= 1"; 250 } elseif ($anniv->day === $anniv->daysInMonth()) { 251 $where .= " AND d_day >= :day"; 252 $args['day'] = $anniv->day; 253 } else { 254 $where .= " AND d_day = :day"; 255 $args['day'] = $anniv->day; 256 } 257 $where .= " AND d_mon = :month"; 258 $args['month'] = $anniv->month; 259 } else { 260 // SPECIAL CASES: 261 switch ($anniv->month) { 262 case 2: 263 // 29 CSH does not include 30 CSH (but would include an invalid 31 CSH if there were no 30 CSH) 264 if ($anniv->day === 1) { 265 $where .= " AND d_day <= 1 AND d_mon = 2"; 266 } elseif ($anniv->day === 30) { 267 $where .= " AND d_day >= 30 AND d_mon = 2"; 268 } elseif ($anniv->day === 29 && $anniv->daysInMonth() === 29) { 269 $where .= " AND (d_day = 29 OR d_day > 30) AND d_mon = 2"; 270 } else { 271 $where .= " AND d_day = :day AND d_mon = 2"; 272 $args['day'] = $anniv->day; 273 } 274 break; 275 case 3: 276 // 1 KSL includes 30 CSH (if this year didn’t have 30 CSH) 277 // 29 KSL does not include 30 KSL (but would include an invalid 31 KSL if there were no 30 KSL) 278 if ($anniv->day === 1) { 279 $tmp = new JewishDate([ 280 (string) $anniv->year, 281 'CSH', 282 1, 283 ]); 284 if ($tmp->daysInMonth() === 29) { 285 $where .= " AND (d_day <= 1 AND d_mon = 3 OR d_day = 30 AND d_mon = 2)"; 286 } else { 287 $where .= " AND d_day <= 1 AND d_mon = 3"; 288 } 289 } elseif ($anniv->day === 30) { 290 $where .= " AND d_day >= 30 AND d_mon = 3"; 291 } elseif ($anniv->day == 29 && $anniv->daysInMonth() === 29) { 292 $where .= " AND (d_day = 29 OR d_day > 30) AND d_mon = 3"; 293 } else { 294 $where .= " AND d_day = :day AND d_mon = 3"; 295 $args['day'] = $anniv->day; 296 } 297 break; 298 case 4: 299 // 1 TVT includes 30 KSL (if this year didn’t have 30 KSL) 300 if ($anniv->day === 1) { 301 $tmp = new JewishDate([ 302 (string) $anniv->year, 303 'KSL', 304 1, 305 ]); 306 if ($tmp->daysInMonth() === 29) { 307 $where .= " AND (d_day <=1 AND d_mon = 4 OR d_day = 30 AND d_mon = 3)"; 308 } else { 309 $where .= " AND d_day <= 1 AND d_mon = 4"; 310 } 311 } elseif ($anniv->day === $anniv->daysInMonth()) { 312 $where .= " AND d_day >= :day AND d_mon=4"; 313 $args['day'] = $anniv->day; 314 } else { 315 $where .= " AND d_day = :day AND d_mon=4"; 316 $args['day'] = $anniv->day; 317 } 318 break; 319 case 7: // ADS includes ADR (non-leap) 320 if ($anniv->day === 1) { 321 $where .= " AND d_day <= 1"; 322 } elseif ($anniv->day === $anniv->daysInMonth()) { 323 $where .= " AND d_day >= :day"; 324 $args['day'] = $anniv->day; 325 } else { 326 $where .= " AND d_day = :day"; 327 $args['day'] = $anniv->day; 328 } 329 $where .= " AND (d_mon = 6 AND MOD(7 * d_year + 1, 19) >= 7 OR d_mon = 7)"; 330 break; 331 case 8: // 1 NSN includes 30 ADR, if this year is non-leap 332 if ($anniv->day === 1) { 333 if ($anniv->isLeapYear()) { 334 $where .= " AND d_day <= 1 AND d_mon = 8"; 335 } else { 336 $where .= " AND (d_day <= 1 AND d_mon = 8 OR d_day = 30 AND d_mon = 6)"; 337 } 338 } elseif ($anniv->day === $anniv->daysInMonth()) { 339 $where .= " AND d_day >= :day AND d_mon = 8"; 340 $args['day'] = $anniv->day; 341 } else { 342 $where .= " AND d_day = :day AND d_mon = 8"; 343 $args['day'] = $anniv->day; 344 } 345 break; 346 } 347 } 348 // Only events in the past (includes dates without a year) 349 $where .= " AND d_year <= :year"; 350 $args['year'] = $anniv->year; 351 352 if ($facts) { 353 // Restrict to certain types of fact 354 $where .= " AND d_fact IN ("; 355 preg_match_all('/([_A-Z]+)/', $facts, $matches); 356 foreach ($matches[1] as $n => $fact) { 357 $where .= $n ? ", " : ""; 358 $where .= ":fact_" . $n; 359 $args['fact_' . $n] = $fact; 360 } 361 $where .= ")"; 362 } else { 363 // If no facts specified, get all except these 364 $where .= " AND d_fact NOT IN ('CHAN', 'BAPL', 'SLGC', 'SLGS', 'ENDL', 'CENS', 'RESI', '_TODO')"; 365 } 366 367 $order_by = " ORDER BY d_day, d_year DESC"; 368 369 // Now fetch these anniversaries 370 foreach ([ 371 'INDI' => $ind_sql . $where . $order_by, 372 'FAM' => $fam_sql . $where . $order_by, 373 ] as $type => $sql) { 374 $rows = Database::prepare($sql)->execute($args)->fetchAll(); 375 foreach ($rows as $row) { 376 if ($type === 'INDI') { 377 $record = Individual::getInstance($row->xref, $tree, $row->gedcom); 378 } else { 379 $record = Family::getInstance($row->xref, $tree, $row->gedcom); 380 } 381 $anniv_date = new Date($row->d_type . ' ' . $row->d_day . ' ' . $row->d_month . ' ' . $row->d_year); 382 foreach ($record->facts() as $fact) { 383 if (($fact->date()->minimumJulianDay() === $anniv_date->minimumJulianDay() || $fact->date()->maximumJulianDay() === $anniv_date->maximumJulianDay()) && $fact->getTag() === $row->d_fact) { 384 $fact->anniv = $row->d_year === '0' ? 0 : $anniv->year - $row->d_year; 385 $fact->jd = $jd; 386 $found_facts[] = $fact; 387 } 388 } 389 } 390 } 391 } 392 393 return $found_facts; 394 } 395} 396