xref: /webtrees/app/Fact.php (revision 7bf6ca817338af2948b7380627bed6f566e43f76)
1a25f0a04SGreg Roach<?php
23976b470SGreg Roach
3a25f0a04SGreg Roach/**
4a25f0a04SGreg Roach * webtrees: online genealogy
5a091ac74SGreg Roach * Copyright (C) 2020 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 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        'MARR_CIVIL',
79bbe4546fSGreg Roach        'MARR_RELIGIOUS',
80bbe4546fSGreg Roach        'MARR_PARTNERS',
81bbe4546fSGreg Roach        'MARR_UNKNOWN',
82bbe4546fSGreg Roach        '_COML',
83bbe4546fSGreg Roach        '_STAT',
84bbe4546fSGreg Roach        '_SEPR',
85bbe4546fSGreg Roach        'DIVF',
86bbe4546fSGreg Roach        'MARS',
87bbe4546fSGreg Roach        '_BIRT_CHIL',
88bbe4546fSGreg Roach        'DIV',
89bbe4546fSGreg Roach        'ANUL',
90bbe4546fSGreg Roach        '_BIRT_',
91bbe4546fSGreg Roach        '_MARR_',
92bbe4546fSGreg Roach        '_DEAT_',
93bbe4546fSGreg Roach        '_BURI_',
94bbe4546fSGreg Roach        'CENS',
95bbe4546fSGreg Roach        'OCCU',
96bbe4546fSGreg Roach        'RESI',
97bbe4546fSGreg Roach        'PROP',
98bbe4546fSGreg Roach        'CHRA',
99bbe4546fSGreg Roach        'RETI',
100bbe4546fSGreg Roach        'FACT',
101bbe4546fSGreg Roach        'EVEN',
102bbe4546fSGreg Roach        '_NMR',
103bbe4546fSGreg Roach        '_NMAR',
104bbe4546fSGreg Roach        'NMR',
105bbe4546fSGreg Roach        'NCHI',
106bbe4546fSGreg Roach        'WILL',
107bbe4546fSGreg Roach        '_HOL',
108bbe4546fSGreg Roach        '_????_',
109bbe4546fSGreg Roach        'DEAT',
110bbe4546fSGreg Roach        '_FNRL',
111bbe4546fSGreg Roach        'CREM',
112bbe4546fSGreg Roach        'BURI',
113bbe4546fSGreg Roach        '_INTE',
114bbe4546fSGreg Roach        '_YART',
115bbe4546fSGreg Roach        '_NLIV',
116bbe4546fSGreg Roach        'PROB',
117bbe4546fSGreg Roach        'TITL',
118bbe4546fSGreg Roach        'COMM',
119bbe4546fSGreg Roach        'NATI',
120bbe4546fSGreg Roach        'CITN',
121bbe4546fSGreg Roach        'CAST',
122bbe4546fSGreg Roach        'RELI',
123bbe4546fSGreg Roach        'SSN',
124bbe4546fSGreg Roach        'IDNO',
125bbe4546fSGreg Roach        'TEMP',
126bbe4546fSGreg Roach        'SLGC',
127bbe4546fSGreg Roach        'BAPL',
128bbe4546fSGreg Roach        'CONL',
129bbe4546fSGreg Roach        'ENDL',
130bbe4546fSGreg Roach        'SLGS',
131bbe4546fSGreg Roach        'ADDR',
132bbe4546fSGreg Roach        'PHON',
133bbe4546fSGreg Roach        'EMAIL',
134bbe4546fSGreg Roach        '_EMAIL',
135bbe4546fSGreg Roach        'EMAL',
136bbe4546fSGreg Roach        'FAX',
137bbe4546fSGreg Roach        'WWW',
138bbe4546fSGreg Roach        'URL',
139bbe4546fSGreg Roach        '_URL',
140bbe4546fSGreg Roach        'AFN',
141bbe4546fSGreg Roach        'REFN',
142bbe4546fSGreg Roach        '_PRMN',
143bbe4546fSGreg Roach        'REF',
144bbe4546fSGreg Roach        'RIN',
145bbe4546fSGreg Roach        '_UID',
146bbe4546fSGreg Roach        'OBJE',
147bbe4546fSGreg Roach        'NOTE',
148bbe4546fSGreg Roach        'SOUR',
149bbe4546fSGreg Roach        'CHAN',
150bbe4546fSGreg Roach        '_TODO',
151bbe4546fSGreg Roach    ];
152bbe4546fSGreg Roach
153a25f0a04SGreg Roach    /** @var string Unique identifier for this fact (currently implemented as a hash of the raw data). */
154905ab80aSGreg Roach    private $id;
155a25f0a04SGreg Roach
156a25f0a04SGreg Roach    /** @var GedcomRecord The GEDCOM record from which this fact is taken */
157e7766c08SGreg Roach    private $record;
158a25f0a04SGreg Roach
159a25f0a04SGreg Roach    /** @var string The raw GEDCOM data for this fact */
160a25f0a04SGreg Roach    private $gedcom;
161a25f0a04SGreg Roach
162a25f0a04SGreg Roach    /** @var string The GEDCOM tag for this record */
163a25f0a04SGreg Roach    private $tag;
164a25f0a04SGreg Roach
165cbc1590aSGreg Roach    /** @var bool Is this a recently deleted fact, pending approval? */
166a25f0a04SGreg Roach    private $pending_deletion = false;
167a25f0a04SGreg Roach
168cbc1590aSGreg Roach    /** @var bool Is this a recently added fact, pending approval? */
169a25f0a04SGreg Roach    private $pending_addition = false;
170a25f0a04SGreg Roach
171a25f0a04SGreg Roach    /** @var Date The date of this fact, from the “2 DATE …” attribute */
172a25f0a04SGreg Roach    private $date;
173a25f0a04SGreg Roach
174a25f0a04SGreg Roach    /** @var Place The place of this fact, from the “2 PLAC …” attribute */
175a25f0a04SGreg Roach    private $place;
176a25f0a04SGreg Roach
177580a4d11SGreg Roach    /** @var int Used by Functions::sortFacts() */
178580a4d11SGreg Roach    private $sortOrder;
179a25f0a04SGreg Roach
180a25f0a04SGreg Roach    /**
181a25f0a04SGreg Roach     * Create an event object from a gedcom fragment.
182a25f0a04SGreg Roach     * We need the parent object (to check privacy) and a (pseudo) fact ID to
183a25f0a04SGreg Roach     * identify the fact within the record.
184a25f0a04SGreg Roach     *
185a25f0a04SGreg Roach     * @param string       $gedcom
186a25f0a04SGreg Roach     * @param GedcomRecord $parent
187905ab80aSGreg Roach     * @param string       $id
188a25f0a04SGreg Roach     *
1898f5f5da8SGreg Roach     * @throws InvalidArgumentException
190a25f0a04SGreg Roach     */
191905ab80aSGreg Roach    public function __construct($gedcom, GedcomRecord $parent, $id)
192c1010edaSGreg Roach    {
1938d0ebef0SGreg Roach        if (preg_match('/^1 (' . Gedcom::REGEX_TAG . ')/', $gedcom, $match)) {
194a25f0a04SGreg Roach            $this->gedcom = $gedcom;
195e7766c08SGreg Roach            $this->record = $parent;
196905ab80aSGreg Roach            $this->id     = $id;
197a25f0a04SGreg Roach            $this->tag    = $match[1];
198a25f0a04SGreg Roach        } else {
1998f5f5da8SGreg Roach            throw new InvalidArgumentException('Invalid GEDCOM data passed to Fact::_construct(' . $gedcom . ')');
200a25f0a04SGreg Roach        }
201a25f0a04SGreg Roach    }
202a25f0a04SGreg Roach
203a25f0a04SGreg Roach    /**
204a25f0a04SGreg Roach     * Get the value of level 1 data in the fact
205a25f0a04SGreg Roach     * Allow for multi-line values
206a25f0a04SGreg Roach     *
207baacc364SGreg Roach     * @return string
208a25f0a04SGreg Roach     */
209dfa848b8SGreg Roach    public function value(): string
210c1010edaSGreg Roach    {
211a25f0a04SGreg Roach        if (preg_match('/^1 (?:' . $this->tag . ') ?(.*(?:(?:\n2 CONT ?.*)*))/', $this->gedcom, $match)) {
212a25f0a04SGreg Roach            return preg_replace("/\n2 CONT ?/", "\n", $match[1]);
213a25f0a04SGreg Roach        }
214b2ce94c6SRico Sonntag
215b2ce94c6SRico Sonntag        return '';
216a25f0a04SGreg Roach    }
217a25f0a04SGreg Roach
218a25f0a04SGreg Roach    /**
219a25f0a04SGreg Roach     * Get the record to which this fact links
220a25f0a04SGreg Roach     *
2210d15532eSGreg Roach     * @return Family|GedcomRecord|Individual|Location|Media|Note|Repository|Source|Submission|Submitter|null
222a25f0a04SGreg Roach     */
223dc124885SGreg Roach    public function target()
224c1010edaSGreg Roach    {
22562b52849SGreg Roach        if (!preg_match('/^@(' . Gedcom::REGEX_XREF . ')@$/', $this->value(), $match)) {
22662b52849SGreg Roach            return null;
22762b52849SGreg Roach        }
22862b52849SGreg Roach
22962b52849SGreg Roach        $xref = $match[1];
23062b52849SGreg Roach
231a25f0a04SGreg Roach        switch ($this->tag) {
232a25f0a04SGreg Roach            case 'FAMC':
233a25f0a04SGreg Roach            case 'FAMS':
234a091ac74SGreg Roach                return Factory::family()->make($xref, $this->record()->tree());
235a25f0a04SGreg Roach            case 'HUSB':
236a25f0a04SGreg Roach            case 'WIFE':
23762b52849SGreg Roach            case 'ALIA':
238a25f0a04SGreg Roach            case 'CHIL':
23962b52849SGreg Roach            case '_ASSO':
240a091ac74SGreg Roach                return Factory::individual()->make($xref, $this->record()->tree());
24162b52849SGreg Roach            case 'ASSO':
24262b52849SGreg Roach                return
243a091ac74SGreg Roach                    Factory::individual()->make($xref, $this->record()->tree()) ??
244a091ac74SGreg Roach                    Factory::submitter()->make($xref, $this->record()->tree());
245a25f0a04SGreg Roach            case 'SOUR':
246a091ac74SGreg Roach                return Factory::source()->make($xref, $this->record()->tree());
247a25f0a04SGreg Roach            case 'OBJE':
248a091ac74SGreg Roach                return Factory::media()->make($xref, $this->record()->tree());
249a25f0a04SGreg Roach            case 'REPO':
250a091ac74SGreg Roach                return Factory::repository()->make($xref, $this->record()->tree());
251a25f0a04SGreg Roach            case 'NOTE':
252a091ac74SGreg Roach                return Factory::note()->make($xref, $this->record()->tree());
25362b52849SGreg Roach            case 'ANCI':
25462b52849SGreg Roach            case 'DESI':
255ffc0a61fSGreg Roach            case 'SUBM':
256a091ac74SGreg Roach                return Factory::submitter()->make($xref, $this->record()->tree());
2571635452cSGreg Roach            case 'SUBN':
258a091ac74SGreg Roach                return Factory::submission()->make($xref, $this->record()->tree());
2590d15532eSGreg Roach            case '_LOC':
2600d15532eSGreg Roach                return Factory::location()->make($xref, $this->record()->tree());
261a25f0a04SGreg Roach            default:
262a091ac74SGreg Roach                return Factory::gedcomRecord()->make($xref, $this->record()->tree());
263a25f0a04SGreg Roach        }
264a25f0a04SGreg Roach    }
265a25f0a04SGreg Roach
266a25f0a04SGreg Roach    /**
267a25f0a04SGreg Roach     * Get the value of level 2 data in the fact
268a25f0a04SGreg Roach     *
269a25f0a04SGreg Roach     * @param string $tag
270a25f0a04SGreg Roach     *
271baacc364SGreg Roach     * @return string
272a25f0a04SGreg Roach     */
273dfa848b8SGreg Roach    public function attribute($tag): string
274c1010edaSGreg Roach    {
275a25f0a04SGreg Roach        if (preg_match('/\n2 (?:' . $tag . ') ?(.*(?:(?:\n3 CONT ?.*)*)*)/', $this->gedcom, $match)) {
276a25f0a04SGreg Roach            return preg_replace("/\n3 CONT ?/", "\n", $match[1]);
277a25f0a04SGreg Roach        }
278b2ce94c6SRico Sonntag
279b2ce94c6SRico Sonntag        return '';
280a25f0a04SGreg Roach    }
281a25f0a04SGreg Roach
282a25f0a04SGreg Roach    /**
2838af6bbf8SGreg Roach     * Get the PLAC:MAP:LATI for the fact.
2848af6bbf8SGreg Roach     *
2858af6bbf8SGreg Roach     * @return float
2868af6bbf8SGreg Roach     */
2878af6bbf8SGreg Roach    public function latitude(): float
2888af6bbf8SGreg Roach    {
2898af6bbf8SGreg Roach        if (preg_match('/\n4 LATI (.+)/', $this->gedcom, $match)) {
2908af6bbf8SGreg Roach            $gedcom_service = new GedcomService();
2918af6bbf8SGreg Roach
2928af6bbf8SGreg Roach            return $gedcom_service->readLatitude($match[1]);
2938af6bbf8SGreg Roach        }
2948af6bbf8SGreg Roach
2958af6bbf8SGreg Roach        return 0.0;
2968af6bbf8SGreg Roach    }
2978af6bbf8SGreg Roach
2988af6bbf8SGreg Roach    /**
2998af6bbf8SGreg Roach     * Get the PLAC:MAP:LONG for the fact.
3008af6bbf8SGreg Roach     *
3018af6bbf8SGreg Roach     * @return float
3028af6bbf8SGreg Roach     */
3038af6bbf8SGreg Roach    public function longitude(): float
3048af6bbf8SGreg Roach    {
305819ac503SRico Sonntag        if (preg_match('/\n4 LONG (.+)/', $this->gedcom, $match)) {
3068af6bbf8SGreg Roach            $gedcom_service = new GedcomService();
3078af6bbf8SGreg Roach
3088af6bbf8SGreg Roach            return $gedcom_service->readLongitude($match[1]);
3098af6bbf8SGreg Roach        }
3108af6bbf8SGreg Roach
3118af6bbf8SGreg Roach        return 0.0;
3128af6bbf8SGreg Roach    }
3138af6bbf8SGreg Roach
3148af6bbf8SGreg Roach    /**
315a25f0a04SGreg Roach     * Do the privacy rules allow us to display this fact to the current user
316a25f0a04SGreg Roach     *
317cbc1590aSGreg Roach     * @param int|null $access_level
318a25f0a04SGreg Roach     *
319cbc1590aSGreg Roach     * @return bool
320a25f0a04SGreg Roach     */
32135584196SGreg Roach    public function canShow(int $access_level = null): bool
322c1010edaSGreg Roach    {
323d9e083e7SGreg Roach        $access_level = $access_level ?? Auth::accessLevel($this->record->tree());
3244b9ff166SGreg Roach
325a25f0a04SGreg Roach        // Does this record have an explicit RESN?
32620ff464cSGreg Roach        if (strpos($this->gedcom, "\n2 RESN confidential") !== false) {
3274b9ff166SGreg Roach            return Auth::PRIV_NONE >= $access_level;
328a25f0a04SGreg Roach        }
32920ff464cSGreg Roach        if (strpos($this->gedcom, "\n2 RESN privacy") !== false) {
3304b9ff166SGreg Roach            return Auth::PRIV_USER >= $access_level;
331a25f0a04SGreg Roach        }
33220ff464cSGreg Roach        if (strpos($this->gedcom, "\n2 RESN none") !== false) {
333a25f0a04SGreg Roach            return true;
334a25f0a04SGreg Roach        }
335a25f0a04SGreg Roach
336a25f0a04SGreg Roach        // Does this record have a default RESN?
337c0935879SGreg Roach        $xref                    = $this->record->xref();
338f4afa648SGreg Roach        $fact_privacy            = $this->record->tree()->getFactPrivacy();
339f4afa648SGreg Roach        $individual_fact_privacy = $this->record->tree()->getIndividualFactPrivacy();
340518bbdc1SGreg Roach        if (isset($individual_fact_privacy[$xref][$this->tag])) {
341518bbdc1SGreg Roach            return $individual_fact_privacy[$xref][$this->tag] >= $access_level;
342a25f0a04SGreg Roach        }
343518bbdc1SGreg Roach        if (isset($fact_privacy[$this->tag])) {
344518bbdc1SGreg Roach            return $fact_privacy[$this->tag] >= $access_level;
345a25f0a04SGreg Roach        }
346a25f0a04SGreg Roach
347a25f0a04SGreg Roach        // No restrictions - it must be public
348a25f0a04SGreg Roach        return true;
349a25f0a04SGreg Roach    }
350a25f0a04SGreg Roach
351a25f0a04SGreg Roach    /**
352a25f0a04SGreg Roach     * Check whether this fact is protected against edit
353a25f0a04SGreg Roach     *
354cbc1590aSGreg Roach     * @return bool
355a25f0a04SGreg Roach     */
3568f53f488SRico Sonntag    public function canEdit(): bool
357c1010edaSGreg Roach    {
3581450f098SGreg Roach        if ($this->isPendingDeletion()) {
3591450f098SGreg Roach            return false;
3601450f098SGreg Roach        }
3611450f098SGreg Roach
3621450f098SGreg Roach        if (Auth::isManager($this->record->tree())) {
3631450f098SGreg Roach            return true;
3641450f098SGreg Roach        }
3651450f098SGreg Roach
366a25f0a04SGreg Roach        // Members cannot edit RESN, CHAN and locked records
3671450f098SGreg Roach        return Auth::isEditor($this->record->tree()) && strpos($this->gedcom, "\n2 RESN locked") === false && $this->getTag() !== 'RESN' && $this->getTag() !== 'CHAN';
368a25f0a04SGreg Roach    }
369a25f0a04SGreg Roach
370a25f0a04SGreg Roach    /**
371a25f0a04SGreg Roach     * The place where the event occured.
372a25f0a04SGreg Roach     *
373a25f0a04SGreg Roach     * @return Place
374a25f0a04SGreg Roach     */
3754fb14fcbSGreg Roach    public function place(): Place
376c1010edaSGreg Roach    {
377a25f0a04SGreg Roach        if ($this->place === null) {
378f4afa648SGreg Roach            $this->place = new Place($this->attribute('PLAC'), $this->record()->tree());
379a25f0a04SGreg Roach        }
380a25f0a04SGreg Roach
381a25f0a04SGreg Roach        return $this->place;
382a25f0a04SGreg Roach    }
383a25f0a04SGreg Roach
384a25f0a04SGreg Roach    /**
385a25f0a04SGreg Roach     * Get the date for this fact.
386a25f0a04SGreg Roach     * We can call this function many times, especially when sorting,
387a25f0a04SGreg Roach     * so keep a copy of the date.
388a25f0a04SGreg Roach     *
389a25f0a04SGreg Roach     * @return Date
390a25f0a04SGreg Roach     */
3912decada7SGreg Roach    public function date(): Date
392c1010edaSGreg Roach    {
393a25f0a04SGreg Roach        if ($this->date === null) {
3943425616eSGreg Roach            $this->date = new Date($this->attribute('DATE'));
395a25f0a04SGreg Roach        }
396a25f0a04SGreg Roach
397a25f0a04SGreg Roach        return $this->date;
398a25f0a04SGreg Roach    }
399a25f0a04SGreg Roach
400a25f0a04SGreg Roach    /**
401a25f0a04SGreg Roach     * The raw GEDCOM data for this fact
402a25f0a04SGreg Roach     *
403a25f0a04SGreg Roach     * @return string
404a25f0a04SGreg Roach     */
405138ca96cSGreg Roach    public function gedcom(): string
406c1010edaSGreg Roach    {
407a25f0a04SGreg Roach        return $this->gedcom;
408a25f0a04SGreg Roach    }
409a25f0a04SGreg Roach
410a25f0a04SGreg Roach    /**
411a25f0a04SGreg Roach     * Get a (pseudo) primary key for this fact.
412a25f0a04SGreg Roach     *
413a25f0a04SGreg Roach     * @return string
414a25f0a04SGreg Roach     */
415905ab80aSGreg Roach    public function id(): string
416c1010edaSGreg Roach    {
417905ab80aSGreg Roach        return $this->id;
418a25f0a04SGreg Roach    }
419a25f0a04SGreg Roach
420a25f0a04SGreg Roach    /**
421a25f0a04SGreg Roach     * What is the tag (type) of this fact, such as BIRT, MARR or DEAT.
422a25f0a04SGreg Roach     *
423a25f0a04SGreg Roach     * @return string
424a25f0a04SGreg Roach     */
4258f53f488SRico Sonntag    public function getTag(): string
426c1010edaSGreg Roach    {
427a25f0a04SGreg Roach        return $this->tag;
428a25f0a04SGreg Roach    }
429a25f0a04SGreg Roach
430a25f0a04SGreg Roach    /**
431a25f0a04SGreg Roach     * Used to convert a real fact (e.g. BIRT) into a close-relative’s fact (e.g. _BIRT_CHIL)
432a25f0a04SGreg Roach     *
433a25f0a04SGreg Roach     * @param string $tag
4347e96c925SGreg Roach     *
4357e96c925SGreg Roach     * @return void
436a25f0a04SGreg Roach     */
437e364afe4SGreg Roach    public function setTag($tag): void
438c1010edaSGreg Roach    {
439a25f0a04SGreg Roach        $this->tag = $tag;
440a25f0a04SGreg Roach    }
441a25f0a04SGreg Roach
442a25f0a04SGreg Roach    /**
443a25f0a04SGreg Roach     * The Person/Family record where this Fact came from
444a25f0a04SGreg Roach     *
445*7bf6ca81SGreg Roach     * @return Individual|Family|Source|Repository|Media|Note|Submitter|Submission|Location|Header|GedcomRecord
446a25f0a04SGreg Roach     */
447e7766c08SGreg Roach    public function record()
448c1010edaSGreg Roach    {
449e7766c08SGreg Roach        return $this->record;
450a25f0a04SGreg Roach    }
451a25f0a04SGreg Roach
452a25f0a04SGreg Roach    /**
453a25f0a04SGreg Roach     * Get the name of this fact type, for use as a label.
454a25f0a04SGreg Roach     *
455a25f0a04SGreg Roach     * @return string
456a25f0a04SGreg Roach     */
4577b7d8067SGreg Roach    public function label(): string
458c1010edaSGreg Roach    {
459a25f0a04SGreg Roach        // Custom FACT/EVEN - with a TYPE
4603425616eSGreg Roach        if (($this->tag === 'FACT' || $this->tag === 'EVEN') && $this->attribute('TYPE') !== '') {
4613425616eSGreg Roach            return I18N::translate(e($this->attribute('TYPE')));
462a25f0a04SGreg Roach        }
4634e5adf68SGreg Roach
464e7766c08SGreg Roach        return GedcomTag::getLabel($this->tag, $this->record);
465a25f0a04SGreg Roach    }
466a25f0a04SGreg Roach
467a25f0a04SGreg Roach    /**
468a25f0a04SGreg Roach     * This is a newly deleted fact, pending approval.
4697e96c925SGreg Roach     *
4707e96c925SGreg Roach     * @return void
471a25f0a04SGreg Roach     */
472e364afe4SGreg Roach    public function setPendingDeletion(): void
473c1010edaSGreg Roach    {
474a25f0a04SGreg Roach        $this->pending_deletion = true;
475a25f0a04SGreg Roach        $this->pending_addition = false;
476a25f0a04SGreg Roach    }
477a25f0a04SGreg Roach
478a25f0a04SGreg Roach    /**
479a25f0a04SGreg Roach     * Is this a newly deleted fact, pending approval.
480a25f0a04SGreg Roach     *
481cbc1590aSGreg Roach     * @return bool
482a25f0a04SGreg Roach     */
4838f53f488SRico Sonntag    public function isPendingDeletion(): bool
484c1010edaSGreg Roach    {
485a25f0a04SGreg Roach        return $this->pending_deletion;
486a25f0a04SGreg Roach    }
487a25f0a04SGreg Roach
488a25f0a04SGreg Roach    /**
489a25f0a04SGreg Roach     * This is a newly added fact, pending approval.
4907e96c925SGreg Roach     *
4917e96c925SGreg Roach     * @return void
492a25f0a04SGreg Roach     */
493e364afe4SGreg Roach    public function setPendingAddition(): void
494c1010edaSGreg Roach    {
495a25f0a04SGreg Roach        $this->pending_addition = true;
496a25f0a04SGreg Roach        $this->pending_deletion = false;
497a25f0a04SGreg Roach    }
498a25f0a04SGreg Roach
499a25f0a04SGreg Roach    /**
500a25f0a04SGreg Roach     * Is this a newly added fact, pending approval.
501a25f0a04SGreg Roach     *
502cbc1590aSGreg Roach     * @return bool
503a25f0a04SGreg Roach     */
5048f53f488SRico Sonntag    public function isPendingAddition(): bool
505c1010edaSGreg Roach    {
506a25f0a04SGreg Roach        return $this->pending_addition;
507a25f0a04SGreg Roach    }
508a25f0a04SGreg Roach
509a25f0a04SGreg Roach    /**
510a25f0a04SGreg Roach     * Source citations linked to this fact
511a25f0a04SGreg Roach     *
512a25f0a04SGreg Roach     * @return string[]
513a25f0a04SGreg Roach     */
5148f53f488SRico Sonntag    public function getCitations(): array
515c1010edaSGreg Roach    {
5168d0ebef0SGreg Roach        preg_match_all('/\n(2 SOUR @(' . Gedcom::REGEX_XREF . ')@(?:\n[3-9] .*)*)/', $this->gedcom(), $matches, PREG_SET_ORDER);
51713abd6f3SGreg Roach        $citations = [];
518a25f0a04SGreg Roach        foreach ($matches as $match) {
519a091ac74SGreg Roach            $source = Factory::source()->make($match[2], $this->record()->tree());
5202859ecedSJonathan Jaubart            if ($source && $source->canShow()) {
521a25f0a04SGreg Roach                $citations[] = $match[1];
522a25f0a04SGreg Roach            }
523a25f0a04SGreg Roach        }
524a25f0a04SGreg Roach
525a25f0a04SGreg Roach        return $citations;
526a25f0a04SGreg Roach    }
527a25f0a04SGreg Roach
528a25f0a04SGreg Roach    /**
529a25f0a04SGreg Roach     * Notes (inline and objects) linked to this fact
530a25f0a04SGreg Roach     *
531a25f0a04SGreg Roach     * @return string[]|Note[]
532a25f0a04SGreg Roach     */
5338f53f488SRico Sonntag    public function getNotes(): array
534c1010edaSGreg Roach    {
53513abd6f3SGreg Roach        $notes = [];
536138ca96cSGreg Roach        preg_match_all('/\n2 NOTE ?(.*(?:\n3.*)*)/', $this->gedcom(), $matches);
537a25f0a04SGreg Roach        foreach ($matches[1] as $match) {
538a25f0a04SGreg Roach            $note = preg_replace("/\n3 CONT ?/", "\n", $match);
5398d0ebef0SGreg Roach            if (preg_match('/@(' . Gedcom::REGEX_XREF . ')@/', $note, $nmatch)) {
540a091ac74SGreg Roach                $note = Factory::note()->make($nmatch[1], $this->record()->tree());
541a25f0a04SGreg Roach                if ($note && $note->canShow()) {
542a25f0a04SGreg Roach                    // A note object
543a25f0a04SGreg Roach                    $notes[] = $note;
544a25f0a04SGreg Roach                }
545a25f0a04SGreg Roach            } else {
546a25f0a04SGreg Roach                // An inline note
547a25f0a04SGreg Roach                $notes[] = $note;
548a25f0a04SGreg Roach            }
549a25f0a04SGreg Roach        }
550a25f0a04SGreg Roach
551a25f0a04SGreg Roach        return $notes;
552a25f0a04SGreg Roach    }
553a25f0a04SGreg Roach
554a25f0a04SGreg Roach    /**
555a25f0a04SGreg Roach     * Media objects linked to this fact
556a25f0a04SGreg Roach     *
557a25f0a04SGreg Roach     * @return Media[]
558a25f0a04SGreg Roach     */
5598f53f488SRico Sonntag    public function getMedia(): array
560c1010edaSGreg Roach    {
56113abd6f3SGreg Roach        $media = [];
5628d0ebef0SGreg Roach        preg_match_all('/\n2 OBJE @(' . Gedcom::REGEX_XREF . ')@/', $this->gedcom(), $matches);
563a25f0a04SGreg Roach        foreach ($matches[1] as $match) {
564a091ac74SGreg Roach            $obje = Factory::media()->make($match, $this->record()->tree());
5652859ecedSJonathan Jaubart            if ($obje && $obje->canShow()) {
566a25f0a04SGreg Roach                $media[] = $obje;
567a25f0a04SGreg Roach            }
568a25f0a04SGreg Roach        }
569a25f0a04SGreg Roach
570a25f0a04SGreg Roach        return $media;
571a25f0a04SGreg Roach    }
572a25f0a04SGreg Roach
573a25f0a04SGreg Roach    /**
574a25f0a04SGreg Roach     * A one-line summary of the fact - for charts, etc.
575a25f0a04SGreg Roach     *
576a25f0a04SGreg Roach     * @return string
577a25f0a04SGreg Roach     */
5788f53f488SRico Sonntag    public function summary(): string
579c1010edaSGreg Roach    {
58013abd6f3SGreg Roach        $attributes = [];
581dc124885SGreg Roach        $target     = $this->target();
582db7bb364SGreg Roach        if ($target instanceof GedcomRecord) {
58339ca88baSGreg Roach            $attributes[] = $target->fullName();
584a25f0a04SGreg Roach        } else {
585726d7b07SGreg Roach            // Fact value
58684586c02SGreg Roach            $value = $this->value();
587726d7b07SGreg Roach            if ($value !== '' && $value !== 'Y') {
588d53324c9SGreg Roach                $attributes[] = '<span dir="auto">' . e($value) . '</span>';
589a25f0a04SGreg Roach            }
590726d7b07SGreg Roach            // Fact date
5912decada7SGreg Roach            $date = $this->date();
592726d7b07SGreg Roach            if ($date->isOK()) {
593e364afe4SGreg Roach                if ($this->record() instanceof Individual && in_array($this->getTag(), Gedcom::BIRTH_EVENTS, true) && $this->record()->tree()->getPreference('SHOW_PARENTS_AGE')) {
594e7766c08SGreg Roach                    $attributes[] = $date->display() . FunctionsPrint::formatParentsAges($this->record(), $date);
595a25f0a04SGreg Roach                } else {
596a25f0a04SGreg Roach                    $attributes[] = $date->display();
597a25f0a04SGreg Roach                }
598726d7b07SGreg Roach            }
599726d7b07SGreg Roach            // Fact place
600e364afe4SGreg Roach            if ($this->place()->gedcomName() !== '') {
601392561bbSGreg Roach                $attributes[] = $this->place()->shortName();
602a25f0a04SGreg Roach            }
603a25f0a04SGreg Roach        }
6042d8f08abSGreg Roach
6052d8f08abSGreg Roach        $class = 'fact_' . $this->getTag();
606a25f0a04SGreg Roach        if ($this->isPendingAddition()) {
60771a69701SGreg Roach            $class .= ' wt-new';
608a25f0a04SGreg Roach        } elseif ($this->isPendingDeletion()) {
60971a69701SGreg Roach            $class .= ' wt-old';
610a25f0a04SGreg Roach        }
6112d8f08abSGreg Roach
6122d8f08abSGreg Roach        return
6132d8f08abSGreg Roach            '<div class="' . $class . '">' .
6142d8f08abSGreg Roach            /* I18N: a label/value pair, such as “Occupation: Farmer”. Some languages may need to change the punctuation. */
6157b7d8067SGreg Roach            I18N::translate('<span class="label">%1$s:</span> <span class="field" dir="auto">%2$s</span>', $this->label(), implode(' — ', $attributes)) .
6162d8f08abSGreg Roach            '</div>';
617a25f0a04SGreg Roach    }
618a25f0a04SGreg Roach
619a25f0a04SGreg Roach    /**
620580a4d11SGreg Roach     * Helper functions to sort facts
621a25f0a04SGreg Roach     *
622580a4d11SGreg Roach     * @return Closure
623a25f0a04SGreg Roach     */
624580a4d11SGreg Roach    private static function dateComparator(): Closure
625c1010edaSGreg Roach    {
6266c2179e2SGreg Roach        return static function (Fact $a, Fact $b): int {
6272decada7SGreg Roach            if ($a->date()->isOK() && $b->date()->isOK()) {
628a25f0a04SGreg Roach                // If both events have dates, compare by date
6292decada7SGreg Roach                $ret = Date::compare($a->date(), $b->date());
630a25f0a04SGreg Roach
631dfa848b8SGreg Roach                if ($ret === 0) {
632580a4d11SGreg Roach                    // If dates overlap, compare by fact type
633580a4d11SGreg Roach                    $ret = self::typeComparator()($a, $b);
634a25f0a04SGreg Roach
635a25f0a04SGreg Roach                    // If the fact type is also the same, retain the initial order
636dfa848b8SGreg Roach                    if ($ret === 0) {
637580a4d11SGreg Roach                        $ret = $a->sortOrder <=> $b->sortOrder;
638a25f0a04SGreg Roach                    }
639a25f0a04SGreg Roach                }
640a25f0a04SGreg Roach
641a25f0a04SGreg Roach                return $ret;
642b2ce94c6SRico Sonntag            }
643b2ce94c6SRico Sonntag
644a25f0a04SGreg Roach            // One or both events have no date - retain the initial order
645580a4d11SGreg Roach            return $a->sortOrder <=> $b->sortOrder;
646580a4d11SGreg Roach        };
647a25f0a04SGreg Roach    }
648a25f0a04SGreg Roach
649a25f0a04SGreg Roach    /**
650580a4d11SGreg Roach     * Helper functions to sort facts.
651a25f0a04SGreg Roach     *
652580a4d11SGreg Roach     * @return Closure
653a25f0a04SGreg Roach     */
654580a4d11SGreg Roach    public static function typeComparator(): Closure
655c1010edaSGreg Roach    {
656bbe4546fSGreg Roach        static $factsort = [];
657a25f0a04SGreg Roach
65854c1ab5eSGreg Roach        if ($factsort === []) {
659bbe4546fSGreg Roach            $factsort = array_flip(self::FACT_ORDER);
660a25f0a04SGreg Roach        }
661a25f0a04SGreg Roach
6626c2179e2SGreg Roach        return static function (Fact $a, Fact $b) use ($factsort): int {
663a25f0a04SGreg Roach            // Facts from same families stay grouped together
664a25f0a04SGreg Roach            // Keep MARR and DIV from the same families from mixing with events from other FAMs
665a25f0a04SGreg Roach            // Use the original order in which the facts were added
666e7766c08SGreg Roach            if ($a->record instanceof Family && $b->record instanceof Family && $a->record !== $b->record) {
667a25f0a04SGreg Roach                return $a->sortOrder - $b->sortOrder;
668a25f0a04SGreg Roach            }
669a25f0a04SGreg Roach
670a25f0a04SGreg Roach            $atag = $a->getTag();
671a25f0a04SGreg Roach            $btag = $b->getTag();
672a25f0a04SGreg Roach
673a25f0a04SGreg Roach            // Events not in the above list get mapped onto one that is.
674a25f0a04SGreg Roach            if (!array_key_exists($atag, $factsort)) {
675a25f0a04SGreg Roach                if (preg_match('/^(_(BIRT|MARR|DEAT|BURI)_)/', $atag, $match)) {
676a25f0a04SGreg Roach                    $atag = $match[1];
677a25f0a04SGreg Roach                } else {
6787a6ee1acSGreg Roach                    $atag = '_????_';
679a25f0a04SGreg Roach                }
680a25f0a04SGreg Roach            }
681a25f0a04SGreg Roach
682a25f0a04SGreg Roach            if (!array_key_exists($btag, $factsort)) {
683a25f0a04SGreg Roach                if (preg_match('/^(_(BIRT|MARR|DEAT|BURI)_)/', $btag, $match)) {
684a25f0a04SGreg Roach                    $btag = $match[1];
685a25f0a04SGreg Roach                } else {
6867a6ee1acSGreg Roach                    $btag = '_????_';
687a25f0a04SGreg Roach                }
688a25f0a04SGreg Roach            }
689a25f0a04SGreg Roach
690a25f0a04SGreg Roach            // - Don't let dated after DEAT/BURI facts sort non-dated facts before DEAT/BURI
691a25f0a04SGreg Roach            // - Treat dated after BURI facts as BURI instead
6923425616eSGreg Roach            if ($a->attribute('DATE') !== '' && $factsort[$atag] > $factsort['BURI'] && $factsort[$atag] < $factsort['CHAN']) {
693a25f0a04SGreg Roach                $atag = 'BURI';
694a25f0a04SGreg Roach            }
695a25f0a04SGreg Roach
6963425616eSGreg Roach            if ($b->attribute('DATE') !== '' && $factsort[$btag] > $factsort['BURI'] && $factsort[$btag] < $factsort['CHAN']) {
697a25f0a04SGreg Roach                $btag = 'BURI';
698a25f0a04SGreg Roach            }
699a25f0a04SGreg Roach
700a25f0a04SGreg Roach            $ret = $factsort[$atag] - $factsort[$btag];
701a25f0a04SGreg Roach
702a25f0a04SGreg Roach            // If facts are the same then put dated facts before non-dated facts
703a25f0a04SGreg Roach            if ($ret == 0) {
7043425616eSGreg Roach                if ($a->attribute('DATE') !== '' && $b->attribute('DATE') === '') {
705a25f0a04SGreg Roach                    return -1;
706a25f0a04SGreg Roach                }
707a25f0a04SGreg Roach
7083425616eSGreg Roach                if ($b->attribute('DATE') !== '' && $a->attribute('DATE') === '') {
709a25f0a04SGreg Roach                    return 1;
710a25f0a04SGreg Roach                }
711a25f0a04SGreg Roach
712a25f0a04SGreg Roach                // If no sorting preference, then keep original ordering
713a25f0a04SGreg Roach                $ret = $a->sortOrder - $b->sortOrder;
714a25f0a04SGreg Roach            }
715a25f0a04SGreg Roach
716a25f0a04SGreg Roach            return $ret;
717580a4d11SGreg Roach        };
718580a4d11SGreg Roach    }
719580a4d11SGreg Roach
720580a4d11SGreg Roach    /**
721580a4d11SGreg Roach     * A multi-key sort
722580a4d11SGreg Roach     * 1. First divide the facts into two arrays one set with dates and one set without dates
723580a4d11SGreg Roach     * 2. Sort each of the two new arrays, the date using the compare date function, the non-dated
724580a4d11SGreg Roach     * using the compare type function
725580a4d11SGreg Roach     * 3. Then merge the arrays back into the original array using the compare type function
726580a4d11SGreg Roach     *
727b5c8fd7eSGreg Roach     * @param Collection<Fact> $unsorted
728580a4d11SGreg Roach     *
729b5c8fd7eSGreg Roach     * @return Collection<Fact>
730580a4d11SGreg Roach     */
7318af3e5c1SGreg Roach    public static function sortFacts(Collection $unsorted): Collection
732580a4d11SGreg Roach    {
733580a4d11SGreg Roach        $dated    = [];
734580a4d11SGreg Roach        $nondated = [];
735580a4d11SGreg Roach        $sorted   = [];
736580a4d11SGreg Roach
737580a4d11SGreg Roach        // Split the array into dated and non-dated arrays
738580a4d11SGreg Roach        $order = 0;
739580a4d11SGreg Roach
740580a4d11SGreg Roach        foreach ($unsorted as $fact) {
741580a4d11SGreg Roach            $fact->sortOrder = $order;
742580a4d11SGreg Roach            $order++;
743580a4d11SGreg Roach
744580a4d11SGreg Roach            if ($fact->date()->isOK()) {
745580a4d11SGreg Roach                $dated[] = $fact;
746580a4d11SGreg Roach            } else {
747580a4d11SGreg Roach                $nondated[] = $fact;
748580a4d11SGreg Roach            }
749580a4d11SGreg Roach        }
750580a4d11SGreg Roach
751580a4d11SGreg Roach        usort($dated, self::dateComparator());
752580a4d11SGreg Roach        usort($nondated, self::typeComparator());
753580a4d11SGreg Roach
754580a4d11SGreg Roach        // Merge the arrays
755580a4d11SGreg Roach        $dc = count($dated);
756580a4d11SGreg Roach        $nc = count($nondated);
757580a4d11SGreg Roach        $i  = 0;
758580a4d11SGreg Roach        $j  = 0;
759580a4d11SGreg Roach
760580a4d11SGreg Roach        // while there is anything in the dated array continue merging
761580a4d11SGreg Roach        while ($i < $dc) {
762580a4d11SGreg Roach            // compare each fact by type to merge them in order
763580a4d11SGreg Roach            if ($j < $nc && self::typeComparator()($dated[$i], $nondated[$j]) > 0) {
764580a4d11SGreg Roach                $sorted[] = $nondated[$j];
765580a4d11SGreg Roach                $j++;
766580a4d11SGreg Roach            } else {
767580a4d11SGreg Roach                $sorted[] = $dated[$i];
768580a4d11SGreg Roach                $i++;
769580a4d11SGreg Roach            }
770580a4d11SGreg Roach        }
771580a4d11SGreg Roach
772580a4d11SGreg Roach        // get anything that might be left in the nondated array
773580a4d11SGreg Roach        while ($j < $nc) {
774580a4d11SGreg Roach            $sorted[] = $nondated[$j];
775580a4d11SGreg Roach            $j++;
776580a4d11SGreg Roach        }
777580a4d11SGreg Roach
7781c7a6874SGreg Roach        return new Collection($sorted);
779a25f0a04SGreg Roach    }
780a25f0a04SGreg Roach
781a25f0a04SGreg Roach    /**
782ae71f733SGreg Roach     * Sort fact/event tags using the same order that we use for facts.
783ae71f733SGreg Roach     *
784ae71f733SGreg Roach     * @param Collection<string> $unsorted
785ae71f733SGreg Roach     *
786ae71f733SGreg Roach     * @return Collection<string>
787ae71f733SGreg Roach     */
788ae71f733SGreg Roach    public static function sortFactTags(Collection $unsorted): Collection
789ae71f733SGreg Roach    {
790ae71f733SGreg Roach        $tag_order = array_flip(self::FACT_ORDER);
791ae71f733SGreg Roach
792ae71f733SGreg Roach        return $unsorted->sort(static function (string $x, string $y) use ($tag_order): int {
793ae71f733SGreg Roach            $sort_x = $tag_order[$x] ?? $tag_order['_????_'];
794ae71f733SGreg Roach            $sort_y = $tag_order[$y] ?? $tag_order['_????_'];
795ae71f733SGreg Roach
796ae71f733SGreg Roach            return $sort_x - $sort_y;
797ae71f733SGreg Roach        });
798ae71f733SGreg Roach    }
799ae71f733SGreg Roach
800ae71f733SGreg Roach    /**
801a25f0a04SGreg Roach     * Allow native PHP functions such as array_unique() to work with objects
802a25f0a04SGreg Roach     *
803a25f0a04SGreg Roach     * @return string
804a25f0a04SGreg Roach     */
805dfa848b8SGreg Roach    public function __toString(): string
806c1010edaSGreg Roach    {
807c0935879SGreg Roach        return $this->id . '@' . $this->record->xref();
808a25f0a04SGreg Roach    }
809a25f0a04SGreg Roach}
810