xref: /webtrees/resources/views/admin/location-edit.phtml (revision 2a93faa69b98fa9892dbcc9aa3a665b3e62e93f1)
1d70512abSGreg Roach<?php
2d70512abSGreg Roach
394e35917SGreg Roachuse Fisharebest\Webtrees\Http\RequestHandlers\MapDataList;
490949315SGreg Roachuse Fisharebest\Webtrees\Http\RequestHandlers\MapDataSave;
5d70512abSGreg Roachuse Fisharebest\Webtrees\I18N;
69a9dfcf7SGreg Roachuse Fisharebest\Webtrees\PlaceLocation;
7d70512abSGreg Roachuse Fisharebest\Webtrees\View;
8d70512abSGreg Roach
99a9dfcf7SGreg Roach/**
109a9dfcf7SGreg Roach * @var array<string,string> $breadcrumbs
119a9dfcf7SGreg Roach * @var string               $latitude
129a9dfcf7SGreg Roach * @var string               $longitude
139a9dfcf7SGreg Roach * @var PlaceLocation        $location
149a9dfcf7SGreg Roach * @var array<array<float>>  $map_bounds
1590949315SGreg Roach * @var array<float>         $marker_position
169a9dfcf7SGreg Roach * @var PlaceLocation        $parent
179a9dfcf7SGreg Roach * @var mixed                $provider
189a9dfcf7SGreg Roach * @var string               $title
199a9dfcf7SGreg Roach */
209a9dfcf7SGreg Roach
21d70512abSGreg Roach?>
22dd6b2bfcSGreg Roach
23dd6b2bfcSGreg Roach<?= view('components/breadcrumbs', ['links' => $breadcrumbs]) ?>
24dd6b2bfcSGreg Roach
25dd6b2bfcSGreg Roach<h1><?= $title ?></h1>
26dd6b2bfcSGreg Roach
27dd6b2bfcSGreg Roach<div class="form-group row">
28e340fe65SGreg Roach    <div class="col-sm-12">
290b4092edSGreg Roach        <div id="osm-map" class="wt-ajax-load col-sm-12 osm-admin-map" dir="ltr"></div>
30dd6b2bfcSGreg Roach    </div>
31dd6b2bfcSGreg Roach</div>
32dd6b2bfcSGreg Roach
33*2a93faa6SGreg Roach<form method="post" action="<?= e(route(MapDataSave::class)) ?>">
34dd6b2bfcSGreg Roach    <?= csrf_field() ?>
35*2a93faa6SGreg Roach    <input type="hidden" name="parent_id" value="<?= e($parent->id()) ?>">
36*2a93faa6SGreg Roach    <input type="hidden" name="place_id" value="<?= e($location->id()) ?>">
37dd6b2bfcSGreg Roach
38dd6b2bfcSGreg Roach    <div class="form-group row">
39dd6b2bfcSGreg Roach        <label class="col-form-label col-sm-1" for="new_place_name">
40dd6b2bfcSGreg Roach            <?= I18N::translate('Place') ?>
41dd6b2bfcSGreg Roach        </label>
42e340fe65SGreg Roach        <div class="col-sm-3">
437dca5265SGreg Roach            <input type="text" id="new_place_name" name="new_place_name" value="<?= e($location->locationName()) ?>" class="form-control" required="required" maxlength="120" pattern="[^,]+" dir="auto">
44dd6b2bfcSGreg Roach        </div>
45dd6b2bfcSGreg Roach    </div>
46dd6b2bfcSGreg Roach
47dd6b2bfcSGreg Roach    <div class="form-group row">
4856a34df1SGreg Roach        <label class="col-form-label col-sm-1" for="new_place_lati">
49dd6b2bfcSGreg Roach            <?= I18N::translate('Latitude') ?>
50dd6b2bfcSGreg Roach        </label>
51dd6b2bfcSGreg Roach        <div class="col-sm-3">
52dd6b2bfcSGreg Roach            <div class="input-group">
5390949315SGreg Roach                <input type="text" dir="ltr" id="new_place_lati" class="editable form-control" name="new_place_lati" placeholder="<?= I18N::translate('degrees') ?>" value="<?= e($latitude) ?>">
54dd6b2bfcSGreg Roach            </div>
55dd6b2bfcSGreg Roach        </div>
56e340fe65SGreg Roach    </div>
57dd6b2bfcSGreg Roach
58e340fe65SGreg Roach    <div class="form-group row">
5956a34df1SGreg Roach        <label class="col-form-label col-sm-1" for="new_place_long">
60dd6b2bfcSGreg Roach            <?= I18N::translate('Longitude') ?>
61dd6b2bfcSGreg Roach        </label>
62dd6b2bfcSGreg Roach        <div class="col-sm-3">
63dd6b2bfcSGreg Roach            <div class="input-group">
6490949315SGreg Roach                <input type="text" dir="ltr" id="new_place_long" class="editable form-control" name="new_place_long" placeholder="<?= I18N::translate('degrees') ?>" value="<?= e($longitude) ?>">
65dd6b2bfcSGreg Roach            </div>
66dd6b2bfcSGreg Roach        </div>
67dd6b2bfcSGreg Roach    </div>
68dd6b2bfcSGreg Roach
69dd6b2bfcSGreg Roach    <div class="form-group row">
70dd6b2bfcSGreg Roach        <div class="col-sm-10 offset-sm-1">
71dd6b2bfcSGreg Roach            <button class="btn btn-primary" type="submit">
72dd6b2bfcSGreg Roach                <?= /* I18N: A button label. */
73dd6b2bfcSGreg Roach                I18N::translate('save')
74dd6b2bfcSGreg Roach                ?>
75dd6b2bfcSGreg Roach            </button>
7694e35917SGreg Roach            <a class="btn btn-secondary" href="<?= e(route(MapDataList::class, ['parent_id' => $parent->id()])) ?>">
77dd6b2bfcSGreg Roach                <?= I18N::translate('cancel') ?>
78dd6b2bfcSGreg Roach            </a>
79dd6b2bfcSGreg Roach        </div>
80dd6b2bfcSGreg Roach    </div>
81dd6b2bfcSGreg Roach</form>
82dd6b2bfcSGreg Roach
83dd6b2bfcSGreg Roach<?php View::push('styles') ?>
84dd6b2bfcSGreg Roach<style>
85dd6b2bfcSGreg Roach    .osm-admin-map {
86dd6b2bfcSGreg Roach        height: 55vh;
87dd6b2bfcSGreg Roach        border: 1px solid darkGrey
88dd6b2bfcSGreg Roach    }
89dd6b2bfcSGreg Roach</style>
90dd6b2bfcSGreg Roach<?php View::endpush() ?>
91dd6b2bfcSGreg Roach
92dd6b2bfcSGreg Roach<?php View::push('javascript') ?>
9374b9ba3fSGreg Roach<script>
94caa53803SDavid Drury    'use strict';
95dd6b2bfcSGreg Roach
96dd6b2bfcSGreg Roach    window.WT_OSM_ADMIN = (function () {
97caa53803SDavid Drury        const minZoom   = 2;
98164ab4fbSDavid Drury        const provider  = <?= json_encode($provider) ?>;
99164ab4fbSDavid Drury        const add_place = <?= json_encode($location->id() === null) ?>;
1002cbb0620SDavid Drury
101dd6b2bfcSGreg Roach        let map = null;
102caa53803SDavid Drury        // map components
103dd6b2bfcSGreg Roach
10475e7614aSGreg Roach        // postcss_image_inliner breaks the autodetection of image paths.
10575e7614aSGreg Roach        L.Icon.Default.imagePath = <?= json_encode(asset('css/images/')) ?>;
10675e7614aSGreg Roach
107caa53803SDavid Drury        // draggable marker
1089a9dfcf7SGreg Roach        let marker = L.marker(<?= json_encode($marker_position) ?>, {
109caa53803SDavid Drury            draggable: true,
110caa53803SDavid Drury        })
1119a9dfcf7SGreg Roach            .on('dragend', function () {
112dd6b2bfcSGreg Roach                let coords = marker.getLatLng();
113dd6b2bfcSGreg Roach                map.panTo(coords);
11462be603fSGreg Roach                $('#new_place_lati').val(Number(coords.lat).toFixed(5));
11562be603fSGreg Roach                $('#new_place_long').val(Number(coords.lng).toFixed(5));
116dd6b2bfcSGreg Roach            });
117dd6b2bfcSGreg Roach
118caa53803SDavid Drury        //reset map to initial state
119caa53803SDavid Drury        let resetControl = L.Control.extend({
120caa53803SDavid Drury            options: {
121caa53803SDavid Drury                position: 'topleft'
122caa53803SDavid Drury            },
123caa53803SDavid Drury            onAdd: function (map) {
124caa53803SDavid Drury                let container = L.DomUtil.create('div', 'leaflet-bar leaflet-control leaflet-control-custom');
125caa53803SDavid Drury                container.onclick = function () {
1269a9dfcf7SGreg Roach                    map.fitBounds(<?= json_encode($map_bounds) ?>, {padding: [50, 30]});
127e340fe65SGreg Roach                    marker.setLatLng(<?= json_encode([$location->latitude(), $location->longitude()]) ?>);
128caa53803SDavid Drury                    $('form').trigger('reset');
129caa53803SDavid Drury                    return false;
130caa53803SDavid Drury                };
13147ca61d5SGreg Roach                let reset = <?= json_encode(I18N::translate('Reset to initial map state')) ?>;
132caa53803SDavid Drury                let anchor = L.DomUtil.create('a', 'leaflet-control-reset', container);
133caa53803SDavid Drury                anchor.setAttribute('aria-label', reset);
134caa53803SDavid Drury                anchor.href = '#';
135caa53803SDavid Drury                anchor.title = reset;
136caa53803SDavid Drury                anchor.role = 'button';
137caa53803SDavid Drury                let image = L.DomUtil.create('i', 'fas fa-redo', anchor);
138caa53803SDavid Drury                image.alt = reset;
139dd6b2bfcSGreg Roach
140caa53803SDavid Drury                return container;
141caa53803SDavid Drury            }
142caa53803SDavid Drury        });
143dd6b2bfcSGreg Roach
144caa53803SDavid Drury        // zoom control with localised text
145caa53803SDavid Drury        let zoomCtl = new L.control.zoom({
146caa53803SDavid Drury            zoomInTitle:  <?= json_encode(I18N::translate('Zoom in')) ?>,
147caa53803SDavid Drury            zoomOutTitle: <?= json_encode(I18N::translate('Zoom out')) ?>,
148caa53803SDavid Drury        });
149caa53803SDavid Drury
150caa53803SDavid Drury        // Geocoder (place lookup)
151caa53803SDavid Drury        let geocoder = new L.Control.geocoder({
152caa53803SDavid Drury            defaultMarkGeocode: false,
15322b076ddSGreg Roach            expand: 'click',
154caa53803SDavid Drury            showResultIcons: true,
155bb776aefSGreg Roach            query: <?= json_encode($location->locationName()) ?>,
156caa53803SDavid Drury            placeholder: <?= json_encode(I18N::translate('Place')) ?>,
157caa53803SDavid Drury            errorMessage: <?= json_encode(I18N::translate('Nothing found.')) ?>,
15847ca61d5SGreg Roach            iconLabel: <?= json_encode(I18N::translate('Search')) ?>
159caa53803SDavid Drury        })
160caa53803SDavid Drury            .on('markgeocode', function (result) {
161caa53803SDavid Drury                let coords = result.geocode.center;
162164ab4fbSDavid Drury                let place = result.geocode.name.split(',', 1).toString();
163caa53803SDavid Drury                marker.setLatLng(coords);
164caa53803SDavid Drury                map.panTo(coords);
16562be603fSGreg Roach                if (add_place) {
166164ab4fbSDavid Drury                    $('#new_place_name').val(place);
167caa53803SDavid Drury                }
16862be603fSGreg Roach                $('#new_place_lati').val(Number(coords.lat).toFixed(5));
16962be603fSGreg Roach                $('#new_place_long').val(Number(coords.lng).toFixed(5));
17062be603fSGreg Roach            });
171caa53803SDavid Drury
172caa53803SDavid Drury        /**
173caa53803SDavid Drury         *
174caa53803SDavid Drury         * @private
175caa53803SDavid Drury         */
176dd6b2bfcSGreg Roach        $(function () {
177caa53803SDavid Drury            // geocoder button tooltip
178caa53803SDavid Drury            $('.leaflet-control-geocoder-icon')
17947ca61d5SGreg Roach                .attr('title', <?= json_encode(I18N::translate('Search')) ?>);
180caa53803SDavid Drury
1819a9dfcf7SGreg Roach            $('.editable').on('change', function () {
182caa53803SDavid Drury                let lat = $('#new_place_lati').val();
183caa53803SDavid Drury                let lng = $('#new_place_long').val();
184dd6b2bfcSGreg Roach                marker.setLatLng([lat, lng]);
185dd6b2bfcSGreg Roach                map.panTo([lat, lng]);
186dd6b2bfcSGreg Roach            });
187dd6b2bfcSGreg Roach        });
188dd6b2bfcSGreg Roach
189caa53803SDavid Drury        // Create the map with all controls and layers
190caa53803SDavid Drury        map = L.map('osm-map', {
191caa53803SDavid Drury            minZoom: minZoom, // maxZoom set by leaflet-providers.js
192caa53803SDavid Drury            zoomControl: false,   // remove default
193caa53803SDavid Drury        })
194caa53803SDavid Drury            .addControl(new resetControl())
195caa53803SDavid Drury            .addControl(zoomCtl)
196caa53803SDavid Drury            .addControl(geocoder)
197caa53803SDavid Drury            .addLayer(marker)
198caa53803SDavid Drury            .addLayer(L.tileLayer(provider.url, provider.options))
1999a9dfcf7SGreg Roach            .fitBounds(<?= json_encode($map_bounds) ?>, {padding: [50, 30]})
2009a9dfcf7SGreg Roach            .on('zoomend', function () {
201d3a6a36aSGreg Roach              if (!map.getBounds().contains(marker.getLatLng())) {
202d3a6a36aSGreg Roach                map.panTo(marker.getLatLng());
203d3a6a36aSGreg Roach              }
204caa53803SDavid Drury            });
205dd6b2bfcSGreg Roach
206caa53803SDavid Drury        return 'Leaflet map interface for webtrees-2';
207dd6b2bfcSGreg Roach    })();
208dd6b2bfcSGreg Roach</script>
209dd6b2bfcSGreg Roach<?php View::endpush() ?>
210