Firefly III: a personal finances manager
PHP accountingbudgetbudgetingbudgetscash-flowcashflowcredit-carddockerexpensesfinancefinancesfinanciallinuxmoneypaycheckpersonal-financephpphp7
Can edit category notes
James Cole6b20b7ecdbba
diff --git a/app/Http/Controllers/Category/EditController.php b/app/Http/Controllers/Category/EditController.phpindex 1aa925a062..56d9d346fb 100644--- a/app/Http/Controllers/Category/EditController.php+++ b/app/Http/Controllers/Category/EditController.php@@ -41,11 +41,8 @@ use Illuminate\View\View;class EditController extends Controller{- /** @var CategoryRepositoryInterface The category repository */- private $repository;-- /** @var AttachmentHelperInterface Helper for attachments. */- private $attachments;+ private CategoryRepositoryInterface $repository;+ private AttachmentHelperInterface $attachments;/*** CategoryController constructor.@@ -58,9 +55,9 @@ class EditController extends Controller$this->middleware(function ($request, $next) {- app('view')->share('title', (string) trans('firefly.categories'));+ app('view')->share('title', (string)trans('firefly.categories'));app('view')->share('mainTitleIcon', 'fa-bookmark');- $this->repository = app(CategoryRepositoryInterface::class);+ $this->repository = app(CategoryRepositoryInterface::class);$this->attachments = app(AttachmentHelperInterface::class);return $next($request);@@ -79,7 +76,7 @@ class EditController extends Controller*/public function edit(Request $request, Category $category){- $subTitle = (string) trans('firefly.edit_category', ['name' => $category->name]);+ $subTitle = (string)trans('firefly.edit_category', ['name' => $category->name]);// put previous url in session if not redirect from store (not "return_to_edit").if (true !== session('categories.edit.fromUpdate')) {@@ -87,7 +84,11 @@ class EditController extends Controller}$request->session()->forget('categories.edit.fromUpdate');- return view('categories.edit', compact('category', 'subTitle'));+ $preFilled = [+ 'notes' => $request->old('notes') ?? $this->repository->getNoteText($category),+ ];++ return view('categories.edit', compact('category', 'subTitle', 'preFilled'));}/**@@ -103,7 +104,7 @@ class EditController extends Controller$data = $request->getCategoryData();$this->repository->update($category, $data);- $request->session()->flash('success', (string) trans('firefly.updated_category', ['name' => $category->name]));+ $request->session()->flash('success', (string)trans('firefly.updated_category', ['name' => $category->name]));app('preferences')->mark();// store new attachment(s):@@ -112,7 +113,7 @@ class EditController extends Controller$this->attachments->saveAttachmentsForModel($category, $files);}if (null !== $files && auth()->user()->hasRole('demo')) {- session()->flash('info',(string)trans('firefly.no_att_demo_user'));+ session()->flash('info', (string)trans('firefly.no_att_demo_user'));}if (count($this->attachments->getMessages()->get('attachments')) > 0) {@@ -122,7 +123,7 @@ class EditController extends Controller$redirect = redirect($this->getPreviousUri('categories.edit.uri'));- if (1 === (int) $request->get('return_to_edit')) {+ if (1 === (int)$request->get('return_to_edit')) {// @codeCoverageIgnoreStart$request->session()->put('categories.edit.fromUpdate', true);diff --git a/app/Services/Internal/Update/CategoryUpdateService.php b/app/Services/Internal/Update/CategoryUpdateService.phpindex 75f9722707..b6d1612b78 100644--- a/app/Services/Internal/Update/CategoryUpdateService.php+++ b/app/Services/Internal/Update/CategoryUpdateService.php@@ -24,6 +24,7 @@ declare(strict_types=1);namespace FireflyIII\Services\Internal\Update;use FireflyIII\Models\Category;+use FireflyIII\Models\Note;use FireflyIII\Models\RecurrenceTransactionMeta;use FireflyIII\Models\RuleAction;use FireflyIII\Models\RuleTrigger;@@ -68,6 +69,7 @@ class CategoryUpdateService$this->updateRuleTriggers($oldName, $data['name']);$this->updateRuleActions($oldName, $data['name']);$this->updateRecurrences($oldName, $data['name']);+ $this->updateNotes($category, $data);return $category;}@@ -137,4 +139,40 @@ class CategoryUpdateService->update(['rt_meta.value' => $newName]);}++ /**+ * @param Category $category+ * @param array $data+ *+ * @throws \Exception+ */+ private function updateNotes(Category $category, array $data): void+ {+ $note = array_key_exists('notes', $data) ? $data['notes'] : null;+ if (null === $note) {+ return;+ }+ if ('' === $note) {+ $dbNote = $category->notes()->first();+ if (null !== $dbNote) {+ try {+ $dbNote->delete();+ } catch (Exception $e) {+ Log::debug($e->getMessage());+ }+ }++ return;+ }+ $dbNote = $category->notes()->first();+ if (null === $dbNote) {+ $dbNote = new Note;+ $dbNote->noteable()->associate($category);+ }+ $dbNote->text = trim($note);+ $dbNote->save();++ return;+ }+}diff --git a/resources/views/v1/categories/edit.twig b/resources/views/v1/categories/edit.twigindex ff2478a09d..62421a4d32 100644--- a/resources/views/v1/categories/edit.twig+++ b/resources/views/v1/categories/edit.twig@@ -25,6 +25,7 @@<h3 class="box-title">{{ 'optionalFields'|_ }}</h3></div><div class="box-body">+ {{ ExpandedForm.textarea('notes', preFilled.notes, {helpText: trans('firefly.field_supports_markdown')} ) }}{{ ExpandedForm.file('attachments[]', {'multiple': 'multiple','helpText': trans('firefly.upload_max_file_size', {'size': uploadSize|filesize}) }) }}</div></div>