xref: /webtrees/app/Fact.php (revision a2143fbec012d69e32cb03fdecc022199ddab2bc)
1a25f0a04SGreg Roach<?php
23976b470SGreg Roach
3a25f0a04SGreg Roach/**
4a25f0a04SGreg Roach * webtrees: online genealogy
55bfc6897SGreg Roach * Copyright (C) 2022 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;
2355105892SGreg Roachuse Fisharebest\Webtrees\Elements\RestrictionNotice;
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_replace;
36dec352c1SGreg Roachuse function str_contains;
3755105892SGreg Roachuse function str_ends_with;
38ae71f733SGreg Roachuse function usort;
39ae71f733SGreg Roach
40a25f0a04SGreg Roach/**
4176692c8bSGreg Roach * A GEDCOM fact or event object.
42a25f0a04SGreg Roach */
43c1010edaSGreg Roachclass Fact
44c1010edaSGreg Roach{
4516d6367aSGreg Roach    private const FACT_ORDER = [
46bbe4546fSGreg Roach        'BIRT',
47bbe4546fSGreg Roach        '_HNM',
48bbe4546fSGreg Roach        'ALIA',
49bbe4546fSGreg Roach        '_AKA',
50bbe4546fSGreg Roach        '_AKAN',
51bbe4546fSGreg Roach        'ADOP',
52bbe4546fSGreg Roach        '_ADPF',
53bbe4546fSGreg Roach        '_ADPF',
54bbe4546fSGreg Roach        '_BRTM',
55bbe4546fSGreg Roach        'CHR',
56bbe4546fSGreg Roach        'BAPM',
57bbe4546fSGreg Roach        'FCOM',
58bbe4546fSGreg Roach        'CONF',
59bbe4546fSGreg Roach        'BARM',
60bbe4546fSGreg Roach        'BASM',
61bbe4546fSGreg Roach        'EDUC',
62bbe4546fSGreg Roach        'GRAD',
63bbe4546fSGreg Roach        '_DEG',
64bbe4546fSGreg Roach        'EMIG',
65bbe4546fSGreg Roach        'IMMI',
66bbe4546fSGreg Roach        'NATU',
67bbe4546fSGreg Roach        '_MILI',
68bbe4546fSGreg Roach        '_MILT',
69bbe4546fSGreg Roach        'ENGA',
70bbe4546fSGreg Roach        'MARB',
71bbe4546fSGreg Roach        'MARC',
72bbe4546fSGreg Roach        'MARL',
73bbe4546fSGreg Roach        '_MARI',
74bbe4546fSGreg Roach        '_MBON',
75bbe4546fSGreg Roach        'MARR',
76bbe4546fSGreg Roach        '_COML',
77bbe4546fSGreg Roach        '_STAT',
78bbe4546fSGreg Roach        '_SEPR',
79bbe4546fSGreg Roach        'DIVF',
80bbe4546fSGreg Roach        'MARS',
81bbe4546fSGreg Roach        'DIV',
82bbe4546fSGreg Roach        'ANUL',
83bbe4546fSGreg Roach        'CENS',
84bbe4546fSGreg Roach        'OCCU',
85bbe4546fSGreg Roach        'RESI',
86bbe4546fSGreg Roach        'PROP',
87bbe4546fSGreg Roach        'CHRA',
88bbe4546fSGreg Roach        'RETI',
89bbe4546fSGreg Roach        'FACT',
90bbe4546fSGreg Roach        'EVEN',
91bbe4546fSGreg Roach        '_NMR',
92bbe4546fSGreg Roach        '_NMAR',
93bbe4546fSGreg Roach        'NMR',
94bbe4546fSGreg Roach        'NCHI',
95bbe4546fSGreg Roach        'WILL',
96bbe4546fSGreg Roach        '_HOL',
97bbe4546fSGreg Roach        '_????_',
98bbe4546fSGreg Roach        'DEAT',
99bbe4546fSGreg Roach        '_FNRL',
100bbe4546fSGreg Roach        'CREM',
101bbe4546fSGreg Roach        'BURI',
102bbe4546fSGreg Roach        '_INTE',
103bbe4546fSGreg Roach        '_YART',
104bbe4546fSGreg Roach        '_NLIV',
105bbe4546fSGreg Roach        'PROB',
106bbe4546fSGreg Roach        'TITL',
107bbe4546fSGreg Roach        'COMM',
108bbe4546fSGreg Roach        'NATI',
109bbe4546fSGreg Roach        'CITN',
110bbe4546fSGreg Roach        'CAST',
111bbe4546fSGreg Roach        'RELI',
112bbe4546fSGreg Roach        'SSN',
113bbe4546fSGreg Roach        'IDNO',
114bbe4546fSGreg Roach        'TEMP',
115bbe4546fSGreg Roach        'SLGC',
116bbe4546fSGreg Roach        'BAPL',
117bbe4546fSGreg Roach        'CONL',
118bbe4546fSGreg Roach        'ENDL',
119bbe4546fSGreg Roach        'SLGS',
120701f5d18SGreg Roach        'NO',
121bbe4546fSGreg Roach        'ADDR',
122bbe4546fSGreg Roach        'PHON',
123bbe4546fSGreg Roach        'EMAIL',
124bbe4546fSGreg Roach        '_EMAIL',
125bbe4546fSGreg Roach        'EMAL',
126bbe4546fSGreg Roach        'FAX',
127bbe4546fSGreg Roach        'WWW',
128bbe4546fSGreg Roach        'URL',
129bbe4546fSGreg Roach        '_URL',
130544f68e3SGreg Roach        '_FSFTID',
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',
140701f5d18SGreg Roach        'CREA',
141bbe4546fSGreg Roach        'CHAN',
142bbe4546fSGreg Roach        '_TODO',
143bbe4546fSGreg Roach    ];
144bbe4546fSGreg Roach
14533c746f1SGreg Roach    // Unique identifier for this fact (currently implemented as a hash of the raw data).
14633c746f1SGreg Roach    private string $id;
147a25f0a04SGreg Roach
14833c746f1SGreg Roach    // The GEDCOM record from which this fact is taken
14933c746f1SGreg Roach    private GedcomRecord $record;
150a25f0a04SGreg Roach
15133c746f1SGreg Roach    // The raw GEDCOM data for this fact
15233c746f1SGreg Roach    private string $gedcom;
153a25f0a04SGreg Roach
15433c746f1SGreg Roach    // The GEDCOM tag for this record
15533c746f1SGreg Roach    private string $tag;
156a25f0a04SGreg Roach
15733c746f1SGreg Roach    private bool $pending_deletion = false;
158a25f0a04SGreg Roach
15933c746f1SGreg Roach    private bool $pending_addition = false;
160a25f0a04SGreg Roach
16133c746f1SGreg Roach    private Date $date;
162a25f0a04SGreg Roach
16333c746f1SGreg Roach    private Place $place;
164a25f0a04SGreg Roach
165bfed30e4SGreg Roach    // Used to sort facts
16633c746f1SGreg Roach    public int $sortOrder;
167a25f0a04SGreg Roach
168bfed30e4SGreg Roach    // Used by anniversary calculations
169bfed30e4SGreg Roach    public int $jd;
170bfed30e4SGreg Roach    public int $anniv;
171bfed30e4SGreg Roach
172a25f0a04SGreg Roach    /**
173a25f0a04SGreg Roach     * Create an event object from a gedcom fragment.
174a25f0a04SGreg Roach     * We need the parent object (to check privacy) and a (pseudo) fact ID to
175a25f0a04SGreg Roach     * identify the fact within the record.
176a25f0a04SGreg Roach     *
177a25f0a04SGreg Roach     * @param string       $gedcom
178a25f0a04SGreg Roach     * @param GedcomRecord $parent
179905ab80aSGreg Roach     * @param string       $id
180a25f0a04SGreg Roach     *
1818f5f5da8SGreg Roach     * @throws InvalidArgumentException
182a25f0a04SGreg Roach     */
18324f2a3afSGreg Roach    public function __construct(string $gedcom, GedcomRecord $parent, string $id)
184c1010edaSGreg Roach    {
1858d0ebef0SGreg Roach        if (preg_match('/^1 (' . Gedcom::REGEX_TAG . ')/', $gedcom, $match)) {
186a25f0a04SGreg Roach            $this->gedcom = $gedcom;
187e7766c08SGreg Roach            $this->record = $parent;
188905ab80aSGreg Roach            $this->id     = $id;
189a25f0a04SGreg Roach            $this->tag    = $match[1];
190a25f0a04SGreg Roach        } else {
19152ac7b12SGreg Roach            throw new InvalidArgumentException('Invalid GEDCOM data passed to Fact::_construct(' . $gedcom . ',' . $parent->xref() . ')');
192a25f0a04SGreg Roach        }
193a25f0a04SGreg Roach    }
194a25f0a04SGreg Roach
195a25f0a04SGreg Roach    /**
196a25f0a04SGreg Roach     * Get the value of level 1 data in the fact
197a25f0a04SGreg Roach     * Allow for multi-line values
198a25f0a04SGreg Roach     *
199baacc364SGreg Roach     * @return string
200a25f0a04SGreg Roach     */
201dfa848b8SGreg Roach    public function value(): string
202c1010edaSGreg Roach    {
203d247596dSGreg Roach        if (preg_match('/^1 ' . $this->tag . ' ?(.*(?:\n2 CONT ?.*)*)/', $this->gedcom, $match)) {
204a25f0a04SGreg Roach            return preg_replace("/\n2 CONT ?/", "\n", $match[1]);
205a25f0a04SGreg Roach        }
206b2ce94c6SRico Sonntag
207b2ce94c6SRico Sonntag        return '';
208a25f0a04SGreg Roach    }
209a25f0a04SGreg Roach
210a25f0a04SGreg Roach    /**
211a25f0a04SGreg Roach     * Get the record to which this fact links
212a25f0a04SGreg Roach     *
2130d15532eSGreg Roach     * @return Family|GedcomRecord|Individual|Location|Media|Note|Repository|Source|Submission|Submitter|null
214a25f0a04SGreg Roach     */
215dc124885SGreg Roach    public function target()
216c1010edaSGreg Roach    {
21762b52849SGreg Roach        if (!preg_match('/^@(' . Gedcom::REGEX_XREF . ')@$/', $this->value(), $match)) {
21862b52849SGreg Roach            return null;
21962b52849SGreg Roach        }
22062b52849SGreg Roach
22162b52849SGreg Roach        $xref = $match[1];
22262b52849SGreg Roach
223a25f0a04SGreg Roach        switch ($this->tag) {
224a25f0a04SGreg Roach            case 'FAMC':
225a25f0a04SGreg Roach            case 'FAMS':
226cc3a55ccSGreg Roach                return Registry::familyFactory()->make($xref, $this->record->tree());
227a25f0a04SGreg Roach            case 'HUSB':
228a25f0a04SGreg Roach            case 'WIFE':
22962b52849SGreg Roach            case 'ALIA':
230a25f0a04SGreg Roach            case 'CHIL':
23162b52849SGreg Roach            case '_ASSO':
232cc3a55ccSGreg Roach                return Registry::individualFactory()->make($xref, $this->record->tree());
23362b52849SGreg Roach            case 'ASSO':
23462b52849SGreg Roach                return
235cc3a55ccSGreg Roach                    Registry::individualFactory()->make($xref, $this->record->tree()) ??
236cc3a55ccSGreg Roach                    Registry::submitterFactory()->make($xref, $this->record->tree());
237a25f0a04SGreg Roach            case 'SOUR':
238cc3a55ccSGreg Roach                return Registry::sourceFactory()->make($xref, $this->record->tree());
239a25f0a04SGreg Roach            case 'OBJE':
240cc3a55ccSGreg Roach                return Registry::mediaFactory()->make($xref, $this->record->tree());
241a25f0a04SGreg Roach            case 'REPO':
242cc3a55ccSGreg Roach                return Registry::repositoryFactory()->make($xref, $this->record->tree());
243a25f0a04SGreg Roach            case 'NOTE':
244cc3a55ccSGreg Roach                return Registry::noteFactory()->make($xref, $this->record->tree());
24562b52849SGreg Roach            case 'ANCI':
24662b52849SGreg Roach            case 'DESI':
247ffc0a61fSGreg Roach            case 'SUBM':
248cc3a55ccSGreg Roach                return Registry::submitterFactory()->make($xref, $this->record->tree());
2491635452cSGreg Roach            case 'SUBN':
250cc3a55ccSGreg Roach                return Registry::submissionFactory()->make($xref, $this->record->tree());
2510d15532eSGreg Roach            case '_LOC':
252cc3a55ccSGreg Roach                return Registry::locationFactory()->make($xref, $this->record->tree());
253a25f0a04SGreg Roach            default:
254cc3a55ccSGreg Roach                return Registry::gedcomRecordFactory()->make($xref, $this->record->tree());
255a25f0a04SGreg Roach        }
256a25f0a04SGreg Roach    }
257a25f0a04SGreg Roach
258a25f0a04SGreg Roach    /**
259a25f0a04SGreg Roach     * Get the value of level 2 data in the fact
260a25f0a04SGreg Roach     *
261a25f0a04SGreg Roach     * @param string $tag
262a25f0a04SGreg Roach     *
263baacc364SGreg Roach     * @return string
264a25f0a04SGreg Roach     */
26524f2a3afSGreg Roach    public function attribute(string $tag): string
266c1010edaSGreg Roach    {
26710d6d973SGreg Roach        if (preg_match('/\n2 ' . $tag . '\b ?(.*(?:(?:\n3 CONT ?.*)*)*)/', $this->gedcom, $match)) {
26849985628SGreg Roach            $value = preg_replace("/\n3 CONT ?/", "\n", $match[1]);
26949985628SGreg Roach
2708939e2c2SGreg Roach            return Registry::elementFactory()->make($this->tag() . ':' . $tag)->canonical($value);
271a25f0a04SGreg Roach        }
272b2ce94c6SRico Sonntag
273b2ce94c6SRico Sonntag        return '';
274a25f0a04SGreg Roach    }
275a25f0a04SGreg Roach
276a25f0a04SGreg Roach    /**
2778af6bbf8SGreg Roach     * Get the PLAC:MAP:LATI for the fact.
2788af6bbf8SGreg Roach     *
27990949315SGreg Roach     * @return float|null
2808af6bbf8SGreg Roach     */
28190949315SGreg Roach    public function latitude(): ?float
2828af6bbf8SGreg Roach    {
2838af6bbf8SGreg Roach        if (preg_match('/\n4 LATI (.+)/', $this->gedcom, $match)) {
2848af6bbf8SGreg Roach            $gedcom_service = new GedcomService();
2858af6bbf8SGreg Roach
2868af6bbf8SGreg Roach            return $gedcom_service->readLatitude($match[1]);
2878af6bbf8SGreg Roach        }
2888af6bbf8SGreg Roach
2890ec592a7SDavid Drury        return null;
2908af6bbf8SGreg Roach    }
2918af6bbf8SGreg Roach
2928af6bbf8SGreg Roach    /**
2938af6bbf8SGreg Roach     * Get the PLAC:MAP:LONG for the fact.
2948af6bbf8SGreg Roach     *
29590949315SGreg Roach     * @return float|null
2968af6bbf8SGreg Roach     */
29790949315SGreg Roach    public function longitude(): ?float
2988af6bbf8SGreg Roach    {
299819ac503SRico Sonntag        if (preg_match('/\n4 LONG (.+)/', $this->gedcom, $match)) {
3008af6bbf8SGreg Roach            $gedcom_service = new GedcomService();
3018af6bbf8SGreg Roach
3028af6bbf8SGreg Roach            return $gedcom_service->readLongitude($match[1]);
3038af6bbf8SGreg Roach        }
3048af6bbf8SGreg Roach
3050ec592a7SDavid Drury        return null;
3068af6bbf8SGreg Roach    }
3078af6bbf8SGreg Roach
3088af6bbf8SGreg Roach    /**
309a25f0a04SGreg Roach     * Do the privacy rules allow us to display this fact to the current user
310a25f0a04SGreg Roach     *
311cbc1590aSGreg Roach     * @param int|null $access_level
312a25f0a04SGreg Roach     *
313cbc1590aSGreg Roach     * @return bool
314a25f0a04SGreg Roach     */
31535584196SGreg Roach    public function canShow(int $access_level = null): bool
316c1010edaSGreg Roach    {
317d9e083e7SGreg Roach        $access_level = $access_level ?? Auth::accessLevel($this->record->tree());
3184b9ff166SGreg Roach
31955105892SGreg Roach        // Does this record have an explicit restriction notice?
3204c2188e4SGreg Roach        $element     = new RestrictionNotice('');
3214c2188e4SGreg Roach        $restriction = $element->canonical($this->attribute('RESN'));
32255105892SGreg Roach
32355105892SGreg Roach        if (str_ends_with($restriction, RestrictionNotice::VALUE_CONFIDENTIAL)) {
3244b9ff166SGreg Roach            return Auth::PRIV_NONE >= $access_level;
325a25f0a04SGreg Roach        }
32655105892SGreg Roach
32755105892SGreg Roach        if (str_ends_with($restriction, RestrictionNotice::VALUE_PRIVACY)) {
3284b9ff166SGreg Roach            return Auth::PRIV_USER >= $access_level;
329a25f0a04SGreg Roach        }
33055105892SGreg Roach        if (str_ends_with($restriction, RestrictionNotice::VALUE_NONE)) {
331a25f0a04SGreg Roach            return true;
332a25f0a04SGreg Roach        }
333a25f0a04SGreg Roach
334b0a785fcSGreg Roach        // A link to a record of the same type: NOTE=>NOTE, OBJE=>OBJE, SOUR=>SOUR, etc.
335b0a785fcSGreg Roach        // Use the privacy of the target record.
336b0a785fcSGreg Roach        $target = $this->target();
337b0a785fcSGreg Roach
338b0a785fcSGreg Roach        if ($target instanceof GedcomRecord && $target->tag() === $this->tag) {
339b0a785fcSGreg Roach            return $target->canShow($access_level);
340b0a785fcSGreg Roach        }
341b0a785fcSGreg Roach
342a25f0a04SGreg Roach        // Does this record have a default RESN?
343c0935879SGreg Roach        $xref                    = $this->record->xref();
344f4afa648SGreg Roach        $fact_privacy            = $this->record->tree()->getFactPrivacy();
345f4afa648SGreg Roach        $individual_fact_privacy = $this->record->tree()->getIndividualFactPrivacy();
346518bbdc1SGreg Roach        if (isset($individual_fact_privacy[$xref][$this->tag])) {
347518bbdc1SGreg Roach            return $individual_fact_privacy[$xref][$this->tag] >= $access_level;
348a25f0a04SGreg Roach        }
349518bbdc1SGreg Roach        if (isset($fact_privacy[$this->tag])) {
350518bbdc1SGreg Roach            return $fact_privacy[$this->tag] >= $access_level;
351a25f0a04SGreg Roach        }
352a25f0a04SGreg Roach
353a25f0a04SGreg Roach        // No restrictions - it must be public
354a25f0a04SGreg Roach        return true;
355a25f0a04SGreg Roach    }
356a25f0a04SGreg Roach
357a25f0a04SGreg Roach    /**
358a25f0a04SGreg Roach     * Check whether this fact is protected against edit
359a25f0a04SGreg Roach     *
360cbc1590aSGreg Roach     * @return bool
361a25f0a04SGreg Roach     */
3628f53f488SRico Sonntag    public function canEdit(): bool
363c1010edaSGreg Roach    {
3641450f098SGreg Roach        if ($this->isPendingDeletion()) {
3651450f098SGreg Roach            return false;
3661450f098SGreg Roach        }
3671450f098SGreg Roach
3681450f098SGreg Roach        if (Auth::isManager($this->record->tree())) {
3691450f098SGreg Roach            return true;
3701450f098SGreg Roach        }
3711450f098SGreg Roach
372a25f0a04SGreg Roach        // Members cannot edit RESN, CHAN and locked records
37355105892SGreg Roach        return Auth::isEditor($this->record->tree()) && !str_ends_with($this->attribute('RESN'), RestrictionNotice::VALUE_LOCKED) && $this->tag !== 'RESN' && $this->tag !== 'CHAN';
374a25f0a04SGreg Roach    }
375a25f0a04SGreg Roach
376a25f0a04SGreg Roach    /**
377a25f0a04SGreg Roach     * The place where the event occured.
378a25f0a04SGreg Roach     *
379a25f0a04SGreg Roach     * @return Place
380a25f0a04SGreg Roach     */
3814fb14fcbSGreg Roach    public function place(): Place
382c1010edaSGreg Roach    {
383cc3a55ccSGreg Roach        $this->place ??= new Place($this->attribute('PLAC'), $this->record->tree());
384a25f0a04SGreg Roach
385a25f0a04SGreg Roach        return $this->place;
386a25f0a04SGreg Roach    }
387a25f0a04SGreg Roach
388a25f0a04SGreg Roach    /**
389a25f0a04SGreg Roach     * Get the date for this fact.
390a25f0a04SGreg Roach     * We can call this function many times, especially when sorting,
391a25f0a04SGreg Roach     * so keep a copy of the date.
392a25f0a04SGreg Roach     *
393a25f0a04SGreg Roach     * @return Date
394a25f0a04SGreg Roach     */
3952decada7SGreg Roach    public function date(): Date
396c1010edaSGreg Roach    {
397448d466cSGreg Roach        $this->date ??= new Date($this->attribute('DATE'));
398a25f0a04SGreg Roach
399a25f0a04SGreg Roach        return $this->date;
400a25f0a04SGreg Roach    }
401a25f0a04SGreg Roach
402a25f0a04SGreg Roach    /**
403a25f0a04SGreg Roach     * The raw GEDCOM data for this fact
404a25f0a04SGreg Roach     *
405a25f0a04SGreg Roach     * @return string
406a25f0a04SGreg Roach     */
407138ca96cSGreg Roach    public function gedcom(): string
408c1010edaSGreg Roach    {
409a25f0a04SGreg Roach        return $this->gedcom;
410a25f0a04SGreg Roach    }
411a25f0a04SGreg Roach
412a25f0a04SGreg Roach    /**
413a25f0a04SGreg Roach     * Get a (pseudo) primary key for this fact.
414a25f0a04SGreg Roach     *
415a25f0a04SGreg Roach     * @return string
416a25f0a04SGreg Roach     */
417905ab80aSGreg Roach    public function id(): string
418c1010edaSGreg Roach    {
419905ab80aSGreg Roach        return $this->id;
420a25f0a04SGreg Roach    }
421a25f0a04SGreg Roach
422a25f0a04SGreg Roach    /**
423a25f0a04SGreg Roach     * What is the tag (type) of this fact, such as BIRT, MARR or DEAT.
424a25f0a04SGreg Roach     *
425a25f0a04SGreg Roach     * @return string
426a25f0a04SGreg Roach     */
4279ecabc6aSGreg Roach    public function tag(): string
428c1010edaSGreg Roach    {
42902467d32SGreg Roach        return $this->record->tag() . ':' . $this->tag;
430a25f0a04SGreg Roach    }
431a25f0a04SGreg Roach
432a25f0a04SGreg Roach    /**
433decb512fSGreg Roach     * The GEDCOM record where this Fact came from
434a25f0a04SGreg Roach     *
435decb512fSGreg Roach     * @return GedcomRecord
436a25f0a04SGreg Roach     */
437decb512fSGreg Roach    public function record(): GedcomRecord
438c1010edaSGreg Roach    {
439e7766c08SGreg Roach        return $this->record;
440a25f0a04SGreg Roach    }
441a25f0a04SGreg Roach
442a25f0a04SGreg Roach    /**
443a25f0a04SGreg Roach     * Get the name of this fact type, for use as a label.
444a25f0a04SGreg Roach     *
445a25f0a04SGreg Roach     * @return string
446a25f0a04SGreg Roach     */
4477b7d8067SGreg Roach    public function label(): string
448c1010edaSGreg Roach    {
449d0fe92f3SGreg Roach        if (str_ends_with($this->tag(), ':NOTE') && preg_match('/^@' . Gedcom::REGEX_XREF . '@$/', $this->value())) {
450ae1057f3SGreg Roach            return I18N::translate('Shared note');
451ae1057f3SGreg Roach        }
452ae1057f3SGreg Roach
45370ac1a22SGreg Roach        // Marriages
45470ac1a22SGreg Roach        if ($this->tag() === 'FAM:MARR') {
45570ac1a22SGreg Roach            $element = Registry::elementFactory()->make('FAM:MARR:TYPE');
45670ac1a22SGreg Roach            $type = $this->attribute('TYPE');
45770ac1a22SGreg Roach
45870ac1a22SGreg Roach            if ($type !== '') {
45970ac1a22SGreg Roach                return $element->value($type, $this->record->tree());
46070ac1a22SGreg Roach            }
46170ac1a22SGreg Roach        }
46270ac1a22SGreg Roach
463a25f0a04SGreg Roach        // Custom FACT/EVEN - with a TYPE
46418475794SGreg Roach        if ($this->tag === 'FACT' || $this->tag === 'EVEN') {
46518475794SGreg Roach            $type = $this->attribute('TYPE');
46618475794SGreg Roach
46718475794SGreg Roach            if ($type !== '') {
4683f9fe4ddSGreg Roach                if (!str_contains($type, '%')) {
46918475794SGreg Roach                    // Allow user-translations of custom types.
47018475794SGreg Roach                    $translated = I18N::translate($type);
47118475794SGreg Roach
47218475794SGreg Roach                    if ($translated !== $type) {
47318475794SGreg Roach                        return $translated;
474a25f0a04SGreg Roach                    }
4753f9fe4ddSGreg Roach                }
4764e5adf68SGreg Roach
47718475794SGreg Roach                return e($type);
47818475794SGreg Roach            }
47918475794SGreg Roach        }
48018475794SGreg Roach
481455a30feSGreg Roach        return Registry::elementFactory()->make($this->tag())->label();
482a25f0a04SGreg Roach    }
483a25f0a04SGreg Roach
484a25f0a04SGreg Roach    /**
485a25f0a04SGreg Roach     * This is a newly deleted fact, pending approval.
4867e96c925SGreg Roach     *
4877e96c925SGreg Roach     * @return void
488a25f0a04SGreg Roach     */
489e364afe4SGreg Roach    public function setPendingDeletion(): void
490c1010edaSGreg Roach    {
491a25f0a04SGreg Roach        $this->pending_deletion = true;
492a25f0a04SGreg Roach        $this->pending_addition = false;
493a25f0a04SGreg Roach    }
494a25f0a04SGreg Roach
495a25f0a04SGreg Roach    /**
496a25f0a04SGreg Roach     * Is this a newly deleted fact, pending approval.
497a25f0a04SGreg Roach     *
498cbc1590aSGreg Roach     * @return bool
499a25f0a04SGreg Roach     */
5008f53f488SRico Sonntag    public function isPendingDeletion(): bool
501c1010edaSGreg Roach    {
502a25f0a04SGreg Roach        return $this->pending_deletion;
503a25f0a04SGreg Roach    }
504a25f0a04SGreg Roach
505a25f0a04SGreg Roach    /**
506a25f0a04SGreg Roach     * This is a newly added fact, pending approval.
5077e96c925SGreg Roach     *
5087e96c925SGreg Roach     * @return void
509a25f0a04SGreg Roach     */
510e364afe4SGreg Roach    public function setPendingAddition(): void
511c1010edaSGreg Roach    {
512a25f0a04SGreg Roach        $this->pending_addition = true;
513a25f0a04SGreg Roach        $this->pending_deletion = false;
514a25f0a04SGreg Roach    }
515a25f0a04SGreg Roach
516a25f0a04SGreg Roach    /**
517a25f0a04SGreg Roach     * Is this a newly added fact, pending approval.
518a25f0a04SGreg Roach     *
519cbc1590aSGreg Roach     * @return bool
520a25f0a04SGreg Roach     */
5218f53f488SRico Sonntag    public function isPendingAddition(): bool
522c1010edaSGreg Roach    {
523a25f0a04SGreg Roach        return $this->pending_addition;
524a25f0a04SGreg Roach    }
525a25f0a04SGreg Roach
526a25f0a04SGreg Roach    /**
527a25f0a04SGreg Roach     * A one-line summary of the fact - for charts, etc.
528a25f0a04SGreg Roach     *
529a25f0a04SGreg Roach     * @return string
530a25f0a04SGreg Roach     */
5318f53f488SRico Sonntag    public function summary(): string
532c1010edaSGreg Roach    {
53313abd6f3SGreg Roach        $attributes = [];
534dc124885SGreg Roach        $target     = $this->target();
535db7bb364SGreg Roach        if ($target instanceof GedcomRecord) {
53639ca88baSGreg Roach            $attributes[] = $target->fullName();
537a25f0a04SGreg Roach        } else {
538726d7b07SGreg Roach            // Fact value
53984586c02SGreg Roach            $value = $this->value();
540726d7b07SGreg Roach            if ($value !== '' && $value !== 'Y') {
541315eb316SGreg Roach                $attributes[] = '<bdi>' . e($value) . '</bdi>';
542a25f0a04SGreg Roach            }
543726d7b07SGreg Roach            // Fact date
5442decada7SGreg Roach            $date = $this->date();
545726d7b07SGreg Roach            if ($date->isOK()) {
546cc3a55ccSGreg Roach                if ($this->record instanceof Individual && in_array($this->tag, Gedcom::BIRTH_EVENTS, true) && $this->record->tree()->getPreference('SHOW_PARENTS_AGE')) {
547cc3a55ccSGreg Roach                    $attributes[] = $date->display() . view('fact-parents-age', ['individual' => $this->record, 'birth_date' => $date]);
548a25f0a04SGreg Roach                } else {
549a25f0a04SGreg Roach                    $attributes[] = $date->display();
550a25f0a04SGreg Roach                }
551726d7b07SGreg Roach            }
552726d7b07SGreg Roach            // Fact place
553e364afe4SGreg Roach            if ($this->place()->gedcomName() !== '') {
554392561bbSGreg Roach                $attributes[] = $this->place()->shortName();
555a25f0a04SGreg Roach            }
556a25f0a04SGreg Roach        }
5572d8f08abSGreg Roach
5587fe676e5SGreg Roach        $class = 'fact_' . $this->tag;
559a25f0a04SGreg Roach        if ($this->isPendingAddition()) {
56071a69701SGreg Roach            $class .= ' wt-new';
561a25f0a04SGreg Roach        } elseif ($this->isPendingDeletion()) {
56271a69701SGreg Roach            $class .= ' wt-old';
563a25f0a04SGreg Roach        }
5642d8f08abSGreg Roach
56506c87791SGreg Roach        $label = '<span class="label">' . $this->label() . '</span>';
56606c87791SGreg Roach        $value = '<span class="field" dir="auto">' . implode(' — ', $attributes) . '</span>';
56706c87791SGreg Roach
5682d8f08abSGreg Roach        /* I18N: a label/value pair, such as “Occupation: Farmer”. Some languages may need to change the punctuation. */
56906c87791SGreg Roach        return '<div class="' . $class . '">' . I18N::translate('%1$s: %2$s', $label, $value) . '</div>';
570a25f0a04SGreg Roach    }
571a25f0a04SGreg Roach
572a25f0a04SGreg Roach    /**
5739927f70fSGreg Roach     * A one-line summary of the fact - for the clipboard, etc.
5749927f70fSGreg Roach     *
5759927f70fSGreg Roach     * @return string
5769927f70fSGreg Roach     */
5779927f70fSGreg Roach    public function name(): string
5789927f70fSGreg Roach    {
5799927f70fSGreg Roach        $items  = [$this->label()];
5809927f70fSGreg Roach        $target = $this->target();
5819927f70fSGreg Roach
5829927f70fSGreg Roach        if ($target instanceof GedcomRecord) {
583315eb316SGreg Roach            $items[] = '<bdi>' . $target->fullName() . '</bdi>';
5849927f70fSGreg Roach        } else {
5859927f70fSGreg Roach            // Fact value
5869927f70fSGreg Roach            $value = $this->value();
5879927f70fSGreg Roach            if ($value !== '' && $value !== 'Y') {
588315eb316SGreg Roach                $items[] = '<bdi>' . e($value) . '</bdi>';
5899927f70fSGreg Roach            }
5909927f70fSGreg Roach
5919927f70fSGreg Roach            // Fact date
5929927f70fSGreg Roach            if ($this->date()->isOK()) {
5939927f70fSGreg Roach                $items[] = $this->date()->minimumDate()->format('%Y');
5949927f70fSGreg Roach            }
5959927f70fSGreg Roach
5969927f70fSGreg Roach            // Fact place
5979927f70fSGreg Roach            if ($this->place()->gedcomName() !== '') {
5989927f70fSGreg Roach                $items[] = $this->place()->shortName();
5999927f70fSGreg Roach            }
6009927f70fSGreg Roach        }
6019927f70fSGreg Roach
6029927f70fSGreg Roach        return implode(' — ', $items);
6039927f70fSGreg Roach    }
6049927f70fSGreg Roach
6059927f70fSGreg Roach    /**
606580a4d11SGreg Roach     * Helper functions to sort facts
607a25f0a04SGreg Roach     *
608580a4d11SGreg Roach     * @return Closure
609a25f0a04SGreg Roach     */
610580a4d11SGreg Roach    private static function dateComparator(): Closure
611c1010edaSGreg Roach    {
6126c2179e2SGreg Roach        return static function (Fact $a, Fact $b): int {
6132decada7SGreg Roach            if ($a->date()->isOK() && $b->date()->isOK()) {
614a25f0a04SGreg Roach                // If both events have dates, compare by date
6152decada7SGreg Roach                $ret = Date::compare($a->date(), $b->date());
616a25f0a04SGreg Roach
617dfa848b8SGreg Roach                if ($ret === 0) {
618580a4d11SGreg Roach                    // If dates overlap, compare by fact type
619580a4d11SGreg Roach                    $ret = self::typeComparator()($a, $b);
620a25f0a04SGreg Roach
621a25f0a04SGreg Roach                    // If the fact type is also the same, retain the initial order
622dfa848b8SGreg Roach                    if ($ret === 0) {
623580a4d11SGreg Roach                        $ret = $a->sortOrder <=> $b->sortOrder;
624a25f0a04SGreg Roach                    }
625a25f0a04SGreg Roach                }
626a25f0a04SGreg Roach
627a25f0a04SGreg Roach                return $ret;
628b2ce94c6SRico Sonntag            }
629b2ce94c6SRico Sonntag
630a25f0a04SGreg Roach            // One or both events have no date - retain the initial order
631580a4d11SGreg Roach            return $a->sortOrder <=> $b->sortOrder;
632580a4d11SGreg Roach        };
633a25f0a04SGreg Roach    }
634a25f0a04SGreg Roach
635a25f0a04SGreg Roach    /**
636580a4d11SGreg Roach     * Helper functions to sort facts.
637a25f0a04SGreg Roach     *
638580a4d11SGreg Roach     * @return Closure
639a25f0a04SGreg Roach     */
640580a4d11SGreg Roach    public static function typeComparator(): Closure
641c1010edaSGreg Roach    {
642bbe4546fSGreg Roach        static $factsort = [];
643a25f0a04SGreg Roach
64454c1ab5eSGreg Roach        if ($factsort === []) {
645bbe4546fSGreg Roach            $factsort = array_flip(self::FACT_ORDER);
646a25f0a04SGreg Roach        }
647a25f0a04SGreg Roach
6486c2179e2SGreg Roach        return static function (Fact $a, Fact $b) use ($factsort): int {
649a25f0a04SGreg Roach            // Facts from same families stay grouped together
650a25f0a04SGreg Roach            // Keep MARR and DIV from the same families from mixing with events from other FAMs
651a25f0a04SGreg Roach            // Use the original order in which the facts were added
652e7766c08SGreg Roach            if ($a->record instanceof Family && $b->record instanceof Family && $a->record !== $b->record) {
6536a12476fSGreg Roach                return $a->sortOrder <=> $b->sortOrder;
654a25f0a04SGreg Roach            }
655a25f0a04SGreg Roach
656*a2143fbeSGreg Roach            // NO events sort as the non-event itself.
657*a2143fbeSGreg Roach            $atag = $a->tag === 'NO' ? $a->value() : $a->tag;
658*a2143fbeSGreg Roach            $btag = $b->tag === 'NO' ? $b->value() : $b->tag;
659a25f0a04SGreg Roach
660a25f0a04SGreg Roach            // Events not in the above list get mapped onto one that is.
661a25f0a04SGreg Roach            if (!array_key_exists($atag, $factsort)) {
6627a6ee1acSGreg Roach                $atag = '_????_';
663a25f0a04SGreg Roach            }
664a25f0a04SGreg Roach
665a25f0a04SGreg Roach            if (!array_key_exists($btag, $factsort)) {
6667a6ee1acSGreg Roach                $btag = '_????_';
667a25f0a04SGreg Roach            }
668a25f0a04SGreg Roach
669a25f0a04SGreg Roach            // - Don't let dated after DEAT/BURI facts sort non-dated facts before DEAT/BURI
670a25f0a04SGreg Roach            // - Treat dated after BURI facts as BURI instead
6713425616eSGreg Roach            if ($a->attribute('DATE') !== '' && $factsort[$atag] > $factsort['BURI'] && $factsort[$atag] < $factsort['CHAN']) {
672a25f0a04SGreg Roach                $atag = 'BURI';
673a25f0a04SGreg Roach            }
674a25f0a04SGreg Roach
6753425616eSGreg Roach            if ($b->attribute('DATE') !== '' && $factsort[$btag] > $factsort['BURI'] && $factsort[$btag] < $factsort['CHAN']) {
676a25f0a04SGreg Roach                $btag = 'BURI';
677a25f0a04SGreg Roach            }
678a25f0a04SGreg Roach
679a25f0a04SGreg Roach            // If facts are the same then put dated facts before non-dated facts
6806a12476fSGreg Roach            if ($atag === $btag) {
6813425616eSGreg Roach                if ($a->attribute('DATE') !== '' && $b->attribute('DATE') === '') {
682a25f0a04SGreg Roach                    return -1;
683a25f0a04SGreg Roach                }
684a25f0a04SGreg Roach
6853425616eSGreg Roach                if ($b->attribute('DATE') !== '' && $a->attribute('DATE') === '') {
686a25f0a04SGreg Roach                    return 1;
687a25f0a04SGreg Roach                }
688a25f0a04SGreg Roach
689a25f0a04SGreg Roach                // If no sorting preference, then keep original ordering
6906a12476fSGreg Roach                return $a->sortOrder <=> $b->sortOrder;
691a25f0a04SGreg Roach            }
692a25f0a04SGreg Roach
6936a12476fSGreg Roach            return $factsort[$atag] <=> $factsort[$btag];
694580a4d11SGreg Roach        };
695580a4d11SGreg Roach    }
696580a4d11SGreg Roach
697580a4d11SGreg Roach    /**
698580a4d11SGreg Roach     * A multi-key sort
699580a4d11SGreg Roach     * 1. First divide the facts into two arrays one set with dates and one set without dates
700580a4d11SGreg Roach     * 2. Sort each of the two new arrays, the date using the compare date function, the non-dated
701580a4d11SGreg Roach     * using the compare type function
702580a4d11SGreg Roach     * 3. Then merge the arrays back into the original array using the compare type function
703580a4d11SGreg Roach     *
70436779af1SGreg Roach     * @param Collection<int,Fact> $unsorted
705580a4d11SGreg Roach     *
70636779af1SGreg Roach     * @return Collection<int,Fact>
707580a4d11SGreg Roach     */
7088af3e5c1SGreg Roach    public static function sortFacts(Collection $unsorted): Collection
709580a4d11SGreg Roach    {
710580a4d11SGreg Roach        $dated    = [];
711580a4d11SGreg Roach        $nondated = [];
712580a4d11SGreg Roach        $sorted   = [];
713580a4d11SGreg Roach
714580a4d11SGreg Roach        // Split the array into dated and non-dated arrays
715580a4d11SGreg Roach        $order = 0;
716580a4d11SGreg Roach
717580a4d11SGreg Roach        foreach ($unsorted as $fact) {
718580a4d11SGreg Roach            $fact->sortOrder = $order;
719580a4d11SGreg Roach            $order++;
720580a4d11SGreg Roach
721580a4d11SGreg Roach            if ($fact->date()->isOK()) {
722580a4d11SGreg Roach                $dated[] = $fact;
723580a4d11SGreg Roach            } else {
724580a4d11SGreg Roach                $nondated[] = $fact;
725580a4d11SGreg Roach            }
726580a4d11SGreg Roach        }
727580a4d11SGreg Roach
728580a4d11SGreg Roach        usort($dated, self::dateComparator());
729580a4d11SGreg Roach        usort($nondated, self::typeComparator());
730580a4d11SGreg Roach
731580a4d11SGreg Roach        // Merge the arrays
732580a4d11SGreg Roach        $dc = count($dated);
733580a4d11SGreg Roach        $nc = count($nondated);
734580a4d11SGreg Roach        $i  = 0;
735580a4d11SGreg Roach        $j  = 0;
736580a4d11SGreg Roach
737580a4d11SGreg Roach        // while there is anything in the dated array continue merging
738580a4d11SGreg Roach        while ($i < $dc) {
739580a4d11SGreg Roach            // compare each fact by type to merge them in order
740580a4d11SGreg Roach            if ($j < $nc && self::typeComparator()($dated[$i], $nondated[$j]) > 0) {
741580a4d11SGreg Roach                $sorted[] = $nondated[$j];
742580a4d11SGreg Roach                $j++;
743580a4d11SGreg Roach            } else {
744580a4d11SGreg Roach                $sorted[] = $dated[$i];
745580a4d11SGreg Roach                $i++;
746580a4d11SGreg Roach            }
747580a4d11SGreg Roach        }
748580a4d11SGreg Roach
749580a4d11SGreg Roach        // get anything that might be left in the nondated array
750580a4d11SGreg Roach        while ($j < $nc) {
751580a4d11SGreg Roach            $sorted[] = $nondated[$j];
752580a4d11SGreg Roach            $j++;
753580a4d11SGreg Roach        }
754580a4d11SGreg Roach
7551c7a6874SGreg Roach        return new Collection($sorted);
756a25f0a04SGreg Roach    }
757a25f0a04SGreg Roach
758a25f0a04SGreg Roach    /**
759ae71f733SGreg Roach     * Sort fact/event tags using the same order that we use for facts.
760ae71f733SGreg Roach     *
76136779af1SGreg Roach     * @param Collection<int,string> $unsorted
762ae71f733SGreg Roach     *
76336779af1SGreg Roach     * @return Collection<int,string>
764ae71f733SGreg Roach     */
765ae71f733SGreg Roach    public static function sortFactTags(Collection $unsorted): Collection
766ae71f733SGreg Roach    {
767ae71f733SGreg Roach        $tag_order = array_flip(self::FACT_ORDER);
768ae71f733SGreg Roach
769ae71f733SGreg Roach        return $unsorted->sort(static function (string $x, string $y) use ($tag_order): int {
770ae71f733SGreg Roach            $sort_x = $tag_order[$x] ?? $tag_order['_????_'];
771ae71f733SGreg Roach            $sort_y = $tag_order[$y] ?? $tag_order['_????_'];
772ae71f733SGreg Roach
773ae71f733SGreg Roach            return $sort_x - $sort_y;
774ae71f733SGreg Roach        });
775ae71f733SGreg Roach    }
776ae71f733SGreg Roach
777ae71f733SGreg Roach    /**
778a25f0a04SGreg Roach     * Allow native PHP functions such as array_unique() to work with objects
779a25f0a04SGreg Roach     *
780a25f0a04SGreg Roach     * @return string
781a25f0a04SGreg Roach     */
782dfa848b8SGreg Roach    public function __toString(): string
783c1010edaSGreg Roach    {
784c0935879SGreg Roach        return $this->id . '@' . $this->record->xref();
785a25f0a04SGreg Roach    }
786a25f0a04SGreg Roach}
787