mirror of
https://github.com/firefly-iii/firefly-iii.git
synced 2025-09-19 10:53:37 +00:00
Move notes for attachments to different object. This sacrifices the original notes.
This commit is contained in:
@@ -26,6 +26,7 @@ use DB;
|
|||||||
use FireflyIII\Models\Account;
|
use FireflyIII\Models\Account;
|
||||||
use FireflyIII\Models\AccountMeta;
|
use FireflyIII\Models\AccountMeta;
|
||||||
use FireflyIII\Models\AccountType;
|
use FireflyIII\Models\AccountType;
|
||||||
|
use FireflyIII\Models\Attachment;
|
||||||
use FireflyIII\Models\Note;
|
use FireflyIII\Models\Note;
|
||||||
use FireflyIII\Models\Transaction;
|
use FireflyIII\Models\Transaction;
|
||||||
use FireflyIII\Models\TransactionCurrency;
|
use FireflyIII\Models\TransactionCurrency;
|
||||||
@@ -86,6 +87,7 @@ class UpgradeDatabase extends Command
|
|||||||
$this->updateOtherCurrencies();
|
$this->updateOtherCurrencies();
|
||||||
$this->line('Done updating currency information..');
|
$this->line('Done updating currency information..');
|
||||||
$this->migrateNotes();
|
$this->migrateNotes();
|
||||||
|
$this->migrateAttachmentData();
|
||||||
$this->info('Firefly III database is up to date.');
|
$this->info('Firefly III database is up to date.');
|
||||||
|
|
||||||
return;
|
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.
|
* Move all the journal_meta notes to their note object counter parts.
|
||||||
*
|
*
|
||||||
|
@@ -141,6 +141,9 @@ class AttachmentController extends Controller
|
|||||||
}
|
}
|
||||||
$request->session()->forget('attachments.edit.fromUpdate');
|
$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'));
|
return view('attachments.edit', compact('attachment', 'subTitleIcon', 'subTitle'));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -44,7 +44,6 @@ class AttachmentFormRequest extends Request
|
|||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
'title' => $this->string('title'),
|
'title' => $this->string('title'),
|
||||||
'description' => $this->string('description'),
|
|
||||||
'notes' => $this->string('notes'),
|
'notes' => $this->string('notes'),
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
@@ -57,7 +56,6 @@ class AttachmentFormRequest extends Request
|
|||||||
// fixed
|
// fixed
|
||||||
return [
|
return [
|
||||||
'title' => 'between:1,255|nullable',
|
'title' => 'between:1,255|nullable',
|
||||||
'description' => 'between:1,65536|nullable',
|
|
||||||
'notes' => 'between:1,65536|nullable',
|
'notes' => 'between:1,65536|nullable',
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
@@ -49,7 +49,7 @@ class Attachment extends Model
|
|||||||
'uploaded' => 'boolean',
|
'uploaded' => 'boolean',
|
||||||
];
|
];
|
||||||
/** @var array */
|
/** @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
|
* @param string $value
|
||||||
@@ -142,7 +142,7 @@ class Attachment extends Model
|
|||||||
* @codeCoverageIgnore
|
* @codeCoverageIgnore
|
||||||
* @return null|string
|
* @return null|string
|
||||||
*/
|
*/
|
||||||
public function getNotesAttribute($value)
|
public function getTitleAttribute($value)
|
||||||
{
|
{
|
||||||
if (null === $value || 0 === strlen($value)) {
|
if (null === $value || 0 === strlen($value)) {
|
||||||
return null;
|
return null;
|
||||||
@@ -152,18 +152,12 @@ class Attachment extends Model
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $value
|
|
||||||
*
|
|
||||||
* @codeCoverageIgnore
|
* @codeCoverageIgnore
|
||||||
* @return null|string
|
* Get all of the notes.
|
||||||
*/
|
*/
|
||||||
public function getTitleAttribute($value)
|
public function notes()
|
||||||
{
|
{
|
||||||
if (null === $value || 0 === strlen($value)) {
|
return $this->morphMany(Note::class, 'noteable');
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
return Crypt::decrypt($value);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -196,16 +190,6 @@ class Attachment extends Model
|
|||||||
$this->attributes['mime'] = Crypt::encrypt($value);
|
$this->attributes['mime'] = Crypt::encrypt($value);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* @codeCoverageIgnore
|
|
||||||
*
|
|
||||||
* @param string $value
|
|
||||||
*/
|
|
||||||
public function setNotesAttribute(string $value)
|
|
||||||
{
|
|
||||||
$this->attributes['notes'] = Crypt::encrypt($value);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @codeCoverageIgnore
|
* @codeCoverageIgnore
|
||||||
*
|
*
|
||||||
|
@@ -26,6 +26,7 @@ use Carbon\Carbon;
|
|||||||
use Crypt;
|
use Crypt;
|
||||||
use FireflyIII\Helpers\Attachments\AttachmentHelperInterface;
|
use FireflyIII\Helpers\Attachments\AttachmentHelperInterface;
|
||||||
use FireflyIII\Models\Attachment;
|
use FireflyIII\Models\Attachment;
|
||||||
|
use FireflyIII\Models\Note;
|
||||||
use FireflyIII\User;
|
use FireflyIII\User;
|
||||||
use Illuminate\Support\Collection;
|
use Illuminate\Support\Collection;
|
||||||
use Log;
|
use Log;
|
||||||
@@ -153,6 +154,23 @@ class AttachmentRepository implements AttachmentRepositoryInterface
|
|||||||
return $content;
|
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
|
* @param User $user
|
||||||
*/
|
*/
|
||||||
@@ -170,10 +188,36 @@ class AttachmentRepository implements AttachmentRepositoryInterface
|
|||||||
public function update(Attachment $attachment, array $data): Attachment
|
public function update(Attachment $attachment, array $data): Attachment
|
||||||
{
|
{
|
||||||
$attachment->title = $data['title'];
|
$attachment->title = $data['title'];
|
||||||
$attachment->description = $data['description'];
|
|
||||||
$attachment->notes = $data['notes'];
|
|
||||||
$attachment->save();
|
$attachment->save();
|
||||||
|
$this->updateNote($attachment, $data['notes']);
|
||||||
|
|
||||||
return $attachment;
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@@ -32,6 +32,7 @@ use Illuminate\Support\Collection;
|
|||||||
*/
|
*/
|
||||||
interface AttachmentRepositoryInterface
|
interface AttachmentRepositoryInterface
|
||||||
{
|
{
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param Attachment $attachment
|
* @param Attachment $attachment
|
||||||
*
|
*
|
||||||
@@ -80,6 +81,15 @@ interface AttachmentRepositoryInterface
|
|||||||
*/
|
*/
|
||||||
public function getContent(Attachment $attachment): string;
|
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
|
* @param User $user
|
||||||
*/
|
*/
|
||||||
|
42
database/migrations/2018_03_19_141348_changes_for_v472.php
Normal file
42
database/migrations/2018_03_19_141348_changes_for_v472.php
Normal 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);
|
||||||
|
}
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
@@ -32,8 +32,7 @@
|
|||||||
</div>
|
</div>
|
||||||
<div class="box-body">
|
<div class="box-body">
|
||||||
{{ ExpandedForm.text('title', attachment.title) }}
|
{{ ExpandedForm.text('title', attachment.title) }}
|
||||||
{{ ExpandedForm.textarea('description', attachment.description) }}
|
{{ ExpandedForm.textarea('notes',null,{helpText: trans('firefly.field_supports_markdown')}) }}
|
||||||
{{ ExpandedForm.textarea('notes', attachment.notes) }}
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
@@ -314,9 +314,8 @@
|
|||||||
{% endif %}
|
{% endif %}
|
||||||
</a>
|
</a>
|
||||||
({{ att.size|filesize }})
|
({{ att.size|filesize }})
|
||||||
{% if att.description %}
|
{% if att.notes.first %}
|
||||||
<br/>
|
{{ att.notes.first.text|markdown}}
|
||||||
<em>{{ att.description }}</em>
|
|
||||||
{% endif %}
|
{% endif %}
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
|
Reference in New Issue
Block a user