xref: /webtrees/app/Census/CensusColumnFullName.php (revision e7f56f2af615447ab1a7646851f88b465ace9e04)
14ccf2a72SGreg Roach<?php
24ccf2a72SGreg Roach/**
34ccf2a72SGreg Roach * webtrees: online genealogy
41062a142SGreg Roach * Copyright (C) 2018 webtrees development team
54ccf2a72SGreg Roach * This program is free software: you can redistribute it and/or modify
64ccf2a72SGreg Roach * it under the terms of the GNU General Public License as published by
74ccf2a72SGreg Roach * the Free Software Foundation, either version 3 of the License, or
84ccf2a72SGreg Roach * (at your option) any later version.
94ccf2a72SGreg Roach * This program is distributed in the hope that it will be useful,
104ccf2a72SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
114ccf2a72SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
124ccf2a72SGreg Roach * GNU General Public License for more details.
134ccf2a72SGreg Roach * You should have received a copy of the GNU General Public License
144ccf2a72SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
154ccf2a72SGreg Roach */
16*e7f56f2aSGreg Roachdeclare(strict_types=1);
1715d603e7SGreg Roach
184ccf2a72SGreg Roachnamespace Fisharebest\Webtrees\Census;
194ccf2a72SGreg Roach
2009db5ef8SGreg Roachuse Fisharebest\Webtrees\Date;
21db7d25eeSGreg Roachuse Fisharebest\Webtrees\Individual;
224ccf2a72SGreg Roach
234ccf2a72SGreg Roach/**
244ccf2a72SGreg Roach * The individual's full name.
254ccf2a72SGreg Roach */
26c1010edaSGreg Roachclass CensusColumnFullName extends AbstractCensusColumn implements CensusColumnInterface
27c1010edaSGreg Roach{
284ccf2a72SGreg Roach    /**
294ccf2a72SGreg Roach     * Generate the likely value of this census column, based on available information.
304ccf2a72SGreg Roach     *
31db7d25eeSGreg Roach     * @param Individual $individual
3215d603e7SGreg Roach     * @param Individual $head
33db7d25eeSGreg Roach     *
344ccf2a72SGreg Roach     * @return string
354ccf2a72SGreg Roach     */
368f53f488SRico Sonntag    public function generate(Individual $individual, Individual $head): string
37c1010edaSGreg Roach    {
3809db5ef8SGreg Roach        $name = $this->nameAtCensusDate($individual, $this->date());
3909db5ef8SGreg Roach
4009db5ef8SGreg Roach        return strip_tags($name['full']);
4109db5ef8SGreg Roach    }
4209db5ef8SGreg Roach
4309db5ef8SGreg Roach    /**
4409db5ef8SGreg Roach     * What was an individual's likely name on a given date, allowing
4509db5ef8SGreg Roach     * for marriages and married names.
4609db5ef8SGreg Roach     *
4709db5ef8SGreg Roach     * @param Individual $individual
4809db5ef8SGreg Roach     * @param Date       $census_date
4909db5ef8SGreg Roach     *
5009db5ef8SGreg Roach     * @return string[]
5109db5ef8SGreg Roach     */
528f53f488SRico Sonntag    protected function nameAtCensusDate(Individual $individual, Date $census_date): array
53c1010edaSGreg Roach    {
5409db5ef8SGreg Roach        $names = $individual->getAllNames();
5509db5ef8SGreg Roach        $name  = $names[0];
5609db5ef8SGreg Roach
5709db5ef8SGreg Roach        foreach ($individual->getSpouseFamilies() as $family) {
5809db5ef8SGreg Roach            foreach ($family->getFacts('MARR') as $marriage) {
5909db5ef8SGreg Roach                if ($marriage->getDate()->isOK() && Date::compare($marriage->getDate(), $census_date) < 0) {
6009db5ef8SGreg Roach                    $spouse = $family->getSpouse($individual);
6109db5ef8SGreg Roach                    foreach ($names as $individual_name) {
6209db5ef8SGreg Roach                        foreach ($spouse->getAllNames() as $spouse_name) {
6309db5ef8SGreg Roach                            if ($individual_name['type'] === '_MARNM' && $individual_name['surn'] === $spouse_name['surn']) {
6409db5ef8SGreg Roach                                return $individual_name;
6509db5ef8SGreg Roach                            }
6609db5ef8SGreg Roach                        }
6709db5ef8SGreg Roach                    }
6809db5ef8SGreg Roach                }
6909db5ef8SGreg Roach            }
7009db5ef8SGreg Roach        }
7109db5ef8SGreg Roach
7209db5ef8SGreg Roach        return $name;
734ccf2a72SGreg Roach    }
744ccf2a72SGreg Roach}
75