Skip to content

Firefly III: a personal finances manager

Can upload attachments for all models #2828

James Cole14f43519200c
diff --git a/app/Helpers/Attachments/AttachmentHelper.php b/app/Helpers/Attachments/AttachmentHelper.php
index 261363410c..b29615721d 100644
--- a/app/Helpers/Attachments/AttachmentHelper.php
+++ b/app/Helpers/Attachments/AttachmentHelper.php
@@ -25,6 +25,7 @@ namespace FireflyIII\Helpers\Attachments;
use Crypt;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Models\Attachment;
+use FireflyIII\Models\PiggyBank;
use Illuminate\Contracts\Encryption\DecryptException;
use Illuminate\Contracts\Encryption\EncryptException;
use Illuminate\Contracts\Filesystem\FileNotFoundException;
@@ -243,8 +244,13 @@ class AttachmentHelper implements AttachmentHelperInterface
$md5 = md5_file($file->getRealPath());
$name = $file->getClientOriginalName();
$class = get_class($model);
- /** @noinspection PhpUndefinedFieldInspection */
- $count = $model->user->attachments()->where('md5', $md5)->where('attachable_id', $model->id)->where('attachable_type', $class)->count();
+ $count = 0;
+ if (PiggyBank::class === $class) {
+ $count = $model->account->user->attachments()->where('md5', $md5)->where('attachable_id', $model->id)->where('attachable_type', $class)->count();
+ }
+ if (PiggyBank::class !== $class) {
+ $count = $model->user->attachments()->where('md5', $md5)->where('attachable_id', $model->id)->where('attachable_type', $class)->count();
+ }
$result = false;
if ($count > 0) {
$msg = (string) trans('validation.file_already_attached', ['name' => $name]);
@@ -272,9 +278,15 @@ class AttachmentHelper implements AttachmentHelperInterface
$validation = $this->validateUpload($file, $model);
$attachment = null;
if (false !== $validation) {
+ $class = get_class($model);
+ $user = $model->user;
+ if (PiggyBank::class === $class) {
+ $user = $model->account->user;
+ }
+
$attachment = new Attachment; // create Attachment object.
/** @noinspection PhpUndefinedFieldInspection */
- $attachment->user()->associate($model->user);
+ $attachment->user()->associate($user);
$attachment->attachable()->associate($model);
$attachment->md5 = md5_file($file->getRealPath());
$attachment->filename = $file->getClientOriginalName();
diff --git a/app/Http/Controllers/Budget/EditController.php b/app/Http/Controllers/Budget/EditController.php
index f0b06e83b1..ea70b33be8 100644
--- a/app/Http/Controllers/Budget/EditController.php
+++ b/app/Http/Controllers/Budget/EditController.php
@@ -136,6 +136,15 @@ class EditController extends Controller
$redirect = redirect($this->getPreviousUri('budgets.edit.uri'));
+ // store new attachment(s):
+ /** @var array $files */
+ $files = $request->hasFile('attachments') ? $request->file('attachments') : null;
+ $this->attachments->saveAttachmentsForModel($budget, $files);
+
+ if (count($this->attachments->getMessages()->get('attachments')) > 0) {
+ $request->session()->flash('info', $this->attachments->getMessages()->get('attachments')); // @codeCoverageIgnore
+ }
+
if (1 === (int) $request->get('return_to_edit')) {
// @codeCoverageIgnoreStart
$request->session()->put('budgets.edit.fromUpdate', true);
diff --git a/app/Http/Controllers/Category/EditController.php b/app/Http/Controllers/Category/EditController.php
index ffb8efe65b..632fa5a2ef 100644
--- a/app/Http/Controllers/Category/EditController.php
+++ b/app/Http/Controllers/Category/EditController.php
@@ -106,6 +106,16 @@ class EditController extends Controller
$request->session()->flash('success', (string) trans('firefly.updated_category', ['name' => $category->name]));
app('preferences')->mark();
+ // store new attachment(s):
+ /** @var array $files */
+ $files = $request->hasFile('attachments') ? $request->file('attachments') : null;
+ $this->attachments->saveAttachmentsForModel($category, $files);
+
+ if (count($this->attachments->getMessages()->get('attachments')) > 0) {
+ $request->session()->flash('info', $this->attachments->getMessages()->get('attachments')); // @codeCoverageIgnore
+ }
+
+
$redirect = redirect($this->getPreviousUri('categories.edit.uri'));
if (1 === (int) $request->get('return_to_edit')) {
diff --git a/app/Http/Controllers/PiggyBankController.php b/app/Http/Controllers/PiggyBankController.php
index 69c32ad53f..11b042eb19 100644
--- a/app/Http/Controllers/PiggyBankController.php
+++ b/app/Http/Controllers/PiggyBankController.php
@@ -490,6 +490,16 @@ class PiggyBankController extends Controller
session()->flash('success', (string) trans('firefly.updated_piggy_bank', ['name' => $piggyBank->name]));
app('preferences')->mark();
+ // store new attachment(s):
+ /** @var array $files */
+ $files = $request->hasFile('attachments') ? $request->file('attachments') : null;
+ $this->attachments->saveAttachmentsForModel($piggyBank, $files);
+
+ if (count($this->attachments->getMessages()->get('attachments')) > 0) {
+ $request->session()->flash('info', $this->attachments->getMessages()->get('attachments')); // @codeCoverageIgnore
+ }
+
+
$redirect = redirect($this->getPreviousUri('piggy-banks.edit.uri'));
if (1 === (int) $request->get('return_to_edit')) {
diff --git a/app/Http/Controllers/TagController.php b/app/Http/Controllers/TagController.php
index 50d0969ed3..4217cbad62 100644
--- a/app/Http/Controllers/TagController.php
+++ b/app/Http/Controllers/TagController.php
@@ -349,11 +349,21 @@ class TagController extends Controller
public function update(TagFormRequest $request, Tag $tag): RedirectResponse
{
$data = $request->collectTagData();
- $this->repository->update($tag, $data);
+ $tag = $this->repository->update($tag, $data);
session()->flash('success', (string) trans('firefly.updated_tag', ['tag' => $data['tag']]));
app('preferences')->mark();
+ // store new attachment(s):
+ /** @var array $files */
+ $files = $request->hasFile('attachments') ? $request->file('attachments') : null;
+ $this->attachments->saveAttachmentsForModel($tag, $files);
+
+ if (count($this->attachments->getMessages()->get('attachments')) > 0) {
+ $request->session()->flash('info', $this->attachments->getMessages()->get('attachments')); // @codeCoverageIgnore
+ }
+
+
$redirect = redirect($this->getPreviousUri('tags.edit.uri'));
if (1 === (int) $request->get('return_to_edit')) {
// @codeCoverageIgnoreStart