Completed
Push — develop ( 9087a8...c9b4ef )
by Greg
16:31 queued 05:44
created

EditFamilyController   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 68
rs 10
c 1
b 0
f 0
wmc 8

2 Methods

Rating   Name   Duplication   Size   Complexity  
B reorderChildrenAction() 0 37 5
A reorderChildren() 0 17 3
1
<?php
2
/**
3
 * webtrees: online genealogy
4
 * Copyright (C) 2018 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
declare(strict_types=1);
17
18
namespace Fisharebest\Webtrees\Http\Controllers;
19
20
use Fisharebest\Webtrees\Family;
21
use Fisharebest\Webtrees\I18N;
22
use Fisharebest\Webtrees\Tree;
23
use Symfony\Component\HttpFoundation\RedirectResponse;
24
use Symfony\Component\HttpFoundation\Request;
25
use Symfony\Component\HttpFoundation\Response;
26
27
/**
28
 * Controller for edit forms and responses.
29
 */
30
class EditFamilyController extends BaseController {
31
	/**
32
	 * @param Request $request
33
	 *
34
	 * @return Response
35
	 */
36
	public function reorderChildren(Request $request): Response {
37
		/** @var Tree $tree */
38
		$tree   = $request->attributes->get('tree');
39
		$xref   = $request->get('xref');
40
		$family = Family::getInstance($xref, $tree);
41
42
		if ($family === null) {
43
			return $this->familyNotFound();
44
		} elseif (!$family->canEdit()) {
45
			return $this->familyNotAllowed();
46
		}
47
48
		$title = $family->getFullName() . ' — ' . I18N::translate('Re-order children');
49
50
		return $this->viewResponse('edit/reorder-children', [
51
			'title'  => $title,
52
			'family' => $family,
53
		]);
54
	}
55
56
	/**
57
	 * @param Request $request
58
	 *
59
	 * @return Response
60
	 */
61
	public function reorderChildrenAction(Request $request): Response {
62
		/** @var Tree $tree */
63
		$tree   = $request->attributes->get('tree');
64
		$xref   = $request->get('xref');
65
		$order  = (array) $request->get('order', []);
66
		$family = Family::getInstance($xref, $tree);
67
68
		if ($family === null) {
69
			return $this->familyNotFound();
70
		} elseif (!$family->canEdit()) {
71
			return $this->familyNotAllowed();
72
		}
73
74
		$dummy_facts = ['0 @' . $family->getXref() . '@ FAM'];
75
		$sort_facts  = [];
76
		$keep_facts  = [];
77
78
		// Split facts into FAMS and other
79
		foreach ($family->getFacts() as $fact) {
80
			if ($fact->getTag() === 'CHIL') {
81
				$sort_facts[$fact->getFactId()] = $fact->getGedcom();
82
			} else {
83
				$keep_facts[] = $fact->getGedcom();
84
			}
85
		}
86
87
		// Sort the facts
88
		uksort($sort_facts, function ($x, $y) use ($order) {
89
			return array_search($x, $order) - array_search($y, $order);
90
		});
91
92
		// Merge the facts
93
		$gedcom = implode("\n", array_merge($dummy_facts, $sort_facts, $keep_facts));
94
95
		$family->updateRecord($gedcom, false);
96
97
		return new RedirectResponse($family->url());
98
	}
99
}
100