Skip to content

Firefly III: a personal finances manager

Allow to edit an attachment.

James Cole0dbe4e94fa33
diff --git a/app/Http/Controllers/AttachmentController.php b/app/Http/Controllers/AttachmentController.php
index 56a8ccfe79..ea38e1dc1b 100644
--- a/app/Http/Controllers/AttachmentController.php
+++ b/app/Http/Controllers/AttachmentController.php
@@ -5,7 +5,12 @@ namespace FireflyIII\Http\Controllers;
use Crypt;
use FireflyIII\Helpers\Attachments\AttachmentHelperInterface;
+use FireflyIII\Http\Requests\AttachmentFormRequest;
use FireflyIII\Models\Attachment;
+use FireflyIII\Repositories\Attachment\AttachmentRepositoryInterface;
+use Input;
+use Preferences;
+use Session;
use View;
/**
@@ -66,7 +71,39 @@ class AttachmentController extends Controller
} else {
abort(404);
}
+ }
+
+
+ /**
+ * @param AttachmentFormRequest $request
+ * @param AttachmentRepositoryInterface $repository
+ * @param Attachment $attachment
+ *
+ * @return \Illuminate\Http\RedirectResponse
+ */
+ public function update(AttachmentFormRequest $request, AttachmentRepositoryInterface $repository, Attachment $attachment)
+ {
+
+ $attachmentData = [
+ 'title' => $request->input('title'),
+ 'description' => $request->input('description'),
+ 'notes' => $request->input('notes'),
+ ];
+
+ $repository->update($attachment, $attachmentData);
+
+ Session::flash('success', 'Attachment "' . $attachment->filename . '" updated.');
+ Preferences::mark();
+
+ if (intval(Input::get('return_to_edit')) === 1) {
+ // set value so edit routine will not overwrite URL:
+ Session::put('accounts.edit.fromUpdate', true);
+
+ return redirect(route('attachment.edit', [$attachment->id]))->withInput(['return_to_edit' => 1]);
+ }
+ // redirect to previous URL.
+ return redirect(Session::get('accounts.edit.url'));
}
diff --git a/app/Http/Requests/AttachmentFormRequest.php b/app/Http/Requests/AttachmentFormRequest.php
new file mode 100644
index 0000000000..50a6665591
--- /dev/null
+++ b/app/Http/Requests/AttachmentFormRequest.php
@@ -0,0 +1,36 @@
+<?php
+
+namespace FireflyIII\Http\Requests;
+
+use Auth;
+
+/**
+ * Class AttachmentFormRequest
+ *
+ * @codeCoverageIgnore
+ * @package FireflyIII\Http\Requests
+ */
+class AttachmentFormRequest extends Request
+{
+ /**
+ * @return bool
+ */
+ public function authorize()
+ {
+ // Only allow logged in users
+ return Auth::check();
+ }
+
+ /**
+ * @return array
+ */
+ public function rules()
+ {
+
+ return [
+ 'title' => 'between:1,255',
+ 'description' => 'between:1,65536',
+ 'notes' => 'between:1,65536',
+ ];
+ }
+}
diff --git a/app/Models/Attachment.php b/app/Models/Attachment.php
index 4384abdc17..87c28524a1 100644
--- a/app/Models/Attachment.php
+++ b/app/Models/Attachment.php
@@ -2,6 +2,7 @@
namespace FireflyIII\Models;
+use Crypt;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
@@ -9,20 +10,20 @@ use Illuminate\Database\Eloquent\SoftDeletes;
* Class Attachment
*
* @package FireflyIII\Models
- * @property integer $id
- * @property \Carbon\Carbon $created_at
- * @property \Carbon\Carbon $updated_at
- * @property string $deleted_at
- * @property integer $attachable_id
- * @property string $attachable_type
- * @property integer $user_id
- * @property string $md5
- * @property string $filename
- * @property string $mime
- * @property integer $size
- * @property boolean $uploaded
- * @property-read \ $attachable
- * @property-read \FireflyIII\User $user
+ * @property integer $id
+ * @property \Carbon\Carbon $created_at
+ * @property \Carbon\Carbon $updated_at
+ * @property string $deleted_at
+ * @property integer $attachable_id
+ * @property string $attachable_type
+ * @property integer $user_id
+ * @property string $md5
+ * @property string $filename
+ * @property string $mime
+ * @property integer $size
+ * @property boolean $uploaded
+ * @property-read \ $attachable
+ * @property-read \FireflyIII\User $user
* @method static \Illuminate\Database\Query\Builder|\FireflyIII\Models\Attachment whereId($value)
* @method static \Illuminate\Database\Query\Builder|\FireflyIII\Models\Attachment whereCreatedAt($value)
* @method static \Illuminate\Database\Query\Builder|\FireflyIII\Models\Attachment whereUpdatedAt($value)
@@ -35,12 +36,18 @@ use Illuminate\Database\Eloquent\SoftDeletes;
* @method static \Illuminate\Database\Query\Builder|\FireflyIII\Models\Attachment whereMime($value)
* @method static \Illuminate\Database\Query\Builder|\FireflyIII\Models\Attachment whereSize($value)
* @method static \Illuminate\Database\Query\Builder|\FireflyIII\Models\Attachment whereUploaded($value)
+ * @property string $title
+ * @property string $description
+ * @property string $notes
+ * @method static \Illuminate\Database\Query\Builder|\FireflyIII\Models\Attachment whereTitle($value)
+ * @method static \Illuminate\Database\Query\Builder|\FireflyIII\Models\Attachment whereDescription($value)
+ * @method static \Illuminate\Database\Query\Builder|\FireflyIII\Models\Attachment whereNotes($value)
*/
class Attachment extends Model
{
use SoftDeletes;
- protected $fillable = ['attachable_id', 'attachable_type', 'user_id', 'md5', 'filename', 'mime', 'size', 'uploaded'];
+ protected $fillable = ['attachable_id', 'attachable_type', 'user_id', 'md5', 'filename', 'mime', 'title', 'notes', 'description', 'size', 'uploaded'];
/**
* Get all of the owning imageable models.
@@ -59,4 +66,125 @@ class Attachment extends Model
return $this->belongsTo('FireflyIII\User');
}
+
+ /**
+ * @codeCoverageIgnore
+ *
+ * @param $value
+ *
+ * @return null|string
+ */
+ public function getFilenameAttribute($value)
+ {
+ if (is_null($value)) {
+ return null;
+ }
+
+ return Crypt::decrypt($value);
+ }
+
+ /**
+ * @param string $value
+ */
+ public function setFilenameAttribute($value)
+ {
+ $this->attributes['filename'] = Crypt::encrypt($value);
+ }
+
+ /**
+ * @codeCoverageIgnore
+ *
+ * @param $value
+ *
+ * @return null|string
+ */
+ public function getMimeAttribute($value)
+ {
+ if (is_null($value)) {
+ return null;
+ }
+
+ return Crypt::decrypt($value);
+ }
+
+ /**
+ * @param string $value
+ */
+ public function setMimeAttribute($value)
+ {
+ $this->attributes['mime'] = Crypt::encrypt($value);
+ }
+
+ /**
+ * @codeCoverageIgnore
+ *
+ * @param $value
+ *
+ * @return null|string
+ */
+ public function getTitleAttribute($value)
+ {
+ if (is_null($value)) {
+ return null;
+ }
+
+ return Crypt::decrypt($value);
+ }
+
+ /**
+ * @param string $value
+ */
+ public function setTitleAttribute($value)
+ {
+ $this->attributes['title'] = Crypt::encrypt($value);
+ }
+
+ /**
+ * @codeCoverageIgnore
+ *
+ * @param $value
+ *
+ * @return null|string
+ */
+ public function getDescriptionAttribute($value)
+ {
+ if (is_null($value)) {
+ return null;
+ }
+
+ return Crypt::decrypt($value);
+ }
+
+ /**
+ * @param string $value
+ */
+ public function setDescriptionAttribute($value)
+ {
+ $this->attributes['description'] = Crypt::encrypt($value);
+ }
+
+ /**
+ * @codeCoverageIgnore
+ *
+ * @param $value
+ *
+ * @return null|string
+ */
+ public function getNotesAttribute($value)
+ {
+ if (is_null($value)) {
+ return null;
+ }
+
+ return Crypt::decrypt($value);
+ }
+
+ /**
+ * @param string $value
+ */
+ public function setNotesAttribute($value)
+ {
+ $this->attributes['notes'] = Crypt::encrypt($value);
+ }
+
}
\ No newline at end of file
diff --git a/app/Providers/FireflyServiceProvider.php b/app/Providers/FireflyServiceProvider.php
index a08a908fde..45e2d5bb62 100644
--- a/app/Providers/FireflyServiceProvider.php
+++ b/app/Providers/FireflyServiceProvider.php
@@ -88,6 +88,7 @@ class FireflyServiceProvider extends ServiceProvider
$this->app->bind('FireflyIII\Repositories\PiggyBank\PiggyBankRepositoryInterface', 'FireflyIII\Repositories\PiggyBank\PiggyBankRepository');
$this->app->bind('FireflyIII\Repositories\Currency\CurrencyRepositoryInterface', 'FireflyIII\Repositories\Currency\CurrencyRepository');
$this->app->bind('FireflyIII\Repositories\Tag\TagRepositoryInterface', 'FireflyIII\Repositories\Tag\TagRepository');
+ $this->app->bind('FireflyIII\Repositories\Attachment\AttachmentRepositoryInterface', 'FireflyIII\Repositories\Attachment\AttachmentRepository');
$this->app->bind('FireflyIII\Support\Search\SearchInterface', 'FireflyIII\Support\Search\Search');
// CSV import
diff --git a/app/Repositories/Attachment/AttachmentRepository.php b/app/Repositories/Attachment/AttachmentRepository.php
new file mode 100644
index 0000000000..2e1b579845
--- /dev/null
+++ b/app/Repositories/Attachment/AttachmentRepository.php
@@ -0,0 +1,32 @@
+<?php
+
+namespace FireflyIII\Repositories\Attachment;
+
+use FireflyIII\Models\Attachment;
+
+/**
+ * Class AttachmentRepository
+ *
+ * @package FireflyIII\Repositories\Attachment
+ */
+class AttachmentRepository implements AttachmentRepositoryInterface
+{
+
+ /**
+ * @param Attachment $attachment
+ * @param array $data
+ *
+ * @return Attachment
+ */
+ public function update(Attachment $attachment, array $data)
+ {
+
+ $attachment->title = $data['title'];
+ $attachment->description = $data['description'];
+ $attachment->notes = $data['notes'];
+ $attachment->save();
+
+ return $attachment;
+
+ }
+}
\ No newline at end of file
diff --git a/app/Repositories/Attachment/AttachmentRepositoryInterface.php b/app/Repositories/Attachment/AttachmentRepositoryInterface.php
new file mode 100644
index 0000000000..3b6c55186a
--- /dev/null
+++ b/app/Repositories/Attachment/AttachmentRepositoryInterface.php
@@ -0,0 +1,22 @@
+<?php
+
+namespace FireflyIII\Repositories\Attachment;
+
+use FireflyIII\Models\Attachment;
+
+/**
+ * Interface AttachmentRepositoryInterface
+ *
+ * @package FireflyIII\Repositories\Attachment
+ */
+interface AttachmentRepositoryInterface
+{
+
+ /**
+ * @param Attachment $attachment
+ * @param array $attachmentData
+ *
+ * @return Attachment
+ */
+ public function update(Attachment $attachment, array $attachmentData);
+}
\ No newline at end of file
diff --git a/database/migrations/2015_07_17_190438_changes_for_v3410.php b/database/migrations/2015_07_17_190438_changes_for_v3410.php
index acb7d6bc2c..3892d3e881 100644
--- a/database/migrations/2015_07_17_190438_changes_for_v3410.php
+++ b/database/migrations/2015_07_17_190438_changes_for_v3410.php
@@ -24,12 +24,11 @@ class ChangesForV3410 extends Migration
$table->string('attachable_type');
$table->integer('user_id')->unsigned();
$table->string('md5', 32);
- $table->string('filename');
- $table->string('title')->nullable();
+ $table->text('filename');
+ $table->text('title')->nullable();
$table->text('description')->nullable();
$table->text('notes')->nullable();
-
- $table->string('mime');
+ $table->text('mime');
$table->integer('size')->unsigned();
$table->tinyInteger('uploaded', false, true)->default(0);
@@ -37,6 +36,8 @@ class ChangesForV3410 extends Migration
);
}
+
+
/**
* Reverse the migrations.
*
diff --git a/resources/twig/attachments/edit.twig b/resources/twig/attachments/edit.twig
index 9aaa0a84d1..600f6b4e3a 100644
--- a/resources/twig/attachments/edit.twig
+++ b/resources/twig/attachments/edit.twig
@@ -31,9 +31,9 @@
<h3 class="box-title">{{ 'optionalFields'|_ }}</h3>
</div>
<div class="box-body">
- {{ ExpandedForm.text('title') }}
- {{ ExpandedForm.textarea('description') }}
- {{ ExpandedForm.textarea('notes') }}
+ {{ ExpandedForm.text('title', attachment.title) }}
+ {{ ExpandedForm.textarea('description', attachment.description) }}
+ {{ ExpandedForm.textarea('notes', attachment.notes) }}
</div>
</div>
diff --git a/resources/twig/transactions/show.twig b/resources/twig/transactions/show.twig
index 420e96e447..095fa53220 100644
--- a/resources/twig/transactions/show.twig
+++ b/resources/twig/transactions/show.twig
@@ -93,8 +93,18 @@
</td>
<td>
<i class="fa fa-file-pdf-o"></i>
- <a href="{{ route('attachment.download', att.id) }}">{{ att.filename }}</a>
+ <a href="{{ route('attachment.download', att.id) }}" title="{{ att.filename }}">
+ {% if att.title %}
+ {{ att.title }}
+ {% else %}
+ {{ att.filename }}
+ {% endif %}
+ </a>
({{ att.size|filesize }})
+ {% if att.description %}
+ <br/>
+ <em>{{ att.description }}</em>
+ {% endif %}
</td>
</tr>
{% endfor %}