xref: /webtrees/resources/views/admin/location-edit.phtml (revision 842a873c0b92c28d4af3fa9db0e9156057005569)
1<?php
2
3use Fisharebest\Webtrees\Http\RequestHandlers\MapDataList;
4use Fisharebest\Webtrees\Http\RequestHandlers\MapDataSave;
5use Fisharebest\Webtrees\I18N;
6use Fisharebest\Webtrees\PlaceLocation;
7use Fisharebest\Webtrees\View;
8
9/**
10 * @var array<string,string> $breadcrumbs
11 * @var string               $latitude
12 * @var string               $longitude
13 * @var PlaceLocation        $location
14 * @var array<array<float>>  $map_bounds
15 * @var array<float>         $marker_position
16 * @var PlaceLocation        $parent
17 * @var mixed                $provider
18 * @var string               $title
19 */
20
21?>
22
23<?= view('components/breadcrumbs', ['links' => $breadcrumbs]) ?>
24
25<h1><?= $title ?></h1>
26
27<div class="form-group row">
28    <div class="col-sm-12">
29        <div id="osm-map" class="wt-ajax-load col-sm-12 osm-admin-map" dir="ltr"></div>
30    </div>
31</div>
32
33<form method="post" action="<?= e(route(MapDataSave::class)) ?>">
34    <?= csrf_field() ?>
35    <input type="hidden" name="place_id" value="<?= e($location->id()) ?>">
36
37    <div class="form-group row">
38        <label class="col-form-label col-sm-1" for="new_place_name">
39            <?= I18N::translate('Place') ?>
40        </label>
41        <div class="col-sm-3">
42            <input type="text" id="new_place_name" name="new_place_name" value="<?= e($location->locationName()) ?>" class="form-control" required maxlength="120" pattern="[^,]+" dir="auto">
43        </div>
44    </div>
45
46    <div class="form-group row">
47        <label class="col-form-label col-sm-1" for="new_place_lati">
48            <?= I18N::translate('Latitude') ?>
49        </label>
50        <div class="col-sm-3">
51            <div class="input-group">
52                <input type="text" dir="ltr" id="new_place_lati" class="editable form-control" name="new_place_lati" placeholder="<?= I18N::translate('degrees') ?>" value="<?= e($latitude) ?>">
53            </div>
54        </div>
55    </div>
56
57    <div class="form-group row">
58        <label class="col-form-label col-sm-1" for="new_place_long">
59            <?= I18N::translate('Longitude') ?>
60        </label>
61        <div class="col-sm-3">
62            <div class="input-group">
63                <input type="text" dir="ltr" id="new_place_long" class="editable form-control" name="new_place_long" placeholder="<?= I18N::translate('degrees') ?>" value="<?= e($longitude) ?>">
64            </div>
65        </div>
66    </div>
67
68    <div class="form-group row">
69        <div class="col-sm-10 offset-sm-1">
70            <button class="btn btn-primary" type="submit">
71                <?= /* I18N: A button label. */
72                I18N::translate('save')
73                ?>
74            </button>
75            <a class="btn btn-secondary" href="<?= e(route(MapDataList::class, ['parent_id' => $parent->id()])) ?>">
76                <?= I18N::translate('cancel') ?>
77            </a>
78        </div>
79    </div>
80</form>
81
82<?php View::push('styles') ?>
83<style>
84    .osm-admin-map {
85        height: 55vh;
86        border: 1px solid darkGrey
87    }
88</style>
89<?php View::endpush() ?>
90
91<?php View::push('javascript') ?>
92<script>
93    'use strict';
94
95    window.WT_OSM_ADMIN = (function () {
96        const minZoom = 2;
97
98        let provider = <?= json_encode($provider) ?>;
99        let map = null;
100        let add_place = <?= json_encode($location->id() === 0) ?>;
101
102        // map components
103
104        // postcss_image_inliner breaks the autodetection of image paths.
105        L.Icon.Default.imagePath = <?= json_encode(asset('css/images/')) ?>;
106
107        // draggable marker
108        let marker = L.marker(<?= json_encode($marker_position) ?>, {
109            draggable: true,
110        })
111            .on('dragend', function () {
112                let coords = marker.getLatLng();
113                map.panTo(coords);
114                $('#new_place_lati').val(Number(coords.lat).toFixed(5));
115                $('#new_place_long').val(Number(coords.lng).toFixed(5));
116                $('#new_zoom_factor').val(Number(map.getZoom()));
117            });
118
119        //reset map to initial state
120        let resetControl = L.Control.extend({
121            options: {
122                position: 'topleft'
123            },
124            onAdd: function (map) {
125                let container = L.DomUtil.create('div', 'leaflet-bar leaflet-control leaflet-control-custom');
126                container.onclick = function () {
127                    map.fitBounds(<?= json_encode($map_bounds) ?>, {padding: [50, 30]});
128                    marker.setLatLng(<?= json_encode([$location->latitude(), $location->longitude()]) ?>);
129                    $('form').trigger('reset');
130                    return false;
131                };
132                let reset = <?= json_encode(I18N::translate('Reset to initial map state')) ?>;
133                let anchor = L.DomUtil.create('a', 'leaflet-control-reset', container);
134                anchor.setAttribute('aria-label', reset);
135                anchor.href = '#';
136                anchor.title = reset;
137                anchor.role = 'button';
138                let image = L.DomUtil.create('i', 'fas fa-redo', anchor);
139                image.alt = reset;
140
141                return container;
142            }
143        });
144
145        // zoom control with localised text
146        let zoomCtl = new L.control.zoom({
147            zoomInTitle:  <?= json_encode(I18N::translate('Zoom in')) ?>,
148            zoomOutTitle: <?= json_encode(I18N::translate('Zoom out')) ?>,
149        });
150
151        // Geocoder (place lookup)
152        let geocoder = new L.Control.geocoder({
153            defaultMarkGeocode: false,
154            expand: 'click',
155            showResultIcons: true,
156            query: <?= json_encode($location->locationName()) ?>,
157            placeholder: <?= json_encode(I18N::translate('Place')) ?>,
158            errorMessage: <?= json_encode(I18N::translate('Nothing found.')) ?>,
159            iconLabel: <?= json_encode(I18N::translate('Search')) ?>
160        })
161            .on('markgeocode', function (result) {
162                let coords = result.geocode.center;
163                let place = result.geocode.name.split(',', 1);
164                marker.setLatLng(coords);
165                map.panTo(coords);
166                if (add_place) {
167                    $('#new_place_name').val(place.shift());
168                }
169                $('#new_place_lati').val(Number(coords.lat).toFixed(5));
170                $('#new_place_long').val(Number(coords.lng).toFixed(5));
171                $('#new_zoom_factor').val(Number(map.getZoom()));
172            });
173
174        /**
175         *
176         * @private
177         */
178        $(function () {
179            // geocoder button tooltip
180            $('.leaflet-control-geocoder-icon')
181                .attr('title', <?= json_encode(I18N::translate('Search')) ?>);
182
183            $('.editable').on('change', function () {
184                let lat = $('#new_place_lati').val();
185                let lng = $('#new_place_long').val();
186                marker.setLatLng([lat, lng]);
187                map.panTo([lat, lng]);
188            });
189        });
190
191        // Create the map with all controls and layers
192        map = L.map('osm-map', {
193            minZoom: minZoom, // maxZoom set by leaflet-providers.js
194            zoomControl: false,   // remove default
195        })
196            .addControl(new resetControl())
197            .addControl(zoomCtl)
198            .addControl(geocoder)
199            .addLayer(marker)
200            .addLayer(L.tileLayer(provider.url, provider.options))
201            .fitBounds(<?= json_encode($map_bounds) ?>, {padding: [50, 30]})
202            .on('zoomend', function () {
203                $('#new_zoom_factor').val(map.getZoom());
204            });
205
206        return 'Leaflet map interface for webtrees-2';
207    })();
208</script>
209<?php View::endpush() ?>
210