From ed12ea7cfbdf8adbd8673b5f8d5fc1b950964d98 Mon Sep 17 00:00:00 2001 From: James Cole Date: Sat, 18 Jul 2015 21:46:16 +0200 Subject: [PATCH] Check for double files and some code clean up. --- app/Helpers/Attachments/AttachmentHelper.php | 25 ++- .../Controllers/TransactionController.php | 17 +- resources/twig/transactions/edit.twig | 157 +++++++++--------- resources/twig/transactions/show.twig | 2 + 4 files changed, 120 insertions(+), 81 deletions(-) diff --git a/app/Helpers/Attachments/AttachmentHelper.php b/app/Helpers/Attachments/AttachmentHelper.php index 7b98a98f4a..4af6557a13 100644 --- a/app/Helpers/Attachments/AttachmentHelper.php +++ b/app/Helpers/Attachments/AttachmentHelper.php @@ -54,6 +54,7 @@ class AttachmentHelper implements AttachmentHelperInterface public function saveAttachmentsForModel(Model $model) { $files = Input::file('attachments'); + foreach ($files as $entry) { if (!is_null($entry)) { $this->processFile($entry, $model); @@ -71,11 +72,19 @@ class AttachmentHelper implements AttachmentHelperInterface */ protected function hasFile(UploadedFile $file, Model $model) { - $md5 = md5_file($file->getPath()); + $md5 = md5_file($file->getRealPath()); + $name = $file->getClientOriginalName(); $class = get_class($model); $count = Auth::user()->attachments()->where('md5', $md5)->where('attachable_id', $model->id)->where('attachable_type', $class)->count(); - return ($count > 0); + if ($count > 0) { + $err = 'File ' . e($name) . ' already attached to this object.'; + $this->errors->add('attachments', $err); + + return true; + } + + return false; } /** @@ -97,7 +106,7 @@ class AttachmentHelper implements AttachmentHelperInterface $attachment = new Attachment; $attachment->user()->associate(Auth::user()); $attachment->attachable()->associate($model); - $attachment->md5 = md5_file($file->getPath()); + $attachment->md5 = md5_file($file->getRealPath()); $attachment->filename = $file->getClientOriginalName(); $attachment->mime = $file->getMimeType(); $attachment->size = $file->getSize(); @@ -128,6 +137,11 @@ class AttachmentHelper implements AttachmentHelperInterface } + /** + * @param UploadedFile $file + * + * @return bool + */ protected function validMime(UploadedFile $file) { $mime = $file->getMimeType(); @@ -143,6 +157,11 @@ class AttachmentHelper implements AttachmentHelperInterface return true; } + /** + * @param UploadedFile $file + * + * @return bool + */ protected function validSize(UploadedFile $file) { $size = $file->getSize(); diff --git a/app/Http/Controllers/TransactionController.php b/app/Http/Controllers/TransactionController.php index 2e2037ed34..de9996bf41 100644 --- a/app/Http/Controllers/TransactionController.php +++ b/app/Http/Controllers/TransactionController.php @@ -311,16 +311,29 @@ class TransactionController extends Controller /** * @param JournalFormRequest $request * @param JournalRepositoryInterface $repository + * @param AttachmentHelperInterface $att * @param TransactionJournal $journal * - * @return \Illuminate\Http\RedirectResponse + * @return $this|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector */ - public function update(JournalFormRequest $request, JournalRepositoryInterface $repository, TransactionJournal $journal) + public function update(JournalFormRequest $request, JournalRepositoryInterface $repository, AttachmentHelperInterface $att, TransactionJournal $journal) { $journalData = $request->getJournalData(); $repository->update($journal, $journalData); + // save attachments: + $att->saveAttachmentsForModel($journal); + + if ($att->getErrors()->count() > 0) { + // todo moet beter + Session::flash('error', ''); + } + if ($att->getMessages()->count() > 0) { + // todo moet beter + Session::flash('info', ''); + } + event(new JournalSaved($journal)); // update, get events by date and sort DESC diff --git a/resources/twig/transactions/edit.twig b/resources/twig/transactions/edit.twig index 5df0982c35..a9a20bc978 100644 --- a/resources/twig/transactions/edit.twig +++ b/resources/twig/transactions/edit.twig @@ -5,94 +5,99 @@ {% endblock %} {% block content %} - {{ Form.open({'class' : 'form-horizontal','id' : 'update','url' : route('transactions.update',journal.id)}) }} +
- - + + + -
-
-
-
-

{{ 'mandatoryFields'|_ }}

+
+
+
+
+

{{ 'mandatoryFields'|_ }}

+
+
+ + {{ ExpandedForm.text('description',journal.description) }} + + + {% if what == 'deposit' or what == 'withdrawal' %} + {{ ExpandedForm.select('account_id',accounts,data['account_id']) }} + {% endif %} + + + {% if what == 'withdrawal' %} + {{ ExpandedForm.text('expense_account',data['expense_account']) }} + {% endif %} + + + {% if what == 'deposit' %} + {{ ExpandedForm.text('revenue_account',data['revenue_account']) }} + {% endif %} + + + {% if what == 'transfer' %} + {{ ExpandedForm.select('account_from_id',accounts,data['account_from_id']) }} + {{ ExpandedForm.select('account_to_id',accounts,data['account_to_id']) }} + {% endif %} + + + {{ ExpandedForm.amount('amount',data.amount,{'currency' : journal.transactionCurrency}) }} + + + {{ ExpandedForm.date('date',data['date']) }} +
-
- - {{ ExpandedForm.text('description',journal.description) }} + - - {% if what == 'deposit' or what == 'withdrawal' %} - {{ ExpandedForm.select('account_id',accounts,data['account_id']) }} - {% endif %} - - - {% if what == 'withdrawal' %} - {{ ExpandedForm.text('expense_account',data['expense_account']) }} - {% endif %} - - - {% if what == 'deposit' %} - {{ ExpandedForm.text('revenue_account',data['revenue_account']) }} - {% endif %} - - - {% if what == 'transfer' %} - {{ ExpandedForm.select('account_from_id',accounts,data['account_from_id']) }} - {{ ExpandedForm.select('account_to_id',accounts,data['account_to_id']) }} - {% endif %} - - - {{ ExpandedForm.amount('amount',data.amount,{'currency' : journal.transactionCurrency}) }} - - - {{ ExpandedForm.date('date',data['date']) }} -
- +
+
+
+

{{ 'optionalFields'|_ }}

+
+
+ + {% if what == 'withdrawal' %} + {{ ExpandedForm.select('budget_id',budgets,data['budget_id']) }} + {% endif %} -
-
-
-
-

{{ 'optionalFields'|_ }}

+ + {{ ExpandedForm.text('category',data['category']) }} + + + {{ ExpandedForm.text('tags') }} + + + {% if what == 'withdrawal' and piggies|length > 0 %} + {{ ExpandedForm.select('piggy_bank_id',piggies,data['piggy_bank_id']) }} + {% endif %} + + + {{ ExpandedForm.file('attachments[]', {'multiple': 'multiple'}) }} + +
-
- - {% if what == 'withdrawal' %} - {{ ExpandedForm.select('budget_id',budgets,data['budget_id']) }} - {% endif %} + - - {{ ExpandedForm.text('category',data['category']) }} - - - {{ ExpandedForm.text('tags') }} - - - {% if what == 'withdrawal' and piggies|length > 0 %} - {{ ExpandedForm.select('piggy_bank_id',piggies,data['piggy_bank_id']) }} - {% endif %} - -
-
- - - -
-
-

{{ 'options'|_ }}

-
-
- {{ ExpandedForm.optionsList('update','transaction') }} -
-
-
- {{ Form.close|raw }} + {% endblock %} diff --git a/resources/twig/transactions/show.twig b/resources/twig/transactions/show.twig index 1e9476c747..e6d51bbe82 100644 --- a/resources/twig/transactions/show.twig +++ b/resources/twig/transactions/show.twig @@ -76,6 +76,7 @@
+ {% if journal.attachments|length > 0 %}

{{ 'attachments'|_ }}

@@ -97,6 +98,7 @@
+ {% endif %} {% if journal.piggyBankEvents|length > 0 %}