Passed
Push — master ( 0e5063...114641 )
by Greg
08:56
created

MapDataList::handle()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 38
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 3
eloc 22
c 2
b 0
f 0
nc 3
nop 1
dl 0
loc 38
rs 9.568
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 Illuminate\Database\Capsule\Manager as DB;
28
use Illuminate\Database\Query\Expression;
29
use Illuminate\Support\Collection;
30
use Psr\Http\Message\ResponseInterface;
31
use Psr\Http\Message\ServerRequestInterface;
32
use Psr\Http\Server\RequestHandlerInterface;
33
use stdClass;
34
35
use function array_reverse;
36
use function redirect;
37
use function route;
38
39
/**
40
 * Show a list of map data.
41
 */
42
class 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