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