summaryrefslogtreecommitdiff
path: root/includes/controllers/family_ctrl.php
blob: dc5b99cf3fd4fcace0951808e8e20444b30847e7 (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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
<?php
/**
* Parses gedcom file and gives access to information about a family.
*
* You must supply a $famid value with the identifier for the family.
*
* webtrees: Web based Family History software
 * Copyright (C) 2010 webtrees development team.
 *
 * Derived from PhpGedView
* Copyright (C) 2002 to 2010 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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*
* @package webtrees
* @subpackage Controllers
* @version $Id$
*/

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

define('WT_FAMILY_CTRL_PHP', '');

require_once WT_ROOT.'includes/functions/functions_print_facts.php';
require_once WT_ROOT.'includes/controllers/basecontrol.php';
require_once WT_ROOT.'includes/functions/functions_charts.php';
require_once WT_ROOT.'includes/classes/class_family.php';
require_once WT_ROOT.'includes/classes/class_menu.php';
require_once WT_ROOT.'includes/functions/functions_import.php';

class FamilyRoot extends BaseController {
	var $user = null;
	var $showLivingHusb = true;
	var $showLivingWife = true;
	var $parents = '';
	var $display = false;
	var $accept_success = false;
	var $show_changes = true;
	var $famrec = '';
	var $link_relation = 0;
	var $title = '';
	var $famid = '';
	var $family = null;
	var $difffam = null;

	/**
	* constructor
	*/
	function FamilyRoot() {
		parent::BaseController();
	}

	function init() {
		global $Dbwidth, $bwidth, $pbwidth, $pbheight, $bheight, $GEDCOM, $CONTACT_EMAIL, $show_famlink;
		$bwidth = $Dbwidth;
		$pbwidth = $bwidth + 12;
		$pbheight = $bheight + 14;

		$show_famlink = $this->view!='preview';

		$this->famid = safe_GET_xref('famid');

		$this->family = Family::getInstance($this->famid);

		if (empty($this->famrec)) {
			$ct = preg_match('/(\w+):(.+)/', $this->famid, $match);
			if ($ct>0) {
				$servid = trim($match[1]);
				$remoteid = trim($match[2]);
				require_once WT_ROOT.'includes/classes/class_serviceclient.php';
				$service = ServiceClient::getInstance($servid);
				if (!is_null($service)) {
					$newrec= $service->mergeGedcomRecord($remoteid, "0 @".$this->famid."@ FAM\n1 RFN ".$this->famid, false);
					$this->famrec = $newrec;
				}
			}
			//-- if no record was found create a default empty one
			if (find_updated_record($this->famid, WT_GED_ID)!==null){
				$this->famrec = "0 @".$this->famid."@ FAM\n";
				$this->family = new Family($this->famrec);
			} else if (empty($this->family)){
				return false;
			}
		}

		$this->famrec = $this->family->getGedcomRecord();
		$this->display = displayDetailsById($this->famid, 'FAM');

		//-- if the user can edit and there are changes then get the new changes
		if ($this->show_changes && WT_USER_CAN_EDIT && find_updated_record($this->famid, WT_GED_ID)!==null) {
			$newrec = find_gedcom_record($this->famid, WT_GED_ID, true);
			$this->difffam = new Family($newrec);
			$this->difffam->setChanged(true);
			$this->family->diffMerge($this->difffam);
			//$this->famrec = $newrec;
			//$this->family = new Family($this->famrec);
		}
		$this->parents = array('HUSB'=>$this->family->getHusbId(), 'WIFE'=>$this->family->getWifeId());

		//-- check if we can display both parents
		if ($this->display == false) {
			$this->showLivingHusb = showLivingNameById($this->parents['HUSB']);
			$this->showLivingWife = showLivingNameById($this->parents['WIFE']);
		}

		//-- add favorites action
		if ($this->action=='addfav' && !empty($_REQUEST['gid']) && WT_USER_NAME) {
			$_REQUEST['gid'] = strtoupper($_REQUEST['gid']);
			$indirec = find_family_record($_REQUEST['gid'], WT_GED_ID);
			if ($indirec) {
				$favorite = array(
					'username' => WT_USER_NAME,
					'gid' => $_REQUEST['gid'],
					'type' => 'FAM',
					'file' => $GEDCOM,
					'url' => '',
					'note' => '',
					'title' => ''
				);
				addFavorite($favorite);
			}
		}

		if (WT_USER_CAN_ACCEPT) {
			if ($this->action=='accept') {
				accept_all_changes($this->famid, WT_GED_ID);
				$this->show_changes = false;
				$this->accept_success = true;
				//-- check if we just deleted the record and redirect to index
				$famrec = find_family_record($this->famid, WT_GED_ID);
				if (empty($famrec)) {
					header("Location: index.php?ctype=gedcom");
					exit;
				}
				$this->family = new Family($famrec);
				$this->parents = find_parents($_REQUEST['famid']);
			}

			if ($this->action=='undo') {
				reject_all_changes($this->famid, WT_GED_ID);
				$this->show_changes = false;
				$this->accept_success = true;
				$this->family = new Family($famrec);
				$this->parents = find_parents($this->famid);
			}
		}

		//-- make sure we have the true id from the record
		$ct = preg_match("/0 @(.*)@/", $this->famrec, $match);
		if ($ct > 0) {
			$this->famid = trim($match[1]);
		}

		if ($this->showLivingHusb == false && $this->showLivingWife == false) {
			print_header(i18n::translate('Private')." ".i18n::translate('Family Information'));
			print_privacy_error();
			print_footer();
			exit;
		}

		$this->title=$this->family->getFullName();

		if (empty($this->parents['HUSB']) || empty($this->parents['WIFE'])) {
			$this->link_relation = 0;
		} else {
			$this->link_relation = 1;
		}
	}

	function getFamilyID() {
		return $this->famid;
	}

	function getFamilyRecord() {
		return $this->famrec;
	}

	function getHusband() {
		if (!is_null($this->difffam)) return $this->difffam->getHusbId();
		return $this->parents['HUSB'];
	}

	function getWife() {
		if (!is_null($this->difffam)) return $this->difffam->getWifeId();
		return $this->parents['WIFE'];
	}

	function getChildren() {
		return find_children_in_record($this->famrec);
	}

	function getChildrenUrlTimeline($start=0) {
		$children = $this->getChildren();
		$c = count($children);
		for ($i = 0; $i < $c; $i++) {
			$children[$i] = 'pids['.($i + $start).']='.$children[$i];
		}
		return join('&amp;', $children);
	}

	function getPageTitle() {
		if ($this->family) {
			return PrintReady($this->title);
		} else {
			return i18n::translate('Unable to find record with ID');
		}
	}

	/**
	* get the family page charts menu
	* @return Menu
	*/
	function &getChartsMenu() {
		global $TEXT_DIRECTION, $WT_IMAGE_DIR, $WT_IMAGES, $GEDCOM;
		if ($TEXT_DIRECTION=="rtl") $ff="_rtl";
		else $ff="";

		$husb = $this->getHusband();
		$wife = $this->getWife();
		$link = '';
		$c = 0;
		if ($husb) {
			$link .= 'pids[0]='.$husb;
			$c++;
			if ($wife) {
				$link .= '&pids[1]='.$wife;
				$c++;
			}
		} else if ($wife) {
			$link .= 'pids[0]='.$wife;
			$c++;
		}

		// charts menu
		$menu = new Menu(i18n::translate('Charts'), encode_url('timeline.php?'.$link));
		if (!empty($WT_IMAGES["timeline"]["small"])) {
			$menu->addIcon("{$WT_IMAGE_DIR}/{$WT_IMAGES['timeline']['small']}");
		}
		$menu->addClass("submenuitem{$ff}", "submenuitem_hover{$ff}", "submenu{$ff}");
		// Build a sortable list of submenu items and then sort it in localized name order
		$menuList = array();
		$menuList["parentTimeLine"] = i18n::translate('Show couple on timeline chart');
		$menuList["childTimeLine"] = i18n::translate('Show children on timeline chart');
		$menuList["familyTimeLine"] = i18n::translate('Show family on timeline chart');
		asort($menuList);

		// Produce the submenus in localized name order

		foreach($menuList as $menuType => $menuName) {
			switch ($menuType) {
			case "parentTimeLine":
				// charts / parents_timeline
				$submenu = new Menu(i18n::translate('Show couple on timeline chart'), encode_url('timeline.php?'.$link));
				if (!empty($WT_IMAGES["timeline"]["small"])) {
					$submenu->addIcon("{$WT_IMAGE_DIR}/{$WT_IMAGES['timeline']['small']}");
				}
				$submenu->addClass("submenuitem{$ff}", "submenuitem_hover{$ff}");
				$menu->addSubmenu($submenu);
				break;

			case "childTimeLine":
				// charts / children_timeline
				$submenu = new Menu(i18n::translate('Show children on timeline chart'), encode_url('timeline.php?'.$this->getChildrenUrlTimeline()));
				if (!empty($WT_IMAGES["timeline"]["small"])) {
					$submenu->addIcon("{$WT_IMAGE_DIR}/{$WT_IMAGES['timeline']['small']}");
				}
				$submenu->addClass("submenuitem{$ff}", "submenuitem_hover{$ff}");
				$menu->addSubmenu($submenu);
				break;

			case "familyTimeLine":
				// charts / family_timeline
				$submenu = new Menu(i18n::translate('Show family on timeline chart'), encode_url('timeline.php?'.$link.'&'.$this->getChildrenUrlTimeline($c)));
				if (!empty($WT_IMAGES["timeline"]["small"])) {
					$submenu->addIcon("{$WT_IMAGE_DIR}/{$WT_IMAGES['timeline']['small']}");
				}
				$submenu->addClass("submenuitem{$ff}", "submenuitem_hover{$ff}");
				$menu->addSubmenu($submenu);
				break;

			}
		}

		return $menu;
	}

	/**
	* get the family page reports menu
	* @deprecated This function has been deprecated by the getReportsMenu function in menu.php
	* @return Menu
	*/
	function &getReportsMenu() {
	/**
		global $TEXT_DIRECTION, $WT_IMAGE_DIR, $WT_IMAGES, $GEDCOM;
		if ($TEXT_DIRECTION=="rtl") $ff="_rtl";
		else $ff="";

		$menu = new Menu(i18n::translate('Reports'), encode_url('reportengine.php?action=setup&report=reports/familygroup.xml&famid='.$this->getFamilyID()));
		if (!empty($WT_IMAGES["reports"]["small"])) {
			$menu->addIcon("{$WT_IMAGE_DIR}/{$WT_IMAGES['reports']['small']}");
		}
		$menu->addClass("submenuitem{$ff}", "submenuitem_hover{$ff}", "submenu{$ff}");

		// reports / family_group_report
		$submenu = new Menu(i18n::translate('Family Group Report'), encode_url('reportengine.php?action=setup&report=reports/familygroup.xml&famid='.$this->getFamilyID()));
		if (!empty($WT_IMAGES["reports"]["small"])) {
			$submenu->addIcon("{$WT_IMAGE_DIR}/{$WT_IMAGES['reports']['small']}");
		}
		$submenu->addClass("submenuitem{$ff}", "submenuitem_hover{$ff}");
		$menu->addSubmenu($submenu);

		return $menu;
		**/
	}

	/**
	* get the family page edit menu
	*/
	function &getEditMenu() {
		global $TEXT_DIRECTION, $WT_IMAGE_DIR, $WT_IMAGES, $GEDCOM;
		global $SHOW_GEDCOM_RECORD;
		if ($TEXT_DIRECTION=="rtl") $ff="_rtl";
		else $ff="";

		// edit_fam menu
		$menu = new Menu(i18n::translate('Edit Family'));
		$menu->addOnclick("return edit_family('".$this->getFamilyID()."');");
		if (!empty($WT_IMAGES["edit_fam"]["large"])) {
			$menu->addIcon($WT_IMAGE_DIR."/".$WT_IMAGES["edit_fam"]["large"]);
		}
		else if (!empty($WT_IMAGES["edit_fam"]["small"])) {
			$menu->addIcon($WT_IMAGE_DIR."/".$WT_IMAGES["edit_fam"]["small"]);
		}
		$menu->addClass("submenuitem{$ff}", "submenuitem_hover{$ff}", "submenu{$ff}");

		// edit_fam / edit_fam
		$submenu = new Menu(i18n::translate('Edit Family'));
		$submenu->addOnclick("return edit_family('".$this->getFamilyID()."');");
		if (!empty($WT_IMAGES["edit_fam"]["small"])) {
			$submenu->addIcon("{$WT_IMAGE_DIR}/{$WT_IMAGES['edit_fam']['small']}");
		}
		$submenu->addClass("submenuitem{$ff}", "submenuitem_hover{$ff}");
		$menu->addSubmenu($submenu);

		// edit_fam / members
		$submenu = new Menu(i18n::translate('Change Family Members'));
		$submenu->addOnclick("return change_family_members('".$this->getFamilyID()."');");
		if (!empty($WT_IMAGES["edit_fam"]["small"])) {
			$submenu->addIcon("{$WT_IMAGE_DIR}/{$WT_IMAGES['edit_fam']['small']}");
		}
		$submenu->addClass("submenuitem{$ff}", "submenuitem_hover{$ff}");
		$menu->addSubmenu($submenu);

		// edit_fam / add child
		$submenu = new Menu(i18n::translate('Add a child to this family'));
		$submenu->addOnclick("return addnewchild('".$this->getFamilyID()."');");
		if (!empty($WT_IMAGES["edit_fam"]["small"])) {
			$submenu->addIcon("{$WT_IMAGE_DIR}/{$WT_IMAGES['edit_fam']['small']}");
		}
		$submenu->addClass("submenuitem{$ff}", "submenuitem_hover{$ff}");
		$menu->addSubmenu($submenu);

		// edit_fam / reorder_children
		if ($this->family->getNumberOfChildren() > 1) {
			$submenu = new Menu(i18n::translate('Re-order children'));
			$submenu->addOnclick("return reorder_children('".$this->getFamilyID()."');");
			if (!empty($WT_IMAGES["edit_fam"]["small"])) {
				$submenu->addIcon("{$WT_IMAGE_DIR}/{$WT_IMAGES['edit_fam']['small']}");
			}
			$submenu->addClass("submenuitem{$ff}", "submenuitem_hover{$ff}");
			$menu->addSubmenu($submenu);
		}

		if (find_updated_record($this->getFamilyID(), WT_GED_ID)!==null) {
			// edit_fam / separator
			$menu->addSeparator();

			// edit_fam / show/hide changes
			if (!$this->show_changes) {
				$submenu = new Menu(i18n::translate('This record has been updated.  Click here to show changes.'), encode_url('family.php?famid='.$this->getFamilyID().'&show_changes=yes'));
				if (!empty($WT_IMAGES["edit_fam"]["small"])) {
					$submenu->addIcon("{$WT_IMAGE_DIR}/{$WT_IMAGES['edit_fam']['small']}");
				}
				$submenu->addClass("submenuitem{$ff}", "submenuitem_hover{$ff}");
				$menu->addSubmenu($submenu);
			} else {
				$submenu = new Menu(i18n::translate('Click here to hide changes.'), encode_url('family.php?famid='.$this->getFamilyID().'&show_changes=no'));
				if (!empty($WT_IMAGES["edit_fam"]["small"])) {
					$submenu->addIcon("{$WT_IMAGE_DIR}/{$WT_IMAGES['edit_fam']['small']}");
				}
				$submenu->addClass("submenuitem{$ff}", "submenuitem_hover{$ff}");
				$menu->addSubmenu($submenu);
			}

			if (WT_USER_CAN_ACCEPT) {
				// edit_fam / accept_all
				$submenu = new Menu(i18n::translate('Undo all changes'), encode_url("family.php?famid={$this->famid}&action=undo"));
				if (!empty($WT_IMAGES["edit_fam"]["small"])) {
					$submenu->addIcon("{$WT_IMAGE_DIR}/{$WT_IMAGES['edit_fam']['small']}");
				}
				$submenu->addClass("submenuitem$ff", "submenuitem_hover$ff");
				$menu->addSubmenu($submenu);
				$submenu = new Menu(i18n::translate('Accept all changes'), encode_url("family.php?famid={$this->famid}&action=accept"));
				if (!empty($WT_IMAGES["edit_fam"]["small"])) {
					$submenu->addIcon("{$WT_IMAGE_DIR}/{$WT_IMAGES['edit_fam']['small']}");
				}
				$submenu->addClass("submenuitem$ff", "submenuitem_hover$ff");
				$menu->addSubmenu($submenu);
			}
		}

		// edit_fam / separator
		$menu->addSeparator();

		// edit_fam / edit_raw
		if ($SHOW_GEDCOM_RECORD || WT_USER_IS_ADMIN) {
			$submenu = new Menu(i18n::translate('Edit raw GEDCOM record'));
			$submenu->addOnclick("return edit_raw('".$this->getFamilyID()."');");
			if (!empty($WT_IMAGES["edit_fam"]["small"])) {
				$submenu->addIcon("{$WT_IMAGE_DIR}/{$WT_IMAGES['edit_fam']['small']}");
			}
			$submenu->addClass("submenuitem{$ff}", "submenuitem_hover{$ff}");
			$menu->addSubmenu($submenu);
		}

		// edit_fam / delete_family
		$submenu = new Menu(i18n::translate('Delete family'));
		$submenu->addOnclick("if (confirm('".i18n::translate('Deleting the family will unlink all of the individuals from each other but will leave the individuals in place.  Are you sure you want to delete this family?')."')) return delete_family('".$this->getFamilyID()."'); else return false;");
		if (!empty($WT_IMAGES["edit_fam"]["small"])) {
			$submenu->addIcon("{$WT_IMAGE_DIR}/{$WT_IMAGES['edit_fam']['small']}");
		}
		$submenu->addClass("submenuitem{$ff}", "submenuitem_hover{$ff}");
		$menu->addSubmenu($submenu);

		//-- get the link for the first submenu and set it as the link for the main menu
		if (isset($menu->submenus[0])) {
			$link = $menu->submenus[0]->onclick;
			$menu->addOnclick($link);
		}

		return $menu;
	}

	/**
	* get the other menu
	* @return Menu
	*/
	function &getOtherMenu() {
		global $TEXT_DIRECTION, $WT_IMAGE_DIR, $WT_IMAGES, $GEDCOM;
		global $SHOW_GEDCOM_RECORD, $ENABLE_CLIPPINGS_CART;

		if ($TEXT_DIRECTION=="rtl") $ff="_rtl";
		else $ff="";

			// other menu
		$menu = new Menu(i18n::translate('Other'));
		$menu->addClass("submenuitem{$ff}", "submenuitem_hover{$ff}", "submenu{$ff}");
		if ($SHOW_GEDCOM_RECORD) {
			$menu->addIcon("{$WT_IMAGE_DIR}/{$WT_IMAGES['gedcom']['small']}");
			if ($this->show_changes && WT_USER_CAN_EDIT) {
				$menu->addLink("javascript:show_gedcom_record('new');");
			} else {
				$menu->addLink("javascript:show_gedcom_record();");
			}
		} else {
			if (!empty($WT_IMAGES["clippings"]["small"])) {
				$menu->addIcon("{$WT_IMAGE_DIR}/{$WT_IMAGES['clippings']['small']}");
			}
			$menu->addLink(encode_url('clippings.php?action=add&id='.$this->getFamilyID().'&type=fam'));
		}
		if ($SHOW_GEDCOM_RECORD) {
				// other / view_gedcom
				$submenu = new Menu(i18n::translate('View GEDCOM Record'));
				if ($this->show_changes && WT_USER_CAN_EDIT) {
					$submenu->addLink("javascript:show_gedcom_record('new');");
				} else {
					$submenu->addLink("javascript:show_gedcom_record();");
				}
				$submenu->addIcon("{$WT_IMAGE_DIR}/{$WT_IMAGES['gedcom']['small']}");
				$submenu->addClass("submenuitem{$ff}", "submenuitem_hover{$ff}");
				$menu->addSubmenu($submenu);
		}
		if ($ENABLE_CLIPPINGS_CART >= WT_USER_ACCESS_LEVEL) {
				// other / add_to_cart
				$submenu = new Menu(i18n::translate('Add to Clippings Cart'), encode_url('clippings.php?action=add&id='.$this->getFamilyID().'&type=fam'));
				if (!empty($WT_IMAGES["clippings"]["small"])) {
					$submenu->addIcon("{$WT_IMAGE_DIR}/{$WT_IMAGES['clippings']['small']}");
				}
				$submenu->addClass("submenuitem{$ff}", "submenuitem_hover{$ff}");
				$menu->addSubmenu($submenu);
		}
		if ($this->display && WT_USER_ID) {
			// other / add_to_my_favorites
			$submenu = new Menu(i18n::translate('Add to My Favorites'), encode_url('family.php?action=addfav&famid='.$this->getFamilyID().'&gid='.$this->getFamilyID()));
			$submenu->addIcon("{$WT_IMAGE_DIR}/{$WT_IMAGES['gedcom']['small']}");
			$submenu->addClass("submenuitem{$ff}", "submenuitem_hover{$ff}");
			$menu->addSubmenu($submenu);
		}
		return $menu;
	}
}

if (file_exists(WT_ROOT.'includes/controllers/family_ctrl_user.php')) {
	require_once WT_ROOT.'includes/controllers/family_ctrl_user.php';
} else {
	class FamilyController extends FamilyRoot {
	}
}

?>