Completed
Pull Request — master (#3652)
by
unknown
07:01
created

LocationController::exportGeoJSON()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 25
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 16
nc 2
nop 2
dl 0
loc 25
rs 9.7333
c 1
b 0
f 0
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\Controllers\Admin;
21
22
use Exception;
23
use Fisharebest\Webtrees\FlashMessages;
24
use Fisharebest\Webtrees\Gedcom;
25
use Fisharebest\Webtrees\Http\RequestHandlers\ControlPanel;
26
use Fisharebest\Webtrees\Http\RequestHandlers\MapDataList;
27
use Fisharebest\Webtrees\I18N;
28
use Fisharebest\Webtrees\PlaceLocation;
29
use Fisharebest\Webtrees\Registry;
30
use Fisharebest\Webtrees\Services\GedcomService;
31
use Illuminate\Database\Capsule\Manager as DB;
32
use Illuminate\Database\Eloquent\Collection;
33
use Illuminate\Database\Query\Expression;
34
use Psr\Http\Message\ResponseInterface;
35
use Psr\Http\Message\ServerRequestInterface;
36
use Psr\Http\Message\UploadedFileInterface;
37
use RuntimeException;
38
use stdClass;
39
40
use function addcslashes;
41
use function array_filter;
42
use function array_merge;
43
use function array_pad;
44
use function array_reverse;
45
use function array_slice;
46
use function assert;
47
use function count;
48
use function e;
49
use function fclose;
50
use function fgetcsv;
51
use function fopen;
52
use function fputcsv;
53
use function implode;
54
use function is_numeric;
55
use function json_decode;
56
use function preg_replace;
57
use function redirect;
58
use function response;
59
use function rewind;
60
use function route;
61
use function str_replace;
62
use function stream_get_contents;
63
use function stripos;
64
use function substr_count;
65
66
use const UPLOAD_ERR_OK;
67
68
/**
69
 * Controller for maintaining geographic data.
70
 */
71
class LocationController extends AbstractAdminController
72
{
73
    // Location of files to import
74
    private const PLACES_FOLDER = 'places/';
75
76
    //Used when exporting csv file
77
    private const FIELD_DELIMITER = ';';
78
79
    /** @var GedcomService */
80
    private $gedcom_service;
81
82
    /**
83
     * Dependency injection.
84
     *
85
     * @param GedcomService $gedcom_service
86
     */
87
    public function __construct(GedcomService $gedcom_service)
88
    {
89
        $this->gedcom_service = $gedcom_service;
90
    }
91
92
    /**
93
     * @param int $id
94
     *
95
     * @return array<stdClass>
96
     */
97
    private function getHierarchy(int $id): array
98
    {
99
        $arr  = [];
100
        $fqpn = [];
101
102
        while ($id !== 0) {
103
            $row = DB::table('placelocation')
104
                ->where('pl_id', '=', $id)
105
                ->first();
106
107
            // For static analysis tools.
108
            assert($row instanceof stdClass);
109
110
            $fqpn[]    = $row->pl_place;
111
            $row->fqpn = implode(Gedcom::PLACE_SEPARATOR, $fqpn);
112
            $id        = (int) $row->pl_parent_id;
113
            $arr[]     = $row;
114
        }
115
116
        return array_reverse($arr);
117
    }
118
119
    /**
120
     * @param ServerRequestInterface $request
121
     *
122
     * @return ResponseInterface
123
     */
124
    public function mapDataEdit(ServerRequestInterface $request): ResponseInterface
125
    {
126
        $parent_id = (int) $request->getQueryParams()['parent_id'];
127
        $hierarchy = $this->getHierarchy($parent_id);
128
        $fqpn      = $hierarchy === [] ? '' : $hierarchy[0]->fqpn;
129
        $parent    = new PlaceLocation($fqpn);
130
131
        $place_id  = (int) $request->getQueryParams()['place_id'];
132
        $hierarchy = $this->getHierarchy($place_id);
133
        $fqpn      = $hierarchy === [] ? '' : $hierarchy[0]->fqpn;
134
        $location  = new PlaceLocation($fqpn);
135
136
        if ($location->id() !== 0) {
137
            $title = e($location->locationName());
138
        } else {
139
            // Add a place
140
            if ($parent_id === 0) {
141
                // We're at the global level so create a minimal
142
                // place for the page title and breadcrumbs
143
                $title     = I18N::translate('World');
144
                $hierarchy = [];
145
            } else {
146
                $hierarchy = $this->getHierarchy($parent_id);
147
                $tmp       = new PlaceLocation($hierarchy[0]->fqpn);
148
                $title     = e($tmp->locationName());
149
150
                if ($tmp->latitude() === 0.0 && $tmp->longitude() === 0.0) {
0 ignored issues
show
introduced by
The condition $tmp->latitude() === 0.0 is always false.
Loading history...
151
                    FlashMessages::addMessage(I18N::translate('%s (coordinates [0,0]) cannot have a subordinate place', $title), 'warning');
152
153
                    return redirect(route(MapDataList::class, ['parent_id' => 0]));
154
                }
155
            }
156
        }
157
158
        $breadcrumbs = [
159
            route(ControlPanel::class) => I18N::translate('Control panel'),
160
            route(MapDataList::class)  => I18N::translate('Geographic data'),
161
        ];
162
163
        foreach ($hierarchy as $row) {
164
            $breadcrumbs[route(MapDataList::class, ['parent_id' => $row->pl_id])] = e($row->pl_place);
165
        }
166
167
        if ($place_id === 0) {
168
            $title .= ' — ' . I18N::translate('Add');
169
            $breadcrumbs[] = I18N::translate('Add');
170
            $latitude      = null;
171
            $longitude     = null;
172
            $map_bounds    = $parent->boundingRectangle();
173
        } else {
174
            $title .= ' — ' . I18N::translate('Edit');
175
            $breadcrumbs[] = I18N::translate('Edit');
176
            $latitude      = $location->latitude();
177
            $longitude     = $location->longitude();
178
            $map_bounds    = $location->boundingRectangle();
179
        }
180
181
        // If the current co-ordinates are unknown, leave the input fields empty,
182
        // and show a marker in the middle of the map.
183
        if ($latitude === null || $longitude === null) {
184
            $marker_position = [
185
                ($map_bounds[0][0] + $map_bounds[1][0]) / 2.0,
186
                ($map_bounds[0][1] + $map_bounds[1][1]) / 2.0,
187
            ];
188
        } else {
189
            $marker_position = [$latitude, $longitude];
190
        }
191
192
        return $this->viewResponse('admin/location-edit', [
193
            'breadcrumbs'     => $breadcrumbs,
194
            'title'           => $title,
195
            'location'        => $location,
196
            'latitude'        => $latitude,
197
            'longitude'       => $longitude,
198
            'map_bounds'      => $map_bounds,
199
            'marker_position' => $marker_position,
200
            'parent'          => $parent,
201
            'level'           => $parent_id,
202
            'provider'        => [
203
                'url'     => 'https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png',
204
                'options' => [
205
                    'attribution' => '<a href="https://www.openstreetmap.org/copyright">&copy; OpenStreetMap</a> contributors',
206
                    'max_zoom'    => 19
207
                ]
208
            ],
209
        ]);
210
    }
211
212
    /**
213
     * @param ServerRequestInterface $request
214
     *
215
     * @return ResponseInterface
216
     */
217
    public function mapDataSave(ServerRequestInterface $request): ResponseInterface
218
    {
219
        $params = (array) $request->getParsedBody();
220
221
        $parent_id = (int) $request->getQueryParams()['parent_id'];
222
        $place_id  = (int) $request->getQueryParams()['place_id'];
223
        $lat       = $this->gedcom_service->writeLatitude((float) $params['new_place_lati']);
224
        $lng       = $this->gedcom_service->writeLongitude((float) $params['new_place_long']);
225
        $hierarchy = $this->getHierarchy($parent_id);
226
        $level     = count($hierarchy);
227
        $icon      = $params['icon'] ?: null;
228
        $zoom      = (int) $params['new_zoom_factor'];
229
230
        if ($parent_id > 0 && $lat === 'N0' && $lng === 'E0') {
231
            FlashMessages::addMessage(I18N::translate('Location [0,0] cannot be subordinate to another place'), 'warning');
232
        } else {
233
            if ($place_id === 0) {
234
                $place_id = 1 + (int) DB::table('placelocation')->max('pl_id');
235
236
                DB::table('placelocation')->insert([
237
                    'pl_id'        => $place_id,
238
                    'pl_parent_id' => $parent_id,
239
                    'pl_level'     => $level,
240
                    'pl_place'     => mb_substr($params['new_place_name'], 0, 120),
241
                    'pl_lati'      => $lat,
242
                    'pl_long'      => $lng,
243
                    'pl_zoom'      => $zoom,
244
                    'pl_icon'      => $icon,
245
                ]);
246
            } else {
247
                DB::table('placelocation')
248
                ->where('pl_id', '=', $place_id)
249
                    ->update([
250
                        'pl_place' => mb_substr($params['new_place_name'], 0, 120),
251
                        'pl_lati'  => $lat,
252
                        'pl_long'  => $lng,
253
                        'pl_zoom'  => $zoom,
254
                        'pl_icon'  => $icon,
255
                    ]);
256
            }
257
258
            FlashMessages::addMessage(
259
                I18N::translate(
260
                    'The details for “%s” have been updated.',
261
                    e($params['new_place_name'])
262
                ),
263
                'success'
264
            );
265
        }
266
        $url = route(MapDataList::class, ['parent_id' => $parent_id]);
267
268
        return redirect($url);
269
    }
270
271
    /**
272
     * @param ServerRequestInterface $request
273
     *
274
     * @return ResponseInterface
275
     */
276
    public function exportLocations(ServerRequestInterface $request): ResponseInterface
277
    {
278
        $parent_id = (int) $request->getQueryParams()['parent_id'];
279
        $format    = $request->getQueryParams()['format'];
280
        $hierarchy = $this->getHierarchy($parent_id);
281
282
        // Create the file name
283
        // $hierarchy[0] always holds the full placename
284
        $place_name = $hierarchy === [] ? 'Global' : $hierarchy[0]->fqpn;
285
        $place_name = str_replace(Gedcom::PLACE_SEPARATOR, '-', $place_name);
286
        $filename   = addcslashes('Places-' . preg_replace('/[^a-zA-Z0-9.-]/', '', $place_name), '"');
287
288
        // Fill in the place names for the starting conditions
289
        $startfqpn = [];
290
        foreach ($hierarchy as $record) {
291
            $startfqpn[] = $record->pl_place;
292
        }
293
294
        // Generate an array containing the data to output.
295
        $places = [];
296
        $this->buildExport($parent_id, $startfqpn, $places);
297
298
        if ($format === 'csv') {
299
            return $this->exportCSV($filename . '.csv', $places);
300
        }
301
302
        return $this->exportGeoJSON($filename . '.geojson', $places);
303
    }
304
305
    /**
306
     * @param int             $parent_id
307
     * @param array<string>   $fqpn
308
     * @param array<stdClass> $places
309
     *
310
     * @return void
311
     * @throws Exception
312
     */
313
    private function buildExport(int $parent_id, array $fqpn, array &$places): void
314
    {
315
        // Data for the next level.
316
        $rows = DB::table('placelocation')
317
            ->where('pl_parent_id', '=', $parent_id)
318
            ->whereNotNull('pl_lati')
319
            ->whereNotNull('pl_long')
320
            ->orderBy(new Expression('pl_place /*! COLLATE ' . I18N::collation() . ' */'))
321
            ->get()
322
            ->map(static function (stdClass $x) use ($fqpn) {
323
                $x->fqpn    = array_merge($fqpn, [$x->pl_place]);
324
                $x->pl_zoom = (int) $x->pl_zoom;
325
326
                return $x;
327
            });
328
329
        foreach ($rows as $row) {
330
            $places[] = $row;
331
            $this->buildExport((int) $row->pl_id, $row->fqpn, $places);
332
        }
333
    }
334
335
    /**
336
     * @param string     $filename
337
     * @param string[][] $places
338
     *
339
     * @return ResponseInterface
340
     */
341
    private function exportCSV(string $filename, array $places): ResponseInterface
342
    {
343
        $resource = fopen('php://temp', 'wb+');
344
345
        if ($resource === false) {
346
            throw new RuntimeException('Failed to create temporary stream');
347
        }
348
349
        $max_level = array_reduce($places, function ($carry, $item) {
350
            return max($carry, count($item->fqpn));
351
        });
352
353
        $places = array_map(static function (stdClass $place) use ($max_level): array {
354
            return array_merge(
355
                [count($place->fqpn) - 1],
356
                array_pad($place->fqpn, $max_level, ''),
357
                [$place->pl_long],
358
                [$place->pl_lati],
359
                [$place->pl_zoom],
360
                [$place->pl_icon]
361
            );
362
        }, $places);
363
364
        // Create the header line for the output file (always English)
365
        $header = [
366
            'Level',
367
        ];
368
369
        for ($i = 0; $i < $max_level; $i++) {
370
            $header[] = 'Place' . $i;
371
        }
372
373
        $header[] = 'Longitude';
374
        $header[] = 'Latitude';
375
        $header[] = 'Zoom';
376
        $header[] = 'Icon';
377
378
        fputcsv($resource, $header, self::FIELD_DELIMITER);
379
380
        foreach ($places as $place) {
381
            fputcsv($resource, $place, self::FIELD_DELIMITER);
382
        }
383
384
        rewind($resource);
385
386
        return response(stream_get_contents($resource))
387
            ->withHeader('Content-Type', 'text/csv; charset=utf-8')
388
            ->withHeader('Content-Disposition', 'attachment; filename="' . $filename . '"');
389
    }
390
391
    /**
392
     * @param string $filename
393
     * @param array  $rows
394
     *
395
     * @return ResponseInterface
396
     */
397
    private function exportGeoJSON(string $filename, array $rows): ResponseInterface
398
    {
399
        $geojson = [
400
            'type'     => 'FeatureCollection',
401
            'features' => [],
402
        ];
403
        foreach ($rows as $place) {
404
            $geojson['features'][] = [
405
                'type'       => 'Feature',
406
                'geometry'   => [
407
                    'type'        => 'Point',
408
                    'coordinates' => [
409
                        $this->gedcom_service->readLongitude($place->pl_long),
410
                        $this->gedcom_service->readLatitude($place->pl_lati),
411
                    ],
412
                ],
413
                'properties' => [
414
                    'name' => implode(GEDCOM::PLACE_SEPARATOR, array_reverse($place->fqpn)),
415
                ],
416
            ];
417
        }
418
419
        return response($geojson)
420
            ->withHeader('Content-Type', 'application/vnd.geo+json')
421
            ->withHeader('Content-Disposition', 'attachment; filename="' . $filename . '"');
422
    }
423
424
    /**
425
     * @param ServerRequestInterface $request
426
     *
427
     * @return ResponseInterface
428
     */
429
    public function importLocations(ServerRequestInterface $request): ResponseInterface
430
    {
431
        $data_filesystem      = Registry::filesystem()->data();
432
        $data_filesystem_name = Registry::filesystem()->dataName();
433
434
        $parent_id = (int) $request->getQueryParams()['parent_id'];
435
436
        $files = Collection::make($data_filesystem->listContents('places'))
437
            ->filter(static function (array $metadata): bool {
438
                $extension = strtolower($metadata['extension'] ?? '');
439
440
                return $extension === 'csv' || $extension === 'geojson';
441
            })
442
            ->map(static function (array $metadata): string {
443
                return $metadata['basename'];
444
            })
445
            ->sort();
446
447
        return $this->viewResponse('admin/map-import-form', [
448
            'place_folder' => $data_filesystem_name . self::PLACES_FOLDER,
449
            'title'        => I18N::translate('Import geographic data'),
450
            'parent_id'    => $parent_id,
451
            'files'        => $files,
452
        ]);
453
    }
454
455
    /**
456
     * This function assumes the input file layout is
457
     * level followed by a variable number of placename fields
458
     * followed by Longitude, Latitude, Zoom & Icon
459
     *
460
     * @param ServerRequestInterface $request
461
     *
462
     * @return ResponseInterface
463
     * @throws Exception
464
     */
465
    public function importLocationsAction(ServerRequestInterface $request): ResponseInterface
466
    {
467
        $data_filesystem = Registry::filesystem()->data();
468
469
        $params = (array) $request->getParsedBody();
470
        $url    = route(MapDataList::class, ['parent_id' => 0]);
471
472
        $serverfile     = $params['serverfile'] ?? '';
473
        $options        = $params['import-options'] ?? '';
474
        $clear_database = (bool) ($params['cleardatabase'] ?? false);
475
        $local_file     = $request->getUploadedFiles()['localfile'] ?? null;
476
477
        $fp = false;
478
479
        if ($serverfile !== '' && $data_filesystem->has(self::PLACES_FOLDER . $serverfile)) {
480
            // first choice is file on server
481
            $fp = $data_filesystem->readStream(self::PLACES_FOLDER . $serverfile);
482
        } elseif ($local_file instanceof UploadedFileInterface && $local_file->getError() === UPLOAD_ERR_OK) {
483
            // 2nd choice is local file
484
            $fp = $local_file->getStream()->detach();
485
        }
486
487
        if ($fp === false) {
488
            return redirect($url);
489
        }
490
491
        $string = stream_get_contents($fp);
492
493
        $places = [];
494
495
        // Check the file type
496
        if (stripos($string, 'FeatureCollection') !== false) {
497
            $input_array = json_decode($string, false);
498
499
            foreach ($input_array->features as $feature) {
500
                $places[] = [
501
                    'pl_level' => $feature->properties->level ?? substr_count($feature->properties->name, ','),
502
                    'pl_long'  => $feature->geometry->coordinates[0],
503
                    'pl_lati'  => $feature->geometry->coordinates[1],
504
                    'pl_zoom'  => $feature->properties->zoom ?? null,
505
                    'pl_icon'  => $feature->properties->icon ?? null,
506
                    'fqpn'     => $feature->properties->name,
507
                ];
508
            }
509
        } else {
510
            rewind($fp);
511
            while (($row = fgetcsv($fp, 0, self::FIELD_DELIMITER)) !== false) {
512
                // Skip the header
513
                if (!is_numeric($row[0])) {
514
                    continue;
515
                }
516
517
                $level = (int) $row[0];
518
                $count = count($row);
519
520
                // convert separate place fields into a comma separated placename
521
                $fqdn = implode(Gedcom::PLACE_SEPARATOR, array_reverse(array_slice($row, 1, 1 + $level)));
522
523
                $places[] = [
524
                    'pl_level' => $level,
525
                    'pl_long'  => (float) strtr($row[$count - 4], ['E' => '', 'W' => '-', ',' => '.']),
526
                    'pl_lati'  => (float) strtr($row[$count - 3], ['N' => '', 'S' => '-', ',' => '.']),
527
                    'pl_zoom'  => $row[$count - 2],
528
                    'pl_icon'  => $row[$count - 1],
529
                    'fqpn'     => $fqdn,
530
                ];
531
            }
532
        }
533
534
        fclose($fp);
535
536
        if ($clear_database) {
537
            DB::table('placelocation')->delete();
538
        }
539
540
        $added   = 0;
541
        $updated = 0;
542
543
        // Remove places with invalid coordinates
544
        $places = array_filter($places, function ($item) {
545
            return $item['pl_level'] === 0 || $item['pl_long'] !== 0.0 || $item['pl_lati'] !== 0.0;
546
        });
547
548
        foreach ($places as $place) {
549
            $location = new PlaceLocation($place['fqpn']);
550
            $exists   = $location->exists();
551
552
            // Only update existing records
553
            if ($options === 'update' && !$exists) {
554
                continue;
555
            }
556
557
            // Only add new records
558
            if ($options === 'add' && $exists) {
559
                continue;
560
            }
561
562
            if (!$exists) {
563
                $added++;
564
            } else {
565
                $updated++;
566
            }
567
568
            DB::table('placelocation')
569
                ->where('pl_id', '=', $location->id())
570
                ->update([
571
                    'pl_lati' => $this->gedcom_service->writeLatitude($place['pl_lati']),
572
                    'pl_long' => $this->gedcom_service->writeLongitude($place['pl_long']),
573
                    'pl_zoom' => $place['pl_zoom'] ?: null,
574
                    'pl_icon' => $place['pl_icon'] ?: null,
575
                ]);
576
        }
577
        FlashMessages::addMessage(
578
            I18N::translate('locations updated: %s, locations added: %s', I18N::number($updated), I18N::number($added)),
579
            'info'
580
        );
581
582
        return redirect($url);
583
    }
584
}
585