From c55ef9c77b6692bf156ad79b3e6ec3968d5ee3d4 Mon Sep 17 00:00:00 2001 From: James Cole Date: Thu, 2 Jan 2020 19:25:42 +0100 Subject: [PATCH] Fix location store for tag. --- app/Http/Requests/Request.php | 9 +++++---- app/Http/Requests/TagFormRequest.php | 25 ++++++------------------- app/Repositories/Tag/TagRepository.php | 8 ++++---- resources/views/v1/form/location.twig | 23 ++++++++++++++++------- 4 files changed, 31 insertions(+), 34 deletions(-) diff --git a/app/Http/Requests/Request.php b/app/Http/Requests/Request.php index 4724ca3f09..cfd3239a68 100644 --- a/app/Http/Requests/Request.php +++ b/app/Http/Requests/Request.php @@ -355,20 +355,21 @@ class Request extends FormRequest */ protected function appendLocationData(array $data, ?string $prefix): array { - Log::debug(sprintf('Now in appendLocationData(%s)', $prefix), $data); + Log::debug(sprintf('Now in appendLocationData("%s")', $prefix), $data); $data['store_location'] = false; $data['update_location'] = false; $data['longitude'] = null; $data['latitude'] = null; $data['zoom_level'] = null; + $longitudeKey = null === $prefix ? 'longitude' : sprintf('%s_longitude', $prefix); $latitudeKey = null === $prefix ? 'latitude' : sprintf('%s_latitude', $prefix); $zoomLevelKey = null === $prefix ? 'zoom_level' : sprintf('%s_zoom_level', $prefix); // for a POST (store, all fields must be present and accounted for: if ( - ('POST' === $this->method() && ($this->routeIs('accounts.store') || $this->routeIs('api.v1.accounts.store'))) + ('POST' === $this->method() && $this->routeIs('*.store')) && ($this->has($longitudeKey) && $this->has($latitudeKey) && $this->has($zoomLevelKey)) ) { Log::debug('Method is POST and all fields present.'); @@ -380,8 +381,8 @@ class Request extends FormRequest if ( ($this->has($longitudeKey) && $this->has($latitudeKey) && $this->has($zoomLevelKey)) && ( - ('PUT' === $this->method() && $this->routeIs('api.v1.accounts.update')) - || ('POST' === $this->method() && $this->routeIs('accounts.update')) + ('PUT' === $this->method() && $this->routeIs('*.update')) + || ('POST' === $this->method() && $this->routeIs('*.update')) ) ) { Log::debug('Method is PUT and all fields present.'); diff --git a/app/Http/Requests/TagFormRequest.php b/app/Http/Requests/TagFormRequest.php index 7bcfd0e9d5..0a236f6321 100644 --- a/app/Http/Requests/TagFormRequest.php +++ b/app/Http/Requests/TagFormRequest.php @@ -48,26 +48,13 @@ class TagFormRequest extends Request */ public function collectTagData(): array { - $latitude = null; - $longitude = null; - $zoomLevel = null; - $hasLocation = false; - if (true === $this->boolean('location_has_location')) { - $latitude = $this->string('location_latitude'); - $longitude = $this->string('location_longitude'); - $zoomLevel = $this->integer('location_zoom_level'); - $hasLocation = true; - } - - return [ - 'tag' => $this->string('tag'), - 'date' => $this->date('date'), - 'description' => $this->string('description'), - 'latitude' => $latitude, - 'longitude' => $longitude, - 'zoom_level' => $zoomLevel, - 'has_location' => $hasLocation, + $data = [ + 'tag' => $this->string('tag'), + 'date' => $this->date('date'), + 'description' => $this->string('description'), ]; + return $this->appendLocationData($data, 'location'); + } /** diff --git a/app/Repositories/Tag/TagRepository.php b/app/Repositories/Tag/TagRepository.php index 70703e4e19..2f5c8d0ce6 100644 --- a/app/Repositories/Tag/TagRepository.php +++ b/app/Repositories/Tag/TagRepository.php @@ -399,13 +399,13 @@ class TagRepository implements TagRepositoryInterface $tag->tag = $data['tag']; $tag->date = $data['date']; $tag->description = $data['description']; - $tag->latitude = $data['latitude']; - $tag->longitude = $data['longitude']; - $tag->zoomLevel = $data['zoom_level']; + $tag->latitude = null; + $tag->longitude = null; + $tag->zoomLevel = null; $tag->save(); // update, delete or create location: - $updateLocation = $data['has_location'] ?? false; + $updateLocation = $data['update_location'] ?? false; // location must be updated? if (true === $updateLocation) { diff --git a/resources/views/v1/form/location.twig b/resources/views/v1/form/location.twig index 82c431f721..8df6d7273a 100644 --- a/resources/views/v1/form/location.twig +++ b/resources/views/v1/form/location.twig @@ -79,20 +79,28 @@ }); }; + // set location thing: function setObjectLocation(e) { - console.log('Set object location: lat(' + e.latlng.lat + '), long(' + e.latlng.lng + '), zoom (' + mymap.getZoom() + ')'); - $('input[name="{{ latitudevar }}"]').val(e.latlng.lat); - $('input[name="{{ longitudevar }}"]').val(e.latlng.lng); + if (typeof e.latlng !== 'undefined') { + console.log('Set object location: lat(' + e.latlng.lat + '), long(' + e.latlng.lng + '), zoom (' + mymap.getZoom() + ')'); + $('input[name="{{ latitudevar }}"]').val(e.latlng.lat); + $('input[name="{{ longitudevar }}"]').val(e.latlng.lng); + } + if (typeof e.latlng === 'undefined') { + console.log('Set object zoom level to ' + mymap.getZoom()); + } $('input[name="{{ zoomlevelvar }}"]').val(mymap.getZoom()); - $('input[name="{{ haslocationvar }}"]').val('true'); + // remove existing marker: - if (typeof marker !== 'undefined') { + if (typeof marker !== 'undefined' && typeof e.latlng !== 'undefined') { marker.remove(); } - // new marker - marker = L.marker({lat: e.latlng.lat, lng: e.latlng.lng}).addTo(mymap); + if (typeof e.latlng !== 'undefined') { + // new marker + marker = L.marker({lat: e.latlng.lat, lng: e.latlng.lng}).addTo(mymap); + } } @@ -110,6 +118,7 @@ }).addTo(mymap); mymap.on('contextmenu', setObjectLocation); + mymap.on('zoomend', setObjectLocation); // add marker if (typeof locations.{{ name }}.has_location !== 'undefined' && locations.{{ name }}.has_location === true) {