summaryrefslogtreecommitdiff
path: root/library/WT/Controller/Familybook.php
blob: 894de6d29a1bc84b8d7dc0f2947f0a680b1b80d0 (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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
<?php
//	Controller for the familybook chart
//
// webtrees: Web based Family History software
// Copyright (C) 2013 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

if (!defined('WT_WEBTREES')) {
	header('HTTP/1.0 403 Forbidden');
	exit;
}

class WT_Controller_Familybook extends WT_Controller_Chart {
	// Data for the view
	public $pid	       =null;
	public $show_full  =null;
	public $show_spouse=null;
	public $descent    =null;
	public $generations=null;
	public $box_width  =null;
	public $rootid     =null;

	// Data for the controller
	private $dgenerations=null;
	public function __construct() {
		parent::__construct();

		$PEDIGREE_FULL_DETAILS      =get_gedcom_setting(WT_GED_ID, 'PEDIGREE_FULL_DETAILS');
		$MAX_DESCENDANCY_GENERATIONS=get_gedcom_setting(WT_GED_ID, 'MAX_DESCENDANCY_GENERATIONS');

		// Extract the request parameters
		$this->show_full   = WT_Filter::getInteger('show_full',   0, 1, $PEDIGREE_FULL_DETAILS);
		$this->show_spouse = WT_Filter::getInteger('show_spouse', 0, 1);
		$this->descent     = WT_Filter::getInteger('descent',     0, 9, 5);
		$this->generations = WT_Filter::getInteger('generations', 2, $MAX_DESCENDANCY_GENERATIONS, 2);
		$this->box_width   = WT_Filter::getInteger('box_width',   50, 300, 100);

		// Box sizes are set globally in the theme.  Modify them here.
		global $bwidth, $bheight, $cbwidth, $cbheight, $Dbwidth, $bhalfheight, $Dbheight;
		$Dbwidth =$this->box_width * $bwidth  / 100;
		$Dbheight=$this->box_width * $bheight / 100;
		$bwidth  =$Dbwidth;
		$bheight =$Dbheight;

		// -- adjust size of the compact box
		if (!$this->show_full) {
			$bwidth = $this->box_width * $cbwidth  / 100;
			$bheight = $cbheight;
		}
		$bhalfheight = $bheight / 2;
		if ($this->root && $this->root->canShowName()) {
			$this->setPageTitle(
				/* I18N: %s is an individual’s name */
				WT_I18N::translate('Family book of %s', $this->root->getFullName())
			);
		} else {
			$this->setPageTitle(WT_I18N::translate('Family book'));
		}
		//Checks how many generations of descendency is for the person for formatting purposes
		$this->dgenerations = $this->max_descendency_generations($this->pid, 0);
		if ($this->dgenerations<1) $this->dgenerations=1;
	}

	/**
	* Prints descendency of passed in person
	*/
	function print_descendency($person, $count) {
		global $TEXT_DIRECTION, $WT_IMAGES, $bwidth, $bheight, $bhalfheight;
		global $show_full, $box_width; // print_pedigree_person() requires these globals.
		if ($count>$this->dgenerations) return 0;
		$show_full=$this->show_full;
		$box_width=$this->box_width;
		echo '<table>';
		echo '<tr>';
		echo '<td width="', ($bwidth), '">';
		$gencount = 0;
		$numkids = 0;
		$familycount = 0;
		$kids = 0;
		$famNum = 0;
		$lh = 0;
		// if real person load child array
		if ($person) {
			$sfamilies=$person->getSpouseFamilies();
			$children = array();
			//count is position from center to left, dgenerations is number of generations
			if ($count < $this->dgenerations) {
				//-- put all of the children in a common array
				foreach ($sfamilies as $family) {
					$famNum ++;
					$chs = $family->getChildren();
					foreach ($chs as $c=>$child) $children[] = $child;
				}
				$ct = count($children);
			}
		} else {
				$ct = 0; // set to 0 for empty boxes
		}
		if ($count < $this->dgenerations) {
			if ($ct==0 ) { // empty boxes
				echo '<table>';
				for ($i=0; $i<1; $i++) { //set to 2 for two child boxes
					echo '<tr>',
						 '<td>';
					 $person2=null;
					 $kids = $this->print_descendency($person2, $count+1);
					$numkids += $kids;
					echo '</td>';
					//Adjust for lines
					if ($i==0) {
						//-- adjust for the first column on left
						$h= round(((($bheight)*$kids)/2)-1);
						//-- adjust for other vertical columns
						if ($kids>1) $h = ((($kids-1)*4)+$h);
							//echo '<td class="tdbot">',
								// '<img class="tvertline" src="',$WT_IMAGES["vline"],'" width="3" height="',$h,'" alt=""></td>';
					} else if ($i==1) {
							//-- adjust for the first column on left
							$h= round(((($bheight)*$kids)/2)+10);
							//-- adjust for other vertical columns
							if ($kids>1) $h = ((($kids-1)*4)+$h);

							//echo '<td class="tdtop">',
							//	 '<img class="bvertline"  src="',$WT_IMAGES["vline"],'" width="3" height="',$h,'" alt=""></td>';
					} else {
						echo '<td style="background: url(',$WT_IMAGES["vline"],');">',
							 '<img class="spacer" src="',$WT_IMAGES["spacer"],'" alt=""></td>';
					}
					echo '</tr>';
				}
				echo '</table>';
			}
			if ($ct>0) { // real people
				echo '<table>';
				for ($i=0; $i<$ct; $i++) {
					$person2 = $children[$i];
					$chil = $person2->getXref();
					echo '<tr>',
						 '<td>';
						 //recursive call to print descendents
					$kids = $this->print_descendency($person2, $count+1);
					$numkids += $kids;
					echo '</td>';
					//-- print the lines
					$twidth = 7;
					if ($ct==1) $twidth+=3;
					if ($ct>1) {
						if ($i==0) {
							//-- adjust for the first column on left
							//$h= round(((($bheight)*$kids)/2)-1);
							$h= round(((($bheight)*$kids)+8)/2);  // Assumes border = 1 and padding = 3
							//-- adjust for other vertical columns
							if ($kids>1) $h = ((($kids-1)*4)+$h);
							echo '<td class="tdbot">',
								 '<img class="tvertline" id="vline_',$chil,'" src="',$WT_IMAGES["vline"],'"  height="',$h-1,'" alt=""></td>';
						} else if ($i==$ct-1) {
							//-- adjust for the first column on left
							$h= round(((($bheight)*$kids)+8)/2);
							//-- adjust for other vertical columns
							if ($kids>1) $h = ((($kids-1)*4)+$h);
							echo '<td class="tdtop">',
								 '<img class="bvertline" id="vline_',$chil,'" src="',$WT_IMAGES["vline"],'" height="',$h+1,'" alt=""></td>';
						} else {
							echo '<td style="background: url(',$WT_IMAGES["vline"],');">',
								 '<img class="spacer" src="',$WT_IMAGES["spacer"],'" alt=""></td>';
						}
					}
					echo '</tr>';
				}
				echo '</table>';
			}
			echo '</td>';
			echo '<td width="',$bwidth,'">';
		}

		if ($numkids==0) {
			$numkids = 1;
		}
		echo '<table><tr><td>';
		if ($person) {
			print_pedigree_person($person);
					echo '</td><td>',
			 '<img class="line2" src="',$WT_IMAGES["hline"],'" width="8" height="3" alt="">';
		} else  if ($kids==1 ) 
			echo '<div style="width:',$bwidth+19,'px; height:',$bheight+8,'px;"></div>',
				 '</td><td>';

		//----- Print the spouse
		if ($count==1 ) {
			if ($this->show_spouse) {
				foreach ($sfamilies as $family) {
					$spouse = $family->getSpouse($person);
					echo '</td></tr><tr><td>';
					//-- shrink the box for the spouses
					$tempw = $bwidth;
					$temph = $bheight;
					$bwidth -= 10;
					$bheight -= 10;
					print_pedigree_person($spouse);
					$bwidth = $tempw;
					$bheight = $temph;
					$numkids += 0.95;
					echo '</td><td></td>';
				}
			}
		}
		echo "</td></tr></table>";
		echo '</td></tr>';
		echo '</table>';
		return $numkids;
	}

	/**
	* Prints pedigree of the person passed in
	*/
	function print_person_pedigree($person, $count) {
		global $WT_IMAGES, $bheight, $bwidth, $bhalfheight;
		if ($count>=$this->generations) return;
		//if (!$person) return;
		$genoffset = $this->generations;  // handle pedigree n generations lines
		//-- calculate how tall the lines should be
		$lh = ($bhalfheight+4) * pow(2, ($genoffset-$count-1));
		//
		//Prints empty table columns for children w/o parents up to the max generation
		//This allows vertical line spacing to be consistent
		//
		if (count($person->getChildFamilies())==0) {
			echo '<table>';
			$this->printEmptyBox($bwidth, $bheight);

			//-- recursively get the father’s family
			$this->print_person_pedigree($person, $count+1);
			echo '</td>',
				 '<td>',
				 '</tr>';
			$this->printEmptyBox($bwidth, $bheight);

			//-- recursively get the mother’s family
			$this->print_person_pedigree($person, $count+1);
			echo '</td>',
				 '<td>',
				 '</tr></table>';
		}

		//Empty box section done, now for regular pedigree
		foreach ($person->getChildFamilies() as $family) {
			echo '<table>',
				 '<tr>',
				 '<td class="tdbot">',
				 '<img class="line3 pvline"  src="',$WT_IMAGES["vline"],'" height="',$lh-1,'" alt=""></td>',
				 '<td>',
				 '<img class="line4" src="',$WT_IMAGES["hline"],'" height="3" alt=""></td>',
				 '<td>';
			//-- print the father box
			print_pedigree_person($family->getHusband());
			echo '</td>';
			if ($family->getHusband()) {
				echo '<td>';
				//-- recursively get the father’s family
				$this->print_person_pedigree($family->getHusband(), $count+1);
				echo '</td>';
			} else {
				echo '<td>';
				for ($i=$count; $i<$genoffset-1; $i++) {
					echo '<table>';
					$this->printEmptyBox($bwidth, $bheight);
					echo '</tr>';
					$this->printEmptyBox($bwidth, $bheight);
					echo '</tr></table>';
				}
			}
			echo '</tr><tr>',
				 '<td class="tdtop"><img class="pvline" src="',$WT_IMAGES["vline"],'" height="',$lh+1,'" alt=""></td>',
				 '<td><img class="line4" src="',$WT_IMAGES["hline"],'" height="3" alt=""></td>',
				 '<td>';
			//-- print the mother box
			print_pedigree_person($family->getWife());
			echo '</td>';
			if ($family->getWife()) {
				echo '<td>';
				//-- recursively print the mother’s family
				$this->print_person_pedigree($family->getWife(), $count+1);
				echo '</td>';
			} else {
				echo '<td>';
				for ($i=$count; $i<$genoffset-1; $i++) {
					echo '<table>';
						 '<tr>';
					$this->printEmptyBox($bwidth, $bheight);
					echo '</tr>';
					$this->printEmptyBox($bwidth, $bheight);
					echo '</tr></table>';
				}
			}
			echo '</tr>',
				 '</table>';
			break;
		}
	}

	/**
	 * Calculates number of generations a person has
	 */
	function max_descendency_generations($pid, $depth) {
		if ($depth > $this->generations) return $depth;
		$person = WT_Individual::getInstance($pid);
		if (is_null($person)) return $depth;
		$maxdc = $depth;
		foreach ($person->getSpouseFamilies() as $family) {
			foreach ($family->getChildren() as $child) {
				$dc = $this->max_descendency_generations($child->getXref(), $depth+1);
				if ($dc >= $this->generations) return $dc;
				if ($dc > $maxdc) $maxdc = $dc;
			}
		}
		$maxdc++;
		if ($maxdc==1) $maxdc++;
		return $maxdc;
	}
	/**
	* Print empty box
	*/
	function printEmptyBox($bwidth, $bheight){
	echo '<tr>',
		 '<td>',
		 '<div style="width:',$bwidth+16,'px; height:',$bheight+8,'px;"></div>',
		 '</td>',
		 '<td>';
	}
	function print_family_book($person, $descent) {
		global $firstrun;
		if ($descent==0 || !$person->canShowName()) {
			return;
		}
		$families=$person->getSpouseFamilies();
		if (count($families)>0 || empty($firstrun)) {
			$firstrun=true;
			echo
				'<h3>',
					/* I18N: A title/heading. %s is an individual’s name */ WT_I18N::translate('Family of %s', $person->getFullName()),
				'</h3>
				<table class="t0"><tr><td class="tdmid">';
			$this->dgenerations = $this->generations;
			$this->print_descendency($person, 1);
			echo '</td><td class="tdmid">';
			$this->print_person_pedigree($person, 1);
			echo '</td></tr></table><br><br><hr style="page-break-after:always;"><br><br>';
			foreach ($families as $family) {
				foreach ($family->getChildren() as $child) {
					$this->print_family_book($child, $descent-1);
				}
			}
		}
	}
}