From afd470075851675327e701c2d7ea078017c1df8b Mon Sep 17 00:00:00 2001 From: James Cole Date: Wed, 24 Nov 2021 20:20:47 +0100 Subject: [PATCH] Switch things. --- .../Transaction/LinkController.php | 6 +++-- .../LinkType/LinkTypeRepository.php | 17 ++++++++++++ .../LinkType/LinkTypeRepositoryInterface.php | 7 +++++ resources/views/v1/admin/link/show.twig | 26 +++++++++++++++++-- resources/views/v1/profile/index.twig | 2 -- resources/views/v1/transactions/show.twig | 21 ++++++++++++++- routes/web.php | 3 +-- 7 files changed, 73 insertions(+), 9 deletions(-) diff --git a/app/Http/Controllers/Transaction/LinkController.php b/app/Http/Controllers/Transaction/LinkController.php index 10190ce489..8aaa065c0f 100644 --- a/app/Http/Controllers/Transaction/LinkController.php +++ b/app/Http/Controllers/Transaction/LinkController.php @@ -30,6 +30,7 @@ use FireflyIII\Repositories\Journal\JournalRepositoryInterface; use FireflyIII\Repositories\LinkType\LinkTypeRepositoryInterface; use Illuminate\Contracts\View\Factory; use Illuminate\Http\RedirectResponse; +use Illuminate\Http\Request; use Illuminate\Routing\Redirector; use Illuminate\View\View; use Log; @@ -157,9 +158,10 @@ class LinkController extends Controller * * @return RedirectResponse|Redirector */ - public function switchLink(TransactionJournalLink $link) + public function switchLink(Request $request) { - $this->repository->switchLink($link); + $linkId = (int)$request->get('id'); + $this->repository->switchLinkById($linkId); return redirect(app('steam')->getSafePreviousUrl()); } diff --git a/app/Repositories/LinkType/LinkTypeRepository.php b/app/Repositories/LinkType/LinkTypeRepository.php index 1b43ef17af..89f9d4511c 100644 --- a/app/Repositories/LinkType/LinkTypeRepository.php +++ b/app/Repositories/LinkType/LinkTypeRepository.php @@ -389,4 +389,21 @@ class LinkTypeRepository implements LinkTypeRepositoryInterface return TransactionJournalLink::whereDestinationId($two->id)->whereSourceId($one->id)->first(); } + + /** + * @inheritDoc + */ + public function switchLinkById(int $linkId): bool + { + /** @var TransactionJournalLink $link */ + $link = TransactionJournalLink::find($linkId); + if (null !== $link) { + + if ($link->source->user->id === $this->user->id) { + $this->switchLink($link); + } + } + + return true; + } } diff --git a/app/Repositories/LinkType/LinkTypeRepositoryInterface.php b/app/Repositories/LinkType/LinkTypeRepositoryInterface.php index 17ce2decea..2898350046 100644 --- a/app/Repositories/LinkType/LinkTypeRepositoryInterface.php +++ b/app/Repositories/LinkType/LinkTypeRepositoryInterface.php @@ -154,6 +154,13 @@ interface LinkTypeRepositoryInterface */ public function switchLink(TransactionJournalLink $link): bool; + /** + * @param int $linkId + * + * @return bool + */ + public function switchLinkById(int $linkId): bool; + /** * @param LinkType $linkType * @param array $data diff --git a/resources/views/v1/admin/link/show.twig b/resources/views/v1/admin/link/show.twig index 652c2b4ac6..72840d920c 100644 --- a/resources/views/v1/admin/link/show.twig +++ b/resources/views/v1/admin/link/show.twig @@ -27,8 +27,8 @@
- - +
@@ -58,6 +58,28 @@ {% endblock %} {% block scripts %} + {% endblock %} {% block styles %} diff --git a/resources/views/v1/profile/index.twig b/resources/views/v1/profile/index.twig index bf01b5411b..3c7e7db198 100644 --- a/resources/views/v1/profile/index.twig +++ b/resources/views/v1/profile/index.twig @@ -126,13 +126,11 @@ {% else %} -

-

{% endif %} diff --git a/resources/views/v1/transactions/show.twig b/resources/views/v1/transactions/show.twig index f00d0303bd..ce50940d90 100644 --- a/resources/views/v1/transactions/show.twig +++ b/resources/views/v1/transactions/show.twig @@ -357,7 +357,7 @@
-
@@ -423,6 +423,25 @@ var acURI = '{{ route('api.v1.autocomplete.transactions-with-id') }}'; var groupURI = '{{ route('transactions.show',['%GROUP%']) }}'; var cloneGroupUrl = '{{ route('transactions.clone') }}'; + + $('.switch-link').on('click', switchLink); + var switchLinkUrl = '{{ route('transactions.link.switch') }}'; + function switchLink(e) { + e.preventDefault(); + var obj = $(e.currentTarget); + $.post(switchLinkUrl, { + _token: token, + id: obj.data('id') + }).done(function () { + location.reload(); + }).fail(function () { + console.error('I failed :('); + }); + + //alert(obj.data('id')); + + return false + } diff --git a/routes/web.php b/routes/web.php index 1c29386366..f01001d3a8 100644 --- a/routes/web.php +++ b/routes/web.php @@ -1079,8 +1079,7 @@ Route::group( // See reference nr. 6 Route::post('store/{tj}', ['uses' => 'LinkController@store', 'as' => 'store']); Route::get('delete/{journalLink}', ['uses' => 'LinkController@delete', 'as' => 'delete']); - Route::post('switch/{journalLink}', ['uses' => 'LinkController@switchLink', 'as' => 'switch']); - + Route::post('switch', ['uses' => 'LinkController@switchLink', 'as' => 'switch']); Route::post('destroy/{journalLink}', ['uses' => 'LinkController@destroy', 'as' => 'destroy']); } );