Passed
Push — master ( dee632...0e5063 )
by Greg
05:39
created

MapDataList::getPlaceListLocation()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 10
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 16
rs 9.9332
1
<?php
2
3
/**
4
 * webtrees: online genealogy
5
 * Copyright (C) 2020 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\Http\ViewResponseTrait;
23
use Fisharebest\Webtrees\I18N;
24
use Fisharebest\Webtrees\Module\PlaceHierarchyListModule;
25
use Fisharebest\Webtrees\Services\MapDataService;
26
use Fisharebest\Webtrees\Services\ModuleService;
27
use Psr\Http\Message\ResponseInterface;
28
use Psr\Http\Message\ServerRequestInterface;
29
use Psr\Http\Server\RequestHandlerInterface;
30
31
use function array_reverse;
32
use function redirect;
33
use function route;
34
35
/**
36
 * Show a list of map data.
37
 */
38
class MapDataList implements RequestHandlerInterface
39
{
40
    use ViewResponseTrait;
41
42
    /** @var MapDataService */
43
    private $map_data_service;
44
45
    /** @var ModuleService */
46
    private $module_service;
47
48
    /**
49
     * Dependency injection.
50
     *
51
     * @param MapDataService $map_data_service
52
     * @param ModuleService  $module_service
53
     */
54
    public function __construct(MapDataService $map_data_service, ModuleService $module_service)
55
    {
56
        $this->map_data_service = $map_data_service;
57
        $this->module_service   = $module_service;
58
    }
59
60
    /**
61
     * @param ServerRequestInterface $request
62
     *
63
     * @return ResponseInterface
64
     */
65
    public function handle(ServerRequestInterface $request): ResponseInterface
66
    {
67
        $parent_id   = (int) ($request->getQueryParams()['parent_id'] ?? 0);
68
        $title       = I18N::translate('Geographic data');
69
        $parent      = $this->map_data_service->findById($parent_id);
70
71
        // Request for a non-existent location?
72
        if ($parent_id !== $parent->id()) {
73
            return redirect(route(__CLASS__));
74
        }
75
76
        // Automatically import any new/missing places.
77
        $this->map_data_service->importMissingLocations();
78
79
        $breadcrumbs = [$parent->locationName()];
80
81
        $tmp = $parent->parent();
82
83
        while ($tmp->id() !== 0) {
84
            $breadcrumbs[route(__CLASS__, ['parent_id' => $tmp->id()])] = $tmp->locationName();
85
86
            $tmp = $tmp->parent();
87
        }
88
89
        $breadcrumbs[route(__CLASS__)]           = $title;
90
        $breadcrumbs[route(ControlPanel::class)] = I18N::translate('Control panel');
91
92
        $show_links = $this->module_service->findByInterface(PlaceHierarchyListModule::class)->isNotEmpty();
93
94
        $this->layout = 'layouts/administration';
95
96
        return $this->viewResponse('admin/locations', [
97
            'active'      => $this->map_data_service->activePlaces($parent),
98
            'breadcrumbs' => array_reverse($breadcrumbs),
99
            'parent_id'   => $parent_id,
100
            'placelist'   => $this->map_data_service->getPlaceListLocation($parent_id),
101
            'show_links'  => $show_links,
102
            'title'       => $title,
103
        ]);
104
    }
105
}
106