xref: /webtrees/app/Fact.php (revision ae71f733426ba390fe96aebda1f69dd50ac56bc0)
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
28*ae71f733SGreg Roachuse function array_flip;
29*ae71f733SGreg Roachuse function array_key_exists;
30*ae71f733SGreg Roachuse function count;
31*ae71f733SGreg Roachuse function e;
32*ae71f733SGreg Roachuse function implode;
33*ae71f733SGreg Roachuse function in_array;
34*ae71f733SGreg Roachuse function preg_match;
35*ae71f733SGreg Roachuse function preg_match_all;
36*ae71f733SGreg Roachuse function preg_replace;
371450f098SGreg Roachuse function strpos;
38*ae71f733SGreg Roachuse function trim;
39*ae71f733SGreg Roachuse function usort;
40*ae71f733SGreg Roach
41*ae71f733SGreg 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     *
222188adf12SGreg Roach     * @return Individual|Family|Source|Repository|Media|Note|GedcomRecord|null
223a25f0a04SGreg Roach     */
224dc124885SGreg Roach    public function target()
225c1010edaSGreg Roach    {
22684586c02SGreg Roach        $xref = trim($this->value(), '@');
227a25f0a04SGreg Roach        switch ($this->tag) {
228a25f0a04SGreg Roach            case 'FAMC':
229a25f0a04SGreg Roach            case 'FAMS':
230f4afa648SGreg Roach                return Family::getInstance($xref, $this->record()->tree());
231a25f0a04SGreg Roach            case 'HUSB':
232a25f0a04SGreg Roach            case 'WIFE':
233a25f0a04SGreg Roach            case 'CHIL':
234f4afa648SGreg Roach                return Individual::getInstance($xref, $this->record()->tree());
235a25f0a04SGreg Roach            case 'SOUR':
236f4afa648SGreg Roach                return Source::getInstance($xref, $this->record()->tree());
237a25f0a04SGreg Roach            case 'OBJE':
238f4afa648SGreg Roach                return Media::getInstance($xref, $this->record()->tree());
239a25f0a04SGreg Roach            case 'REPO':
240f4afa648SGreg Roach                return Repository::getInstance($xref, $this->record()->tree());
241a25f0a04SGreg Roach            case 'NOTE':
242f4afa648SGreg Roach                return Note::getInstance($xref, $this->record()->tree());
243ffc0a61fSGreg Roach            case 'SUBM':
244ffc0a61fSGreg Roach                return Submitter::getInstance($xref, $this->record()->tree());
245a25f0a04SGreg Roach            default:
246f4afa648SGreg Roach                return GedcomRecord::getInstance($xref, $this->record()->tree());
247a25f0a04SGreg Roach        }
248a25f0a04SGreg Roach    }
249a25f0a04SGreg Roach
250a25f0a04SGreg Roach    /**
251a25f0a04SGreg Roach     * Get the value of level 2 data in the fact
252a25f0a04SGreg Roach     *
253a25f0a04SGreg Roach     * @param string $tag
254a25f0a04SGreg Roach     *
255baacc364SGreg Roach     * @return string
256a25f0a04SGreg Roach     */
257dfa848b8SGreg Roach    public function attribute($tag): string
258c1010edaSGreg Roach    {
259a25f0a04SGreg Roach        if (preg_match('/\n2 (?:' . $tag . ') ?(.*(?:(?:\n3 CONT ?.*)*)*)/', $this->gedcom, $match)) {
260a25f0a04SGreg Roach            return preg_replace("/\n3 CONT ?/", "\n", $match[1]);
261a25f0a04SGreg Roach        }
262b2ce94c6SRico Sonntag
263b2ce94c6SRico Sonntag        return '';
264a25f0a04SGreg Roach    }
265a25f0a04SGreg Roach
266a25f0a04SGreg Roach    /**
2678af6bbf8SGreg Roach     * Get the PLAC:MAP:LATI for the fact.
2688af6bbf8SGreg Roach     *
2698af6bbf8SGreg Roach     * @return float
2708af6bbf8SGreg Roach     */
2718af6bbf8SGreg Roach    public function latitude(): float
2728af6bbf8SGreg Roach    {
2738af6bbf8SGreg Roach        if (preg_match('/\n4 LATI (.+)/', $this->gedcom, $match)) {
2748af6bbf8SGreg Roach            $gedcom_service = new GedcomService();
2758af6bbf8SGreg Roach
2768af6bbf8SGreg Roach            return $gedcom_service->readLatitude($match[1]);
2778af6bbf8SGreg Roach        }
2788af6bbf8SGreg Roach
2798af6bbf8SGreg Roach        return 0.0;
2808af6bbf8SGreg Roach    }
2818af6bbf8SGreg Roach
2828af6bbf8SGreg Roach    /**
2838af6bbf8SGreg Roach     * Get the PLAC:MAP:LONG for the fact.
2848af6bbf8SGreg Roach     *
2858af6bbf8SGreg Roach     * @return float
2868af6bbf8SGreg Roach     */
2878af6bbf8SGreg Roach    public function longitude(): float
2888af6bbf8SGreg Roach    {
289819ac503SRico Sonntag        if (preg_match('/\n4 LONG (.+)/', $this->gedcom, $match)) {
2908af6bbf8SGreg Roach            $gedcom_service = new GedcomService();
2918af6bbf8SGreg Roach
2928af6bbf8SGreg Roach            return $gedcom_service->readLongitude($match[1]);
2938af6bbf8SGreg Roach        }
2948af6bbf8SGreg Roach
2958af6bbf8SGreg Roach        return 0.0;
2968af6bbf8SGreg Roach    }
2978af6bbf8SGreg Roach
2988af6bbf8SGreg Roach    /**
299a25f0a04SGreg Roach     * Do the privacy rules allow us to display this fact to the current user
300a25f0a04SGreg Roach     *
301cbc1590aSGreg Roach     * @param int|null $access_level
302a25f0a04SGreg Roach     *
303cbc1590aSGreg Roach     * @return bool
304a25f0a04SGreg Roach     */
30535584196SGreg Roach    public function canShow(int $access_level = null): bool
306c1010edaSGreg Roach    {
307d9e083e7SGreg Roach        $access_level = $access_level ?? Auth::accessLevel($this->record->tree());
3084b9ff166SGreg Roach
309a25f0a04SGreg Roach        // Does this record have an explicit RESN?
31020ff464cSGreg Roach        if (strpos($this->gedcom, "\n2 RESN confidential") !== false) {
3114b9ff166SGreg Roach            return Auth::PRIV_NONE >= $access_level;
312a25f0a04SGreg Roach        }
31320ff464cSGreg Roach        if (strpos($this->gedcom, "\n2 RESN privacy") !== false) {
3144b9ff166SGreg Roach            return Auth::PRIV_USER >= $access_level;
315a25f0a04SGreg Roach        }
31620ff464cSGreg Roach        if (strpos($this->gedcom, "\n2 RESN none") !== false) {
317a25f0a04SGreg Roach            return true;
318a25f0a04SGreg Roach        }
319a25f0a04SGreg Roach
320a25f0a04SGreg Roach        // Does this record have a default RESN?
321c0935879SGreg Roach        $xref                    = $this->record->xref();
322f4afa648SGreg Roach        $fact_privacy            = $this->record->tree()->getFactPrivacy();
323f4afa648SGreg Roach        $individual_fact_privacy = $this->record->tree()->getIndividualFactPrivacy();
324518bbdc1SGreg Roach        if (isset($individual_fact_privacy[$xref][$this->tag])) {
325518bbdc1SGreg Roach            return $individual_fact_privacy[$xref][$this->tag] >= $access_level;
326a25f0a04SGreg Roach        }
327518bbdc1SGreg Roach        if (isset($fact_privacy[$this->tag])) {
328518bbdc1SGreg Roach            return $fact_privacy[$this->tag] >= $access_level;
329a25f0a04SGreg Roach        }
330a25f0a04SGreg Roach
331a25f0a04SGreg Roach        // No restrictions - it must be public
332a25f0a04SGreg Roach        return true;
333a25f0a04SGreg Roach    }
334a25f0a04SGreg Roach
335a25f0a04SGreg Roach    /**
336a25f0a04SGreg Roach     * Check whether this fact is protected against edit
337a25f0a04SGreg Roach     *
338cbc1590aSGreg Roach     * @return bool
339a25f0a04SGreg Roach     */
3408f53f488SRico Sonntag    public function canEdit(): bool
341c1010edaSGreg Roach    {
3421450f098SGreg Roach        if ($this->isPendingDeletion()) {
3431450f098SGreg Roach            return false;
3441450f098SGreg Roach        }
3451450f098SGreg Roach
3461450f098SGreg Roach        if (Auth::isManager($this->record->tree())) {
3471450f098SGreg Roach            return true;
3481450f098SGreg Roach        }
3491450f098SGreg Roach
350a25f0a04SGreg Roach        // Members cannot edit RESN, CHAN and locked records
3511450f098SGreg Roach        return Auth::isEditor($this->record->tree()) && strpos($this->gedcom, "\n2 RESN locked") === false && $this->getTag() !== 'RESN' && $this->getTag() !== 'CHAN';
352a25f0a04SGreg Roach    }
353a25f0a04SGreg Roach
354a25f0a04SGreg Roach    /**
355a25f0a04SGreg Roach     * The place where the event occured.
356a25f0a04SGreg Roach     *
357a25f0a04SGreg Roach     * @return Place
358a25f0a04SGreg Roach     */
3594fb14fcbSGreg Roach    public function place(): Place
360c1010edaSGreg Roach    {
361a25f0a04SGreg Roach        if ($this->place === null) {
362f4afa648SGreg Roach            $this->place = new Place($this->attribute('PLAC'), $this->record()->tree());
363a25f0a04SGreg Roach        }
364a25f0a04SGreg Roach
365a25f0a04SGreg Roach        return $this->place;
366a25f0a04SGreg Roach    }
367a25f0a04SGreg Roach
368a25f0a04SGreg Roach    /**
369a25f0a04SGreg Roach     * Get the date for this fact.
370a25f0a04SGreg Roach     * We can call this function many times, especially when sorting,
371a25f0a04SGreg Roach     * so keep a copy of the date.
372a25f0a04SGreg Roach     *
373a25f0a04SGreg Roach     * @return Date
374a25f0a04SGreg Roach     */
3752decada7SGreg Roach    public function date(): Date
376c1010edaSGreg Roach    {
377a25f0a04SGreg Roach        if ($this->date === null) {
3783425616eSGreg Roach            $this->date = new Date($this->attribute('DATE'));
379a25f0a04SGreg Roach        }
380a25f0a04SGreg Roach
381a25f0a04SGreg Roach        return $this->date;
382a25f0a04SGreg Roach    }
383a25f0a04SGreg Roach
384a25f0a04SGreg Roach    /**
385a25f0a04SGreg Roach     * The raw GEDCOM data for this fact
386a25f0a04SGreg Roach     *
387a25f0a04SGreg Roach     * @return string
388a25f0a04SGreg Roach     */
389138ca96cSGreg Roach    public function gedcom(): string
390c1010edaSGreg Roach    {
391a25f0a04SGreg Roach        return $this->gedcom;
392a25f0a04SGreg Roach    }
393a25f0a04SGreg Roach
394a25f0a04SGreg Roach    /**
395a25f0a04SGreg Roach     * Get a (pseudo) primary key for this fact.
396a25f0a04SGreg Roach     *
397a25f0a04SGreg Roach     * @return string
398a25f0a04SGreg Roach     */
399905ab80aSGreg Roach    public function id(): string
400c1010edaSGreg Roach    {
401905ab80aSGreg Roach        return $this->id;
402a25f0a04SGreg Roach    }
403a25f0a04SGreg Roach
404a25f0a04SGreg Roach    /**
405a25f0a04SGreg Roach     * What is the tag (type) of this fact, such as BIRT, MARR or DEAT.
406a25f0a04SGreg Roach     *
407a25f0a04SGreg Roach     * @return string
408a25f0a04SGreg Roach     */
4098f53f488SRico Sonntag    public function getTag(): string
410c1010edaSGreg Roach    {
411a25f0a04SGreg Roach        return $this->tag;
412a25f0a04SGreg Roach    }
413a25f0a04SGreg Roach
414a25f0a04SGreg Roach    /**
415a25f0a04SGreg Roach     * Used to convert a real fact (e.g. BIRT) into a close-relative’s fact (e.g. _BIRT_CHIL)
416a25f0a04SGreg Roach     *
417a25f0a04SGreg Roach     * @param string $tag
4187e96c925SGreg Roach     *
4197e96c925SGreg Roach     * @return void
420a25f0a04SGreg Roach     */
421e364afe4SGreg Roach    public function setTag($tag): void
422c1010edaSGreg Roach    {
423a25f0a04SGreg Roach        $this->tag = $tag;
424a25f0a04SGreg Roach    }
425a25f0a04SGreg Roach
426a25f0a04SGreg Roach    /**
427a25f0a04SGreg Roach     * The Person/Family record where this Fact came from
428a25f0a04SGreg Roach     *
4296e59b7c4SGreg Roach     * @return Individual|Family|Source|Repository|Media|Note|GedcomRecord
430a25f0a04SGreg Roach     */
431e7766c08SGreg Roach    public function record()
432c1010edaSGreg Roach    {
433e7766c08SGreg Roach        return $this->record;
434a25f0a04SGreg Roach    }
435a25f0a04SGreg Roach
436a25f0a04SGreg Roach    /**
437a25f0a04SGreg Roach     * Get the name of this fact type, for use as a label.
438a25f0a04SGreg Roach     *
439a25f0a04SGreg Roach     * @return string
440a25f0a04SGreg Roach     */
4417b7d8067SGreg Roach    public function label(): string
442c1010edaSGreg Roach    {
443a25f0a04SGreg Roach        // Custom FACT/EVEN - with a TYPE
4443425616eSGreg Roach        if (($this->tag === 'FACT' || $this->tag === 'EVEN') && $this->attribute('TYPE') !== '') {
4453425616eSGreg Roach            return I18N::translate(e($this->attribute('TYPE')));
446a25f0a04SGreg Roach        }
4474e5adf68SGreg Roach
448e7766c08SGreg Roach        return GedcomTag::getLabel($this->tag, $this->record);
449a25f0a04SGreg Roach    }
450a25f0a04SGreg Roach
451a25f0a04SGreg Roach    /**
452a25f0a04SGreg Roach     * This is a newly deleted fact, pending approval.
4537e96c925SGreg Roach     *
4547e96c925SGreg Roach     * @return void
455a25f0a04SGreg Roach     */
456e364afe4SGreg Roach    public function setPendingDeletion(): void
457c1010edaSGreg Roach    {
458a25f0a04SGreg Roach        $this->pending_deletion = true;
459a25f0a04SGreg Roach        $this->pending_addition = false;
460a25f0a04SGreg Roach    }
461a25f0a04SGreg Roach
462a25f0a04SGreg Roach    /**
463a25f0a04SGreg Roach     * Is this a newly deleted fact, pending approval.
464a25f0a04SGreg Roach     *
465cbc1590aSGreg Roach     * @return bool
466a25f0a04SGreg Roach     */
4678f53f488SRico Sonntag    public function isPendingDeletion(): bool
468c1010edaSGreg Roach    {
469a25f0a04SGreg Roach        return $this->pending_deletion;
470a25f0a04SGreg Roach    }
471a25f0a04SGreg Roach
472a25f0a04SGreg Roach    /**
473a25f0a04SGreg Roach     * This is a newly added fact, pending approval.
4747e96c925SGreg Roach     *
4757e96c925SGreg Roach     * @return void
476a25f0a04SGreg Roach     */
477e364afe4SGreg Roach    public function setPendingAddition(): void
478c1010edaSGreg Roach    {
479a25f0a04SGreg Roach        $this->pending_addition = true;
480a25f0a04SGreg Roach        $this->pending_deletion = false;
481a25f0a04SGreg Roach    }
482a25f0a04SGreg Roach
483a25f0a04SGreg Roach    /**
484a25f0a04SGreg Roach     * Is this a newly added fact, pending approval.
485a25f0a04SGreg Roach     *
486cbc1590aSGreg Roach     * @return bool
487a25f0a04SGreg Roach     */
4888f53f488SRico Sonntag    public function isPendingAddition(): bool
489c1010edaSGreg Roach    {
490a25f0a04SGreg Roach        return $this->pending_addition;
491a25f0a04SGreg Roach    }
492a25f0a04SGreg Roach
493a25f0a04SGreg Roach    /**
494a25f0a04SGreg Roach     * Source citations linked to this fact
495a25f0a04SGreg Roach     *
496a25f0a04SGreg Roach     * @return string[]
497a25f0a04SGreg Roach     */
4988f53f488SRico Sonntag    public function getCitations(): array
499c1010edaSGreg Roach    {
5008d0ebef0SGreg Roach        preg_match_all('/\n(2 SOUR @(' . Gedcom::REGEX_XREF . ')@(?:\n[3-9] .*)*)/', $this->gedcom(), $matches, PREG_SET_ORDER);
50113abd6f3SGreg Roach        $citations = [];
502a25f0a04SGreg Roach        foreach ($matches as $match) {
503f4afa648SGreg Roach            $source = Source::getInstance($match[2], $this->record()->tree());
5042859ecedSJonathan Jaubart            if ($source && $source->canShow()) {
505a25f0a04SGreg Roach                $citations[] = $match[1];
506a25f0a04SGreg Roach            }
507a25f0a04SGreg Roach        }
508a25f0a04SGreg Roach
509a25f0a04SGreg Roach        return $citations;
510a25f0a04SGreg Roach    }
511a25f0a04SGreg Roach
512a25f0a04SGreg Roach    /**
513a25f0a04SGreg Roach     * Notes (inline and objects) linked to this fact
514a25f0a04SGreg Roach     *
515a25f0a04SGreg Roach     * @return string[]|Note[]
516a25f0a04SGreg Roach     */
5178f53f488SRico Sonntag    public function getNotes(): array
518c1010edaSGreg Roach    {
51913abd6f3SGreg Roach        $notes = [];
520138ca96cSGreg Roach        preg_match_all('/\n2 NOTE ?(.*(?:\n3.*)*)/', $this->gedcom(), $matches);
521a25f0a04SGreg Roach        foreach ($matches[1] as $match) {
522a25f0a04SGreg Roach            $note = preg_replace("/\n3 CONT ?/", "\n", $match);
5238d0ebef0SGreg Roach            if (preg_match('/@(' . Gedcom::REGEX_XREF . ')@/', $note, $nmatch)) {
524f4afa648SGreg Roach                $note = Note::getInstance($nmatch[1], $this->record()->tree());
525a25f0a04SGreg Roach                if ($note && $note->canShow()) {
526a25f0a04SGreg Roach                    // A note object
527a25f0a04SGreg Roach                    $notes[] = $note;
528a25f0a04SGreg Roach                }
529a25f0a04SGreg Roach            } else {
530a25f0a04SGreg Roach                // An inline note
531a25f0a04SGreg Roach                $notes[] = $note;
532a25f0a04SGreg Roach            }
533a25f0a04SGreg Roach        }
534a25f0a04SGreg Roach
535a25f0a04SGreg Roach        return $notes;
536a25f0a04SGreg Roach    }
537a25f0a04SGreg Roach
538a25f0a04SGreg Roach    /**
539a25f0a04SGreg Roach     * Media objects linked to this fact
540a25f0a04SGreg Roach     *
541a25f0a04SGreg Roach     * @return Media[]
542a25f0a04SGreg Roach     */
5438f53f488SRico Sonntag    public function getMedia(): array
544c1010edaSGreg Roach    {
54513abd6f3SGreg Roach        $media = [];
5468d0ebef0SGreg Roach        preg_match_all('/\n2 OBJE @(' . Gedcom::REGEX_XREF . ')@/', $this->gedcom(), $matches);
547a25f0a04SGreg Roach        foreach ($matches[1] as $match) {
548f4afa648SGreg Roach            $obje = Media::getInstance($match, $this->record()->tree());
5492859ecedSJonathan Jaubart            if ($obje && $obje->canShow()) {
550a25f0a04SGreg Roach                $media[] = $obje;
551a25f0a04SGreg Roach            }
552a25f0a04SGreg Roach        }
553a25f0a04SGreg Roach
554a25f0a04SGreg Roach        return $media;
555a25f0a04SGreg Roach    }
556a25f0a04SGreg Roach
557a25f0a04SGreg Roach    /**
558a25f0a04SGreg Roach     * A one-line summary of the fact - for charts, etc.
559a25f0a04SGreg Roach     *
560a25f0a04SGreg Roach     * @return string
561a25f0a04SGreg Roach     */
5628f53f488SRico Sonntag    public function summary(): string
563c1010edaSGreg Roach    {
56413abd6f3SGreg Roach        $attributes = [];
565dc124885SGreg Roach        $target     = $this->target();
566db7bb364SGreg Roach        if ($target instanceof GedcomRecord) {
56739ca88baSGreg Roach            $attributes[] = $target->fullName();
568a25f0a04SGreg Roach        } else {
569726d7b07SGreg Roach            // Fact value
57084586c02SGreg Roach            $value = $this->value();
571726d7b07SGreg Roach            if ($value !== '' && $value !== 'Y') {
572d53324c9SGreg Roach                $attributes[] = '<span dir="auto">' . e($value) . '</span>';
573a25f0a04SGreg Roach            }
574726d7b07SGreg Roach            // Fact date
5752decada7SGreg Roach            $date = $this->date();
576726d7b07SGreg Roach            if ($date->isOK()) {
577e364afe4SGreg Roach                if ($this->record() instanceof Individual && in_array($this->getTag(), Gedcom::BIRTH_EVENTS, true) && $this->record()->tree()->getPreference('SHOW_PARENTS_AGE')) {
578e7766c08SGreg Roach                    $attributes[] = $date->display() . FunctionsPrint::formatParentsAges($this->record(), $date);
579a25f0a04SGreg Roach                } else {
580a25f0a04SGreg Roach                    $attributes[] = $date->display();
581a25f0a04SGreg Roach                }
582726d7b07SGreg Roach            }
583726d7b07SGreg Roach            // Fact place
584e364afe4SGreg Roach            if ($this->place()->gedcomName() !== '') {
585392561bbSGreg Roach                $attributes[] = $this->place()->shortName();
586a25f0a04SGreg Roach            }
587a25f0a04SGreg Roach        }
5882d8f08abSGreg Roach
5892d8f08abSGreg Roach        $class = 'fact_' . $this->getTag();
590a25f0a04SGreg Roach        if ($this->isPendingAddition()) {
5912d8f08abSGreg Roach            $class .= ' new';
592a25f0a04SGreg Roach        } elseif ($this->isPendingDeletion()) {
5932d8f08abSGreg Roach            $class .= ' old';
594a25f0a04SGreg Roach        }
5952d8f08abSGreg Roach
5962d8f08abSGreg Roach        return
5972d8f08abSGreg Roach            '<div class="' . $class . '">' .
5982d8f08abSGreg Roach            /* I18N: a label/value pair, such as “Occupation: Farmer”. Some languages may need to change the punctuation. */
5997b7d8067SGreg Roach            I18N::translate('<span class="label">%1$s:</span> <span class="field" dir="auto">%2$s</span>', $this->label(), implode(' — ', $attributes)) .
6002d8f08abSGreg Roach            '</div>';
601a25f0a04SGreg Roach    }
602a25f0a04SGreg Roach
603a25f0a04SGreg Roach    /**
604580a4d11SGreg Roach     * Helper functions to sort facts
605a25f0a04SGreg Roach     *
606580a4d11SGreg Roach     * @return Closure
607a25f0a04SGreg Roach     */
608580a4d11SGreg Roach    private static function dateComparator(): Closure
609c1010edaSGreg Roach    {
6106c2179e2SGreg Roach        return static function (Fact $a, Fact $b): int {
6112decada7SGreg Roach            if ($a->date()->isOK() && $b->date()->isOK()) {
612a25f0a04SGreg Roach                // If both events have dates, compare by date
6132decada7SGreg Roach                $ret = Date::compare($a->date(), $b->date());
614a25f0a04SGreg Roach
615dfa848b8SGreg Roach                if ($ret === 0) {
616580a4d11SGreg Roach                    // If dates overlap, compare by fact type
617580a4d11SGreg Roach                    $ret = self::typeComparator()($a, $b);
618a25f0a04SGreg Roach
619a25f0a04SGreg Roach                    // If the fact type is also the same, retain the initial order
620dfa848b8SGreg Roach                    if ($ret === 0) {
621580a4d11SGreg Roach                        $ret = $a->sortOrder <=> $b->sortOrder;
622a25f0a04SGreg Roach                    }
623a25f0a04SGreg Roach                }
624a25f0a04SGreg Roach
625a25f0a04SGreg Roach                return $ret;
626b2ce94c6SRico Sonntag            }
627b2ce94c6SRico Sonntag
628a25f0a04SGreg Roach            // One or both events have no date - retain the initial order
629580a4d11SGreg Roach            return $a->sortOrder <=> $b->sortOrder;
630580a4d11SGreg Roach        };
631a25f0a04SGreg Roach    }
632a25f0a04SGreg Roach
633a25f0a04SGreg Roach    /**
634580a4d11SGreg Roach     * Helper functions to sort facts.
635a25f0a04SGreg Roach     *
636580a4d11SGreg Roach     * @return Closure
637a25f0a04SGreg Roach     */
638580a4d11SGreg Roach    public static function typeComparator(): Closure
639c1010edaSGreg Roach    {
640bbe4546fSGreg Roach        static $factsort = [];
641a25f0a04SGreg Roach
64254c1ab5eSGreg Roach        if ($factsort === []) {
643bbe4546fSGreg Roach            $factsort = array_flip(self::FACT_ORDER);
644a25f0a04SGreg Roach        }
645a25f0a04SGreg Roach
6466c2179e2SGreg Roach        return static function (Fact $a, Fact $b) use ($factsort): int {
647a25f0a04SGreg Roach            // Facts from same families stay grouped together
648a25f0a04SGreg Roach            // Keep MARR and DIV from the same families from mixing with events from other FAMs
649a25f0a04SGreg Roach            // Use the original order in which the facts were added
650e7766c08SGreg Roach            if ($a->record instanceof Family && $b->record instanceof Family && $a->record !== $b->record) {
651a25f0a04SGreg Roach                return $a->sortOrder - $b->sortOrder;
652a25f0a04SGreg Roach            }
653a25f0a04SGreg Roach
654a25f0a04SGreg Roach            $atag = $a->getTag();
655a25f0a04SGreg Roach            $btag = $b->getTag();
656a25f0a04SGreg Roach
657a25f0a04SGreg Roach            // Events not in the above list get mapped onto one that is.
658a25f0a04SGreg Roach            if (!array_key_exists($atag, $factsort)) {
659a25f0a04SGreg Roach                if (preg_match('/^(_(BIRT|MARR|DEAT|BURI)_)/', $atag, $match)) {
660a25f0a04SGreg Roach                    $atag = $match[1];
661a25f0a04SGreg Roach                } else {
6627a6ee1acSGreg Roach                    $atag = '_????_';
663a25f0a04SGreg Roach                }
664a25f0a04SGreg Roach            }
665a25f0a04SGreg Roach
666a25f0a04SGreg Roach            if (!array_key_exists($btag, $factsort)) {
667a25f0a04SGreg Roach                if (preg_match('/^(_(BIRT|MARR|DEAT|BURI)_)/', $btag, $match)) {
668a25f0a04SGreg Roach                    $btag = $match[1];
669a25f0a04SGreg Roach                } else {
6707a6ee1acSGreg Roach                    $btag = '_????_';
671a25f0a04SGreg Roach                }
672a25f0a04SGreg Roach            }
673a25f0a04SGreg Roach
674a25f0a04SGreg Roach            // - Don't let dated after DEAT/BURI facts sort non-dated facts before DEAT/BURI
675a25f0a04SGreg Roach            // - Treat dated after BURI facts as BURI instead
6763425616eSGreg Roach            if ($a->attribute('DATE') !== '' && $factsort[$atag] > $factsort['BURI'] && $factsort[$atag] < $factsort['CHAN']) {
677a25f0a04SGreg Roach                $atag = 'BURI';
678a25f0a04SGreg Roach            }
679a25f0a04SGreg Roach
6803425616eSGreg Roach            if ($b->attribute('DATE') !== '' && $factsort[$btag] > $factsort['BURI'] && $factsort[$btag] < $factsort['CHAN']) {
681a25f0a04SGreg Roach                $btag = 'BURI';
682a25f0a04SGreg Roach            }
683a25f0a04SGreg Roach
684a25f0a04SGreg Roach            $ret = $factsort[$atag] - $factsort[$btag];
685a25f0a04SGreg Roach
686a25f0a04SGreg Roach            // If facts are the same then put dated facts before non-dated facts
687a25f0a04SGreg Roach            if ($ret == 0) {
6883425616eSGreg Roach                if ($a->attribute('DATE') !== '' && $b->attribute('DATE') === '') {
689a25f0a04SGreg Roach                    return -1;
690a25f0a04SGreg Roach                }
691a25f0a04SGreg Roach
6923425616eSGreg Roach                if ($b->attribute('DATE') !== '' && $a->attribute('DATE') === '') {
693a25f0a04SGreg Roach                    return 1;
694a25f0a04SGreg Roach                }
695a25f0a04SGreg Roach
696a25f0a04SGreg Roach                // If no sorting preference, then keep original ordering
697a25f0a04SGreg Roach                $ret = $a->sortOrder - $b->sortOrder;
698a25f0a04SGreg Roach            }
699a25f0a04SGreg Roach
700a25f0a04SGreg Roach            return $ret;
701580a4d11SGreg Roach        };
702580a4d11SGreg Roach    }
703580a4d11SGreg Roach
704580a4d11SGreg Roach    /**
705580a4d11SGreg Roach     * A multi-key sort
706580a4d11SGreg Roach     * 1. First divide the facts into two arrays one set with dates and one set without dates
707580a4d11SGreg Roach     * 2. Sort each of the two new arrays, the date using the compare date function, the non-dated
708580a4d11SGreg Roach     * using the compare type function
709580a4d11SGreg Roach     * 3. Then merge the arrays back into the original array using the compare type function
710580a4d11SGreg Roach     *
711b5c8fd7eSGreg Roach     * @param Collection<Fact> $unsorted
712580a4d11SGreg Roach     *
713b5c8fd7eSGreg Roach     * @return Collection<Fact>
714580a4d11SGreg Roach     */
7158af3e5c1SGreg Roach    public static function sortFacts(Collection $unsorted): Collection
716580a4d11SGreg Roach    {
717580a4d11SGreg Roach        $dated    = [];
718580a4d11SGreg Roach        $nondated = [];
719580a4d11SGreg Roach        $sorted   = [];
720580a4d11SGreg Roach
721580a4d11SGreg Roach        // Split the array into dated and non-dated arrays
722580a4d11SGreg Roach        $order = 0;
723580a4d11SGreg Roach
724580a4d11SGreg Roach        foreach ($unsorted as $fact) {
725580a4d11SGreg Roach            $fact->sortOrder = $order;
726580a4d11SGreg Roach            $order++;
727580a4d11SGreg Roach
728580a4d11SGreg Roach            if ($fact->date()->isOK()) {
729580a4d11SGreg Roach                $dated[] = $fact;
730580a4d11SGreg Roach            } else {
731580a4d11SGreg Roach                $nondated[] = $fact;
732580a4d11SGreg Roach            }
733580a4d11SGreg Roach        }
734580a4d11SGreg Roach
735580a4d11SGreg Roach        usort($dated, self::dateComparator());
736580a4d11SGreg Roach        usort($nondated, self::typeComparator());
737580a4d11SGreg Roach
738580a4d11SGreg Roach        // Merge the arrays
739580a4d11SGreg Roach        $dc = count($dated);
740580a4d11SGreg Roach        $nc = count($nondated);
741580a4d11SGreg Roach        $i  = 0;
742580a4d11SGreg Roach        $j  = 0;
743580a4d11SGreg Roach
744580a4d11SGreg Roach        // while there is anything in the dated array continue merging
745580a4d11SGreg Roach        while ($i < $dc) {
746580a4d11SGreg Roach            // compare each fact by type to merge them in order
747580a4d11SGreg Roach            if ($j < $nc && self::typeComparator()($dated[$i], $nondated[$j]) > 0) {
748580a4d11SGreg Roach                $sorted[] = $nondated[$j];
749580a4d11SGreg Roach                $j++;
750580a4d11SGreg Roach            } else {
751580a4d11SGreg Roach                $sorted[] = $dated[$i];
752580a4d11SGreg Roach                $i++;
753580a4d11SGreg Roach            }
754580a4d11SGreg Roach        }
755580a4d11SGreg Roach
756580a4d11SGreg Roach        // get anything that might be left in the nondated array
757580a4d11SGreg Roach        while ($j < $nc) {
758580a4d11SGreg Roach            $sorted[] = $nondated[$j];
759580a4d11SGreg Roach            $j++;
760580a4d11SGreg Roach        }
761580a4d11SGreg Roach
7621c7a6874SGreg Roach        return new Collection($sorted);
763a25f0a04SGreg Roach    }
764a25f0a04SGreg Roach
765a25f0a04SGreg Roach    /**
766*ae71f733SGreg Roach     * Sort fact/event tags using the same order that we use for facts.
767*ae71f733SGreg Roach     *
768*ae71f733SGreg Roach     * @param Collection<string> $unsorted
769*ae71f733SGreg Roach     *
770*ae71f733SGreg Roach     * @return Collection<string>
771*ae71f733SGreg Roach     */
772*ae71f733SGreg Roach    public static function sortFactTags(Collection $unsorted): Collection
773*ae71f733SGreg Roach    {
774*ae71f733SGreg Roach        $tag_order = array_flip(self::FACT_ORDER);
775*ae71f733SGreg Roach
776*ae71f733SGreg Roach        return $unsorted->sort(static function (string $x, string $y) use ($tag_order): int {
777*ae71f733SGreg Roach            $sort_x = $tag_order[$x] ?? $tag_order['_????_'];
778*ae71f733SGreg Roach            $sort_y = $tag_order[$y] ?? $tag_order['_????_'];
779*ae71f733SGreg Roach
780*ae71f733SGreg Roach            return $sort_x - $sort_y;
781*ae71f733SGreg Roach        });
782*ae71f733SGreg Roach    }
783*ae71f733SGreg Roach
784*ae71f733SGreg Roach    /**
785a25f0a04SGreg Roach     * Allow native PHP functions such as array_unique() to work with objects
786a25f0a04SGreg Roach     *
787a25f0a04SGreg Roach     * @return string
788a25f0a04SGreg Roach     */
789dfa848b8SGreg Roach    public function __toString(): string
790c1010edaSGreg Roach    {
791c0935879SGreg Roach        return $this->id . '@' . $this->record->xref();
792a25f0a04SGreg Roach    }
793a25f0a04SGreg Roach}
794