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

This commit is contained in:
James Cole 2018-03-19 15:28:35 +01:00
parent 6a1d39d5f8
commit 909f72e6be
No known key found for this signature in database
GPG Key ID: C16961E655E74B5E
9 changed files with 144 additions and 31 deletions

View File

@ -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.
*

View File

@ -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'));
}

View File

@ -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',
];
}

View File

@ -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
*

View File

@ -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;
}
}

View File

@ -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
*/

View File

@ -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);
}
);
}
}

View File

@ -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>

View File

@ -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>