xref: /webtrees/resources/views/admin/location-edit.phtml (revision 5062b1ca554e9384a3865a8ed7fa201483b30720)
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="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        const provider  = <?= json_encode($provider) ?>;
98        const add_place = <?= json_encode($location->id() === null) ?>;
99
100        let map = null;
101        // map components
102
103        // postcss_image_inliner breaks the autodetection of image paths.
104        L.Icon.Default.imagePath = <?= json_encode(asset('css/images/')) ?>;
105
106        // draggable marker
107        let marker = L.marker(<?= json_encode($marker_position) ?>, {
108            draggable: true,
109        })
110            .on('dragend', function () {
111                let coords = marker.getLatLng();
112                map.panTo(coords);
113                $('#new_place_lati').val(Number(coords.lat).toFixed(5));
114                $('#new_place_long').val(Number(coords.lng).toFixed(5));
115            });
116
117        //reset map to initial state
118        let resetControl = L.Control.extend({
119            options: {
120                position: 'topleft'
121            },
122            onAdd: function (map) {
123                let container = L.DomUtil.create('div', 'leaflet-bar leaflet-control leaflet-control-custom');
124                container.onclick = function () {
125                    map.fitBounds(<?= json_encode($map_bounds) ?>, {padding: [50, 30]});
126                    marker.setLatLng(<?= json_encode([$location->latitude(), $location->longitude()]) ?>);
127                    $('form').trigger('reset');
128                    return false;
129                };
130                let reset = <?= json_encode(I18N::translate('Reset to initial map state')) ?>;
131                let anchor = L.DomUtil.create('a', 'leaflet-control-reset', container);
132                anchor.setAttribute('aria-label', reset);
133                anchor.href = '#';
134                anchor.title = reset;
135                anchor.role = 'button';
136                let image = L.DomUtil.create('i', 'fas fa-redo', anchor);
137                image.alt = reset;
138
139                return container;
140            }
141        });
142
143        // zoom control with localised text
144        let zoomCtl = new L.control.zoom({
145            zoomInTitle:  <?= json_encode(I18N::translate('Zoom in')) ?>,
146            zoomOutTitle: <?= json_encode(I18N::translate('Zoom out')) ?>,
147        });
148
149        // Geocoder (place lookup)
150        let geocoder = new L.Control.geocoder({
151            defaultMarkGeocode: false,
152            expand: 'click',
153            showResultIcons: true,
154            query: <?= json_encode($location->locationName()) ?>,
155            placeholder: <?= json_encode(I18N::translate('Place')) ?>,
156            errorMessage: <?= json_encode(I18N::translate('Nothing found.')) ?>,
157            iconLabel: <?= json_encode(I18N::translate('Search')) ?>
158        })
159            .on('markgeocode', function (result) {
160                let coords = result.geocode.center;
161                let place = result.geocode.name.split(',', 1).toString();
162                marker.setLatLng(coords);
163                map.panTo(coords);
164                if (add_place) {
165                    $('#new_place_name').val(place);
166                }
167                $('#new_place_lati').val(Number(coords.lat).toFixed(5));
168                $('#new_place_long').val(Number(coords.lng).toFixed(5));
169            });
170
171        /**
172         *
173         * @private
174         */
175        $(function () {
176            // geocoder button tooltip
177            $('.leaflet-control-geocoder-icon')
178                .attr('title', <?= json_encode(I18N::translate('Search')) ?>);
179
180            $('.editable').on('change', function () {
181                let lat = $('#new_place_lati').val();
182                let lng = $('#new_place_long').val();
183                marker.setLatLng([lat, lng]);
184                map.panTo([lat, lng]);
185            });
186        });
187
188        // Create the map with all controls and layers
189        map = L.map('osm-map', {
190            minZoom: minZoom, // maxZoom set by leaflet-providers.js
191            zoomControl: false,   // remove default
192        })
193            .addControl(new resetControl())
194            .addControl(zoomCtl)
195            .addControl(geocoder)
196            .addLayer(marker)
197            .addLayer(L.tileLayer(provider.url, provider.options))
198            .fitBounds(<?= json_encode($map_bounds) ?>, {padding: [50, 30]})
199            .on('zoomend', function () {
200              $('.editable').trigger('change');
201            });
202
203        return 'Leaflet map interface for webtrees-2';
204    })();
205</script>
206<?php View::endpush() ?>
207