xref: /webtrees/app/Census/AbstractCensusColumn.php (revision 1062a1429914c995339f502856821457aa975a5a)
14ccf2a72SGreg Roach<?php
24ccf2a72SGreg Roach/**
34ccf2a72SGreg Roach * webtrees: online genealogy
4*1062a142SGreg 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 */
1615d603e7SGreg Roach
174ccf2a72SGreg Roachnamespace Fisharebest\Webtrees\Census;
184ccf2a72SGreg Roach
194ccf2a72SGreg Roachuse Fisharebest\Webtrees\Date;
20b26116c9SGreg Roachuse Fisharebest\Webtrees\Family;
2140150762SGreg Roachuse Fisharebest\Webtrees\Individual;
224ccf2a72SGreg Roach
234ccf2a72SGreg Roach/**
244ccf2a72SGreg Roach * Definitions for a census column
254ccf2a72SGreg Roach */
264ccf2a72SGreg Roachclass AbstractCensusColumn {
27ef21b467SGreg Roach	/** @var CensusInterface */
28ef21b467SGreg Roach	private $census;
29ef21b467SGreg Roach
30ef21b467SGreg Roach	/** @var string */
31ef21b467SGreg Roach	private $abbr;
32ef21b467SGreg Roach
33ef21b467SGreg Roach	/** @var string */
34ef21b467SGreg Roach	private $title;
354ccf2a72SGreg Roach
364ccf2a72SGreg Roach	/**
37db7d25eeSGreg Roach	 * Create a column for a census
384ccf2a72SGreg Roach	 *
39ef21b467SGreg Roach	 * @param CensusInterface $census - The census to which this column forms part.
40ef21b467SGreg Roach	 * @param string          $abbr   - The abbrievated on-screen name "BiC"
41ef21b467SGreg Roach	 * @param string          $title  - The full column heading "Born in the county"
424ccf2a72SGreg Roach	 */
43ef21b467SGreg Roach	public function __construct(CensusInterface $census, $abbr, $title) {
44db7d25eeSGreg Roach		$this->census = $census;
45ef21b467SGreg Roach		$this->abbr   = $abbr;
46ef21b467SGreg Roach		$this->title  = $title;
47ef21b467SGreg Roach	}
48ef21b467SGreg Roach
49ef21b467SGreg Roach	/**
50ef21b467SGreg Roach	 * A short version of the column's name.
51ef21b467SGreg Roach	 *
52ef21b467SGreg Roach	 * @return string
53ef21b467SGreg Roach	 */
54ef21b467SGreg Roach	public function abbreviation() {
55ef21b467SGreg Roach		return $this->abbr;
56db7d25eeSGreg Roach	}
57db7d25eeSGreg Roach
58db7d25eeSGreg Roach	/**
5940150762SGreg Roach	 * Find the father of an individual
6040150762SGreg Roach	 *
61b26116c9SGreg Roach	 * @param Individual $individual
62b26116c9SGreg Roach	 *
63b26116c9SGreg Roach	 * @return Individual|null
6440150762SGreg Roach	 */
6540150762SGreg Roach	public function father(Individual $individual) {
6640150762SGreg Roach		$family = $individual->getPrimaryChildFamily();
6740150762SGreg Roach
6840150762SGreg Roach		if ($family) {
6940150762SGreg Roach			return $family->getHusband();
7040150762SGreg Roach		} else {
7140150762SGreg Roach			return null;
7240150762SGreg Roach		}
7340150762SGreg Roach	}
7440150762SGreg Roach
7540150762SGreg Roach	/**
7640150762SGreg Roach	 * Find the mother of an individual
7740150762SGreg Roach	 *
78b26116c9SGreg Roach	 * @param Individual $individual
79b26116c9SGreg Roach	 *
80b26116c9SGreg Roach	 * @return Individual|null
8140150762SGreg Roach	 */
8240150762SGreg Roach	public function mother(Individual $individual) {
8340150762SGreg Roach		$family = $individual->getPrimaryChildFamily();
8440150762SGreg Roach
8540150762SGreg Roach		if ($family) {
8640150762SGreg Roach			return $family->getWife();
8740150762SGreg Roach		} else {
8840150762SGreg Roach			return null;
8940150762SGreg Roach		}
9040150762SGreg Roach	}
9140150762SGreg Roach
9240150762SGreg Roach	/**
9315d603e7SGreg Roach	 * Find the current spouse family of an individual
9415d603e7SGreg Roach	 *
9515d603e7SGreg Roach	 * @param Individual $individual
9615d603e7SGreg Roach	 *
9715d603e7SGreg Roach	 * @return Family|null
9815d603e7SGreg Roach	 */
9915d603e7SGreg Roach	public function spouseFamily(Individual $individual) {
10015d603e7SGreg Roach		// Exclude families that were created after this census date
10115d603e7SGreg Roach		$families = [];
10215d603e7SGreg Roach		foreach ($individual->getSpouseFamilies() as $family) {
10315d603e7SGreg Roach			if (Date::compare($family->getMarriageDate(), $this->date()) <= 0) {
10415d603e7SGreg Roach				$families[] = $family;
10515d603e7SGreg Roach			}
10615d603e7SGreg Roach		}
10715d603e7SGreg Roach
10815d603e7SGreg Roach		if (empty($families)) {
10915d603e7SGreg Roach			return null;
11015d603e7SGreg Roach		} else {
11115d603e7SGreg Roach			usort($families, function (Family $x, Family $y) {
11215d603e7SGreg Roach				return Date::compare($x->getMarriageDate(), $y->getMarriageDate());
11315d603e7SGreg Roach			});
11415d603e7SGreg Roach
11515d603e7SGreg Roach			return end($families);
11615d603e7SGreg Roach		}
11715d603e7SGreg Roach	}
11815d603e7SGreg Roach
11915d603e7SGreg Roach	/**
12015d603e7SGreg Roach	 * When did this census occur
12115d603e7SGreg Roach	 *
12215d603e7SGreg Roach	 * @return Date
12315d603e7SGreg Roach	 */
12415d603e7SGreg Roach	public function date() {
12515d603e7SGreg Roach		return new Date($this->census->censusDate());
12615d603e7SGreg Roach	}
12715d603e7SGreg Roach
12815d603e7SGreg Roach	/**
12915d603e7SGreg Roach	 * The full version of the column's name.
13015d603e7SGreg Roach	 *
13115d603e7SGreg Roach	 * @return string
13215d603e7SGreg Roach	 */
13315d603e7SGreg Roach	public function title() {
13415d603e7SGreg Roach		return $this->title;
13515d603e7SGreg Roach	}
13615d603e7SGreg Roach
13715d603e7SGreg Roach	/**
13815d603e7SGreg Roach	 * Extract the country (last part) of a place name.
13915d603e7SGreg Roach	 *
14015d603e7SGreg Roach	 * @param string $place - e.g. "London, England"
14115d603e7SGreg Roach	 *
14215d603e7SGreg Roach	 * @return string - e.g. "England"
14315d603e7SGreg Roach	 */
14415d603e7SGreg Roach	protected function lastPartOfPlace($place) {
14515d603e7SGreg Roach		$place = explode(', ', $place);
14615d603e7SGreg Roach
14715d603e7SGreg Roach		return end($place);
14815d603e7SGreg Roach	}
14915d603e7SGreg Roach
15015d603e7SGreg Roach	/**
151a53db70dSGreg Roach	 * Remove the country of a place name, where it is the same as the census place
152a53db70dSGreg Roach	 *
153a53db70dSGreg Roach	 * @param string $place - e.g. "London, England"
154a53db70dSGreg Roach	 *
155a53db70dSGreg Roach	 * @return string - e.g. "London" (for census of England) and "London, England" elsewhere
156a53db70dSGreg Roach	 */
157a53db70dSGreg Roach	protected function notCountry($place) {
158a53db70dSGreg Roach		$parts = explode(', ', $place);
159a53db70dSGreg Roach
160a53db70dSGreg Roach		if (end($parts) === $this->place()) {
161a53db70dSGreg Roach			return implode(', ', array_slice($parts, 0, -1));
162a53db70dSGreg Roach		} else {
163a53db70dSGreg Roach			return $place;
164a53db70dSGreg Roach		}
165a53db70dSGreg Roach	}
166a53db70dSGreg Roach
167a53db70dSGreg Roach	/**
168db7d25eeSGreg Roach	 * Where did this census occur
169db7d25eeSGreg Roach	 *
170ef21b467SGreg Roach	 * @return string
171db7d25eeSGreg Roach	 */
172db7d25eeSGreg Roach	public function place() {
173db7d25eeSGreg Roach		return $this->census->censusPlace();
1744ccf2a72SGreg Roach	}
1754ccf2a72SGreg Roach}
176