xref: /webtrees/resources/views/admin/location-edit.phtml (revision 9bf16a00403d0095ce5486d5759e9ae53c03ef9a)
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 object               $leaflet_config
13 * @var string               $longitude
14 * @var PlaceLocation        $location
15 * @var array<array<float>>  $map_bounds
16 * @var array<float>         $marker_position
17 * @var PlaceLocation        $parent
18 * @var string               $title
19 */
20
21?>
22
23<?= view('components/breadcrumbs', ['links' => $breadcrumbs]) ?>
24
25<h1><?= $title ?></h1>
26
27<div id="wt-map" class="wt-ajax-load mb-3 border wt-location-edit-map" dir="ltr"></div>
28
29<form method="post" action="<?= e(route(MapDataSave::class)) ?>">
30    <?= csrf_field() ?>
31    <input type="hidden" name="parent_id" value="<?= e($parent->id()) ?>">
32    <input type="hidden" name="place_id" value="<?= e($location->id()) ?>">
33
34    <div class="row form-group mb-3">
35        <label class="col-form-label col-sm-1" for="new_place_name">
36            <?= I18N::translate('Place') ?>
37        </label>
38        <div class="col-sm-3">
39            <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">
40        </div>
41    </div>
42
43    <div class="row form-group mb-3">
44        <label class="col-form-label col-sm-1" for="new_place_lati">
45            <?= I18N::translate('Latitude') ?>
46        </label>
47        <div class="col-sm-3">
48            <div class="input-group">
49                <input type="text" dir="ltr" id="new_place_lati" class="editable form-control" name="new_place_lati" placeholder="<?= I18N::translate('degrees') ?>" value="<?= e($latitude) ?>">
50            </div>
51        </div>
52    </div>
53
54    <div class="row form-group mb-3">
55        <label class="col-form-label col-sm-1" for="new_place_long">
56            <?= I18N::translate('Longitude') ?>
57        </label>
58        <div class="col-sm-3">
59            <div class="input-group">
60                <input type="text" dir="ltr" id="new_place_long" class="editable form-control" name="new_place_long" placeholder="<?= I18N::translate('degrees') ?>" value="<?= e($longitude) ?>">
61            </div>
62        </div>
63    </div>
64
65    <div class="row form-group mb-3">
66        <div class="col-sm-10 offset-sm-1">
67            <button class="btn btn-primary" type="submit">
68                <?= /* I18N: A button label. */
69                I18N::translate('save')
70                ?>
71            </button>
72            <a class="btn btn-secondary" href="<?= e(route(MapDataList::class, ['parent_id' => $parent->id()])) ?>">
73                <?= I18N::translate('cancel') ?>
74            </a>
75        </div>
76    </div>
77</form>
78
79<?php View::push('javascript') ?>
80<script>
81  'use strict';
82
83  (function () {
84    const config = <?= json_encode($leaflet_config, JSON_THROW_ON_ERROR) ?>;
85    const add_place = <?= json_encode($location->id() === null, JSON_THROW_ON_ERROR) ?>;
86
87    let new_place_lati = document.getElementById('new_place_lati');
88    let new_place_long = document.getElementById('new_place_long');
89
90    // postcss_image_inliner breaks the autodetection of image paths.
91    L.Icon.Default.imagePath = <?= json_encode(asset('css/images/'), JSON_THROW_ON_ERROR) ?>;
92
93    // draggable marker
94    let marker = L.marker(<?= json_encode($marker_position, JSON_THROW_ON_ERROR) ?>, {
95      draggable: true,
96    })
97    .on('dragend', function () {
98      let coords = marker.getLatLng();
99      map.panTo(coords);
100      new_place_lati.value = Number(coords.lat).toFixed(5);
101      new_place_long.value = Number(coords.lng).toFixed(5);
102    });
103
104    /**
105     * Passed to resetControl to
106     * perform necessary reset actions on map
107     */
108    let resetCallback = function () {
109      map.fitBounds(<?= json_encode($map_bounds, JSON_THROW_ON_ERROR) ?>, {padding: [50, 30]});
110      marker.setLatLng(<?= json_encode([$location->latitude(), $location->longitude()], JSON_THROW_ON_ERROR) ?>);
111      document.querySelector('form').reset();
112
113      return false;
114    }
115
116    // Geocoder (place lookup)
117    let geocoder = new L.Control.geocoder({
118      position: 'bottomleft',
119      defaultMarkGeocode: false,
120      expand: 'click',
121      showResultIcons: true,
122      query: <?= json_encode($location->locationName(), JSON_THROW_ON_ERROR) ?>,
123      placeholder: <?= json_encode(I18N::translate('Place'), JSON_THROW_ON_ERROR) ?>,
124      errorMessage: <?= json_encode(I18N::translate('Nothing found.'), JSON_THROW_ON_ERROR) ?>,
125      iconLabel: <?= json_encode(I18N::translate('Search'), JSON_THROW_ON_ERROR) ?>
126    })
127    .on('markgeocode', function (result) {
128      let coords = result.geocode.center;
129      let place = result.geocode.name.split(',', 1).toString();
130      marker.setLatLng(coords);
131      map.panTo(coords);
132      if (add_place) {
133        document.getElementById('new_place_name').value = place
134      }
135      new_place_lati.value = Number(coords.lat).toFixed(5);
136      new_place_long.value = Number(coords.lng).toFixed(5);
137    });
138
139    const map = webtrees.buildLeafletJsMap('wt-map', config, resetCallback)
140      .addControl(geocoder)
141      .addLayer(marker)
142      .fitBounds(<?= json_encode($map_bounds, JSON_THROW_ON_ERROR) ?>, {padding: [50, 30]})
143      .on('zoomend', function () {
144        if (!map.getBounds().contains(marker.getLatLng())) {
145          map.panTo(marker.getLatLng());
146        }
147    });
148
149    document.querySelectorAll('.editable').forEach((element) => {
150      element.addEventListener('change', () => {
151        let lat = new_place_lati.value;
152        let lng = new_place_long.value;
153        marker.setLatLng([lat, lng]);
154        map.panTo([lat, lng]);
155      });
156    });
157
158    window.onload = function() {
159      let icon = document.querySelector('.leaflet-control-geocoder-icon');
160      icon.setAttribute('title', <?= json_encode(I18N::translate('Search'), JSON_THROW_ON_ERROR) ?>);
161    }
162  })();
163</script>
164<?php View::endpush() ?>
165