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