Skip to content

Firefly III: a personal finances manager

Move notes for attachments to different object. This sacrifices the original notes.

James Cole909f72e6bea2
diff --git a/app/Console/Commands/UpgradeDatabase.php b/app/Console/Commands/UpgradeDatabase.php
index 4342e1666e..f5c8c45e4a 100644
--- a/app/Console/Commands/UpgradeDatabase.php
+++ b/app/Console/Commands/UpgradeDatabase.php
@@ -26,6 +26,7 @@ use DB;
use FireflyIII\Models\Account;
use FireflyIII\Models\AccountMeta;
use FireflyIII\Models\AccountType;
+use FireflyIII\Models\Attachment;
use FireflyIII\Models\Note;
use FireflyIII\Models\Transaction;
use FireflyIII\Models\TransactionCurrency;
@@ -86,6 +87,7 @@ class UpgradeDatabase extends Command
$this->updateOtherCurrencies();
$this->line('Done updating currency information..');
$this->migrateNotes();
+ $this->migrateAttachmentData();
$this->info('Firefly III database is up to date.');
return;
@@ -281,6 +283,38 @@ class UpgradeDatabase extends Command
}
}
+ /**
+ * Move the description of each attachment (when not NULL) to the notes or to a new note object
+ * for all attachments.
+ */
+ private function migrateAttachmentData(): void
+ {
+ $attachments = Attachment::get();
+
+ /** @var Attachment $att */
+ foreach ($attachments as $att) {
+
+ // move description:
+ $description = strval($att->description);
+ if (strlen($description) > 0) {
+ // find or create note:
+ $note = $att->notes()->first();
+ if (is_null($note)) {
+ $note = new Note;
+ $note->noteable()->associate($att);
+ }
+ $note->text = $description;
+ $note->save();
+
+ // clear description:
+ $att->description = '';
+ $att->save();
+
+ Log::debug(sprintf('Migrated attachment #%s description to note #%d', $att->id, $note->id));
+ }
+ }
+ }
+
/**
* Move all the journal_meta notes to their note object counter parts.
*
diff --git a/app/Http/Controllers/AttachmentController.php b/app/Http/Controllers/AttachmentController.php
index 74cd0b66ee..14617d4970 100644
--- a/app/Http/Controllers/AttachmentController.php
+++ b/app/Http/Controllers/AttachmentController.php
@@ -141,6 +141,9 @@ class AttachmentController extends Controller
}
$request->session()->forget('attachments.edit.fromUpdate');
+ $preFilled['notes'] = $this->repository->getNoteText($attachment);
+ $request->session()->flash('preFilled', $preFilled);
+
return view('attachments.edit', compact('attachment', 'subTitleIcon', 'subTitle'));
}
diff --git a/app/Http/Requests/AttachmentFormRequest.php b/app/Http/Requests/AttachmentFormRequest.php
index 1e31e6973f..a05ac1ac87 100644
--- a/app/Http/Requests/AttachmentFormRequest.php
+++ b/app/Http/Requests/AttachmentFormRequest.php
@@ -44,7 +44,6 @@ class AttachmentFormRequest extends Request
{
return [
'title' => $this->string('title'),
- 'description' => $this->string('description'),
'notes' => $this->string('notes'),
];
}
@@ -57,7 +56,6 @@ class AttachmentFormRequest extends Request
// fixed
return [
'title' => 'between:1,255|nullable',
- 'description' => 'between:1,65536|nullable',
'notes' => 'between:1,65536|nullable',
];
}
diff --git a/app/Models/Attachment.php b/app/Models/Attachment.php
index f78478dde8..32699744a2 100644
--- a/app/Models/Attachment.php
+++ b/app/Models/Attachment.php
@@ -49,7 +49,7 @@ class Attachment extends Model
'uploaded' => 'boolean',
];
/** @var array */
- protected $fillable = ['attachable_id', 'attachable_type', 'user_id', 'md5', 'filename', 'mime', 'title', 'notes', 'description', 'size', 'uploaded'];
+ protected $fillable = ['attachable_id', 'attachable_type', 'user_id', 'md5', 'filename', 'mime', 'title', 'description', 'size', 'uploaded'];
/**
* @param string $value
@@ -142,7 +142,7 @@ class Attachment extends Model
* @codeCoverageIgnore
* @return null|string
*/
- public function getNotesAttribute($value)
+ public function getTitleAttribute($value)
{
if (null === $value || 0 === strlen($value)) {
return null;
@@ -152,18 +152,12 @@ class Attachment extends Model
}
/**
- * @param $value
- *
* @codeCoverageIgnore
- * @return null|string
+ * Get all of the notes.
*/
- public function getTitleAttribute($value)
+ public function notes()
{
- if (null === $value || 0 === strlen($value)) {
- return null;
- }
-
- return Crypt::decrypt($value);
+ return $this->morphMany(Note::class, 'noteable');
}
/**
@@ -196,16 +190,6 @@ class Attachment extends Model
$this->attributes['mime'] = Crypt::encrypt($value);
}
- /**
- * @codeCoverageIgnore
- *
- * @param string $value
- */
- public function setNotesAttribute(string $value)
- {
- $this->attributes['notes'] = Crypt::encrypt($value);
- }
-
/**
* @codeCoverageIgnore
*
diff --git a/app/Repositories/Attachment/AttachmentRepository.php b/app/Repositories/Attachment/AttachmentRepository.php
index 05e3176293..dc8f1bbbbc 100644
--- a/app/Repositories/Attachment/AttachmentRepository.php
+++ b/app/Repositories/Attachment/AttachmentRepository.php
@@ -26,6 +26,7 @@ use Carbon\Carbon;
use Crypt;
use FireflyIII\Helpers\Attachments\AttachmentHelperInterface;
use FireflyIII\Models\Attachment;
+use FireflyIII\Models\Note;
use FireflyIII\User;
use Illuminate\Support\Collection;
use Log;
@@ -153,6 +154,23 @@ class AttachmentRepository implements AttachmentRepositoryInterface
return $content;
}
+ /**
+ * Get attachment note text or empty string.
+ *
+ * @param Attachment $attachment
+ *
+ * @return string
+ */
+ public function getNoteText(Attachment $attachment): string
+ {
+ $note = $attachment->notes()->first();
+ if (!is_null($note)) {
+ return strval($note->text);
+ }
+
+ return '';
+ }
+
/**
* @param User $user
*/
@@ -169,11 +187,37 @@ class AttachmentRepository implements AttachmentRepositoryInterface
*/
public function update(Attachment $attachment, array $data): Attachment
{
- $attachment->title = $data['title'];
- $attachment->description = $data['description'];
- $attachment->notes = $data['notes'];
+ $attachment->title = $data['title'];
$attachment->save();
+ $this->updateNote($attachment, $data['notes']);
return $attachment;
}
+
+ /**
+ * @param Attachment $attachment
+ * @param string $note
+ *
+ * @return bool
+ */
+ public function updateNote(Attachment $attachment, string $note): bool
+ {
+ if (0 === strlen($note)) {
+ $dbNote = $attachment->notes()->first();
+ if (null !== $dbNote) {
+ $dbNote->delete();
+ }
+
+ return true;
+ }
+ $dbNote = $attachment->notes()->first();
+ if (null === $dbNote) {
+ $dbNote = new Note;
+ $dbNote->noteable()->associate($attachment);
+ }
+ $dbNote->text = trim($note);
+ $dbNote->save();
+
+ return true;
+ }
}
diff --git a/app/Repositories/Attachment/AttachmentRepositoryInterface.php b/app/Repositories/Attachment/AttachmentRepositoryInterface.php
index 262d776cb2..43ae9100db 100644
--- a/app/Repositories/Attachment/AttachmentRepositoryInterface.php
+++ b/app/Repositories/Attachment/AttachmentRepositoryInterface.php
@@ -32,6 +32,7 @@ use Illuminate\Support\Collection;
*/
interface AttachmentRepositoryInterface
{
+
/**
* @param Attachment $attachment
*
@@ -80,6 +81,15 @@ interface AttachmentRepositoryInterface
*/
public function getContent(Attachment $attachment): string;
+ /**
+ * Get attachment note text or empty string.
+ *
+ * @param Attachment $attachment
+ *
+ * @return string
+ */
+ public function getNoteText(Attachment $attachment): string;
+
/**
* @param User $user
*/
diff --git a/database/migrations/2018_03_19_141348_changes_for_v472.php b/database/migrations/2018_03_19_141348_changes_for_v472.php
new file mode 100644
index 0000000000..6d675f098e
--- /dev/null
+++ b/database/migrations/2018_03_19_141348_changes_for_v472.php
@@ -0,0 +1,42 @@
+<?php
+
+use Illuminate\Database\Migrations\Migration;
+use Illuminate\Database\Schema\Blueprint;
+
+/**
+ * Class ChangesForV472
+ */
+class ChangesForV472 extends Migration
+{
+ /**
+ * Reverse the migrations.
+ *
+ * @return void
+ */
+ public function down()
+ {
+ //
+ }
+
+ /**
+ * Run the migrations.
+ *
+ * @return void
+ */
+ public function up()
+ {
+ Schema::table(
+ 'attachments',
+ function (Blueprint $table) {
+ $table->dropColumn('notes');
+ }
+ );
+
+ Schema::table(
+ 'budgets',
+ function (Blueprint $table) {
+ $table->mediumInteger('order', false, true)->default(0);
+ }
+ );
+ }
+}
diff --git a/resources/views/attachments/edit.twig b/resources/views/attachments/edit.twig
index affb40ef0f..1c663ccdb8 100644
--- a/resources/views/attachments/edit.twig
+++ b/resources/views/attachments/edit.twig
@@ -32,8 +32,7 @@
</div>
<div class="box-body">
{{ ExpandedForm.text('title', attachment.title) }}
- {{ ExpandedForm.textarea('description', attachment.description) }}
- {{ ExpandedForm.textarea('notes', attachment.notes) }}
+ {{ ExpandedForm.textarea('notes',null,{helpText: trans('firefly.field_supports_markdown')}) }}
</div>
</div>
diff --git a/resources/views/transactions/show.twig b/resources/views/transactions/show.twig
index f6e2702124..d5f4ce084d 100644
--- a/resources/views/transactions/show.twig
+++ b/resources/views/transactions/show.twig
@@ -314,9 +314,8 @@
{% endif %}
</a>
({{ att.size|filesize }})
- {% if att.description %}
- <br/>
- <em>{{ att.description }}</em>
+ {% if att.notes.first %}
+ {{ att.notes.first.text|markdown}}
{% endif %}
</td>
</tr>