summaryrefslogtreecommitdiff
path: root/library/WT/Controller/Timeline.php
blob: 23598dd754fdce48badc8919a31a3da57a0ae8db (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
<?php
// Controller for the timeline chart
//
// webtrees: Web based Family History software
// Copyright (C) 2014 webtrees development team.
//
// Derived from PhpGedView
// Copyright (C) 2002 to 2009 PGV Development Team.  All rights reserved.
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

class WT_Controller_Timeline extends WT_Controller_Page {
	var $bheight = 30;
	var $placements = array();
	var $indifacts = array(); // array to store the fact records in for sorting and displaying
	var $birthyears = array();
	var $birthmonths = array();
	var $birthdays = array();
	var $baseyear = 0;
	var $topyear = 0;
	var $pids = array();
	var $people = array();
	var $pidlinks = '';
	var $scale = 2;

	// GEDCOM elements that may have DATE data, but should not be displayed
	private $nonfacts = array('BAPL', 'ENDL', 'SLGC', 'SLGS', '_TODO', 'CHAN');

	function __construct() {
		parent::__construct();

		$this->setPageTitle(WT_I18N::translate('Timeline'));

		$this->baseyear = (int)date('Y');
		// new pid
		$newpid = WT_Filter::get('newpid', WT_REGEX_XREF);

		// pids array
		$this->pids = WT_Filter::getArray('pids', WT_REGEX_XREF);
		// make sure that arrays are indexed by numbers
		$this->pids = array_values($this->pids);
		if (!empty($newpid) && !in_array($newpid, $this->pids)) {
			$this->pids[] = $newpid;
		}
		if (count($this->pids) == 0) {
			$this->pids[] = $this->getSignificantIndividual()->getXref();
		}
		$remove = WT_Filter::get('remove', WT_REGEX_XREF);
		// cleanup user input
		$newpids = array();
		foreach ($this->pids as $value) {
			if ($value != $remove) {
				$newpids[] = $value;
				$person    = WT_Individual::getInstance($value);
				if ($person) {
					$this->people[] = $person;
				}
			}
		}
		$this->pids     = $newpids;
		$this->pidlinks = '';

		foreach ($this->people as $indi) {
			if (!is_null($indi) && $indi->canShow()) {
				// setup string of valid pids for links
				$this->pidlinks .= 'pids%5B%5D=' . $indi->getXref() . '&amp;';
				$bdate = $indi->getBirthDate();
				if ($bdate->isOK()) {
					$date                                = new WT_Date_Gregorian($bdate->MinDate()->minJD);
					$this->birthyears [$indi->getXref()] = $date->y;
					$this->birthmonths[$indi->getXref()] = max(1, $date->m);
					$this->birthdays  [$indi->getXref()] = max(1, $date->d);
				}
				// find all the fact information
				$facts = $indi->getFacts();
				foreach ($indi->getSpouseFamilies() as $family) {
					foreach ($family->getFacts() as $fact) {
						$facts[] = $fact;
					}
				}
				foreach ($facts as $event) {
					// get the fact type
					$fact = $event->getTag();
					if (!in_array($fact, $this->nonfacts)) {
						// check for a date
						$date = $event->getDate();
						if ($date->isOK()) {
							$date           = new WT_Date_Gregorian($date->MinDate()->minJD);
							$this->baseyear = min($this->baseyear, $date->y);
							$this->topyear  = max($this->topyear, $date->y);

							if (!$indi->isDead()) {
								$this->topyear = max($this->topyear, (int)date('Y'));
							}

							// do not add the same fact twice (prevents marriages from being added multiple times)
							if (!in_array($event, $this->indifacts, true)) {
								$this->indifacts[] = $event;
							}
						}
					}
				}
			}
		}
		$scale = WT_Filter::getInteger('scale', 0, 200);
		if ($scale === 0) {
			$this->scale = (int)(($this->topyear - $this->baseyear) / 20 * count($this->indifacts) / 4);
			if ($this->scale < 6) {
				$this->scale = 6;
			}
		} else {
			$this->scale = $scale;
		}
		if ($this->scale < 2) {
			$this->scale = 2;
		}
		$this->baseyear -= 5;
		$this->topyear += 5;
	}

	function print_time_fact(WT_Fact $event) {
		global $basexoffset, $baseyoffset, $factcount, $TEXT_DIRECTION, $WT_IMAGES, $placements;

		$desc = $event->getValue();
		// check if this is a family fact
		$gdate    = $event->getDate();
		$date     = $gdate->MinDate();
		$date     = $date->convertToCalendar('gregorian');
		$year     = $date->y;
		$month    = max(1, $date->m);
		$day      = max(1, $date->d);
		$xoffset  = $basexoffset + 22;
		$yoffset  = $baseyoffset + (($year - $this->baseyear) * $this->scale) - ($this->scale);
		$yoffset  = $yoffset + (($month / 12) * $this->scale);
		$yoffset  = $yoffset + (($day / 30) * ($this->scale / 12));
		$yoffset  = (int)($yoffset);
		$place    = (int)($yoffset / $this->bheight);
		$i        = 1;
		$j        = 0;
		$tyoffset = 0;
		while (isset($placements[$place])) {
			if ($i === $j) {
				$tyoffset = $this->bheight * $i;
				$i++;
			} else {
				$tyoffset = -1 * $this->bheight * $j;
				$j++;
			}
			$place = (int)(($yoffset + $tyoffset) / ($this->bheight));
		}
		$yoffset += $tyoffset;
		$xoffset += abs($tyoffset);
		$placements[$place] = $yoffset;

		echo "<div id=\"fact$factcount\" style=\"position:absolute; " . ($TEXT_DIRECTION == "ltr" ? "left: " . ($xoffset) : "right: " . ($xoffset)) . "px; top:" . ($yoffset) . "px; font-size: 8pt; height: " . ($this->bheight) . "px;\" onmousedown=\"factMouseDown(this, '" . $factcount . "', " . ($yoffset - $tyoffset) . ");\">";
		echo "<table cellspacing=\"0\" cellpadding=\"0\" border=\"0\" style=\"cursor: hand;\"><tr><td>";
		echo "<img src=\"" . $WT_IMAGES["hline"] . "\" name=\"boxline$factcount\" id=\"boxline$factcount\" height=\"3\" align=\"left\" width=\"10\" alt=\"\" style=\"padding-";
		if ($TEXT_DIRECTION == 'ltr') {
			echo 'left: 3px;">';
		} else {
			echo 'right: 3px;">';
		}

		$col = array_search($event->getParent(), $this->people);
		if ($col === false) {
			// Marriage event - use the color of the husband
			$col = array_search($event->getParent()->getHusband(), $this->people);
		}
		if ($col === false) {
			// Marriage event - use the color of the wife
			$col = array_search($event->getParent()->getWife(), $this->people);
		}
		$col = $col % 6;
		echo '</td><td valign="top" class="person' . $col . '">';
		if (count($this->pids) > 6) {
			echo $event->getParent()->getFullName() . ' — ';
		}
		$record = $event->getParent();
		echo $event->getLabel();
		echo ' — ';
		if ($record instanceof WT_Individual) {
			echo format_fact_date($event, $record, false, false);
		} elseif ($record instanceof WT_Family) {
			echo $gdate->display();
			if ($record->getHusband() && $record->getHusband()->getBirthDate()->isOK()) {
				$ageh = get_age_at_event(WT_Date::GetAgeGedcom($record->getHusband()->getBirthDate(), $gdate), false);
			} else {
				$ageh = null;
			}
			if ($record->getWife() && $record->getWife()->getBirthDate()->isOK()) {
				$agew = get_age_at_event(WT_Date::GetAgeGedcom($record->getWife()->getBirthDate(), $gdate), false);
			} else {
				$agew = null;
			}
			if ($ageh && $agew) {
				echo '<span class="age"> ', WT_I18N::translate('Husband’s age'), ' ', $ageh, ' ', WT_I18N::translate('Wife’s age'), ' ', $agew, '</span>';
			} elseif ($ageh) {
				echo '<span class="age"> ', WT_I18N::translate('Age'), ' ', $ageh, '</span>';
			} elseif ($agew) {
				echo '<span class="age"> ', WT_I18N::translate('Age'), ' ', $ageh, '</span>';
			}
		}
		echo ' ' . WT_Filter::escapeHtml($desc);
		if (!$event->getPlace()->isEmpty()) {
			echo ' — ' . $event->getPlace()->getShortName();
		}
		// Print spouses names for family events
		if ($event->getParent() instanceof WT_Family) {
			echo ' — <a href="', $event->getParent()->getHtmlUrl(), '">', $event->getParent()->getFullName(), '</a>';
		}
		echo '</td></tr></table>';
		echo '</div>';
		if ($TEXT_DIRECTION === 'ltr') {
			$img  = 'dline2';
			$ypos = '0%';
		} else {
			$img  = 'dline';
			$ypos = '100%';
		}
		$dyoffset = ($yoffset - $tyoffset) + $this->bheight / 3;
		if ($tyoffset < 0) {
			$dyoffset = $yoffset + $this->bheight / 3;
			if ($TEXT_DIRECTION === 'ltr') {
				$img  = 'dline';
				$ypos = '100%';
			} else {
				$img  = 'dline2';
				$ypos = '0%';
			}
		}
		// Print the diagonal line
		echo '<div id="dbox' . $factcount . '" style="position:absolute; ' . ($TEXT_DIRECTION == 'ltr' ? 'left: ' . ($basexoffset + 25) : 'right: ' . ($basexoffset + 25)) . 'px; top:' . ($dyoffset) . 'px; font-size: 8pt; height: ' . abs($tyoffset) . 'px; width: ' . abs($tyoffset) . 'px;';
		echo ' background-image: url(\'' . $WT_IMAGES[$img] . '\');';
		echo ' background-position: 0% ' . $ypos . ';">';
		echo '</div>';
	}

	public function getSignificantIndividual() {
		if ($this->pids) {
			return WT_Individual::getInstance($this->pids[0]);
		} else {
			return parent::getSignificantIndividual();
		}
	}
}