Passed
Push — develop ( 5f211d...930448 )
by Greg
13:36 queued 06:57
created

MapDataSave   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 32
dl 0
loc 55
rs 10
c 1
b 0
f 0
wmc 7

1 Method

Rating   Name   Duplication   Size   Complexity  
B handle() 0 48 7
1
<?php
2
3
/**
4
 * webtrees: online genealogy
5
 * Copyright (C) 2021 webtrees development team
6
 * This program is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
 * GNU General Public License for more details.
14
 * You should have received a copy of the GNU General Public License
15
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16
 */
17
18
declare(strict_types=1);
19
20
namespace Fisharebest\Webtrees\Http\RequestHandlers;
21
22
use Fisharebest\Webtrees\FlashMessages;
23
use Fisharebest\Webtrees\I18N;
24
use Illuminate\Database\Capsule\Manager as DB;
25
use Psr\Http\Message\ResponseInterface;
26
use Psr\Http\Message\ServerRequestInterface;
27
use Psr\Http\Server\RequestHandlerInterface;
28
29
use function e;
30
use function redirect;
31
use function round;
32
use function route;
33
34
/**
35
 * Controller for maintaining geographic data.
36
 */
37
class MapDataSave implements RequestHandlerInterface
38
{
39
    /**
40
     * @param ServerRequestInterface $request
41
     *
42
     * @return ResponseInterface
43
     */
44
    public function handle(ServerRequestInterface $request): ResponseInterface
45
    {
46
        $params = (array) $request->getParsedBody();
47
48
        $place_id  = $params['place_id'] ?? null;
49
        $parent_id = $params['parent_id'] ?? null;
50
        $latitude  = $params['new_place_lati'] ?? '';
51
        $longitude = $params['new_place_long'] ?? '';
52
        $name      = mb_substr($params['new_place_name'] ?? '', 0, 120);
53
54
        if ($latitude === '' || $longitude === '') {
55
            $latitude  = null;
56
            $longitude = null;
57
        } else {
58
            // 5 decimal places (locate to within about 1 metre)
59
            $latitude  = round((float) $latitude, 5);
60
            $longitude = round((float) $longitude, 5);
61
62
            // 0,0 is only allowed at the top level
63
            if ($parent_id !== null && $latitude === 0.0 && $longitude === 0.0) {
64
                $latitude  = null;
65
                $longitude = null;
66
            }
67
        }
68
69
        if ($place_id === null) {
70
            DB::table('place_location')->insert([
71
                'parent_id' => $parent_id,
72
                'place'     => $name,
73
                'latitude'  => $latitude,
74
                'longitude' => $longitude,
75
            ]);
76
        } else {
77
            DB::table('place_location')
78
                ->where('id', '=', $place_id)
79
                ->update([
80
                    'place'     => $name,
81
                    'latitude'  => $latitude,
82
                    'longitude' => $longitude,
83
                ]);
84
        }
85
86
        $message = I18N::translate('The details for ā€œ%sā€ have been updated.', e($name));
87
        FlashMessages::addMessage($message, 'success');
88
89
        $url = route(MapDataList::class, ['parent_id' => $parent_id]);
90
91
        return redirect($url);
92
    }
93
}
94