xref: /webtrees/app/Fact.php (revision b0a785fc58e1249481a395a102fbae242a2ccace)
1a25f0a04SGreg Roach<?php
23976b470SGreg Roach
3a25f0a04SGreg Roach/**
4a25f0a04SGreg Roach * webtrees: online genealogy
590949315SGreg Roach * Copyright (C) 2021 webtrees development team
6a25f0a04SGreg Roach * This program is free software: you can redistribute it and/or modify
7a25f0a04SGreg Roach * it under the terms of the GNU General Public License as published by
8a25f0a04SGreg Roach * the Free Software Foundation, either version 3 of the License, or
9a25f0a04SGreg Roach * (at your option) any later version.
10a25f0a04SGreg Roach * This program is distributed in the hope that it will be useful,
11a25f0a04SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12a25f0a04SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13a25f0a04SGreg Roach * GNU General Public License for more details.
14a25f0a04SGreg 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/>.
16a25f0a04SGreg Roach */
17fcfa147eSGreg Roach
18e7f56f2aSGreg Roachdeclare(strict_types=1);
19e7f56f2aSGreg Roach
2076692c8bSGreg Roachnamespace Fisharebest\Webtrees;
2176692c8bSGreg Roach
22580a4d11SGreg Roachuse Closure;
233d7a8a4cSGreg Roachuse Fisharebest\Webtrees\Functions\FunctionsPrint;
248af6bbf8SGreg Roachuse Fisharebest\Webtrees\Services\GedcomService;
251c7a6874SGreg Roachuse Illuminate\Support\Collection;
268f5f5da8SGreg Roachuse InvalidArgumentException;
273976b470SGreg Roach
28ae71f733SGreg Roachuse function array_flip;
29ae71f733SGreg Roachuse function array_key_exists;
30ae71f733SGreg Roachuse function count;
31ae71f733SGreg Roachuse function e;
32ae71f733SGreg Roachuse function implode;
33ae71f733SGreg Roachuse function in_array;
34ae71f733SGreg Roachuse function preg_match;
35ae71f733SGreg Roachuse function preg_match_all;
36ae71f733SGreg Roachuse function preg_replace;
37dec352c1SGreg Roachuse function str_contains;
38ae71f733SGreg Roachuse function usort;
39ae71f733SGreg Roach
40ae71f733SGreg Roachuse const PREG_SET_ORDER;
41a25f0a04SGreg Roach
42a25f0a04SGreg Roach/**
4376692c8bSGreg Roach * A GEDCOM fact or event object.
44a25f0a04SGreg Roach */
45c1010edaSGreg Roachclass Fact
46c1010edaSGreg Roach{
4716d6367aSGreg Roach    private const FACT_ORDER = [
48bbe4546fSGreg Roach        'BIRT',
49bbe4546fSGreg Roach        '_HNM',
50bbe4546fSGreg Roach        'ALIA',
51bbe4546fSGreg Roach        '_AKA',
52bbe4546fSGreg Roach        '_AKAN',
53bbe4546fSGreg Roach        'ADOP',
54bbe4546fSGreg Roach        '_ADPF',
55bbe4546fSGreg Roach        '_ADPF',
56bbe4546fSGreg Roach        '_BRTM',
57bbe4546fSGreg Roach        'CHR',
58bbe4546fSGreg Roach        'BAPM',
59bbe4546fSGreg Roach        'FCOM',
60bbe4546fSGreg Roach        'CONF',
61bbe4546fSGreg Roach        'BARM',
62bbe4546fSGreg Roach        'BASM',
63bbe4546fSGreg Roach        'EDUC',
64bbe4546fSGreg Roach        'GRAD',
65bbe4546fSGreg Roach        '_DEG',
66bbe4546fSGreg Roach        'EMIG',
67bbe4546fSGreg Roach        'IMMI',
68bbe4546fSGreg Roach        'NATU',
69bbe4546fSGreg Roach        '_MILI',
70bbe4546fSGreg Roach        '_MILT',
71bbe4546fSGreg Roach        'ENGA',
72bbe4546fSGreg Roach        'MARB',
73bbe4546fSGreg Roach        'MARC',
74bbe4546fSGreg Roach        'MARL',
75bbe4546fSGreg Roach        '_MARI',
76bbe4546fSGreg Roach        '_MBON',
77bbe4546fSGreg Roach        'MARR',
78bbe4546fSGreg Roach        '_COML',
79bbe4546fSGreg Roach        '_STAT',
80bbe4546fSGreg Roach        '_SEPR',
81bbe4546fSGreg Roach        'DIVF',
82bbe4546fSGreg Roach        'MARS',
83bbe4546fSGreg Roach        'DIV',
84bbe4546fSGreg Roach        'ANUL',
85bbe4546fSGreg Roach        'CENS',
86bbe4546fSGreg Roach        'OCCU',
87bbe4546fSGreg Roach        'RESI',
88bbe4546fSGreg Roach        'PROP',
89bbe4546fSGreg Roach        'CHRA',
90bbe4546fSGreg Roach        'RETI',
91bbe4546fSGreg Roach        'FACT',
92bbe4546fSGreg Roach        'EVEN',
93bbe4546fSGreg Roach        '_NMR',
94bbe4546fSGreg Roach        '_NMAR',
95bbe4546fSGreg Roach        'NMR',
96bbe4546fSGreg Roach        'NCHI',
97bbe4546fSGreg Roach        'WILL',
98bbe4546fSGreg Roach        '_HOL',
99bbe4546fSGreg Roach        '_????_',
100bbe4546fSGreg Roach        'DEAT',
101bbe4546fSGreg Roach        '_FNRL',
102bbe4546fSGreg Roach        'CREM',
103bbe4546fSGreg Roach        'BURI',
104bbe4546fSGreg Roach        '_INTE',
105bbe4546fSGreg Roach        '_YART',
106bbe4546fSGreg Roach        '_NLIV',
107bbe4546fSGreg Roach        'PROB',
108bbe4546fSGreg Roach        'TITL',
109bbe4546fSGreg Roach        'COMM',
110bbe4546fSGreg Roach        'NATI',
111bbe4546fSGreg Roach        'CITN',
112bbe4546fSGreg Roach        'CAST',
113bbe4546fSGreg Roach        'RELI',
114bbe4546fSGreg Roach        'SSN',
115bbe4546fSGreg Roach        'IDNO',
116bbe4546fSGreg Roach        'TEMP',
117bbe4546fSGreg Roach        'SLGC',
118bbe4546fSGreg Roach        'BAPL',
119bbe4546fSGreg Roach        'CONL',
120bbe4546fSGreg Roach        'ENDL',
121bbe4546fSGreg Roach        'SLGS',
122bbe4546fSGreg Roach        'ADDR',
123bbe4546fSGreg Roach        'PHON',
124bbe4546fSGreg Roach        'EMAIL',
125bbe4546fSGreg Roach        '_EMAIL',
126bbe4546fSGreg Roach        'EMAL',
127bbe4546fSGreg Roach        'FAX',
128bbe4546fSGreg Roach        'WWW',
129bbe4546fSGreg Roach        'URL',
130bbe4546fSGreg Roach        '_URL',
131bbe4546fSGreg Roach        'AFN',
132bbe4546fSGreg Roach        'REFN',
133bbe4546fSGreg Roach        '_PRMN',
134bbe4546fSGreg Roach        'REF',
135bbe4546fSGreg Roach        'RIN',
136bbe4546fSGreg Roach        '_UID',
137bbe4546fSGreg Roach        'OBJE',
138bbe4546fSGreg Roach        'NOTE',
139bbe4546fSGreg Roach        'SOUR',
140bbe4546fSGreg Roach        'CHAN',
141bbe4546fSGreg Roach        '_TODO',
142bbe4546fSGreg Roach    ];
143bbe4546fSGreg Roach
14433c746f1SGreg Roach    // Unique identifier for this fact (currently implemented as a hash of the raw data).
14533c746f1SGreg Roach    private string $id;
146a25f0a04SGreg Roach
14733c746f1SGreg Roach    // The GEDCOM record from which this fact is taken
14833c746f1SGreg Roach    private GedcomRecord $record;
149a25f0a04SGreg Roach
15033c746f1SGreg Roach    // The raw GEDCOM data for this fact
15133c746f1SGreg Roach    private string $gedcom;
152a25f0a04SGreg Roach
15333c746f1SGreg Roach    // The GEDCOM tag for this record
15433c746f1SGreg Roach    private string $tag;
155a25f0a04SGreg Roach
15633c746f1SGreg Roach    private bool $pending_deletion = false;
157a25f0a04SGreg Roach
15833c746f1SGreg Roach    private bool $pending_addition = false;
159a25f0a04SGreg Roach
16033c746f1SGreg Roach    private Date $date;
161a25f0a04SGreg Roach
16233c746f1SGreg Roach    private Place $place;
163a25f0a04SGreg Roach
16433c746f1SGreg Roach    // Used by Functions::sortFacts()
16533c746f1SGreg Roach    public int $sortOrder;
166a25f0a04SGreg Roach
167a25f0a04SGreg Roach    /**
168a25f0a04SGreg Roach     * Create an event object from a gedcom fragment.
169a25f0a04SGreg Roach     * We need the parent object (to check privacy) and a (pseudo) fact ID to
170a25f0a04SGreg Roach     * identify the fact within the record.
171a25f0a04SGreg Roach     *
172a25f0a04SGreg Roach     * @param string       $gedcom
173a25f0a04SGreg Roach     * @param GedcomRecord $parent
174905ab80aSGreg Roach     * @param string       $id
175a25f0a04SGreg Roach     *
1768f5f5da8SGreg Roach     * @throws InvalidArgumentException
177a25f0a04SGreg Roach     */
17824f2a3afSGreg Roach    public function __construct(string $gedcom, GedcomRecord $parent, string $id)
179c1010edaSGreg Roach    {
1808d0ebef0SGreg Roach        if (preg_match('/^1 (' . Gedcom::REGEX_TAG . ')/', $gedcom, $match)) {
181a25f0a04SGreg Roach            $this->gedcom = $gedcom;
182e7766c08SGreg Roach            $this->record = $parent;
183905ab80aSGreg Roach            $this->id     = $id;
184a25f0a04SGreg Roach            $this->tag    = $match[1];
185a25f0a04SGreg Roach        } else {
18652ac7b12SGreg Roach            throw new InvalidArgumentException('Invalid GEDCOM data passed to Fact::_construct(' . $gedcom . ',' . $parent->xref() . ')');
187a25f0a04SGreg Roach        }
188a25f0a04SGreg Roach    }
189a25f0a04SGreg Roach
190a25f0a04SGreg Roach    /**
191a25f0a04SGreg Roach     * Get the value of level 1 data in the fact
192a25f0a04SGreg Roach     * Allow for multi-line values
193a25f0a04SGreg Roach     *
194baacc364SGreg Roach     * @return string
195a25f0a04SGreg Roach     */
196dfa848b8SGreg Roach    public function value(): string
197c1010edaSGreg Roach    {
198d247596dSGreg Roach        if (preg_match('/^1 ' . $this->tag . ' ?(.*(?:\n2 CONT ?.*)*)/', $this->gedcom, $match)) {
199a25f0a04SGreg Roach            return preg_replace("/\n2 CONT ?/", "\n", $match[1]);
200a25f0a04SGreg Roach        }
201b2ce94c6SRico Sonntag
202b2ce94c6SRico Sonntag        return '';
203a25f0a04SGreg Roach    }
204a25f0a04SGreg Roach
205a25f0a04SGreg Roach    /**
206a25f0a04SGreg Roach     * Get the record to which this fact links
207a25f0a04SGreg Roach     *
2080d15532eSGreg Roach     * @return Family|GedcomRecord|Individual|Location|Media|Note|Repository|Source|Submission|Submitter|null
209a25f0a04SGreg Roach     */
210dc124885SGreg Roach    public function target()
211c1010edaSGreg Roach    {
21262b52849SGreg Roach        if (!preg_match('/^@(' . Gedcom::REGEX_XREF . ')@$/', $this->value(), $match)) {
21362b52849SGreg Roach            return null;
21462b52849SGreg Roach        }
21562b52849SGreg Roach
21662b52849SGreg Roach        $xref = $match[1];
21762b52849SGreg Roach
218a25f0a04SGreg Roach        switch ($this->tag) {
219a25f0a04SGreg Roach            case 'FAMC':
220a25f0a04SGreg Roach            case 'FAMS':
2216b9cb339SGreg Roach                return Registry::familyFactory()->make($xref, $this->record()->tree());
222a25f0a04SGreg Roach            case 'HUSB':
223a25f0a04SGreg Roach            case 'WIFE':
22462b52849SGreg Roach            case 'ALIA':
225a25f0a04SGreg Roach            case 'CHIL':
22662b52849SGreg Roach            case '_ASSO':
2276b9cb339SGreg Roach                return Registry::individualFactory()->make($xref, $this->record()->tree());
22862b52849SGreg Roach            case 'ASSO':
22962b52849SGreg Roach                return
2306b9cb339SGreg Roach                    Registry::individualFactory()->make($xref, $this->record()->tree()) ??
2316b9cb339SGreg Roach                    Registry::submitterFactory()->make($xref, $this->record()->tree());
232a25f0a04SGreg Roach            case 'SOUR':
2336b9cb339SGreg Roach                return Registry::sourceFactory()->make($xref, $this->record()->tree());
234a25f0a04SGreg Roach            case 'OBJE':
2356b9cb339SGreg Roach                return Registry::mediaFactory()->make($xref, $this->record()->tree());
236a25f0a04SGreg Roach            case 'REPO':
2376b9cb339SGreg Roach                return Registry::repositoryFactory()->make($xref, $this->record()->tree());
238a25f0a04SGreg Roach            case 'NOTE':
2396b9cb339SGreg Roach                return Registry::noteFactory()->make($xref, $this->record()->tree());
24062b52849SGreg Roach            case 'ANCI':
24162b52849SGreg Roach            case 'DESI':
242ffc0a61fSGreg Roach            case 'SUBM':
2436b9cb339SGreg Roach                return Registry::submitterFactory()->make($xref, $this->record()->tree());
2441635452cSGreg Roach            case 'SUBN':
2456b9cb339SGreg Roach                return Registry::submissionFactory()->make($xref, $this->record()->tree());
2460d15532eSGreg Roach            case '_LOC':
2476b9cb339SGreg Roach                return Registry::locationFactory()->make($xref, $this->record()->tree());
248a25f0a04SGreg Roach            default:
2496b9cb339SGreg Roach                return Registry::gedcomRecordFactory()->make($xref, $this->record()->tree());
250a25f0a04SGreg Roach        }
251a25f0a04SGreg Roach    }
252a25f0a04SGreg Roach
253a25f0a04SGreg Roach    /**
254a25f0a04SGreg Roach     * Get the value of level 2 data in the fact
255a25f0a04SGreg Roach     *
256a25f0a04SGreg Roach     * @param string $tag
257a25f0a04SGreg Roach     *
258baacc364SGreg Roach     * @return string
259a25f0a04SGreg Roach     */
26024f2a3afSGreg Roach    public function attribute(string $tag): string
261c1010edaSGreg Roach    {
262dce4f3a4SGreg Roach        if (preg_match('/\n2 ' . $tag . ' ?(.*(?:(?:\n3 CONT ?.*)*)*)/', $this->gedcom, $match)) {
263a25f0a04SGreg Roach            return preg_replace("/\n3 CONT ?/", "\n", $match[1]);
264a25f0a04SGreg Roach        }
265b2ce94c6SRico Sonntag
266b2ce94c6SRico Sonntag        return '';
267a25f0a04SGreg Roach    }
268a25f0a04SGreg Roach
269a25f0a04SGreg Roach    /**
2708af6bbf8SGreg Roach     * Get the PLAC:MAP:LATI for the fact.
2718af6bbf8SGreg Roach     *
27290949315SGreg Roach     * @return float|null
2738af6bbf8SGreg Roach     */
27490949315SGreg Roach    public function latitude(): ?float
2758af6bbf8SGreg Roach    {
2768af6bbf8SGreg Roach        if (preg_match('/\n4 LATI (.+)/', $this->gedcom, $match)) {
2778af6bbf8SGreg Roach            $gedcom_service = new GedcomService();
2788af6bbf8SGreg Roach
2798af6bbf8SGreg Roach            return $gedcom_service->readLatitude($match[1]);
2808af6bbf8SGreg Roach        }
2818af6bbf8SGreg Roach
2820ec592a7SDavid Drury        return null;
2838af6bbf8SGreg Roach    }
2848af6bbf8SGreg Roach
2858af6bbf8SGreg Roach    /**
2868af6bbf8SGreg Roach     * Get the PLAC:MAP:LONG for the fact.
2878af6bbf8SGreg Roach     *
28890949315SGreg Roach     * @return float|null
2898af6bbf8SGreg Roach     */
29090949315SGreg Roach    public function longitude(): ?float
2918af6bbf8SGreg Roach    {
292819ac503SRico Sonntag        if (preg_match('/\n4 LONG (.+)/', $this->gedcom, $match)) {
2938af6bbf8SGreg Roach            $gedcom_service = new GedcomService();
2948af6bbf8SGreg Roach
2958af6bbf8SGreg Roach            return $gedcom_service->readLongitude($match[1]);
2968af6bbf8SGreg Roach        }
2978af6bbf8SGreg Roach
2980ec592a7SDavid Drury        return null;
2998af6bbf8SGreg Roach    }
3008af6bbf8SGreg Roach
3018af6bbf8SGreg Roach    /**
302a25f0a04SGreg Roach     * Do the privacy rules allow us to display this fact to the current user
303a25f0a04SGreg Roach     *
304cbc1590aSGreg Roach     * @param int|null $access_level
305a25f0a04SGreg Roach     *
306cbc1590aSGreg Roach     * @return bool
307a25f0a04SGreg Roach     */
30835584196SGreg Roach    public function canShow(int $access_level = null): bool
309c1010edaSGreg Roach    {
310d9e083e7SGreg Roach        $access_level = $access_level ?? Auth::accessLevel($this->record->tree());
3114b9ff166SGreg Roach
312a25f0a04SGreg Roach        // Does this record have an explicit RESN?
313dec352c1SGreg Roach        if (str_contains($this->gedcom, "\n2 RESN confidential")) {
3144b9ff166SGreg Roach            return Auth::PRIV_NONE >= $access_level;
315a25f0a04SGreg Roach        }
316dec352c1SGreg Roach        if (str_contains($this->gedcom, "\n2 RESN privacy")) {
3174b9ff166SGreg Roach            return Auth::PRIV_USER >= $access_level;
318a25f0a04SGreg Roach        }
319dec352c1SGreg Roach        if (str_contains($this->gedcom, "\n2 RESN none")) {
320a25f0a04SGreg Roach            return true;
321a25f0a04SGreg Roach        }
322a25f0a04SGreg Roach
323*b0a785fcSGreg Roach        // A link to a record of the same type: NOTE=>NOTE, OBJE=>OBJE, SOUR=>SOUR, etc.
324*b0a785fcSGreg Roach        // Use the privacy of the target record.
325*b0a785fcSGreg Roach        $target = $this->target();
326*b0a785fcSGreg Roach
327*b0a785fcSGreg Roach        if ($target instanceof GedcomRecord && $target->tag() === $this->tag) {
328*b0a785fcSGreg Roach            return $target->canShow($access_level);
329*b0a785fcSGreg Roach        }
330*b0a785fcSGreg Roach
331a25f0a04SGreg Roach        // Does this record have a default RESN?
332c0935879SGreg Roach        $xref                    = $this->record->xref();
333f4afa648SGreg Roach        $fact_privacy            = $this->record->tree()->getFactPrivacy();
334f4afa648SGreg Roach        $individual_fact_privacy = $this->record->tree()->getIndividualFactPrivacy();
335518bbdc1SGreg Roach        if (isset($individual_fact_privacy[$xref][$this->tag])) {
336518bbdc1SGreg Roach            return $individual_fact_privacy[$xref][$this->tag] >= $access_level;
337a25f0a04SGreg Roach        }
338518bbdc1SGreg Roach        if (isset($fact_privacy[$this->tag])) {
339518bbdc1SGreg Roach            return $fact_privacy[$this->tag] >= $access_level;
340a25f0a04SGreg Roach        }
341a25f0a04SGreg Roach
342a25f0a04SGreg Roach        // No restrictions - it must be public
343a25f0a04SGreg Roach        return true;
344a25f0a04SGreg Roach    }
345a25f0a04SGreg Roach
346a25f0a04SGreg Roach    /**
347a25f0a04SGreg Roach     * Check whether this fact is protected against edit
348a25f0a04SGreg Roach     *
349cbc1590aSGreg Roach     * @return bool
350a25f0a04SGreg Roach     */
3518f53f488SRico Sonntag    public function canEdit(): bool
352c1010edaSGreg Roach    {
3531450f098SGreg Roach        if ($this->isPendingDeletion()) {
3541450f098SGreg Roach            return false;
3551450f098SGreg Roach        }
3561450f098SGreg Roach
3571450f098SGreg Roach        if (Auth::isManager($this->record->tree())) {
3581450f098SGreg Roach            return true;
3591450f098SGreg Roach        }
3601450f098SGreg Roach
361a25f0a04SGreg Roach        // Members cannot edit RESN, CHAN and locked records
362dec352c1SGreg Roach        return Auth::isEditor($this->record->tree()) && !str_contains($this->gedcom, "\n2 RESN locked") && $this->tag !== 'RESN' && $this->tag !== 'CHAN';
363a25f0a04SGreg Roach    }
364a25f0a04SGreg Roach
365a25f0a04SGreg Roach    /**
366a25f0a04SGreg Roach     * The place where the event occured.
367a25f0a04SGreg Roach     *
368a25f0a04SGreg Roach     * @return Place
369a25f0a04SGreg Roach     */
3704fb14fcbSGreg Roach    public function place(): Place
371c1010edaSGreg Roach    {
372448d466cSGreg Roach        $this->place ??= new Place($this->attribute('PLAC'), $this->record()->tree());
373a25f0a04SGreg Roach
374a25f0a04SGreg Roach        return $this->place;
375a25f0a04SGreg Roach    }
376a25f0a04SGreg Roach
377a25f0a04SGreg Roach    /**
378a25f0a04SGreg Roach     * Get the date for this fact.
379a25f0a04SGreg Roach     * We can call this function many times, especially when sorting,
380a25f0a04SGreg Roach     * so keep a copy of the date.
381a25f0a04SGreg Roach     *
382a25f0a04SGreg Roach     * @return Date
383a25f0a04SGreg Roach     */
3842decada7SGreg Roach    public function date(): Date
385c1010edaSGreg Roach    {
386448d466cSGreg Roach        $this->date ??= new Date($this->attribute('DATE'));
387a25f0a04SGreg Roach
388a25f0a04SGreg Roach        return $this->date;
389a25f0a04SGreg Roach    }
390a25f0a04SGreg Roach
391a25f0a04SGreg Roach    /**
392a25f0a04SGreg Roach     * The raw GEDCOM data for this fact
393a25f0a04SGreg Roach     *
394a25f0a04SGreg Roach     * @return string
395a25f0a04SGreg Roach     */
396138ca96cSGreg Roach    public function gedcom(): string
397c1010edaSGreg Roach    {
398a25f0a04SGreg Roach        return $this->gedcom;
399a25f0a04SGreg Roach    }
400a25f0a04SGreg Roach
401a25f0a04SGreg Roach    /**
402a25f0a04SGreg Roach     * Get a (pseudo) primary key for this fact.
403a25f0a04SGreg Roach     *
404a25f0a04SGreg Roach     * @return string
405a25f0a04SGreg Roach     */
406905ab80aSGreg Roach    public function id(): string
407c1010edaSGreg Roach    {
408905ab80aSGreg Roach        return $this->id;
409a25f0a04SGreg Roach    }
410a25f0a04SGreg Roach
411a25f0a04SGreg Roach    /**
412a25f0a04SGreg Roach     * What is the tag (type) of this fact, such as BIRT, MARR or DEAT.
413a25f0a04SGreg Roach     *
414a25f0a04SGreg Roach     * @return string
415a25f0a04SGreg Roach     */
4169ecabc6aSGreg Roach    public function tag(): string
417c1010edaSGreg Roach    {
41802467d32SGreg Roach        return $this->record->tag() . ':' . $this->tag;
419a25f0a04SGreg Roach    }
420a25f0a04SGreg Roach
421a25f0a04SGreg Roach    /**
422decb512fSGreg Roach     * The GEDCOM record where this Fact came from
423a25f0a04SGreg Roach     *
424decb512fSGreg Roach     * @return GedcomRecord
425a25f0a04SGreg Roach     */
426decb512fSGreg Roach    public function record(): GedcomRecord
427c1010edaSGreg Roach    {
428e7766c08SGreg Roach        return $this->record;
429a25f0a04SGreg Roach    }
430a25f0a04SGreg Roach
431a25f0a04SGreg Roach    /**
432a25f0a04SGreg Roach     * Get the name of this fact type, for use as a label.
433a25f0a04SGreg Roach     *
434a25f0a04SGreg Roach     * @return string
435a25f0a04SGreg Roach     */
4367b7d8067SGreg Roach    public function label(): string
437c1010edaSGreg Roach    {
43870ac1a22SGreg Roach        // Marriages
43970ac1a22SGreg Roach        if ($this->tag() === 'FAM:MARR') {
44070ac1a22SGreg Roach            $element = Registry::elementFactory()->make('FAM:MARR:TYPE');
44170ac1a22SGreg Roach            $type = $this->attribute('TYPE');
44270ac1a22SGreg Roach
44370ac1a22SGreg Roach            if ($type !== '') {
44470ac1a22SGreg Roach                return $element->value($type, $this->record->tree());
44570ac1a22SGreg Roach            }
44670ac1a22SGreg Roach        }
44770ac1a22SGreg Roach
448a25f0a04SGreg Roach        // Custom FACT/EVEN - with a TYPE
44918475794SGreg Roach        if ($this->tag === 'FACT' || $this->tag === 'EVEN') {
45018475794SGreg Roach            $type = $this->attribute('TYPE');
45118475794SGreg Roach
45218475794SGreg Roach            if ($type !== '') {
4533f9fe4ddSGreg Roach                if (!str_contains($type, '%')) {
45418475794SGreg Roach                    // Allow user-translations of custom types.
45518475794SGreg Roach                    $translated = I18N::translate($type);
45618475794SGreg Roach
45718475794SGreg Roach                    if ($translated !== $type) {
45818475794SGreg Roach                        return $translated;
459a25f0a04SGreg Roach                    }
4603f9fe4ddSGreg Roach                }
4614e5adf68SGreg Roach
46218475794SGreg Roach                return e($type);
46318475794SGreg Roach            }
46418475794SGreg Roach        }
46518475794SGreg Roach
466455a30feSGreg Roach        return Registry::elementFactory()->make($this->tag())->label();
467a25f0a04SGreg Roach    }
468a25f0a04SGreg Roach
469a25f0a04SGreg Roach    /**
470a25f0a04SGreg Roach     * This is a newly deleted fact, pending approval.
4717e96c925SGreg Roach     *
4727e96c925SGreg Roach     * @return void
473a25f0a04SGreg Roach     */
474e364afe4SGreg Roach    public function setPendingDeletion(): void
475c1010edaSGreg Roach    {
476a25f0a04SGreg Roach        $this->pending_deletion = true;
477a25f0a04SGreg Roach        $this->pending_addition = false;
478a25f0a04SGreg Roach    }
479a25f0a04SGreg Roach
480a25f0a04SGreg Roach    /**
481a25f0a04SGreg Roach     * Is this a newly deleted fact, pending approval.
482a25f0a04SGreg Roach     *
483cbc1590aSGreg Roach     * @return bool
484a25f0a04SGreg Roach     */
4858f53f488SRico Sonntag    public function isPendingDeletion(): bool
486c1010edaSGreg Roach    {
487a25f0a04SGreg Roach        return $this->pending_deletion;
488a25f0a04SGreg Roach    }
489a25f0a04SGreg Roach
490a25f0a04SGreg Roach    /**
491a25f0a04SGreg Roach     * This is a newly added fact, pending approval.
4927e96c925SGreg Roach     *
4937e96c925SGreg Roach     * @return void
494a25f0a04SGreg Roach     */
495e364afe4SGreg Roach    public function setPendingAddition(): void
496c1010edaSGreg Roach    {
497a25f0a04SGreg Roach        $this->pending_addition = true;
498a25f0a04SGreg Roach        $this->pending_deletion = false;
499a25f0a04SGreg Roach    }
500a25f0a04SGreg Roach
501a25f0a04SGreg Roach    /**
502a25f0a04SGreg Roach     * Is this a newly added fact, pending approval.
503a25f0a04SGreg Roach     *
504cbc1590aSGreg Roach     * @return bool
505a25f0a04SGreg Roach     */
5068f53f488SRico Sonntag    public function isPendingAddition(): bool
507c1010edaSGreg Roach    {
508a25f0a04SGreg Roach        return $this->pending_addition;
509a25f0a04SGreg Roach    }
510a25f0a04SGreg Roach
511a25f0a04SGreg Roach    /**
512a25f0a04SGreg Roach     * Source citations linked to this fact
513a25f0a04SGreg Roach     *
51424f2a3afSGreg Roach     * @return array<string>
515a25f0a04SGreg Roach     */
5168f53f488SRico Sonntag    public function getCitations(): array
517c1010edaSGreg Roach    {
5188d0ebef0SGreg Roach        preg_match_all('/\n(2 SOUR @(' . Gedcom::REGEX_XREF . ')@(?:\n[3-9] .*)*)/', $this->gedcom(), $matches, PREG_SET_ORDER);
51913abd6f3SGreg Roach        $citations = [];
520a25f0a04SGreg Roach        foreach ($matches as $match) {
5216b9cb339SGreg Roach            $source = Registry::sourceFactory()->make($match[2], $this->record()->tree());
5222859ecedSJonathan Jaubart            if ($source && $source->canShow()) {
523a25f0a04SGreg Roach                $citations[] = $match[1];
524a25f0a04SGreg Roach            }
525a25f0a04SGreg Roach        }
526a25f0a04SGreg Roach
527a25f0a04SGreg Roach        return $citations;
528a25f0a04SGreg Roach    }
529a25f0a04SGreg Roach
530a25f0a04SGreg Roach    /**
531a25f0a04SGreg Roach     * Notes (inline and objects) linked to this fact
532a25f0a04SGreg Roach     *
53309482a55SGreg Roach     * @return array<string|Note>
534a25f0a04SGreg Roach     */
5358f53f488SRico Sonntag    public function getNotes(): array
536c1010edaSGreg Roach    {
53713abd6f3SGreg Roach        $notes = [];
538138ca96cSGreg Roach        preg_match_all('/\n2 NOTE ?(.*(?:\n3.*)*)/', $this->gedcom(), $matches);
539a25f0a04SGreg Roach        foreach ($matches[1] as $match) {
540a25f0a04SGreg Roach            $note = preg_replace("/\n3 CONT ?/", "\n", $match);
5418d0ebef0SGreg Roach            if (preg_match('/@(' . Gedcom::REGEX_XREF . ')@/', $note, $nmatch)) {
5426b9cb339SGreg Roach                $note = Registry::noteFactory()->make($nmatch[1], $this->record()->tree());
543a25f0a04SGreg Roach                if ($note && $note->canShow()) {
544a25f0a04SGreg Roach                    // A note object
545a25f0a04SGreg Roach                    $notes[] = $note;
546a25f0a04SGreg Roach                }
547a25f0a04SGreg Roach            } else {
548a25f0a04SGreg Roach                // An inline note
549a25f0a04SGreg Roach                $notes[] = $note;
550a25f0a04SGreg Roach            }
551a25f0a04SGreg Roach        }
552a25f0a04SGreg Roach
553a25f0a04SGreg Roach        return $notes;
554a25f0a04SGreg Roach    }
555a25f0a04SGreg Roach
556a25f0a04SGreg Roach    /**
557a25f0a04SGreg Roach     * Media objects linked to this fact
558a25f0a04SGreg Roach     *
55909482a55SGreg Roach     * @return array<Media>
560a25f0a04SGreg Roach     */
5618f53f488SRico Sonntag    public function getMedia(): array
562c1010edaSGreg Roach    {
56313abd6f3SGreg Roach        $media = [];
5648d0ebef0SGreg Roach        preg_match_all('/\n2 OBJE @(' . Gedcom::REGEX_XREF . ')@/', $this->gedcom(), $matches);
565a25f0a04SGreg Roach        foreach ($matches[1] as $match) {
5666b9cb339SGreg Roach            $obje = Registry::mediaFactory()->make($match, $this->record()->tree());
5672859ecedSJonathan Jaubart            if ($obje && $obje->canShow()) {
568a25f0a04SGreg Roach                $media[] = $obje;
569a25f0a04SGreg Roach            }
570a25f0a04SGreg Roach        }
571a25f0a04SGreg Roach
572a25f0a04SGreg Roach        return $media;
573a25f0a04SGreg Roach    }
574a25f0a04SGreg Roach
575a25f0a04SGreg Roach    /**
576a25f0a04SGreg Roach     * A one-line summary of the fact - for charts, etc.
577a25f0a04SGreg Roach     *
578a25f0a04SGreg Roach     * @return string
579a25f0a04SGreg Roach     */
5808f53f488SRico Sonntag    public function summary(): string
581c1010edaSGreg Roach    {
58213abd6f3SGreg Roach        $attributes = [];
583dc124885SGreg Roach        $target     = $this->target();
584db7bb364SGreg Roach        if ($target instanceof GedcomRecord) {
58539ca88baSGreg Roach            $attributes[] = $target->fullName();
586a25f0a04SGreg Roach        } else {
587726d7b07SGreg Roach            // Fact value
58884586c02SGreg Roach            $value = $this->value();
589726d7b07SGreg Roach            if ($value !== '' && $value !== 'Y') {
590315eb316SGreg Roach                $attributes[] = '<bdi>' . e($value) . '</bdi>';
591a25f0a04SGreg Roach            }
592726d7b07SGreg Roach            // Fact date
5932decada7SGreg Roach            $date = $this->date();
594726d7b07SGreg Roach            if ($date->isOK()) {
5957fe676e5SGreg Roach                if ($this->record() instanceof Individual && in_array($this->tag, Gedcom::BIRTH_EVENTS, true) && $this->record()->tree()->getPreference('SHOW_PARENTS_AGE')) {
596e7766c08SGreg Roach                    $attributes[] = $date->display() . FunctionsPrint::formatParentsAges($this->record(), $date);
597a25f0a04SGreg Roach                } else {
598a25f0a04SGreg Roach                    $attributes[] = $date->display();
599a25f0a04SGreg Roach                }
600726d7b07SGreg Roach            }
601726d7b07SGreg Roach            // Fact place
602e364afe4SGreg Roach            if ($this->place()->gedcomName() !== '') {
603392561bbSGreg Roach                $attributes[] = $this->place()->shortName();
604a25f0a04SGreg Roach            }
605a25f0a04SGreg Roach        }
6062d8f08abSGreg Roach
6077fe676e5SGreg Roach        $class = 'fact_' . $this->tag;
608a25f0a04SGreg Roach        if ($this->isPendingAddition()) {
60971a69701SGreg Roach            $class .= ' wt-new';
610a25f0a04SGreg Roach        } elseif ($this->isPendingDeletion()) {
61171a69701SGreg Roach            $class .= ' wt-old';
612a25f0a04SGreg Roach        }
6132d8f08abSGreg Roach
6142d8f08abSGreg Roach        return
6152d8f08abSGreg Roach            '<div class="' . $class . '">' .
6162d8f08abSGreg Roach            /* I18N: a label/value pair, such as “Occupation: Farmer”. Some languages may need to change the punctuation. */
6177b7d8067SGreg Roach            I18N::translate('<span class="label">%1$s:</span> <span class="field" dir="auto">%2$s</span>', $this->label(), implode(' — ', $attributes)) .
6182d8f08abSGreg Roach            '</div>';
619a25f0a04SGreg Roach    }
620a25f0a04SGreg Roach
621a25f0a04SGreg Roach    /**
6229927f70fSGreg Roach     * A one-line summary of the fact - for the clipboard, etc.
6239927f70fSGreg Roach     *
6249927f70fSGreg Roach     * @return string
6259927f70fSGreg Roach     */
6269927f70fSGreg Roach    public function name(): string
6279927f70fSGreg Roach    {
6289927f70fSGreg Roach        $items  = [$this->label()];
6299927f70fSGreg Roach        $target = $this->target();
6309927f70fSGreg Roach
6319927f70fSGreg Roach        if ($target instanceof GedcomRecord) {
632315eb316SGreg Roach            $items[] = '<bdi>' . $target->fullName() . '</bdi>';
6339927f70fSGreg Roach        } else {
6349927f70fSGreg Roach            // Fact value
6359927f70fSGreg Roach            $value = $this->value();
6369927f70fSGreg Roach            if ($value !== '' && $value !== 'Y') {
637315eb316SGreg Roach                $items[] = '<bdi>' . e($value) . '</bdi>';
6389927f70fSGreg Roach            }
6399927f70fSGreg Roach
6409927f70fSGreg Roach            // Fact date
6419927f70fSGreg Roach            if ($this->date()->isOK()) {
6429927f70fSGreg Roach                $items[] = $this->date()->minimumDate()->format('%Y');
6439927f70fSGreg Roach            }
6449927f70fSGreg Roach
6459927f70fSGreg Roach            // Fact place
6469927f70fSGreg Roach            if ($this->place()->gedcomName() !== '') {
6479927f70fSGreg Roach                $items[] = $this->place()->shortName();
6489927f70fSGreg Roach            }
6499927f70fSGreg Roach        }
6509927f70fSGreg Roach
6519927f70fSGreg Roach        return implode(' — ', $items);
6529927f70fSGreg Roach    }
6539927f70fSGreg Roach
6549927f70fSGreg Roach    /**
655580a4d11SGreg Roach     * Helper functions to sort facts
656a25f0a04SGreg Roach     *
657580a4d11SGreg Roach     * @return Closure
658a25f0a04SGreg Roach     */
659580a4d11SGreg Roach    private static function dateComparator(): Closure
660c1010edaSGreg Roach    {
6616c2179e2SGreg Roach        return static function (Fact $a, Fact $b): int {
6622decada7SGreg Roach            if ($a->date()->isOK() && $b->date()->isOK()) {
663a25f0a04SGreg Roach                // If both events have dates, compare by date
6642decada7SGreg Roach                $ret = Date::compare($a->date(), $b->date());
665a25f0a04SGreg Roach
666dfa848b8SGreg Roach                if ($ret === 0) {
667580a4d11SGreg Roach                    // If dates overlap, compare by fact type
668580a4d11SGreg Roach                    $ret = self::typeComparator()($a, $b);
669a25f0a04SGreg Roach
670a25f0a04SGreg Roach                    // If the fact type is also the same, retain the initial order
671dfa848b8SGreg Roach                    if ($ret === 0) {
672580a4d11SGreg Roach                        $ret = $a->sortOrder <=> $b->sortOrder;
673a25f0a04SGreg Roach                    }
674a25f0a04SGreg Roach                }
675a25f0a04SGreg Roach
676a25f0a04SGreg Roach                return $ret;
677b2ce94c6SRico Sonntag            }
678b2ce94c6SRico Sonntag
679a25f0a04SGreg Roach            // One or both events have no date - retain the initial order
680580a4d11SGreg Roach            return $a->sortOrder <=> $b->sortOrder;
681580a4d11SGreg Roach        };
682a25f0a04SGreg Roach    }
683a25f0a04SGreg Roach
684a25f0a04SGreg Roach    /**
685580a4d11SGreg Roach     * Helper functions to sort facts.
686a25f0a04SGreg Roach     *
687580a4d11SGreg Roach     * @return Closure
688a25f0a04SGreg Roach     */
689580a4d11SGreg Roach    public static function typeComparator(): Closure
690c1010edaSGreg Roach    {
691bbe4546fSGreg Roach        static $factsort = [];
692a25f0a04SGreg Roach
69354c1ab5eSGreg Roach        if ($factsort === []) {
694bbe4546fSGreg Roach            $factsort = array_flip(self::FACT_ORDER);
695a25f0a04SGreg Roach        }
696a25f0a04SGreg Roach
6976c2179e2SGreg Roach        return static function (Fact $a, Fact $b) use ($factsort): int {
698a25f0a04SGreg Roach            // Facts from same families stay grouped together
699a25f0a04SGreg Roach            // Keep MARR and DIV from the same families from mixing with events from other FAMs
700a25f0a04SGreg Roach            // Use the original order in which the facts were added
701e7766c08SGreg Roach            if ($a->record instanceof Family && $b->record instanceof Family && $a->record !== $b->record) {
702a25f0a04SGreg Roach                return $a->sortOrder - $b->sortOrder;
703a25f0a04SGreg Roach            }
704a25f0a04SGreg Roach
7057fe676e5SGreg Roach            $atag = $a->tag;
7067fe676e5SGreg Roach            $btag = $b->tag;
707a25f0a04SGreg Roach
708a25f0a04SGreg Roach            // Events not in the above list get mapped onto one that is.
709a25f0a04SGreg Roach            if (!array_key_exists($atag, $factsort)) {
7107a6ee1acSGreg Roach                $atag = '_????_';
711a25f0a04SGreg Roach            }
712a25f0a04SGreg Roach
713a25f0a04SGreg Roach            if (!array_key_exists($btag, $factsort)) {
7147a6ee1acSGreg Roach                $btag = '_????_';
715a25f0a04SGreg Roach            }
716a25f0a04SGreg Roach
717a25f0a04SGreg Roach            // - Don't let dated after DEAT/BURI facts sort non-dated facts before DEAT/BURI
718a25f0a04SGreg Roach            // - Treat dated after BURI facts as BURI instead
7193425616eSGreg Roach            if ($a->attribute('DATE') !== '' && $factsort[$atag] > $factsort['BURI'] && $factsort[$atag] < $factsort['CHAN']) {
720a25f0a04SGreg Roach                $atag = 'BURI';
721a25f0a04SGreg Roach            }
722a25f0a04SGreg Roach
7233425616eSGreg Roach            if ($b->attribute('DATE') !== '' && $factsort[$btag] > $factsort['BURI'] && $factsort[$btag] < $factsort['CHAN']) {
724a25f0a04SGreg Roach                $btag = 'BURI';
725a25f0a04SGreg Roach            }
726a25f0a04SGreg Roach
727a25f0a04SGreg Roach            $ret = $factsort[$atag] - $factsort[$btag];
728a25f0a04SGreg Roach
729a25f0a04SGreg Roach            // If facts are the same then put dated facts before non-dated facts
730a25f0a04SGreg Roach            if ($ret == 0) {
7313425616eSGreg Roach                if ($a->attribute('DATE') !== '' && $b->attribute('DATE') === '') {
732a25f0a04SGreg Roach                    return -1;
733a25f0a04SGreg Roach                }
734a25f0a04SGreg Roach
7353425616eSGreg Roach                if ($b->attribute('DATE') !== '' && $a->attribute('DATE') === '') {
736a25f0a04SGreg Roach                    return 1;
737a25f0a04SGreg Roach                }
738a25f0a04SGreg Roach
739a25f0a04SGreg Roach                // If no sorting preference, then keep original ordering
740a25f0a04SGreg Roach                $ret = $a->sortOrder - $b->sortOrder;
741a25f0a04SGreg Roach            }
742a25f0a04SGreg Roach
743a25f0a04SGreg Roach            return $ret;
744580a4d11SGreg Roach        };
745580a4d11SGreg Roach    }
746580a4d11SGreg Roach
747580a4d11SGreg Roach    /**
748580a4d11SGreg Roach     * A multi-key sort
749580a4d11SGreg Roach     * 1. First divide the facts into two arrays one set with dates and one set without dates
750580a4d11SGreg Roach     * 2. Sort each of the two new arrays, the date using the compare date function, the non-dated
751580a4d11SGreg Roach     * using the compare type function
752580a4d11SGreg Roach     * 3. Then merge the arrays back into the original array using the compare type function
753580a4d11SGreg Roach     *
754b5c8fd7eSGreg Roach     * @param Collection<Fact> $unsorted
755580a4d11SGreg Roach     *
756b5c8fd7eSGreg Roach     * @return Collection<Fact>
757580a4d11SGreg Roach     */
7588af3e5c1SGreg Roach    public static function sortFacts(Collection $unsorted): Collection
759580a4d11SGreg Roach    {
760580a4d11SGreg Roach        $dated    = [];
761580a4d11SGreg Roach        $nondated = [];
762580a4d11SGreg Roach        $sorted   = [];
763580a4d11SGreg Roach
764580a4d11SGreg Roach        // Split the array into dated and non-dated arrays
765580a4d11SGreg Roach        $order = 0;
766580a4d11SGreg Roach
767580a4d11SGreg Roach        foreach ($unsorted as $fact) {
768580a4d11SGreg Roach            $fact->sortOrder = $order;
769580a4d11SGreg Roach            $order++;
770580a4d11SGreg Roach
771580a4d11SGreg Roach            if ($fact->date()->isOK()) {
772580a4d11SGreg Roach                $dated[] = $fact;
773580a4d11SGreg Roach            } else {
774580a4d11SGreg Roach                $nondated[] = $fact;
775580a4d11SGreg Roach            }
776580a4d11SGreg Roach        }
777580a4d11SGreg Roach
778580a4d11SGreg Roach        usort($dated, self::dateComparator());
779580a4d11SGreg Roach        usort($nondated, self::typeComparator());
780580a4d11SGreg Roach
781580a4d11SGreg Roach        // Merge the arrays
782580a4d11SGreg Roach        $dc = count($dated);
783580a4d11SGreg Roach        $nc = count($nondated);
784580a4d11SGreg Roach        $i  = 0;
785580a4d11SGreg Roach        $j  = 0;
786580a4d11SGreg Roach
787580a4d11SGreg Roach        // while there is anything in the dated array continue merging
788580a4d11SGreg Roach        while ($i < $dc) {
789580a4d11SGreg Roach            // compare each fact by type to merge them in order
790580a4d11SGreg Roach            if ($j < $nc && self::typeComparator()($dated[$i], $nondated[$j]) > 0) {
791580a4d11SGreg Roach                $sorted[] = $nondated[$j];
792580a4d11SGreg Roach                $j++;
793580a4d11SGreg Roach            } else {
794580a4d11SGreg Roach                $sorted[] = $dated[$i];
795580a4d11SGreg Roach                $i++;
796580a4d11SGreg Roach            }
797580a4d11SGreg Roach        }
798580a4d11SGreg Roach
799580a4d11SGreg Roach        // get anything that might be left in the nondated array
800580a4d11SGreg Roach        while ($j < $nc) {
801580a4d11SGreg Roach            $sorted[] = $nondated[$j];
802580a4d11SGreg Roach            $j++;
803580a4d11SGreg Roach        }
804580a4d11SGreg Roach
8051c7a6874SGreg Roach        return new Collection($sorted);
806a25f0a04SGreg Roach    }
807a25f0a04SGreg Roach
808a25f0a04SGreg Roach    /**
809ae71f733SGreg Roach     * Sort fact/event tags using the same order that we use for facts.
810ae71f733SGreg Roach     *
811ae71f733SGreg Roach     * @param Collection<string> $unsorted
812ae71f733SGreg Roach     *
813ae71f733SGreg Roach     * @return Collection<string>
814ae71f733SGreg Roach     */
815ae71f733SGreg Roach    public static function sortFactTags(Collection $unsorted): Collection
816ae71f733SGreg Roach    {
817ae71f733SGreg Roach        $tag_order = array_flip(self::FACT_ORDER);
818ae71f733SGreg Roach
819ae71f733SGreg Roach        return $unsorted->sort(static function (string $x, string $y) use ($tag_order): int {
820ae71f733SGreg Roach            $sort_x = $tag_order[$x] ?? $tag_order['_????_'];
821ae71f733SGreg Roach            $sort_y = $tag_order[$y] ?? $tag_order['_????_'];
822ae71f733SGreg Roach
823ae71f733SGreg Roach            return $sort_x - $sort_y;
824ae71f733SGreg Roach        });
825ae71f733SGreg Roach    }
826ae71f733SGreg Roach
827ae71f733SGreg Roach    /**
828a25f0a04SGreg Roach     * Allow native PHP functions such as array_unique() to work with objects
829a25f0a04SGreg Roach     *
830a25f0a04SGreg Roach     * @return string
831a25f0a04SGreg Roach     */
832dfa848b8SGreg Roach    public function __toString(): string
833c1010edaSGreg Roach    {
834c0935879SGreg Roach        return $this->id . '@' . $this->record->xref();
835a25f0a04SGreg Roach    }
836a25f0a04SGreg Roach}
837