xref: /webtrees/app/Http/RequestHandlers/MapDataList.php (revision ac34be60da3ad5156d9e991e4803eec163673042)
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
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Http\RequestHandlers;
21
22use Fisharebest\Webtrees\Http\ViewResponseTrait;
23use Fisharebest\Webtrees\I18N;
24use Fisharebest\Webtrees\Module\PlaceHierarchyListModule;
25use Fisharebest\Webtrees\Services\MapDataService;
26use Fisharebest\Webtrees\Services\ModuleService;
27use Fisharebest\Webtrees\Services\TreeService;
28use Illuminate\Database\Capsule\Manager as DB;
29use Illuminate\Database\Query\Expression;
30use Psr\Http\Message\ResponseInterface;
31use Psr\Http\Message\ServerRequestInterface;
32use Psr\Http\Server\RequestHandlerInterface;
33use stdClass;
34use function array_reverse;
35use function redirect;
36use function route;
37
38/**
39 * Show a list of map data.
40 */
41class MapDataList implements RequestHandlerInterface
42{
43    use ViewResponseTrait;
44
45    /** @var MapDataService */
46    private $map_data_service;
47
48    /** @var ModuleService */
49    private $module_service;
50
51    /** @var TreeService */
52    private $tree_service;
53
54    /**
55     * Dependency injection.
56     *
57     * @param MapDataService $map_data_service
58     * @param ModuleService  $module_service
59     */
60    public function __construct(MapDataService $map_data_service, ModuleService $module_service, TreeService $tree_service)
61    {
62        $this->map_data_service = $map_data_service;
63        $this->module_service   = $module_service;
64        $this->tree_service   = $tree_service;
65    }
66
67    /**
68     * @param ServerRequestInterface $request
69     *
70     * @return ResponseInterface
71     */
72    public function handle(ServerRequestInterface $request): ResponseInterface
73    {
74        $parent_id   = (int) ($request->getQueryParams()['parent_id'] ?? 0);
75        $title       = I18N::translate('Geographic data');
76        $parent      = $this->map_data_service->findById($parent_id);
77
78        // Request for a non-existent location?
79        if ($parent_id !== $parent->id()) {
80            return redirect(route(__CLASS__));
81        }
82
83        // Automatically import any new/missing places.
84        $this->map_data_service->importMissingLocations();
85
86        $breadcrumbs = [$parent->locationName()];
87
88        $tmp = $parent->parent();
89
90        while ($tmp->id() !== 0) {
91            $breadcrumbs[route(__CLASS__, ['parent_id' => $tmp->id()])] = $tmp->locationName();
92
93            $tmp = $tmp->parent();
94        }
95
96        $breadcrumbs[route(__CLASS__)]           = $title;
97        $breadcrumbs[route(ControlPanel::class)] = I18N::translate('Control panel');
98
99        $show_links_via_module = $this->module_service->findByInterface(PlaceHierarchyListModule::class)->first();
100
101        $this->layout = 'layouts/administration';
102
103        return $this->viewResponse('admin/locations', [
104            'active'                => $this->map_data_service->activePlaces($parent),
105            'breadcrumbs'           => array_reverse($breadcrumbs),
106            'parent_id'             => $parent_id,
107            'placelist'             => $this->map_data_service->getPlaceListLocation($parent_id),
108            'show_links_via_module' => $show_links_via_module,
109            'title'                 => $title,
110            'tree_service'          => $this->tree_service,
111        ]);
112    }
113
114
115    /**
116     * Find all of the places in the hierarchy
117     *
118     * @param int $id
119     *
120     * @return stdClass[]
121     */
122    private function getPlaceListLocation(int $id): array
123    {
124        return DB::table('placelocation')
125            ->where('pl_parent_id', '=', $id)
126            ->orderBy(new Expression('pl_place /*! COLLATE ' . I18N::collation() . ' */'))
127            ->get()
128            ->map(function (stdClass $row): stdClass {
129                // Find/count places without co-ordinates
130                $children = $this->childLocationStatus((int) $row->pl_id);
131
132                $row->child_count = (int) $children->child_count;
133                $row->no_coord    = (int) $children->no_coord;
134
135                return $row;
136            })
137            ->all();
138    }
139
140    /**
141     * How many children does place have?  How many have co-ordinates?
142     *
143     * @param int $parent_id
144     *
145     * @return stdClass
146     */
147    private function childLocationStatus(int $parent_id): stdClass
148    {
149        $prefix = DB::connection()->getTablePrefix();
150
151        $expression =
152            $prefix . 'p0.pl_place IS NOT NULL AND COALESCE(' . $prefix . "p0.pl_lati, '') = '' OR " .
153            $prefix . 'p1.pl_place IS NOT NULL AND COALESCE(' . $prefix . "p1.pl_lati, '') = '' OR " .
154            $prefix . 'p2.pl_place IS NOT NULL AND COALESCE(' . $prefix . "p2.pl_lati, '') = '' OR " .
155            $prefix . 'p3.pl_place IS NOT NULL AND COALESCE(' . $prefix . "p3.pl_lati, '') = '' OR " .
156            $prefix . 'p4.pl_place IS NOT NULL AND COALESCE(' . $prefix . "p4.pl_lati, '') = '' OR " .
157            $prefix . 'p5.pl_place IS NOT NULL AND COALESCE(' . $prefix . "p5.pl_lati, '') = '' OR " .
158            $prefix . 'p6.pl_place IS NOT NULL AND COALESCE(' . $prefix . "p6.pl_lati, '') = '' OR " .
159            $prefix . 'p7.pl_place IS NOT NULL AND COALESCE(' . $prefix . "p7.pl_lati, '') = '' OR " .
160            $prefix . 'p8.pl_place IS NOT NULL AND COALESCE(' . $prefix . "p8.pl_lati, '') = '' OR " .
161            $prefix . 'p9.pl_place IS NOT NULL AND COALESCE(' . $prefix . "p9.pl_lati, '') = ''";
162
163        return DB::table('placelocation AS p0')
164            ->leftJoin('placelocation AS p1', 'p1.pl_parent_id', '=', 'p0.pl_id')
165            ->leftJoin('placelocation AS p2', 'p2.pl_parent_id', '=', 'p1.pl_id')
166            ->leftJoin('placelocation AS p3', 'p3.pl_parent_id', '=', 'p2.pl_id')
167            ->leftJoin('placelocation AS p4', 'p4.pl_parent_id', '=', 'p3.pl_id')
168            ->leftJoin('placelocation AS p5', 'p5.pl_parent_id', '=', 'p4.pl_id')
169            ->leftJoin('placelocation AS p6', 'p6.pl_parent_id', '=', 'p5.pl_id')
170            ->leftJoin('placelocation AS p7', 'p7.pl_parent_id', '=', 'p6.pl_id')
171            ->leftJoin('placelocation AS p8', 'p8.pl_parent_id', '=', 'p7.pl_id')
172            ->leftJoin('placelocation AS p9', 'p9.pl_parent_id', '=', 'p8.pl_id')
173            ->where('p0.pl_parent_id', '=', $parent_id)
174            ->select([new Expression('COUNT(*) AS child_count'), new Expression('SUM(' . $expression . ') AS no_coord')])
175            ->first();
176    }
177}
178