xref: /webtrees/app/Census/AbstractCensusColumnCondition.php (revision 0a016d046f63b3b1ece7ec913216782c17f0cbad)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2017 webtrees development team
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16
17namespace Fisharebest\Webtrees\Census;
18
19use Fisharebest\Webtrees\Date;
20use Fisharebest\Webtrees\Individual;
21
22/**
23 * Marital status.
24 */
25abstract class AbstractCensusColumnCondition extends AbstractCensusColumn implements CensusColumnInterface {
26	/* Text to display for married males */
27	protected $husband = '';
28
29	/* Text to display for married females */
30	protected $wife = '';
31
32	/* Text to display for unmarried males */
33	protected $bachelor = '';
34
35	/* Text to display for unmarried females */
36	protected $spinster = '';
37
38	/* Text to display for male children */
39	protected $boy = '';
40
41	/* Text to display for female children */
42	protected $girl = '';
43
44	/* Text to display for divorced males */
45	protected $divorce = '';
46
47	/* Text to display for divorced females */
48	protected $divorcee = '';
49
50	/* Text to display for widowed males */
51	protected $widower = '';
52
53	/* Text to display for widowed females */
54	protected $widow = '';
55
56	/* At what age is this individual recorded as an adult */
57	protected $age_adult = 15;
58
59	/**
60	 * Generate the likely value of this census column, based on available information.
61	 *
62	 * @param Individual $individual
63	 * @param Individual $head
64	 *
65	 * @return string
66	 */
67	public function generate(Individual $individual, Individual $head = null) {
68		$family = $this->spouseFamily($individual);
69		$sex    = $individual->getSex();
70
71		if ($family === null || count($family->getFacts('MARR')) === 0) {
72			if ($this->isChild($individual)) {
73				return $this->conditionChild($sex);
74			} else {
75				return $this->conditionSingle($sex);
76			}
77		} elseif (count($family->getFacts('DIV')) > 0) {
78			return $this->conditionDivorced($sex);
79		} else {
80			$spouse = $family->getSpouse($individual);
81			if ($spouse instanceof Individual && $this->isDead($spouse)) {
82				return $this->conditionWidowed($sex);
83			} else {
84				return $this->conditionMarried($sex);
85			}
86		}
87	}
88
89	/**
90	 * Is the individual a child.
91	 *
92	 * @param Individual $individual
93	 *
94	 * @return bool
95	 */
96	private function isChild(Individual $individual) {
97		$age = (int)Date::getAge($individual->getEstimatedBirthDate(), $this->date(), 0);
98
99		return $age < $this->age_adult;
100	}
101
102	/**
103	 * How is this condition written in a census column.
104	 *
105	 * @param string $sex
106	 *
107	 * @return string
108	 */
109	private function conditionChild($sex) {
110		if ($sex === 'F') {
111			return $this->girl;
112		} else {
113			return $this->boy;
114		}
115	}
116
117	/**
118	 * How is this condition written in a census column.
119	 *
120	 * @param string $sex
121	 *
122	 * @return string
123	 */
124	private function conditionSingle($sex) {
125		if ($sex === 'F') {
126			return $this->spinster;
127		} else {
128			return $this->bachelor;
129		}
130	}
131
132	/**
133	 * How is this condition written in a census column.
134	 *
135	 * @param string $sex
136	 *
137	 * @return string
138	 */
139	private function conditionDivorced($sex) {
140		if ($sex === 'F') {
141			return $this->divorcee;
142		} else {
143			return $this->divorce;
144		}
145	}
146
147	/**
148	 * Is the individual dead.
149	 *
150	 * @param Individual $individual
151	 *
152	 * @return bool
153	 */
154	private function isDead(Individual $individual) {
155		return $individual->getDeathDate()->isOK() && Date::compare($individual->getDeathDate(), $this->date()) < 0;
156	}
157
158	/**
159	 * How is this condition written in a census column.
160	 *
161	 * @param string $sex
162	 *
163	 * @return string
164	 */
165	private function conditionWidowed($sex) {
166		if ($sex === 'F') {
167			return $this->widow;
168		} else {
169			return $this->widower;
170		}
171	}
172
173	/**
174	 * How is this condition written in a census column.
175	 *
176	 * @param string $sex
177	 *
178	 * @return string
179	 */
180	private function conditionMarried($sex) {
181		if ($sex === 'F') {
182			return $this->wife;
183		} else {
184			return $this->husband;
185		}
186	}
187}
188