xref: /webtrees/app/Census/AbstractCensusColumnCondition.php (revision 4eb71cfaf9604652cd62536a0b3b56c6dfd715ad)
1<?php
2/**
3 * webtrees: online genealogy
4 * Copyright (C) 2016 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 */
16namespace Fisharebest\Webtrees\Census;
17
18use Fisharebest\Webtrees\Date;
19use Fisharebest\Webtrees\Individual;
20
21/**
22 * Marital status.
23 */
24abstract class AbstractCensusColumnCondition extends AbstractCensusColumn implements CensusColumnInterface {
25	/* Text to display for married males */
26	protected $husband = '';
27
28	/* Text to display for married females */
29	protected $wife    = '';
30
31	/* Text to display for unmarried males */
32	protected $bachelor = '';
33
34	/* Text to display for unmarried females */
35	protected $spinster = '';
36
37	/* Text to display for male children */
38	protected $boy  = '';
39
40	/* Text to display for female children */
41	protected $girl = '';
42
43	/* Text to display for divorced males */
44	protected $divorce  = '';
45
46	/* Text to display for divorced females */
47	protected $divorcee = '';
48
49	/* Text to display for widowed males */
50	protected $widower = '';
51
52	/* Text to display for widowed females */
53	protected $widow   = '';
54
55	/* At what age is this individual recorded as an adult */
56	protected $age_adult = 15;
57
58	/**
59	 * Generate the likely value of this census column, based on available information.
60	 *
61	 * @param Individual      $individual
62	 * @param Individual|null $head
63	 *
64	 * @return string
65	 */
66	public function generate(Individual $individual, Individual $head = null) {
67		$family = $this->spouseFamily($individual);
68		$sex    = $individual->getSex();
69
70		if ($family === null || count($family->getFacts('_NMR')) > 0) {
71			if ($this->isChild($individual)) {
72				return $this->conditionChild($sex);
73			} else {
74				return $this->conditionSingle($sex);
75			}
76		} elseif (count($family->getFacts('DIV')) > 0) {
77			return $this->conditionDivorced($sex);
78		} else {
79			$spouse = $family->getSpouse($individual);
80			if ($spouse instanceof Individual && $this->isDead($spouse)) {
81				return $this->conditionWidowed($sex);
82			} else {
83				return $this->conditionMarried($sex);
84			}
85		}
86	}
87
88	/**
89	 * How is this condition written in a census column.
90	 *
91	 * @param $sex
92	 *
93	 * @return string
94	 */
95	private function conditionChild($sex) {
96		if ($sex === 'F') {
97			return $this->girl;
98		} else {
99			return $this->boy;
100		}
101	}
102
103	/**
104	 * How is this condition written in a census column.
105	 *
106	 * @param $sex
107	 *
108	 * @return string
109	 */
110	private function conditionDivorced($sex) {
111		if ($sex === 'F') {
112			return $this->divorcee;
113		} else {
114			return $this->divorce;
115		}
116	}
117
118	/**
119	 * How is this condition written in a census column.
120	 *
121	 * @param $sex
122	 *
123	 * @return string
124	 */
125	private function conditionMarried($sex) {
126		if ($sex === 'F') {
127			return $this->wife;
128		} else {
129			return $this->husband;
130		}
131	}
132
133	/**
134	 * How is this condition written in a census column.
135	 *
136	 * @param $sex
137	 *
138	 * @return string
139	 */
140	private function conditionSingle($sex) {
141		if ($sex === 'F') {
142			return $this->spinster;
143		} else {
144			return $this->bachelor;
145		}
146	}
147
148	/**
149	 * How is this condition written in a census column.
150	 *
151	 * @param $sex
152	 *
153	 * @return string
154	 */
155	private function conditionWidowed($sex) {
156		if ($sex === 'F') {
157			return $this->widow;
158		} else {
159			return $this->widower;
160		}
161	}
162
163	/**
164	 * Is the individual a child.
165	 *
166	 * @param Individual $individual
167	 *
168	 * @return bool
169	 */
170	private function isChild(Individual $individual) {
171		$age = (int) Date::getAge($individual->getEstimatedBirthDate(), $this->date(), 0);
172
173		return $age < $this->age_adult;
174	}
175
176	/**
177	 * Is the individual dead.
178	 *
179	 * @param Individual $individual
180	 *
181	 * @return bool
182	 */
183	private function isDead(Individual $individual) {
184		return $individual->getDeathDate()->isOK() && Date::compare($individual->getDeathDate(), $this->date()) < 0;
185	}
186}
187