From edf764aaf431d6dee409a0e97fdd2a0c156145b7 Mon Sep 17 00:00:00 2001 From: James Cole Date: Sun, 11 Nov 2018 07:03:36 +0100 Subject: [PATCH] Will now warn about non-existent attachments. --- .../Controllers/TransactionController.php | 21 ++++++++- .../Journal/JournalRepository.php | 20 ++++++-- .../Journal/JournalRepositoryInterface.php | 10 ++++ resources/lang/en_US/firefly.php | 1 + resources/views/v1/transactions/show.twig | 47 +++++++++++++------ 5 files changed, 79 insertions(+), 20 deletions(-) diff --git a/app/Http/Controllers/TransactionController.php b/app/Http/Controllers/TransactionController.php index e6f19afc26..2cabef11f7 100644 --- a/app/Http/Controllers/TransactionController.php +++ b/app/Http/Controllers/TransactionController.php @@ -26,13 +26,16 @@ namespace FireflyIII\Http\Controllers; use Carbon\Carbon; use FireflyIII\Exceptions\FireflyException; +use FireflyIII\Helpers\Attachments\AttachmentHelper; use FireflyIII\Helpers\Collector\TransactionCollectorInterface; use FireflyIII\Helpers\Filter\CountAttachmentsFilter; use FireflyIII\Helpers\Filter\InternalTransferFilter; use FireflyIII\Helpers\Filter\SplitIndicatorFilter; +use FireflyIII\Models\Attachment; use FireflyIII\Models\Transaction; use FireflyIII\Models\TransactionJournal; use FireflyIII\Models\TransactionType; +use FireflyIII\Repositories\Attachment\AttachmentRepositoryInterface; use FireflyIII\Repositories\Journal\JournalRepositoryInterface; use FireflyIII\Repositories\LinkType\LinkTypeRepositoryInterface; use FireflyIII\Support\Http\Controllers\ModelInformation; @@ -53,6 +56,8 @@ use View; class TransactionController extends Controller { use ModelInformation, PeriodOverview; + /** @var AttachmentRepositoryInterface */ + private $attachmentRepository; /** @var JournalRepositoryInterface Journals and transactions overview */ private $repository; @@ -67,7 +72,8 @@ class TransactionController extends Controller function ($request, $next) { app('view')->share('title', (string)trans('firefly.transactions')); app('view')->share('mainTitleIcon', 'fa-repeat'); - $this->repository = app(JournalRepositoryInterface::class); + $this->repository = app(JournalRepositoryInterface::class); + $this->attachmentRepository = app(AttachmentRepositoryInterface::class); return $next($request); } @@ -212,6 +218,7 @@ class TransactionController extends Controller * * @param TransactionJournal $journal * @param LinkTypeRepositoryInterface $linkTypeRepository + * @param AttachmentHelper $attHelper * * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|View * @throws FireflyException @@ -228,6 +235,16 @@ class TransactionController extends Controller $linkTypes = $linkTypeRepository->get(); $links = $linkTypeRepository->getLinks($journal); + // get attachments: + $attachments = $this->repository->getAttachments($journal); + $attachments = $attachments->each( + function (Attachment $attachment) { + $attachment->file_exists = $this->attachmentRepository->exists($attachment); + + return $attachment; + } + ); + // get transactions using the collector: $collector = app(TransactionCollectorInterface::class); $collector->setUser(auth()->user()); @@ -246,7 +263,7 @@ class TransactionController extends Controller $what = strtolower($transactionType); $subTitle = (string)trans('firefly.' . $what) . ' "' . $journal->description . '"'; - return view('transactions.show', compact('journal', 'events', 'subTitle', 'what', 'transactions', 'linkTypes', 'links')); + return view('transactions.show', compact('journal','attachments', 'events', 'subTitle', 'what', 'transactions', 'linkTypes', 'links')); } diff --git a/app/Repositories/Journal/JournalRepository.php b/app/Repositories/Journal/JournalRepository.php index 0b42bdfb3d..61fe3e768a 100644 --- a/app/Repositories/Journal/JournalRepository.php +++ b/app/Repositories/Journal/JournalRepository.php @@ -29,7 +29,6 @@ use FireflyIII\Factory\TransactionJournalFactory; use FireflyIII\Factory\TransactionJournalMetaFactory; use FireflyIII\Helpers\Collector\TransactionCollectorInterface; use FireflyIII\Helpers\Filter\InternalTransferFilter; -use FireflyIII\Helpers\Filter\TransferFilter; use FireflyIII\Models\Account; use FireflyIII\Models\AccountType; use FireflyIII\Models\PiggyBankEvent; @@ -263,6 +262,18 @@ class JournalRepository implements JournalRepositoryInterface return null; } + /** + * Return all attachments for journal. + * + * @param TransactionJournal $journal + * + * @return Collection + */ + public function getAttachments(TransactionJournal $journal): Collection + { + return $journal->attachments; + } + /** * Returns the first positive transaction for the journal. Useful when editing journals. * @@ -585,10 +596,10 @@ class JournalRepository implements JournalRepositoryInterface public function getTransactionsById(array $transactionIds): Collection { $journalIds = Transaction::whereIn('id', $transactionIds)->get(['transaction_journal_id'])->pluck('transaction_journal_id')->toArray(); - $journals = new Collection; - foreach($journalIds as $journalId) { + $journals = new Collection; + foreach ($journalIds as $journalId) { $result = $this->findNull((int)$journalId); - if(null !== $result) { + if (null !== $result) { $journals->push($result); } } @@ -600,6 +611,7 @@ class JournalRepository implements JournalRepositoryInterface //$collector->addFilter(TransferFilter::class); $collector->setJournals($journals)->withOpposingAccount(); + return $collector->getTransactions(); } diff --git a/app/Repositories/Journal/JournalRepositoryInterface.php b/app/Repositories/Journal/JournalRepositoryInterface.php index 0b38323d41..42d1d59322 100644 --- a/app/Repositories/Journal/JournalRepositoryInterface.php +++ b/app/Repositories/Journal/JournalRepositoryInterface.php @@ -38,6 +38,16 @@ use Illuminate\Support\MessageBag; */ interface JournalRepositoryInterface { + /** + * Return all attachments for journal. + * + * @param TransactionJournal $journal + * + * @return Collection + */ + public function getAttachments(TransactionJournal $journal): Collection; + + /** @noinspection MoreThanThreeArgumentsInspection */ /** * @param TransactionJournal $journal diff --git a/resources/lang/en_US/firefly.php b/resources/lang/en_US/firefly.php index 644acc911b..f69e2a5d81 100644 --- a/resources/lang/en_US/firefly.php +++ b/resources/lang/en_US/firefly.php @@ -851,6 +851,7 @@ return [ 'opt_group_l_Credit card' => 'Liability: Credit card', 'notes' => 'Notes', 'unknown_journal_error' => 'Could not store the transaction. Please check the log files.', + 'attachment_not_found' => 'This attachment could not found in the storage.', // new user: 'welcome' => 'Welcome to Firefly III!', diff --git a/resources/views/v1/transactions/show.twig b/resources/views/v1/transactions/show.twig index 6d564adef0..94ce47984e 100644 --- a/resources/views/v1/transactions/show.twig +++ b/resources/views/v1/transactions/show.twig @@ -249,7 +249,7 @@ - {% if journal.attachments|length > 0 %} + {% if attachments|length > 0 %}

{{ 'attachments'|_ }}

@@ -262,21 +262,38 @@
- + {% if att.file_exists %} + + {% endif %} + {% if not att.file_exists %} + + {% endif %}
- - - {% if att.title %} - {{ att.title }} - {% else %} - {{ att.filename }} + {% if att.file_exists %} + + + {% if att.title %} + {{ att.title }} + {% else %} + {{ att.filename }} + {% endif %} + + ({{ att.size|filesize }}) + {% if att.notes.first %} + {{ att.notes.first.text|markdown }} {% endif %} - - ({{ att.size|filesize }}) - {% if att.notes.first %} - {{ att.notes.first.text|markdown }} + {% endif %} + {% if not att.file_exists %} + + {% if att.title %} + {{ att.title }} + {% else %} + {{ att.filename }} + {% endif %} +
+ {{ 'attachment_not_found'|_ }} {% endif %} @@ -426,8 +443,10 @@