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