Add attachment to recurring.

This commit is contained in:
James Cole
2020-06-12 20:56:58 +02:00
parent b1732d0de8
commit 54d5778bf3
9 changed files with 61 additions and 17 deletions

View File

@@ -49,8 +49,7 @@ class EditController extends Controller
/** @var AccountRepositoryInterface The account repository */
private $repository;
/** @var AttachmentHelperInterface Helper for attachments. */
private $attachments;
private AttachmentHelperInterface $attachments;
/**
* EditController constructor.

View File

@@ -25,6 +25,7 @@ namespace FireflyIII\Http\Controllers\Recurring;
use Carbon\Carbon;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Helpers\Attachments\AttachmentHelperInterface;
use FireflyIII\Http\Controllers\Controller;
use FireflyIII\Http\Requests\RecurrenceFormRequest;
use FireflyIII\Models\RecurrenceRepetition;
@@ -47,6 +48,8 @@ class CreateController extends Controller
/** @var RecurringRepositoryInterface Recurring repository */
private $recurring;
private AttachmentHelperInterface $attachments;
/**
* CreateController constructor.
*
@@ -65,6 +68,7 @@ class CreateController extends Controller
$this->recurring = app(RecurringRepositoryInterface::class);
$this->budgets = app(BudgetRepositoryInterface::class);
$this->attachments = app(AttachmentHelperInterface::class);
return $next($request);
}
@@ -140,6 +144,21 @@ class CreateController extends Controller
$request->session()->flash('success', (string) trans('firefly.stored_new_recurrence', ['title' => $recurrence->title]));
app('preferences')->mark();
// store attachment(s):
/** @var array $files */
$files = $request->hasFile('attachments') ? $request->file('attachments') : null;
if (null !== $files && !auth()->user()->hasRole('demo')) {
$this->attachments->saveAttachmentsForModel($recurrence, $files);
}
if (null !== $files && auth()->user()->hasRole('demo')) {
session()->flash('info', (string) trans('firefly.no_att_demo_user'));
}
if (count($this->attachments->getMessages()->get('attachments')) > 0) {
$request->session()->flash('info', $this->attachments->getMessages()->get('attachments')); // @codeCoverageIgnore
}
$redirect = redirect($this->getPreviousUri('recurring.create.uri'));
if (1 === (int) $request->get('create_another')) {
// set value so create routine will not overwrite URL:

View File

@@ -25,6 +25,7 @@ namespace FireflyIII\Http\Controllers\Recurring;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Helpers\Attachments\AttachmentHelperInterface;
use FireflyIII\Http\Controllers\Controller;
use FireflyIII\Http\Requests\RecurrenceFormRequest;
use FireflyIII\Models\Recurrence;
@@ -49,6 +50,7 @@ class EditController extends Controller
private $budgets;
/** @var RecurringRepositoryInterface Recurring repository */
private $recurring;
private AttachmentHelperInterface $attachments;
/**
* EditController constructor.
@@ -68,7 +70,7 @@ class EditController extends Controller
$this->recurring = app(RecurringRepositoryInterface::class);
$this->budgets = app(BudgetRepositoryInterface::class);
$this->attachments = app(AttachmentHelperInterface::class);
return $next($request);
}
);
@@ -159,6 +161,21 @@ class EditController extends Controller
$this->recurring->update($recurrence, $data);
$request->session()->flash('success', (string) trans('firefly.updated_recurrence', ['title' => $recurrence->title]));
// store new attachment(s):
$files = $request->hasFile('attachments') ? $request->file('attachments') : null;
if (null !== $files && !auth()->user()->hasRole('demo')) {
$this->attachments->saveAttachmentsForModel($recurrence, $files);
}
if (null !== $files && auth()->user()->hasRole('demo')) {
session()->flash('info', (string) trans('firefly.no_att_demo_user'));
}
if (count($this->attachments->getMessages()->get('attachments')) > 0) {
$request->session()->flash('info', $this->attachments->getMessages()->get('attachments')); // @codeCoverageIgnore
}
app('preferences')->mark();
$redirect = redirect($this->getPreviousUri('recurrences.edit.uri'));
if (1 === (int) $request->get('return_to_edit')) {

View File

@@ -111,6 +111,8 @@ class IndexController extends Controller
$array['first_date'] = new Carbon($array['first_date']);
$array['repeat_until'] = null === $array['repeat_until'] ? null : new Carbon($array['repeat_until']);
$array['latest_date'] = null === $array['latest_date'] ? null : new Carbon($array['latest_date']);
// lazy but OK
$array['attachments'] = $recurrence->attachments()->count();
// make carbon objects out of occurrences
foreach ($array['repetitions'] as $repIndex => $repetition) {

View File

@@ -94,12 +94,6 @@ use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
* @property-read int|null $notes_count
* @property-read int|null $piggy_banks_count
* @property-read int|null $transactions_count
* @property \Illuminate\Support\Carbon|null $created_at
* @property \Illuminate\Support\Carbon|null $updated_at
* @property int $account_type_id
* @property bool $encrypted
* @property-read \Illuminate\Database\Eloquent\Collection|\FireflyIII\Models\AccountMeta[] $accountMeta
* @property-read \Illuminate\Database\Eloquent\Collection|\FireflyIII\Models\PiggyBank[] $piggyBanks
*/
class Account extends Model
{

View File

@@ -189,6 +189,15 @@ class Recurrence extends Model
return $this->belongsTo(TransactionCurrency::class);
}
/**
* @codeCoverageIgnore
* @return MorphMany
*/
public function attachments(): MorphMany
{
return $this->morphMany(Attachment::class, 'attachable');
}
/**
* @codeCoverageIgnore
* @return BelongsTo

View File

@@ -4,7 +4,7 @@
{% endblock %}
{% block content %}
<form action="{{ route('recurring.store') }}" method="post" id="store" class="form-horizontal">
<form action="{{ route('recurring.store') }}" method="post" id="store" class="form-horizontal" enctype="multipart/form-data">
<input type="hidden" name="_token" value="{{ csrf_token() }}"/>
{# row with recurrence information #}
<div class="row">
@@ -51,8 +51,8 @@
{# only correct way to do active checkbox #}
{{ ExpandedForm.checkbox('active', 1) }}
{{ ExpandedForm.checkbox('apply_rules',1) }}
{{ ExpandedForm.file('attachments[]', {'multiple': 'multiple','helpText': trans('firefly.upload_max_file_size', {'size': uploadSize|filesize}) }) }}
</div>
</div>
</div>

View File

@@ -52,6 +52,7 @@
{# only correct way to do active checkbox #}
{{ ExpandedForm.checkbox('active', 1, preFilled.active) }}
{{ ExpandedForm.checkbox('apply_rules', 1, preFilled.apply_rules) }}
{{ ExpandedForm.file('attachments[]', {'multiple': 'multiple','helpText': trans('firefly.upload_max_file_size', {'size': uploadSize|filesize}) }) }}
</div>
</div>
</div>

View File

@@ -56,6 +56,9 @@
</div>
</td>
<td data-value="{{ rt.title }}">
{% if rt.attachments > 0 %}
<i class="fa fa-fw fa-paperclip"></i>
{% endif %}
{% if rt.active == false %}<s>{% endif %}
{{ rt.type|_ }}:
<a href="{{ route('recurring.show',rt.id) }}">{{ rt.title }}</a>