From becab15ab8ce3a9671fcd91cad8f3b59623e8707 Mon Sep 17 00:00:00 2001 From: James Cole Date: Mon, 30 Dec 2019 17:43:04 +0100 Subject: [PATCH] Make sure that accounts and tags both can handle locations. --- app/Factory/TagFactory.php | 20 +++++++++--- app/Http/Controllers/TagController.php | 37 ++++++++++++++++++--- app/Http/Requests/AccountFormRequest.php | 8 +++-- app/Http/Requests/TagFormRequest.php | 34 ++++++++++---------- app/Models/Tag.php | 2 +- app/Repositories/Tag/TagRepository.php | 17 ++++++++++ public/v1/js/ff/tags/create-edit.js | 2 +- public/v1/js/ff/tags/show.js | 8 ++--- resources/views/v1/accounts/create.twig | 2 +- resources/views/v1/accounts/show.twig | 12 +++---- resources/views/v1/tags/create.twig | 40 ++++------------------- resources/views/v1/tags/edit.twig | 41 ++++-------------------- resources/views/v1/tags/show.twig | 24 ++++++-------- 13 files changed, 123 insertions(+), 124 deletions(-) diff --git a/app/Factory/TagFactory.php b/app/Factory/TagFactory.php index b8e2352340..79eb4ff60c 100644 --- a/app/Factory/TagFactory.php +++ b/app/Factory/TagFactory.php @@ -24,6 +24,7 @@ declare(strict_types=1); namespace FireflyIII\Factory; +use FireflyIII\Models\Location; use FireflyIII\Models\Tag; use FireflyIII\User; use Illuminate\Support\Collection; @@ -66,11 +67,22 @@ class TagFactory 'tagMode' => 'nothing', 'date' => $data['date'], 'description' => $data['description'], - 'latitude' => $latitude, - 'longitude' => $longitude, - 'zoomLevel' => $zoomLevel, + 'latitude' => null, + 'longitude' => null, + 'zoomLevel' => null, ]; - return Tag::create($array); + $tag = Tag::create($array); + if (null !== $tag && null !== $latitude && null !== $longitude) { + // create location object. + $location = new Location; + $location->latitude = $latitude; + $location->longitude = $longitude; + $location->zoom_level = $zoomLevel; + $location->locatable()->associate($tag); + $location->save(); + } + + return $tag; } /** diff --git a/app/Http/Controllers/TagController.php b/app/Http/Controllers/TagController.php index ec7404f7cb..d439656e47 100644 --- a/app/Http/Controllers/TagController.php +++ b/app/Http/Controllers/TagController.php @@ -92,18 +92,29 @@ class TagController extends Controller * * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View */ - public function create() + public function create(Request $request) { $subTitle = (string)trans('firefly.new_tag'); $subTitleIcon = 'fa-tag'; + // location info: + $hasOldInput = null !== $request->old('_token'); + $locations = [ + 'location' => [ + 'latitude' => $hasOldInput ? old('location_latitude') : config('firefly.default_location.latitude'), + 'longitude' => $hasOldInput ? old('location_longitude') : config('firefly.default_location.longitude'), + 'zoom_level' => $hasOldInput ? old('location_zoom_level') : config('firefly.default_location.zoom_level'), + 'has_location' => $hasOldInput ? 'true' === old('location_has_location') : false, + ], + ]; + // put previous url in session if not redirect from store (not "create another"). if (true !== session('tags.create.fromStore')) { $this->rememberPreviousUri('tags.create.uri'); } session()->forget('tags.create.fromStore'); - return view('tags.create', compact('subTitle', 'subTitleIcon')); + return view('tags.create', compact('subTitle', 'subTitleIcon', 'locations')); } /** @@ -153,13 +164,27 @@ class TagController extends Controller $subTitle = (string)trans('firefly.edit_tag', ['tag' => $tag->tag]); $subTitleIcon = 'fa-tag'; + $location = $this->repository->getLocation($tag); + $latitude = $location ? $location->latitude : config('firefly.default_location.latitude'); + $longitude = $location ? $location->longitude : config('firefly.default_location.longitude'); + $zoomLevel = $location ? $location->zoom_level : config('firefly.default_location.zoom_level'); + $hasLocation = null !== $location; + $locations = [ + 'location' => [ + 'latitude' => old('location_latitude') ?? $latitude, + 'longitude' => old('location_longitude') ?? $longitude, + 'zoom_level' => old('location_zoom_level') ?? $zoomLevel, + 'has_location' => $hasLocation || 'true' === old('location_has_location'), + ], + ]; + // put previous url in session if not redirect from store (not "return_to_edit"). if (true !== session('tags.edit.fromUpdate')) { $this->rememberPreviousUri('tags.edit.uri'); } session()->forget('tags.edit.fromUpdate'); - return view('tags.edit', compact('tag', 'subTitle', 'subTitleIcon')); + return view('tags.edit', compact('tag', 'subTitle', 'subTitleIcon','locations')); } /** @@ -209,6 +234,7 @@ class TagController extends Controller $pageSize = (int)app('preferences')->get('listPageSize', 50)->data; $start = $start ?? session('start'); $end = $end ?? session('end'); + $location = $this->repository->getLocation($tag); $subTitle = trans( 'firefly.journals_in_period_for_tag', ['tag' => $tag->tag, 'start' => $start->formatLocalized($this->monthAndDayFormat), 'end' => $end->formatLocalized($this->monthAndDayFormat),] @@ -229,7 +255,7 @@ class TagController extends Controller $groups->setPath($path); $sums = $this->repository->sumsOfTag($tag, $start, $end); - return view('tags.show', compact('tag', 'sums', 'periods', 'subTitle', 'subTitleIcon', 'groups', 'start', 'end')); + return view('tags.show', compact('tag', 'sums', 'periods', 'subTitle', 'subTitleIcon', 'groups', 'start', 'end', 'location')); } /** @@ -252,6 +278,7 @@ class TagController extends Controller $start = $this->repository->firstUseDate($tag) ?? new Carbon; $end = new Carbon; $path = route('tags.show', [$tag->id, 'all']); + $location = $this->repository->getLocation($tag); /** @var GroupCollectorInterface $collector */ $collector = app(GroupCollectorInterface::class); $collector->setRange($start, $end)->setLimit($pageSize)->setPage($page)->withAccountInformation() @@ -260,7 +287,7 @@ class TagController extends Controller $groups->setPath($path); $sums = $this->repository->sumsOfTag($tag, $start, $end); - return view('tags.show', compact('tag', 'sums', 'periods', 'subTitle', 'subTitleIcon', 'groups', 'start', 'end')); + return view('tags.show', compact('tag', 'sums', 'periods', 'subTitle', 'subTitleIcon', 'groups', 'start', 'end', 'location')); } /** diff --git a/app/Http/Requests/AccountFormRequest.php b/app/Http/Requests/AccountFormRequest.php index 3a560a5953..a5df985ee0 100644 --- a/app/Http/Requests/AccountFormRequest.php +++ b/app/Http/Requests/AccountFormRequest.php @@ -67,9 +67,10 @@ class AccountFormRequest extends Request 'interest' => $this->string('interest'), 'interest_period' => $this->string('interest_period'), 'include_net_worth' => '1', + // new: location - 'longitude' => $this->float('location_longitude'), - 'latitude' => $this->float('location_latitude'), + 'longitude' => $this->string('location_longitude'), + 'latitude' => $this->string('location_latitude'), 'zoom_level' => $this->integer('location_zoom_level'), 'has_location' => $this->boolean('location_has_location'), ]; @@ -113,6 +114,9 @@ class AccountFormRequest extends Request 'amount_currency_id_virtual_balance' => 'exists:transaction_currencies,id', 'what' => 'in:' . $types, 'interest_period' => 'in:daily,monthly,yearly', + 'latitude' => 'numeric|min:-90|max:90|nullable|required_with:longitude', + 'longitude' => 'numeric|min:-180|max:180|nullable|required_with:latitude', + 'zoom_level' => 'numeric|min:0|max:80|nullable', ]; if ('liabilities' === $this->get('objectType')) { diff --git a/app/Http/Requests/TagFormRequest.php b/app/Http/Requests/TagFormRequest.php index 428d9df23c..ed40f2ff89 100644 --- a/app/Http/Requests/TagFormRequest.php +++ b/app/Http/Requests/TagFormRequest.php @@ -47,26 +47,26 @@ class TagFormRequest extends Request */ public function collectTagData(): array { - $latitude = null; - $longitude = null; - $zoomLevel = null; - - if ('true' === $this->get('tag_position_has_tag')) { - $latitude = $this->string('tag_position_latitude'); - $longitude = $this->string('tag_position_longitude'); - $zoomLevel = $this->integer('tag_position_zoomlevel'); + $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; } - $data = [ - 'tag' => $this->string('tag'), - 'date' => $this->date('date'), - 'description' => $this->string('description'), - 'latitude' => $latitude, - 'longitude' => $longitude, - 'zoom_level' => $zoomLevel, + return [ + 'tag' => $this->string('tag'), + 'date' => $this->date('date'), + 'description' => $this->string('description'), + 'latitude' => $latitude, + 'longitude' => $longitude, + 'zoom_level' => $zoomLevel, + 'has_location' => $hasLocation, ]; - - return $data; } /** diff --git a/app/Models/Tag.php b/app/Models/Tag.php index c82eeb579c..2957473dc4 100644 --- a/app/Models/Tag.php +++ b/app/Models/Tag.php @@ -92,7 +92,7 @@ class Tag extends Model 'longitude' => 'float', ]; /** @var array Fields that can be filled */ - protected $fillable = ['user_id', 'tag', 'date', 'description']; + protected $fillable = ['user_id', 'tag', 'date', 'description','tagMode']; protected $hidden = ['zoomLevel', 'latitude', 'longitude']; diff --git a/app/Repositories/Tag/TagRepository.php b/app/Repositories/Tag/TagRepository.php index c14cecd951..4c25c14bf3 100644 --- a/app/Repositories/Tag/TagRepository.php +++ b/app/Repositories/Tag/TagRepository.php @@ -404,6 +404,23 @@ class TagRepository implements TagRepositoryInterface $tag->zoomLevel = $data['zoom_level']; $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); + } + $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/public/v1/js/ff/tags/create-edit.js b/public/v1/js/ff/tags/create-edit.js index 9a981e9678..2c2e62ec48 100644 --- a/public/v1/js/ff/tags/create-edit.js +++ b/public/v1/js/ff/tags/create-edit.js @@ -1,4 +1,4 @@ -/* + /* * create-edit.js * Copyright (c) 2019 thegrumpydictator@gmail.com * diff --git a/public/v1/js/ff/tags/show.js b/public/v1/js/ff/tags/show.js index e5475ea9da..12a153b96b 100644 --- a/public/v1/js/ff/tags/show.js +++ b/public/v1/js/ff/tags/show.js @@ -31,7 +31,7 @@ $(function () { */ // make map: - var mymap = L.map('tag_location_map', { + var mymap = L.map('location_map', { zoomControl: false, touchZoom: false, doubleClickZoom: false, @@ -46,9 +46,7 @@ $(function () { id: 'mapbox.streets', accessToken: mapboxToken }).addTo(mymap); - - if (doPlaceMarker) { - L.marker([latitude, longitude]).addTo(mymap); - } + L.marker([latitude, longitude]).addTo(mymap); } + }); diff --git a/resources/views/v1/accounts/create.twig b/resources/views/v1/accounts/create.twig index a176f591f7..08eddbf47f 100644 --- a/resources/views/v1/accounts/create.twig +++ b/resources/views/v1/accounts/create.twig @@ -58,7 +58,7 @@ {# only correct way to do active checkbox #} {{ ExpandedForm.checkbox('include_net_worth', 1) }} {{ ExpandedForm.textarea('notes',null,{helpText: trans('firefly.field_supports_markdown')}) }} - {{ ExpandedForm.location('location', null, {locations:locations}) }} + {{ ExpandedForm.location('location', null, {locations: locations}) }} diff --git a/resources/views/v1/accounts/show.twig b/resources/views/v1/accounts/show.twig index 5acff5f48b..a21dda5903 100644 --- a/resources/views/v1/accounts/show.twig +++ b/resources/views/v1/accounts/show.twig @@ -160,12 +160,12 @@ // location stuff {% if location %} - var latitude = {{ location.latitude|default("52.3167") }}; - var longitude = {{ location.longitude|default("5.5500") }}; - var zoomLevel = {{ location.zoom_level|default("6") }}; - var doPlaceMarker = true; - // token for Mapbox: - var mapboxToken = "{{ config('firefly.mapbox_api_key') }}"; + var latitude = {{ location.latitude|default("52.3167") }}; + var longitude = {{ location.longitude|default("5.5500") }}; + var zoomLevel = {{ location.zoom_level|default("6") }}; + var doPlaceMarker = true; + // token for Mapbox: + var mapboxToken = "{{ config('firefly.mapbox_api_key') }}"; {% endif %} var showAll = true; diff --git a/resources/views/v1/tags/create.twig b/resources/views/v1/tags/create.twig index 7ef8d57bb1..c47a3a28d0 100644 --- a/resources/views/v1/tags/create.twig +++ b/resources/views/v1/tags/create.twig @@ -5,6 +5,11 @@ {% endblock %} {% block content %} + +
@@ -28,7 +33,7 @@
{{ ExpandedForm.date('date') }} {{ ExpandedForm.textarea('description') }} - {{ ExpandedForm.location('tag_position') }} + {{ ExpandedForm.location('location', null, {locations: locations}) }}
@@ -52,39 +57,6 @@
{% endblock %} {% block scripts %} - diff --git a/resources/views/v1/tags/edit.twig b/resources/views/v1/tags/edit.twig index 2d321882be..fba7a76b80 100644 --- a/resources/views/v1/tags/edit.twig +++ b/resources/views/v1/tags/edit.twig @@ -5,6 +5,12 @@ {% endblock %} {% block content %} + + + {{ Form.model(tag, {'class' : 'form-horizontal','id' : 'update','url' : route('tags.update',tag.id)}) }} @@ -30,7 +36,7 @@
{{ ExpandedForm.date('date', tag.date.format('Y-m-d')) }} {{ ExpandedForm.textarea('description', tag.description) }} - {{ ExpandedForm.location('tag_position') }} + {{ ExpandedForm.location('location', null, {locations: locations}) }}
@@ -56,39 +62,6 @@ {% endblock %} {% block scripts %} - diff --git a/resources/views/v1/tags/show.twig b/resources/views/v1/tags/show.twig index a8f0b05723..3aebafdec7 100644 --- a/resources/views/v1/tags/show.twig +++ b/resources/views/v1/tags/show.twig @@ -102,8 +102,8 @@
- {% if tag.latitude and tag.longitude and tag.zoomLevel %} -
+ {% if(location) %} +
{% else %}

{{ 'no_location_set'|_ }}

{% endif %} @@ -178,19 +178,15 @@ {% endblock %} {% block scripts %}