xref: /webtrees/resources/views/admin/location-edit.phtml (revision 0e54db381d5bda609c346b550e2e1584b72f5545)
1d70512abSGreg Roach<?php
2d70512abSGreg Roach
310e06497SGreg Roachdeclare(strict_types=1);
410e06497SGreg Roach
594e35917SGreg Roachuse Fisharebest\Webtrees\Http\RequestHandlers\MapDataList;
690949315SGreg Roachuse Fisharebest\Webtrees\Http\RequestHandlers\MapDataSave;
7d70512abSGreg Roachuse Fisharebest\Webtrees\I18N;
89a9dfcf7SGreg Roachuse Fisharebest\Webtrees\PlaceLocation;
9d70512abSGreg Roachuse Fisharebest\Webtrees\View;
10d70512abSGreg Roach
119a9dfcf7SGreg Roach/**
129a9dfcf7SGreg Roach * @var array<string,string> $breadcrumbs
139a9dfcf7SGreg Roach * @var string               $latitude
14c9c6f2ecSGreg Roach * @var object               $leaflet_config
159a9dfcf7SGreg Roach * @var string               $longitude
169a9dfcf7SGreg Roach * @var PlaceLocation        $location
179a9dfcf7SGreg Roach * @var array<array<float>>  $map_bounds
1890949315SGreg Roach * @var array<float>         $marker_position
199a9dfcf7SGreg Roach * @var PlaceLocation        $parent
209a9dfcf7SGreg Roach * @var string               $title
21*0e54db38SGreg Roach * @var string               $url
229a9dfcf7SGreg Roach */
239a9dfcf7SGreg Roach
24d70512abSGreg Roach?>
25dd6b2bfcSGreg Roach
26dd6b2bfcSGreg Roach<?= view('components/breadcrumbs', ['links' => $breadcrumbs]) ?>
27dd6b2bfcSGreg Roach
28dd6b2bfcSGreg Roach<h1><?= $title ?></h1>
29dd6b2bfcSGreg Roach
3084df6051SGreg Roach<div id="wt-map" class="wt-ajax-load mb-3 border wt-location-edit-map" dir="ltr"></div>
31dd6b2bfcSGreg Roach
322a93faa6SGreg Roach<form method="post" action="<?= e(route(MapDataSave::class)) ?>">
33*0e54db38SGreg Roach    <input type="hidden" name="parent_id" value="<?= $parent->id() ?>">
34*0e54db38SGreg Roach    <input type="hidden" name="place_id" value="<?= $location->id() ?>">
35*0e54db38SGreg Roach    <input type="hidden" name="url" value="<?= e($url) ?>">
36dd6b2bfcSGreg Roach
379e3c2cf9SGreg Roach    <div class="row mb-3">
38dd6b2bfcSGreg Roach        <label class="col-form-label col-sm-1" for="new_place_name">
39dd6b2bfcSGreg Roach            <?= I18N::translate('Place') ?>
40dd6b2bfcSGreg Roach        </label>
41e340fe65SGreg Roach        <div class="col-sm-3">
427dca5265SGreg 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">
43dd6b2bfcSGreg Roach        </div>
44dd6b2bfcSGreg Roach    </div>
45dd6b2bfcSGreg Roach
469e3c2cf9SGreg Roach    <div class="row mb-3">
4756a34df1SGreg Roach        <label class="col-form-label col-sm-1" for="new_place_lati">
48dd6b2bfcSGreg Roach            <?= I18N::translate('Latitude') ?>
49dd6b2bfcSGreg Roach        </label>
50dd6b2bfcSGreg Roach        <div class="col-sm-3">
51dd6b2bfcSGreg Roach            <div class="input-group">
5290949315SGreg 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) ?>">
53dd6b2bfcSGreg Roach            </div>
54dd6b2bfcSGreg Roach        </div>
55e340fe65SGreg Roach    </div>
56dd6b2bfcSGreg Roach
579e3c2cf9SGreg Roach    <div class="row mb-3">
5856a34df1SGreg Roach        <label class="col-form-label col-sm-1" for="new_place_long">
59dd6b2bfcSGreg Roach            <?= I18N::translate('Longitude') ?>
60dd6b2bfcSGreg Roach        </label>
61dd6b2bfcSGreg Roach        <div class="col-sm-3">
62dd6b2bfcSGreg Roach            <div class="input-group">
6390949315SGreg 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) ?>">
64dd6b2bfcSGreg Roach            </div>
65dd6b2bfcSGreg Roach        </div>
66dd6b2bfcSGreg Roach    </div>
67dd6b2bfcSGreg Roach
689e3c2cf9SGreg Roach    <div class="row mb-3">
69dd6b2bfcSGreg Roach        <div class="col-sm-10 offset-sm-1">
70dd6b2bfcSGreg Roach            <button class="btn btn-primary" type="submit">
71dd6b2bfcSGreg Roach                <?= /* I18N: A button label. */
72dd6b2bfcSGreg Roach                I18N::translate('save')
73dd6b2bfcSGreg Roach                ?>
74dd6b2bfcSGreg Roach            </button>
75*0e54db38SGreg Roach            <a class="btn btn-secondary" href="<?= e($url) ?>">
76dd6b2bfcSGreg Roach                <?= I18N::translate('cancel') ?>
77dd6b2bfcSGreg Roach            </a>
78dd6b2bfcSGreg Roach        </div>
79dd6b2bfcSGreg Roach    </div>
8081443e3cSGreg Roach
8181443e3cSGreg Roach    <?= csrf_field() ?>
82dd6b2bfcSGreg Roach</form>
83dd6b2bfcSGreg Roach
84dd6b2bfcSGreg Roach<?php View::push('javascript') ?>
8574b9ba3fSGreg Roach<script>
86caa53803SDavid Drury  'use strict';
87dd6b2bfcSGreg Roach
8804a72e8fSDavid Drury  (function () {
89728c8c27SGreg Roach    const config = <?= json_encode($leaflet_config, JSON_THROW_ON_ERROR) ?>;
90728c8c27SGreg Roach    const add_place = <?= json_encode($location->id() === null, JSON_THROW_ON_ERROR) ?>;
912cbb0620SDavid Drury
9204a72e8fSDavid Drury    let new_place_lati = document.getElementById('new_place_lati');
9304a72e8fSDavid Drury    let new_place_long = document.getElementById('new_place_long');
9404a72e8fSDavid Drury
9575e7614aSGreg Roach    // postcss_image_inliner breaks the autodetection of image paths.
96728c8c27SGreg Roach    L.Icon.Default.imagePath = <?= json_encode(asset('css/images/'), JSON_THROW_ON_ERROR) ?>;
9775e7614aSGreg Roach
98caa53803SDavid Drury    // draggable marker
99728c8c27SGreg Roach    let marker = L.marker(<?= json_encode($marker_position, JSON_THROW_ON_ERROR) ?>, {
100caa53803SDavid Drury      draggable: true,
101caa53803SDavid Drury    })
1029a9dfcf7SGreg Roach    .on('dragend', function () {
103dd6b2bfcSGreg Roach      let coords = marker.getLatLng();
104dd6b2bfcSGreg Roach      map.panTo(coords);
10504a72e8fSDavid Drury      new_place_lati.value = Number(coords.lat).toFixed(5);
10604a72e8fSDavid Drury      new_place_long.value = Number(coords.lng).toFixed(5);
107dd6b2bfcSGreg Roach    });
108dd6b2bfcSGreg Roach
109f352d954SDavid Drury    /**
110f352d954SDavid Drury     * Passed to resetControl to
111f352d954SDavid Drury     * perform necessary reset actions on map
112b2e5f20eSGreg Roach     *
113b2e5f20eSGreg Roach     * @param {Event} event
114f352d954SDavid Drury     */
115b2e5f20eSGreg Roach    let resetCallback = function (event) {
116b2e5f20eSGreg Roach      event.preventDefault();
117728c8c27SGreg Roach      map.fitBounds(<?= json_encode($map_bounds, JSON_THROW_ON_ERROR) ?>, {padding: [50, 30]});
11860f8717eSDavid Drury      marker.setLatLng(<?= json_encode($marker_position, JSON_THROW_ON_ERROR) ?>);
11904a72e8fSDavid Drury      document.querySelector('form').reset();
120caa53803SDavid Drury    }
121dd6b2bfcSGreg Roach
122caa53803SDavid Drury    // Geocoder (place lookup)
123caa53803SDavid Drury    let geocoder = new L.Control.geocoder({
124c9c6f2ecSGreg Roach      position: 'bottomleft',
125caa53803SDavid Drury      defaultMarkGeocode: false,
12622b076ddSGreg Roach      expand: 'click',
127caa53803SDavid Drury      showResultIcons: true,
128728c8c27SGreg Roach      query: <?= json_encode($location->locationName(), JSON_THROW_ON_ERROR) ?>,
129728c8c27SGreg Roach      placeholder: <?= json_encode(I18N::translate('Place'), JSON_THROW_ON_ERROR) ?>,
130728c8c27SGreg Roach      errorMessage: <?= json_encode(I18N::translate('Nothing found.'), JSON_THROW_ON_ERROR) ?>,
131728c8c27SGreg Roach      iconLabel: <?= json_encode(I18N::translate('Search'), JSON_THROW_ON_ERROR) ?>
132caa53803SDavid Drury    })
133caa53803SDavid Drury    .on('markgeocode', function (result) {
134caa53803SDavid Drury      let coords = result.geocode.center;
135164ab4fbSDavid Drury      let place = result.geocode.name.split(',', 1).toString();
136caa53803SDavid Drury      marker.setLatLng(coords);
137caa53803SDavid Drury      map.panTo(coords);
13862be603fSGreg Roach      if (add_place) {
13904a72e8fSDavid Drury        document.getElementById('new_place_name').value = place
140caa53803SDavid Drury      }
14104a72e8fSDavid Drury      new_place_lati.value = Number(coords.lat).toFixed(5);
14204a72e8fSDavid Drury      new_place_long.value = Number(coords.lng).toFixed(5);
14362be603fSGreg Roach    });
144caa53803SDavid Drury
145b7b71725SGreg Roach    const map = webtrees.buildLeafletJsMap('wt-map', config, resetCallback)
146caa53803SDavid Drury      .addControl(geocoder)
147caa53803SDavid Drury      .addLayer(marker)
148728c8c27SGreg Roach      .fitBounds(<?= json_encode($map_bounds, JSON_THROW_ON_ERROR) ?>, {padding: [50, 30]})
1499a9dfcf7SGreg Roach      .on('zoomend', function () {
150d3a6a36aSGreg Roach        if (!map.getBounds().contains(marker.getLatLng())) {
151d3a6a36aSGreg Roach          map.panTo(marker.getLatLng());
152d3a6a36aSGreg Roach        }
153caa53803SDavid Drury    });
154dd6b2bfcSGreg Roach
15504a72e8fSDavid Drury    document.querySelectorAll('.editable').forEach((element) => {
15604a72e8fSDavid Drury      element.addEventListener('change', () => {
15704a72e8fSDavid Drury        let lat = new_place_lati.value;
15804a72e8fSDavid Drury        let lng = new_place_long.value;
15904a72e8fSDavid Drury        marker.setLatLng([lat, lng]);
16004a72e8fSDavid Drury        map.panTo([lat, lng]);
16104a72e8fSDavid Drury      });
16204a72e8fSDavid Drury    });
16304a72e8fSDavid Drury
16404a72e8fSDavid Drury    window.onload = function() {
16504a72e8fSDavid Drury      let icon = document.querySelector('.leaflet-control-geocoder-icon');
16604a72e8fSDavid Drury      icon.setAttribute('title', <?= json_encode(I18N::translate('Search'), JSON_THROW_ON_ERROR) ?>);
16704a72e8fSDavid Drury    }
168dd6b2bfcSGreg Roach  })();
169dd6b2bfcSGreg Roach</script>
170dd6b2bfcSGreg Roach<?php View::endpush() ?>
171