diff --git a/app/Api/V1/Requests/TagStoreRequest.php b/app/Api/V1/Requests/TagStoreRequest.php index 472fffa6eb..82411df7bb 100644 --- a/app/Api/V1/Requests/TagStoreRequest.php +++ b/app/Api/V1/Requests/TagStoreRequest.php @@ -23,8 +23,6 @@ declare(strict_types=1); namespace FireflyIII\Api\V1\Requests; -use FireflyIII\Models\Tag; - /** * Class TagStoreRequest * @@ -52,13 +50,19 @@ class TagStoreRequest extends Request */ public function getAll(): array { + $hasLocation = false; + if ($this->has('longitude') && $this->has('latitude') && $this->has('zoom_level')) { + $hasLocation = true; + } + return [ - 'tag' => $this->string('tag'), - 'date' => $this->date('date'), - 'description' => $this->string('description'), - 'latitude' => '' === $this->string('latitude') ? null : $this->string('latitude'), - 'longitude' => '' === $this->string('longitude') ? null : $this->string('longitude'), - 'zoom_level' => $this->integer('zoom_level'), + 'tag' => $this->string('tag'), + 'date' => $this->date('date'), + 'description' => $this->string('description'), + 'has_location' => $hasLocation, + 'longitude' => $this->string('longitude'), + 'latitude' => $this->string('latitude'), + 'zoom_level' => $this->integer('zoom_level'), ]; } diff --git a/app/Api/V1/Requests/TagUpdateRequest.php b/app/Api/V1/Requests/TagUpdateRequest.php index c833aeef4a..2e3829e19b 100644 --- a/app/Api/V1/Requests/TagUpdateRequest.php +++ b/app/Api/V1/Requests/TagUpdateRequest.php @@ -51,13 +51,19 @@ class TagUpdateRequest extends Request */ public function getAll(): array { + $updateLocation = false; + if ($this->has('longitude') && $this->has('latitude') && $this->has('zoom_level')) { + $updateLocation = true; + } + return [ - 'tag' => $this->string('tag'), - 'date' => $this->date('date'), - 'description' => $this->string('description'), - 'latitude' => '' === $this->string('latitude') ? null : $this->string('latitude'), - 'longitude' => '' === $this->string('longitude') ? null : $this->string('longitude'), - 'zoom_level' => $this->integer('zoom_level'), + 'tag' => $this->string('tag'), + 'date' => $this->date('date'), + 'description' => $this->string('description'), + 'has_location' => $updateLocation, + 'longitude' => '' === $this->string('longitude') ? null : $this->string('longitude'), + 'latitude' => '' === $this->string('latitude') ? null : $this->string('latitude'), + 'zoom_level' => '' === $this->string('zoom_level') ? null : $this->integer('zoom_level'), ]; } diff --git a/app/Repositories/Tag/TagRepository.php b/app/Repositories/Tag/TagRepository.php index 4c25c14bf3..70703e4e19 100644 --- a/app/Repositories/Tag/TagRepository.php +++ b/app/Repositories/Tag/TagRepository.php @@ -405,20 +405,28 @@ class TagRepository implements TagRepositoryInterface $tag->save(); // update, delete or create location: - $hasLocation = $data['has_location'] ?? false; - if (false === $hasLocation) { - $tag->locations()->delete(); - } - if (true === $hasLocation) { - $location = $this->getLocation($tag); - if (null === $location) { - $location = new Location; - $location->locatable()->associate($tag); + $updateLocation = $data['has_location'] ?? false; + + // location must be updated? + if (true === $updateLocation) { + // if all set to NULL, delete + if (null === $data['latitude'] && null === $data['longitude'] && null === $data['zoom_level']) { + $tag->locations()->delete(); + } + + // otherwise, update or create. + if (!(null === $data['latitude'] && null === $data['longitude'] && null === $data['zoom_level'])) { + $location = $this->getLocation($tag); + if (null === $location) { + $location = new Location; + $location->locatable()->associate($tag); + } + + $location->latitude = $data['latitude'] ?? config('firefly.default_location.latitude'); + $location->longitude = $data['longitude'] ?? config('firefly.default_location.longitude'); + $location->zoom_level = $data['zoom_level'] ?? config('firefly.default_location.zoom_level'); + $location->save(); } - $location->latitude = $data['latitude'] ?? config('firefly.default_location.latitude'); - $location->longitude = $data['longitude'] ?? config('firefly.default_location.longitude'); - $location->zoom_level = $data['zoom_level'] ?? config('firefly.default_location.zoom_level'); - $location->save(); } return $tag; diff --git a/app/Services/Internal/Update/AccountUpdateService.php b/app/Services/Internal/Update/AccountUpdateService.php index e32d0c1cd6..11244f273c 100644 --- a/app/Services/Internal/Update/AccountUpdateService.php +++ b/app/Services/Internal/Update/AccountUpdateService.php @@ -106,12 +106,12 @@ class AccountUpdateService // location must be updated? if (true === $updateLocation) { // if all set to NULL, delete - if(null === $data['latitude'] && null === $data['longitude'] && null === $data['zoom_level']) { + if (null === $data['latitude'] && null === $data['longitude'] && null === $data['zoom_level']) { $account->locations()->delete(); } // otherwise, update or create. - if(!(null === $data['latitude'] && null === $data['longitude'] && null === $data['zoom_level'])) { + if (!(null === $data['latitude'] && null === $data['longitude'] && null === $data['zoom_level'])) { $location = $this->accountRepository->getLocation($account); if (null === $location) { $location = new Location; diff --git a/app/Transformers/TagTransformer.php b/app/Transformers/TagTransformer.php index 7e11c8d2fd..a9a26e9296 100644 --- a/app/Transformers/TagTransformer.php +++ b/app/Transformers/TagTransformer.php @@ -24,6 +24,7 @@ declare(strict_types=1); namespace FireflyIII\Transformers; +use FireflyIII\Models\Location; use FireflyIII\Models\Tag; use Log; @@ -56,6 +57,16 @@ class TagTransformer extends AbstractTransformer public function transform(Tag $tag): array { $date = null === $tag->date ? null : $tag->date->format('Y-m-d'); + /** @var Location $location */ + $location = $tag->locations()->first(); + $latitude = null; + $longitude = null; + $zoomLevel = null; + if (null !== $location) { + $latitude = $location->latitude; + $longitude = $location->longitude; + $zoomLevel = $location->zoom_level; + } $data = [ 'id' => (int)$tag->id, 'created_at' => $tag->created_at->toAtomString(), @@ -63,9 +74,9 @@ class TagTransformer extends AbstractTransformer 'tag' => $tag->tag, 'date' => $date, 'description' => '' === $tag->description ? null : $tag->description, - 'latitude' => null === $tag->latitude ? null : (float)$tag->latitude, - 'longitude' => null === $tag->longitude ? null : (float)$tag->longitude, - 'zoom_level' => null === $tag->zoomLevel ? null : (int)$tag->zoomLevel, + 'longitude' => $longitude, + 'latitude' => $latitude, + 'zoom_level' => $zoomLevel, 'links' => [ [ 'rel' => 'self',