xref: /webtrees/app/Fact.php (revision 1635452c5b89c89ae049ee7ed6a6c423bdd28af4)
1a25f0a04SGreg Roach<?php
23976b470SGreg Roach
3a25f0a04SGreg Roach/**
4a25f0a04SGreg Roach * webtrees: online genealogy
58fcd0d32SGreg Roach * Copyright (C) 2019 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
15a25f0a04SGreg Roach * along with this program. If not, see <http://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;
371450f098SGreg Roachuse function strpos;
38ae71f733SGreg Roachuse function trim;
39ae71f733SGreg Roachuse function usort;
40ae71f733SGreg Roach
41ae71f733SGreg Roachuse const PREG_SET_ORDER;
42a25f0a04SGreg Roach
43a25f0a04SGreg Roach/**
4476692c8bSGreg Roach * A GEDCOM fact or event object.
45a25f0a04SGreg Roach */
46c1010edaSGreg Roachclass Fact
47c1010edaSGreg Roach{
4816d6367aSGreg Roach    private const FACT_ORDER = [
49bbe4546fSGreg Roach        'BIRT',
50bbe4546fSGreg Roach        '_HNM',
51bbe4546fSGreg Roach        'ALIA',
52bbe4546fSGreg Roach        '_AKA',
53bbe4546fSGreg Roach        '_AKAN',
54bbe4546fSGreg Roach        'ADOP',
55bbe4546fSGreg Roach        '_ADPF',
56bbe4546fSGreg Roach        '_ADPF',
57bbe4546fSGreg Roach        '_BRTM',
58bbe4546fSGreg Roach        'CHR',
59bbe4546fSGreg Roach        'BAPM',
60bbe4546fSGreg Roach        'FCOM',
61bbe4546fSGreg Roach        'CONF',
62bbe4546fSGreg Roach        'BARM',
63bbe4546fSGreg Roach        'BASM',
64bbe4546fSGreg Roach        'EDUC',
65bbe4546fSGreg Roach        'GRAD',
66bbe4546fSGreg Roach        '_DEG',
67bbe4546fSGreg Roach        'EMIG',
68bbe4546fSGreg Roach        'IMMI',
69bbe4546fSGreg Roach        'NATU',
70bbe4546fSGreg Roach        '_MILI',
71bbe4546fSGreg Roach        '_MILT',
72bbe4546fSGreg Roach        'ENGA',
73bbe4546fSGreg Roach        'MARB',
74bbe4546fSGreg Roach        'MARC',
75bbe4546fSGreg Roach        'MARL',
76bbe4546fSGreg Roach        '_MARI',
77bbe4546fSGreg Roach        '_MBON',
78bbe4546fSGreg Roach        'MARR',
79bbe4546fSGreg Roach        'MARR_CIVIL',
80bbe4546fSGreg Roach        'MARR_RELIGIOUS',
81bbe4546fSGreg Roach        'MARR_PARTNERS',
82bbe4546fSGreg Roach        'MARR_UNKNOWN',
83bbe4546fSGreg Roach        '_COML',
84bbe4546fSGreg Roach        '_STAT',
85bbe4546fSGreg Roach        '_SEPR',
86bbe4546fSGreg Roach        'DIVF',
87bbe4546fSGreg Roach        'MARS',
88bbe4546fSGreg Roach        '_BIRT_CHIL',
89bbe4546fSGreg Roach        'DIV',
90bbe4546fSGreg Roach        'ANUL',
91bbe4546fSGreg Roach        '_BIRT_',
92bbe4546fSGreg Roach        '_MARR_',
93bbe4546fSGreg Roach        '_DEAT_',
94bbe4546fSGreg Roach        '_BURI_',
95bbe4546fSGreg Roach        'CENS',
96bbe4546fSGreg Roach        'OCCU',
97bbe4546fSGreg Roach        'RESI',
98bbe4546fSGreg Roach        'PROP',
99bbe4546fSGreg Roach        'CHRA',
100bbe4546fSGreg Roach        'RETI',
101bbe4546fSGreg Roach        'FACT',
102bbe4546fSGreg Roach        'EVEN',
103bbe4546fSGreg Roach        '_NMR',
104bbe4546fSGreg Roach        '_NMAR',
105bbe4546fSGreg Roach        'NMR',
106bbe4546fSGreg Roach        'NCHI',
107bbe4546fSGreg Roach        'WILL',
108bbe4546fSGreg Roach        '_HOL',
109bbe4546fSGreg Roach        '_????_',
110bbe4546fSGreg Roach        'DEAT',
111bbe4546fSGreg Roach        '_FNRL',
112bbe4546fSGreg Roach        'CREM',
113bbe4546fSGreg Roach        'BURI',
114bbe4546fSGreg Roach        '_INTE',
115bbe4546fSGreg Roach        '_YART',
116bbe4546fSGreg Roach        '_NLIV',
117bbe4546fSGreg Roach        'PROB',
118bbe4546fSGreg Roach        'TITL',
119bbe4546fSGreg Roach        'COMM',
120bbe4546fSGreg Roach        'NATI',
121bbe4546fSGreg Roach        'CITN',
122bbe4546fSGreg Roach        'CAST',
123bbe4546fSGreg Roach        'RELI',
124bbe4546fSGreg Roach        'SSN',
125bbe4546fSGreg Roach        'IDNO',
126bbe4546fSGreg Roach        'TEMP',
127bbe4546fSGreg Roach        'SLGC',
128bbe4546fSGreg Roach        'BAPL',
129bbe4546fSGreg Roach        'CONL',
130bbe4546fSGreg Roach        'ENDL',
131bbe4546fSGreg Roach        'SLGS',
132bbe4546fSGreg Roach        'ADDR',
133bbe4546fSGreg Roach        'PHON',
134bbe4546fSGreg Roach        'EMAIL',
135bbe4546fSGreg Roach        '_EMAIL',
136bbe4546fSGreg Roach        'EMAL',
137bbe4546fSGreg Roach        'FAX',
138bbe4546fSGreg Roach        'WWW',
139bbe4546fSGreg Roach        'URL',
140bbe4546fSGreg Roach        '_URL',
141bbe4546fSGreg Roach        'AFN',
142bbe4546fSGreg Roach        'REFN',
143bbe4546fSGreg Roach        '_PRMN',
144bbe4546fSGreg Roach        'REF',
145bbe4546fSGreg Roach        'RIN',
146bbe4546fSGreg Roach        '_UID',
147bbe4546fSGreg Roach        'OBJE',
148bbe4546fSGreg Roach        'NOTE',
149bbe4546fSGreg Roach        'SOUR',
150bbe4546fSGreg Roach        'CHAN',
151bbe4546fSGreg Roach        '_TODO',
152bbe4546fSGreg Roach    ];
153bbe4546fSGreg Roach
154a25f0a04SGreg Roach    /** @var string Unique identifier for this fact (currently implemented as a hash of the raw data). */
155905ab80aSGreg Roach    private $id;
156a25f0a04SGreg Roach
157a25f0a04SGreg Roach    /** @var GedcomRecord The GEDCOM record from which this fact is taken */
158e7766c08SGreg Roach    private $record;
159a25f0a04SGreg Roach
160a25f0a04SGreg Roach    /** @var string The raw GEDCOM data for this fact */
161a25f0a04SGreg Roach    private $gedcom;
162a25f0a04SGreg Roach
163a25f0a04SGreg Roach    /** @var string The GEDCOM tag for this record */
164a25f0a04SGreg Roach    private $tag;
165a25f0a04SGreg Roach
166cbc1590aSGreg Roach    /** @var bool Is this a recently deleted fact, pending approval? */
167a25f0a04SGreg Roach    private $pending_deletion = false;
168a25f0a04SGreg Roach
169cbc1590aSGreg Roach    /** @var bool Is this a recently added fact, pending approval? */
170a25f0a04SGreg Roach    private $pending_addition = false;
171a25f0a04SGreg Roach
172a25f0a04SGreg Roach    /** @var Date The date of this fact, from the “2 DATE …” attribute */
173a25f0a04SGreg Roach    private $date;
174a25f0a04SGreg Roach
175a25f0a04SGreg Roach    /** @var Place The place of this fact, from the “2 PLAC …” attribute */
176a25f0a04SGreg Roach    private $place;
177a25f0a04SGreg Roach
178580a4d11SGreg Roach    /** @var int Used by Functions::sortFacts() */
179580a4d11SGreg Roach    private $sortOrder;
180a25f0a04SGreg Roach
181a25f0a04SGreg Roach    /**
182a25f0a04SGreg Roach     * Create an event object from a gedcom fragment.
183a25f0a04SGreg Roach     * We need the parent object (to check privacy) and a (pseudo) fact ID to
184a25f0a04SGreg Roach     * identify the fact within the record.
185a25f0a04SGreg Roach     *
186a25f0a04SGreg Roach     * @param string       $gedcom
187a25f0a04SGreg Roach     * @param GedcomRecord $parent
188905ab80aSGreg Roach     * @param string       $id
189a25f0a04SGreg Roach     *
1908f5f5da8SGreg Roach     * @throws InvalidArgumentException
191a25f0a04SGreg Roach     */
192905ab80aSGreg Roach    public function __construct($gedcom, GedcomRecord $parent, $id)
193c1010edaSGreg Roach    {
1948d0ebef0SGreg Roach        if (preg_match('/^1 (' . Gedcom::REGEX_TAG . ')/', $gedcom, $match)) {
195a25f0a04SGreg Roach            $this->gedcom = $gedcom;
196e7766c08SGreg Roach            $this->record = $parent;
197905ab80aSGreg Roach            $this->id     = $id;
198a25f0a04SGreg Roach            $this->tag    = $match[1];
199a25f0a04SGreg Roach        } else {
2008f5f5da8SGreg Roach            throw new InvalidArgumentException('Invalid GEDCOM data passed to Fact::_construct(' . $gedcom . ')');
201a25f0a04SGreg Roach        }
202a25f0a04SGreg Roach    }
203a25f0a04SGreg Roach
204a25f0a04SGreg Roach    /**
205a25f0a04SGreg Roach     * Get the value of level 1 data in the fact
206a25f0a04SGreg Roach     * Allow for multi-line values
207a25f0a04SGreg Roach     *
208baacc364SGreg Roach     * @return string
209a25f0a04SGreg Roach     */
210dfa848b8SGreg Roach    public function value(): string
211c1010edaSGreg Roach    {
212a25f0a04SGreg Roach        if (preg_match('/^1 (?:' . $this->tag . ') ?(.*(?:(?:\n2 CONT ?.*)*))/', $this->gedcom, $match)) {
213a25f0a04SGreg Roach            return preg_replace("/\n2 CONT ?/", "\n", $match[1]);
214a25f0a04SGreg Roach        }
215b2ce94c6SRico Sonntag
216b2ce94c6SRico Sonntag        return '';
217a25f0a04SGreg Roach    }
218a25f0a04SGreg Roach
219a25f0a04SGreg Roach    /**
220a25f0a04SGreg Roach     * Get the record to which this fact links
221a25f0a04SGreg Roach     *
222*1635452cSGreg Roach     * @return Individual|Family|Source|Submitter|Submission|Repository|Media|Note|GedcomRecord|null
223a25f0a04SGreg Roach     */
224dc124885SGreg Roach    public function target()
225c1010edaSGreg Roach    {
22662b52849SGreg Roach        if (!preg_match('/^@(' . Gedcom::REGEX_XREF . ')@$/', $this->value(), $match)) {
22762b52849SGreg Roach            return null;
22862b52849SGreg Roach        }
22962b52849SGreg Roach
23062b52849SGreg Roach        $xref = $match[1];
23162b52849SGreg Roach
232a25f0a04SGreg Roach        switch ($this->tag) {
233a25f0a04SGreg Roach            case 'FAMC':
234a25f0a04SGreg Roach            case 'FAMS':
235f4afa648SGreg Roach                return Family::getInstance($xref, $this->record()->tree());
236a25f0a04SGreg Roach            case 'HUSB':
237a25f0a04SGreg Roach            case 'WIFE':
23862b52849SGreg Roach            case 'ALIA':
239a25f0a04SGreg Roach            case 'CHIL':
24062b52849SGreg Roach            case '_ASSO':
241f4afa648SGreg Roach                return Individual::getInstance($xref, $this->record()->tree());
24262b52849SGreg Roach            case 'ASSO':
24362b52849SGreg Roach                return
24462b52849SGreg Roach                    Individual::getInstance($xref, $this->record()->tree()) ??
24562b52849SGreg Roach                    Submitter::getInstance($xref, $this->record()->tree());
246a25f0a04SGreg Roach            case 'SOUR':
247f4afa648SGreg Roach                return Source::getInstance($xref, $this->record()->tree());
248a25f0a04SGreg Roach            case 'OBJE':
249f4afa648SGreg Roach                return Media::getInstance($xref, $this->record()->tree());
250a25f0a04SGreg Roach            case 'REPO':
251f4afa648SGreg Roach                return Repository::getInstance($xref, $this->record()->tree());
252a25f0a04SGreg Roach            case 'NOTE':
253f4afa648SGreg Roach                return Note::getInstance($xref, $this->record()->tree());
25462b52849SGreg Roach            case 'ANCI':
25562b52849SGreg Roach            case 'DESI':
256ffc0a61fSGreg Roach            case 'SUBM':
257ffc0a61fSGreg Roach                return Submitter::getInstance($xref, $this->record()->tree());
258*1635452cSGreg Roach            case 'SUBN':
259*1635452cSGreg Roach                return Submission::getInstance($xref, $this->record()->tree());
260a25f0a04SGreg Roach            default:
261f4afa648SGreg Roach                return GedcomRecord::getInstance($xref, $this->record()->tree());
262a25f0a04SGreg Roach        }
263a25f0a04SGreg Roach    }
264a25f0a04SGreg Roach
265a25f0a04SGreg Roach    /**
266a25f0a04SGreg Roach     * Get the value of level 2 data in the fact
267a25f0a04SGreg Roach     *
268a25f0a04SGreg Roach     * @param string $tag
269a25f0a04SGreg Roach     *
270baacc364SGreg Roach     * @return string
271a25f0a04SGreg Roach     */
272dfa848b8SGreg Roach    public function attribute($tag): string
273c1010edaSGreg Roach    {
274a25f0a04SGreg Roach        if (preg_match('/\n2 (?:' . $tag . ') ?(.*(?:(?:\n3 CONT ?.*)*)*)/', $this->gedcom, $match)) {
275a25f0a04SGreg Roach            return preg_replace("/\n3 CONT ?/", "\n", $match[1]);
276a25f0a04SGreg Roach        }
277b2ce94c6SRico Sonntag
278b2ce94c6SRico Sonntag        return '';
279a25f0a04SGreg Roach    }
280a25f0a04SGreg Roach
281a25f0a04SGreg Roach    /**
2828af6bbf8SGreg Roach     * Get the PLAC:MAP:LATI for the fact.
2838af6bbf8SGreg Roach     *
2848af6bbf8SGreg Roach     * @return float
2858af6bbf8SGreg Roach     */
2868af6bbf8SGreg Roach    public function latitude(): float
2878af6bbf8SGreg Roach    {
2888af6bbf8SGreg Roach        if (preg_match('/\n4 LATI (.+)/', $this->gedcom, $match)) {
2898af6bbf8SGreg Roach            $gedcom_service = new GedcomService();
2908af6bbf8SGreg Roach
2918af6bbf8SGreg Roach            return $gedcom_service->readLatitude($match[1]);
2928af6bbf8SGreg Roach        }
2938af6bbf8SGreg Roach
2948af6bbf8SGreg Roach        return 0.0;
2958af6bbf8SGreg Roach    }
2968af6bbf8SGreg Roach
2978af6bbf8SGreg Roach    /**
2988af6bbf8SGreg Roach     * Get the PLAC:MAP:LONG for the fact.
2998af6bbf8SGreg Roach     *
3008af6bbf8SGreg Roach     * @return float
3018af6bbf8SGreg Roach     */
3028af6bbf8SGreg Roach    public function longitude(): float
3038af6bbf8SGreg Roach    {
304819ac503SRico Sonntag        if (preg_match('/\n4 LONG (.+)/', $this->gedcom, $match)) {
3058af6bbf8SGreg Roach            $gedcom_service = new GedcomService();
3068af6bbf8SGreg Roach
3078af6bbf8SGreg Roach            return $gedcom_service->readLongitude($match[1]);
3088af6bbf8SGreg Roach        }
3098af6bbf8SGreg Roach
3108af6bbf8SGreg Roach        return 0.0;
3118af6bbf8SGreg Roach    }
3128af6bbf8SGreg Roach
3138af6bbf8SGreg Roach    /**
314a25f0a04SGreg Roach     * Do the privacy rules allow us to display this fact to the current user
315a25f0a04SGreg Roach     *
316cbc1590aSGreg Roach     * @param int|null $access_level
317a25f0a04SGreg Roach     *
318cbc1590aSGreg Roach     * @return bool
319a25f0a04SGreg Roach     */
32035584196SGreg Roach    public function canShow(int $access_level = null): bool
321c1010edaSGreg Roach    {
322d9e083e7SGreg Roach        $access_level = $access_level ?? Auth::accessLevel($this->record->tree());
3234b9ff166SGreg Roach
324a25f0a04SGreg Roach        // Does this record have an explicit RESN?
32520ff464cSGreg Roach        if (strpos($this->gedcom, "\n2 RESN confidential") !== false) {
3264b9ff166SGreg Roach            return Auth::PRIV_NONE >= $access_level;
327a25f0a04SGreg Roach        }
32820ff464cSGreg Roach        if (strpos($this->gedcom, "\n2 RESN privacy") !== false) {
3294b9ff166SGreg Roach            return Auth::PRIV_USER >= $access_level;
330a25f0a04SGreg Roach        }
33120ff464cSGreg Roach        if (strpos($this->gedcom, "\n2 RESN none") !== false) {
332a25f0a04SGreg Roach            return true;
333a25f0a04SGreg Roach        }
334a25f0a04SGreg Roach
335a25f0a04SGreg Roach        // Does this record have a default RESN?
336c0935879SGreg Roach        $xref                    = $this->record->xref();
337f4afa648SGreg Roach        $fact_privacy            = $this->record->tree()->getFactPrivacy();
338f4afa648SGreg Roach        $individual_fact_privacy = $this->record->tree()->getIndividualFactPrivacy();
339518bbdc1SGreg Roach        if (isset($individual_fact_privacy[$xref][$this->tag])) {
340518bbdc1SGreg Roach            return $individual_fact_privacy[$xref][$this->tag] >= $access_level;
341a25f0a04SGreg Roach        }
342518bbdc1SGreg Roach        if (isset($fact_privacy[$this->tag])) {
343518bbdc1SGreg Roach            return $fact_privacy[$this->tag] >= $access_level;
344a25f0a04SGreg Roach        }
345a25f0a04SGreg Roach
346a25f0a04SGreg Roach        // No restrictions - it must be public
347a25f0a04SGreg Roach        return true;
348a25f0a04SGreg Roach    }
349a25f0a04SGreg Roach
350a25f0a04SGreg Roach    /**
351a25f0a04SGreg Roach     * Check whether this fact is protected against edit
352a25f0a04SGreg Roach     *
353cbc1590aSGreg Roach     * @return bool
354a25f0a04SGreg Roach     */
3558f53f488SRico Sonntag    public function canEdit(): bool
356c1010edaSGreg Roach    {
3571450f098SGreg Roach        if ($this->isPendingDeletion()) {
3581450f098SGreg Roach            return false;
3591450f098SGreg Roach        }
3601450f098SGreg Roach
3611450f098SGreg Roach        if (Auth::isManager($this->record->tree())) {
3621450f098SGreg Roach            return true;
3631450f098SGreg Roach        }
3641450f098SGreg Roach
365a25f0a04SGreg Roach        // Members cannot edit RESN, CHAN and locked records
3661450f098SGreg Roach        return Auth::isEditor($this->record->tree()) && strpos($this->gedcom, "\n2 RESN locked") === false && $this->getTag() !== 'RESN' && $this->getTag() !== 'CHAN';
367a25f0a04SGreg Roach    }
368a25f0a04SGreg Roach
369a25f0a04SGreg Roach    /**
370a25f0a04SGreg Roach     * The place where the event occured.
371a25f0a04SGreg Roach     *
372a25f0a04SGreg Roach     * @return Place
373a25f0a04SGreg Roach     */
3744fb14fcbSGreg Roach    public function place(): Place
375c1010edaSGreg Roach    {
376a25f0a04SGreg Roach        if ($this->place === null) {
377f4afa648SGreg Roach            $this->place = new Place($this->attribute('PLAC'), $this->record()->tree());
378a25f0a04SGreg Roach        }
379a25f0a04SGreg Roach
380a25f0a04SGreg Roach        return $this->place;
381a25f0a04SGreg Roach    }
382a25f0a04SGreg Roach
383a25f0a04SGreg Roach    /**
384a25f0a04SGreg Roach     * Get the date for this fact.
385a25f0a04SGreg Roach     * We can call this function many times, especially when sorting,
386a25f0a04SGreg Roach     * so keep a copy of the date.
387a25f0a04SGreg Roach     *
388a25f0a04SGreg Roach     * @return Date
389a25f0a04SGreg Roach     */
3902decada7SGreg Roach    public function date(): Date
391c1010edaSGreg Roach    {
392a25f0a04SGreg Roach        if ($this->date === null) {
3933425616eSGreg Roach            $this->date = new Date($this->attribute('DATE'));
394a25f0a04SGreg Roach        }
395a25f0a04SGreg Roach
396a25f0a04SGreg Roach        return $this->date;
397a25f0a04SGreg Roach    }
398a25f0a04SGreg Roach
399a25f0a04SGreg Roach    /**
400a25f0a04SGreg Roach     * The raw GEDCOM data for this fact
401a25f0a04SGreg Roach     *
402a25f0a04SGreg Roach     * @return string
403a25f0a04SGreg Roach     */
404138ca96cSGreg Roach    public function gedcom(): string
405c1010edaSGreg Roach    {
406a25f0a04SGreg Roach        return $this->gedcom;
407a25f0a04SGreg Roach    }
408a25f0a04SGreg Roach
409a25f0a04SGreg Roach    /**
410a25f0a04SGreg Roach     * Get a (pseudo) primary key for this fact.
411a25f0a04SGreg Roach     *
412a25f0a04SGreg Roach     * @return string
413a25f0a04SGreg Roach     */
414905ab80aSGreg Roach    public function id(): string
415c1010edaSGreg Roach    {
416905ab80aSGreg Roach        return $this->id;
417a25f0a04SGreg Roach    }
418a25f0a04SGreg Roach
419a25f0a04SGreg Roach    /**
420a25f0a04SGreg Roach     * What is the tag (type) of this fact, such as BIRT, MARR or DEAT.
421a25f0a04SGreg Roach     *
422a25f0a04SGreg Roach     * @return string
423a25f0a04SGreg Roach     */
4248f53f488SRico Sonntag    public function getTag(): string
425c1010edaSGreg Roach    {
426a25f0a04SGreg Roach        return $this->tag;
427a25f0a04SGreg Roach    }
428a25f0a04SGreg Roach
429a25f0a04SGreg Roach    /**
430a25f0a04SGreg Roach     * Used to convert a real fact (e.g. BIRT) into a close-relative’s fact (e.g. _BIRT_CHIL)
431a25f0a04SGreg Roach     *
432a25f0a04SGreg Roach     * @param string $tag
4337e96c925SGreg Roach     *
4347e96c925SGreg Roach     * @return void
435a25f0a04SGreg Roach     */
436e364afe4SGreg Roach    public function setTag($tag): void
437c1010edaSGreg Roach    {
438a25f0a04SGreg Roach        $this->tag = $tag;
439a25f0a04SGreg Roach    }
440a25f0a04SGreg Roach
441a25f0a04SGreg Roach    /**
442a25f0a04SGreg Roach     * The Person/Family record where this Fact came from
443a25f0a04SGreg Roach     *
4446e59b7c4SGreg Roach     * @return Individual|Family|Source|Repository|Media|Note|GedcomRecord
445a25f0a04SGreg Roach     */
446e7766c08SGreg Roach    public function record()
447c1010edaSGreg Roach    {
448e7766c08SGreg Roach        return $this->record;
449a25f0a04SGreg Roach    }
450a25f0a04SGreg Roach
451a25f0a04SGreg Roach    /**
452a25f0a04SGreg Roach     * Get the name of this fact type, for use as a label.
453a25f0a04SGreg Roach     *
454a25f0a04SGreg Roach     * @return string
455a25f0a04SGreg Roach     */
4567b7d8067SGreg Roach    public function label(): string
457c1010edaSGreg Roach    {
458a25f0a04SGreg Roach        // Custom FACT/EVEN - with a TYPE
4593425616eSGreg Roach        if (($this->tag === 'FACT' || $this->tag === 'EVEN') && $this->attribute('TYPE') !== '') {
4603425616eSGreg Roach            return I18N::translate(e($this->attribute('TYPE')));
461a25f0a04SGreg Roach        }
4624e5adf68SGreg Roach
463e7766c08SGreg Roach        return GedcomTag::getLabel($this->tag, $this->record);
464a25f0a04SGreg Roach    }
465a25f0a04SGreg Roach
466a25f0a04SGreg Roach    /**
467a25f0a04SGreg Roach     * This is a newly deleted fact, pending approval.
4687e96c925SGreg Roach     *
4697e96c925SGreg Roach     * @return void
470a25f0a04SGreg Roach     */
471e364afe4SGreg Roach    public function setPendingDeletion(): void
472c1010edaSGreg Roach    {
473a25f0a04SGreg Roach        $this->pending_deletion = true;
474a25f0a04SGreg Roach        $this->pending_addition = false;
475a25f0a04SGreg Roach    }
476a25f0a04SGreg Roach
477a25f0a04SGreg Roach    /**
478a25f0a04SGreg Roach     * Is this a newly deleted fact, pending approval.
479a25f0a04SGreg Roach     *
480cbc1590aSGreg Roach     * @return bool
481a25f0a04SGreg Roach     */
4828f53f488SRico Sonntag    public function isPendingDeletion(): bool
483c1010edaSGreg Roach    {
484a25f0a04SGreg Roach        return $this->pending_deletion;
485a25f0a04SGreg Roach    }
486a25f0a04SGreg Roach
487a25f0a04SGreg Roach    /**
488a25f0a04SGreg Roach     * This is a newly added fact, pending approval.
4897e96c925SGreg Roach     *
4907e96c925SGreg Roach     * @return void
491a25f0a04SGreg Roach     */
492e364afe4SGreg Roach    public function setPendingAddition(): void
493c1010edaSGreg Roach    {
494a25f0a04SGreg Roach        $this->pending_addition = true;
495a25f0a04SGreg Roach        $this->pending_deletion = false;
496a25f0a04SGreg Roach    }
497a25f0a04SGreg Roach
498a25f0a04SGreg Roach    /**
499a25f0a04SGreg Roach     * Is this a newly added fact, pending approval.
500a25f0a04SGreg Roach     *
501cbc1590aSGreg Roach     * @return bool
502a25f0a04SGreg Roach     */
5038f53f488SRico Sonntag    public function isPendingAddition(): bool
504c1010edaSGreg Roach    {
505a25f0a04SGreg Roach        return $this->pending_addition;
506a25f0a04SGreg Roach    }
507a25f0a04SGreg Roach
508a25f0a04SGreg Roach    /**
509a25f0a04SGreg Roach     * Source citations linked to this fact
510a25f0a04SGreg Roach     *
511a25f0a04SGreg Roach     * @return string[]
512a25f0a04SGreg Roach     */
5138f53f488SRico Sonntag    public function getCitations(): array
514c1010edaSGreg Roach    {
5158d0ebef0SGreg Roach        preg_match_all('/\n(2 SOUR @(' . Gedcom::REGEX_XREF . ')@(?:\n[3-9] .*)*)/', $this->gedcom(), $matches, PREG_SET_ORDER);
51613abd6f3SGreg Roach        $citations = [];
517a25f0a04SGreg Roach        foreach ($matches as $match) {
518f4afa648SGreg Roach            $source = Source::getInstance($match[2], $this->record()->tree());
5192859ecedSJonathan Jaubart            if ($source && $source->canShow()) {
520a25f0a04SGreg Roach                $citations[] = $match[1];
521a25f0a04SGreg Roach            }
522a25f0a04SGreg Roach        }
523a25f0a04SGreg Roach
524a25f0a04SGreg Roach        return $citations;
525a25f0a04SGreg Roach    }
526a25f0a04SGreg Roach
527a25f0a04SGreg Roach    /**
528a25f0a04SGreg Roach     * Notes (inline and objects) linked to this fact
529a25f0a04SGreg Roach     *
530a25f0a04SGreg Roach     * @return string[]|Note[]
531a25f0a04SGreg Roach     */
5328f53f488SRico Sonntag    public function getNotes(): array
533c1010edaSGreg Roach    {
53413abd6f3SGreg Roach        $notes = [];
535138ca96cSGreg Roach        preg_match_all('/\n2 NOTE ?(.*(?:\n3.*)*)/', $this->gedcom(), $matches);
536a25f0a04SGreg Roach        foreach ($matches[1] as $match) {
537a25f0a04SGreg Roach            $note = preg_replace("/\n3 CONT ?/", "\n", $match);
5388d0ebef0SGreg Roach            if (preg_match('/@(' . Gedcom::REGEX_XREF . ')@/', $note, $nmatch)) {
539f4afa648SGreg Roach                $note = Note::getInstance($nmatch[1], $this->record()->tree());
540a25f0a04SGreg Roach                if ($note && $note->canShow()) {
541a25f0a04SGreg Roach                    // A note object
542a25f0a04SGreg Roach                    $notes[] = $note;
543a25f0a04SGreg Roach                }
544a25f0a04SGreg Roach            } else {
545a25f0a04SGreg Roach                // An inline note
546a25f0a04SGreg Roach                $notes[] = $note;
547a25f0a04SGreg Roach            }
548a25f0a04SGreg Roach        }
549a25f0a04SGreg Roach
550a25f0a04SGreg Roach        return $notes;
551a25f0a04SGreg Roach    }
552a25f0a04SGreg Roach
553a25f0a04SGreg Roach    /**
554a25f0a04SGreg Roach     * Media objects linked to this fact
555a25f0a04SGreg Roach     *
556a25f0a04SGreg Roach     * @return Media[]
557a25f0a04SGreg Roach     */
5588f53f488SRico Sonntag    public function getMedia(): array
559c1010edaSGreg Roach    {
56013abd6f3SGreg Roach        $media = [];
5618d0ebef0SGreg Roach        preg_match_all('/\n2 OBJE @(' . Gedcom::REGEX_XREF . ')@/', $this->gedcom(), $matches);
562a25f0a04SGreg Roach        foreach ($matches[1] as $match) {
563f4afa648SGreg Roach            $obje = Media::getInstance($match, $this->record()->tree());
5642859ecedSJonathan Jaubart            if ($obje && $obje->canShow()) {
565a25f0a04SGreg Roach                $media[] = $obje;
566a25f0a04SGreg Roach            }
567a25f0a04SGreg Roach        }
568a25f0a04SGreg Roach
569a25f0a04SGreg Roach        return $media;
570a25f0a04SGreg Roach    }
571a25f0a04SGreg Roach
572a25f0a04SGreg Roach    /**
573a25f0a04SGreg Roach     * A one-line summary of the fact - for charts, etc.
574a25f0a04SGreg Roach     *
575a25f0a04SGreg Roach     * @return string
576a25f0a04SGreg Roach     */
5778f53f488SRico Sonntag    public function summary(): string
578c1010edaSGreg Roach    {
57913abd6f3SGreg Roach        $attributes = [];
580dc124885SGreg Roach        $target     = $this->target();
581db7bb364SGreg Roach        if ($target instanceof GedcomRecord) {
58239ca88baSGreg Roach            $attributes[] = $target->fullName();
583a25f0a04SGreg Roach        } else {
584726d7b07SGreg Roach            // Fact value
58584586c02SGreg Roach            $value = $this->value();
586726d7b07SGreg Roach            if ($value !== '' && $value !== 'Y') {
587d53324c9SGreg Roach                $attributes[] = '<span dir="auto">' . e($value) . '</span>';
588a25f0a04SGreg Roach            }
589726d7b07SGreg Roach            // Fact date
5902decada7SGreg Roach            $date = $this->date();
591726d7b07SGreg Roach            if ($date->isOK()) {
592e364afe4SGreg Roach                if ($this->record() instanceof Individual && in_array($this->getTag(), Gedcom::BIRTH_EVENTS, true) && $this->record()->tree()->getPreference('SHOW_PARENTS_AGE')) {
593e7766c08SGreg Roach                    $attributes[] = $date->display() . FunctionsPrint::formatParentsAges($this->record(), $date);
594a25f0a04SGreg Roach                } else {
595a25f0a04SGreg Roach                    $attributes[] = $date->display();
596a25f0a04SGreg Roach                }
597726d7b07SGreg Roach            }
598726d7b07SGreg Roach            // Fact place
599e364afe4SGreg Roach            if ($this->place()->gedcomName() !== '') {
600392561bbSGreg Roach                $attributes[] = $this->place()->shortName();
601a25f0a04SGreg Roach            }
602a25f0a04SGreg Roach        }
6032d8f08abSGreg Roach
6042d8f08abSGreg Roach        $class = 'fact_' . $this->getTag();
605a25f0a04SGreg Roach        if ($this->isPendingAddition()) {
60671a69701SGreg Roach            $class .= ' wt-new';
607a25f0a04SGreg Roach        } elseif ($this->isPendingDeletion()) {
60871a69701SGreg Roach            $class .= ' wt-old';
609a25f0a04SGreg Roach        }
6102d8f08abSGreg Roach
6112d8f08abSGreg Roach        return
6122d8f08abSGreg Roach            '<div class="' . $class . '">' .
6132d8f08abSGreg Roach            /* I18N: a label/value pair, such as “Occupation: Farmer”. Some languages may need to change the punctuation. */
6147b7d8067SGreg Roach            I18N::translate('<span class="label">%1$s:</span> <span class="field" dir="auto">%2$s</span>', $this->label(), implode(' — ', $attributes)) .
6152d8f08abSGreg Roach            '</div>';
616a25f0a04SGreg Roach    }
617a25f0a04SGreg Roach
618a25f0a04SGreg Roach    /**
619580a4d11SGreg Roach     * Helper functions to sort facts
620a25f0a04SGreg Roach     *
621580a4d11SGreg Roach     * @return Closure
622a25f0a04SGreg Roach     */
623580a4d11SGreg Roach    private static function dateComparator(): Closure
624c1010edaSGreg Roach    {
6256c2179e2SGreg Roach        return static function (Fact $a, Fact $b): int {
6262decada7SGreg Roach            if ($a->date()->isOK() && $b->date()->isOK()) {
627a25f0a04SGreg Roach                // If both events have dates, compare by date
6282decada7SGreg Roach                $ret = Date::compare($a->date(), $b->date());
629a25f0a04SGreg Roach
630dfa848b8SGreg Roach                if ($ret === 0) {
631580a4d11SGreg Roach                    // If dates overlap, compare by fact type
632580a4d11SGreg Roach                    $ret = self::typeComparator()($a, $b);
633a25f0a04SGreg Roach
634a25f0a04SGreg Roach                    // If the fact type is also the same, retain the initial order
635dfa848b8SGreg Roach                    if ($ret === 0) {
636580a4d11SGreg Roach                        $ret = $a->sortOrder <=> $b->sortOrder;
637a25f0a04SGreg Roach                    }
638a25f0a04SGreg Roach                }
639a25f0a04SGreg Roach
640a25f0a04SGreg Roach                return $ret;
641b2ce94c6SRico Sonntag            }
642b2ce94c6SRico Sonntag
643a25f0a04SGreg Roach            // One or both events have no date - retain the initial order
644580a4d11SGreg Roach            return $a->sortOrder <=> $b->sortOrder;
645580a4d11SGreg Roach        };
646a25f0a04SGreg Roach    }
647a25f0a04SGreg Roach
648a25f0a04SGreg Roach    /**
649580a4d11SGreg Roach     * Helper functions to sort facts.
650a25f0a04SGreg Roach     *
651580a4d11SGreg Roach     * @return Closure
652a25f0a04SGreg Roach     */
653580a4d11SGreg Roach    public static function typeComparator(): Closure
654c1010edaSGreg Roach    {
655bbe4546fSGreg Roach        static $factsort = [];
656a25f0a04SGreg Roach
65754c1ab5eSGreg Roach        if ($factsort === []) {
658bbe4546fSGreg Roach            $factsort = array_flip(self::FACT_ORDER);
659a25f0a04SGreg Roach        }
660a25f0a04SGreg Roach
6616c2179e2SGreg Roach        return static function (Fact $a, Fact $b) use ($factsort): int {
662a25f0a04SGreg Roach            // Facts from same families stay grouped together
663a25f0a04SGreg Roach            // Keep MARR and DIV from the same families from mixing with events from other FAMs
664a25f0a04SGreg Roach            // Use the original order in which the facts were added
665e7766c08SGreg Roach            if ($a->record instanceof Family && $b->record instanceof Family && $a->record !== $b->record) {
666a25f0a04SGreg Roach                return $a->sortOrder - $b->sortOrder;
667a25f0a04SGreg Roach            }
668a25f0a04SGreg Roach
669a25f0a04SGreg Roach            $atag = $a->getTag();
670a25f0a04SGreg Roach            $btag = $b->getTag();
671a25f0a04SGreg Roach
672a25f0a04SGreg Roach            // Events not in the above list get mapped onto one that is.
673a25f0a04SGreg Roach            if (!array_key_exists($atag, $factsort)) {
674a25f0a04SGreg Roach                if (preg_match('/^(_(BIRT|MARR|DEAT|BURI)_)/', $atag, $match)) {
675a25f0a04SGreg Roach                    $atag = $match[1];
676a25f0a04SGreg Roach                } else {
6777a6ee1acSGreg Roach                    $atag = '_????_';
678a25f0a04SGreg Roach                }
679a25f0a04SGreg Roach            }
680a25f0a04SGreg Roach
681a25f0a04SGreg Roach            if (!array_key_exists($btag, $factsort)) {
682a25f0a04SGreg Roach                if (preg_match('/^(_(BIRT|MARR|DEAT|BURI)_)/', $btag, $match)) {
683a25f0a04SGreg Roach                    $btag = $match[1];
684a25f0a04SGreg Roach                } else {
6857a6ee1acSGreg Roach                    $btag = '_????_';
686a25f0a04SGreg Roach                }
687a25f0a04SGreg Roach            }
688a25f0a04SGreg Roach
689a25f0a04SGreg Roach            // - Don't let dated after DEAT/BURI facts sort non-dated facts before DEAT/BURI
690a25f0a04SGreg Roach            // - Treat dated after BURI facts as BURI instead
6913425616eSGreg Roach            if ($a->attribute('DATE') !== '' && $factsort[$atag] > $factsort['BURI'] && $factsort[$atag] < $factsort['CHAN']) {
692a25f0a04SGreg Roach                $atag = 'BURI';
693a25f0a04SGreg Roach            }
694a25f0a04SGreg Roach
6953425616eSGreg Roach            if ($b->attribute('DATE') !== '' && $factsort[$btag] > $factsort['BURI'] && $factsort[$btag] < $factsort['CHAN']) {
696a25f0a04SGreg Roach                $btag = 'BURI';
697a25f0a04SGreg Roach            }
698a25f0a04SGreg Roach
699a25f0a04SGreg Roach            $ret = $factsort[$atag] - $factsort[$btag];
700a25f0a04SGreg Roach
701a25f0a04SGreg Roach            // If facts are the same then put dated facts before non-dated facts
702a25f0a04SGreg Roach            if ($ret == 0) {
7033425616eSGreg Roach                if ($a->attribute('DATE') !== '' && $b->attribute('DATE') === '') {
704a25f0a04SGreg Roach                    return -1;
705a25f0a04SGreg Roach                }
706a25f0a04SGreg Roach
7073425616eSGreg Roach                if ($b->attribute('DATE') !== '' && $a->attribute('DATE') === '') {
708a25f0a04SGreg Roach                    return 1;
709a25f0a04SGreg Roach                }
710a25f0a04SGreg Roach
711a25f0a04SGreg Roach                // If no sorting preference, then keep original ordering
712a25f0a04SGreg Roach                $ret = $a->sortOrder - $b->sortOrder;
713a25f0a04SGreg Roach            }
714a25f0a04SGreg Roach
715a25f0a04SGreg Roach            return $ret;
716580a4d11SGreg Roach        };
717580a4d11SGreg Roach    }
718580a4d11SGreg Roach
719580a4d11SGreg Roach    /**
720580a4d11SGreg Roach     * A multi-key sort
721580a4d11SGreg Roach     * 1. First divide the facts into two arrays one set with dates and one set without dates
722580a4d11SGreg Roach     * 2. Sort each of the two new arrays, the date using the compare date function, the non-dated
723580a4d11SGreg Roach     * using the compare type function
724580a4d11SGreg Roach     * 3. Then merge the arrays back into the original array using the compare type function
725580a4d11SGreg Roach     *
726b5c8fd7eSGreg Roach     * @param Collection<Fact> $unsorted
727580a4d11SGreg Roach     *
728b5c8fd7eSGreg Roach     * @return Collection<Fact>
729580a4d11SGreg Roach     */
7308af3e5c1SGreg Roach    public static function sortFacts(Collection $unsorted): Collection
731580a4d11SGreg Roach    {
732580a4d11SGreg Roach        $dated    = [];
733580a4d11SGreg Roach        $nondated = [];
734580a4d11SGreg Roach        $sorted   = [];
735580a4d11SGreg Roach
736580a4d11SGreg Roach        // Split the array into dated and non-dated arrays
737580a4d11SGreg Roach        $order = 0;
738580a4d11SGreg Roach
739580a4d11SGreg Roach        foreach ($unsorted as $fact) {
740580a4d11SGreg Roach            $fact->sortOrder = $order;
741580a4d11SGreg Roach            $order++;
742580a4d11SGreg Roach
743580a4d11SGreg Roach            if ($fact->date()->isOK()) {
744580a4d11SGreg Roach                $dated[] = $fact;
745580a4d11SGreg Roach            } else {
746580a4d11SGreg Roach                $nondated[] = $fact;
747580a4d11SGreg Roach            }
748580a4d11SGreg Roach        }
749580a4d11SGreg Roach
750580a4d11SGreg Roach        usort($dated, self::dateComparator());
751580a4d11SGreg Roach        usort($nondated, self::typeComparator());
752580a4d11SGreg Roach
753580a4d11SGreg Roach        // Merge the arrays
754580a4d11SGreg Roach        $dc = count($dated);
755580a4d11SGreg Roach        $nc = count($nondated);
756580a4d11SGreg Roach        $i  = 0;
757580a4d11SGreg Roach        $j  = 0;
758580a4d11SGreg Roach
759580a4d11SGreg Roach        // while there is anything in the dated array continue merging
760580a4d11SGreg Roach        while ($i < $dc) {
761580a4d11SGreg Roach            // compare each fact by type to merge them in order
762580a4d11SGreg Roach            if ($j < $nc && self::typeComparator()($dated[$i], $nondated[$j]) > 0) {
763580a4d11SGreg Roach                $sorted[] = $nondated[$j];
764580a4d11SGreg Roach                $j++;
765580a4d11SGreg Roach            } else {
766580a4d11SGreg Roach                $sorted[] = $dated[$i];
767580a4d11SGreg Roach                $i++;
768580a4d11SGreg Roach            }
769580a4d11SGreg Roach        }
770580a4d11SGreg Roach
771580a4d11SGreg Roach        // get anything that might be left in the nondated array
772580a4d11SGreg Roach        while ($j < $nc) {
773580a4d11SGreg Roach            $sorted[] = $nondated[$j];
774580a4d11SGreg Roach            $j++;
775580a4d11SGreg Roach        }
776580a4d11SGreg Roach
7771c7a6874SGreg Roach        return new Collection($sorted);
778a25f0a04SGreg Roach    }
779a25f0a04SGreg Roach
780a25f0a04SGreg Roach    /**
781ae71f733SGreg Roach     * Sort fact/event tags using the same order that we use for facts.
782ae71f733SGreg Roach     *
783ae71f733SGreg Roach     * @param Collection<string> $unsorted
784ae71f733SGreg Roach     *
785ae71f733SGreg Roach     * @return Collection<string>
786ae71f733SGreg Roach     */
787ae71f733SGreg Roach    public static function sortFactTags(Collection $unsorted): Collection
788ae71f733SGreg Roach    {
789ae71f733SGreg Roach        $tag_order = array_flip(self::FACT_ORDER);
790ae71f733SGreg Roach
791ae71f733SGreg Roach        return $unsorted->sort(static function (string $x, string $y) use ($tag_order): int {
792ae71f733SGreg Roach            $sort_x = $tag_order[$x] ?? $tag_order['_????_'];
793ae71f733SGreg Roach            $sort_y = $tag_order[$y] ?? $tag_order['_????_'];
794ae71f733SGreg Roach
795ae71f733SGreg Roach            return $sort_x - $sort_y;
796ae71f733SGreg Roach        });
797ae71f733SGreg Roach    }
798ae71f733SGreg Roach
799ae71f733SGreg Roach    /**
800a25f0a04SGreg Roach     * Allow native PHP functions such as array_unique() to work with objects
801a25f0a04SGreg Roach     *
802a25f0a04SGreg Roach     * @return string
803a25f0a04SGreg Roach     */
804dfa848b8SGreg Roach    public function __toString(): string
805c1010edaSGreg Roach    {
806c0935879SGreg Roach        return $this->id . '@' . $this->record->xref();
807a25f0a04SGreg Roach    }
808a25f0a04SGreg Roach}
809