Files
firefly-iii/app/Services/Internal/Update/CategoryUpdateService.php

155 lines
4.9 KiB
PHP
Raw Normal View History

2018-03-25 09:01:43 +02:00
<?php
/**
* CategoryUpdateService.php
2020-02-16 13:56:35 +01:00
* Copyright (c) 2019 james@firefly-iii.org
2018-03-25 09:01:43 +02:00
*
* This file is part of Firefly III (https://github.com/firefly-iii).
2018-03-25 09:01:43 +02:00
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
2018-03-25 09:01:43 +02:00
*
* This program is distributed in the hope that it will be useful,
2018-03-25 09:01:43 +02:00
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
2018-03-25 09:01:43 +02:00
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
2018-03-25 09:01:43 +02:00
*/
declare(strict_types=1);
namespace FireflyIII\Services\Internal\Update;
use FireflyIII\Models\Category;
2020-11-05 06:25:56 +01:00
use FireflyIII\Models\Note;
2020-10-18 07:59:28 +02:00
use FireflyIII\Models\RecurrenceTransactionMeta;
2020-03-20 15:52:20 +01:00
use FireflyIII\Models\RuleAction;
use FireflyIII\Models\RuleTrigger;
2023-11-04 11:31:14 +01:00
use FireflyIII\User;
2018-03-25 09:01:43 +02:00
/**
* Class CategoryUpdateService
*/
class CategoryUpdateService
{
2023-11-04 11:31:14 +01:00
private User $user;
/**
* Constructor.
*/
public function __construct()
{
2020-10-18 07:59:28 +02:00
if (auth()->check()) {
2023-11-04 11:31:14 +01:00
/** @var User $user */
2023-12-10 06:51:59 +01:00
$user = auth()->user();
2023-11-04 11:31:14 +01:00
$this->user = $user;
2020-04-08 06:43:58 +02:00
}
}
2021-03-12 18:31:19 +01:00
/**
2023-06-21 12:34:58 +02:00
* @param mixed $user
2021-03-12 18:31:19 +01:00
*/
public function setUser($user): void
{
$this->user = $user;
}
2018-03-25 09:01:43 +02:00
/**
2023-12-20 19:35:52 +01:00
* @throws \Exception
2018-03-25 09:01:43 +02:00
*/
public function update(Category $category, array $data): Category
{
2021-03-15 10:31:11 +01:00
$oldName = $category->name;
if (array_key_exists('name', $data)) {
2021-03-14 06:20:23 +01:00
$category->name = $data['name'];
$category->save();
// update triggers and actions
$this->updateRuleTriggers($oldName, $data['name']);
$this->updateRuleActions($oldName, $data['name']);
$this->updateRecurrences($oldName, $data['name']);
}
2018-03-25 09:01:43 +02:00
2020-11-05 06:25:56 +01:00
$this->updateNotes($category, $data);
2020-03-20 15:52:20 +01:00
2018-03-25 09:01:43 +02:00
return $category;
}
2023-06-21 12:34:58 +02:00
private function updateRuleTriggers(string $oldName, string $newName): void
2023-05-29 13:56:55 +02:00
{
2023-12-20 19:35:52 +01:00
$types = ['category_is'];
2023-06-21 12:34:58 +02:00
$triggers = RuleTrigger::leftJoin('rules', 'rules.id', '=', 'rule_triggers.rule_id')
2023-12-20 19:35:52 +01:00
->where('rules.user_id', $this->user->id)
->whereIn('rule_triggers.trigger_type', $types)
->where('rule_triggers.trigger_value', $oldName)
->get(['rule_triggers.*'])
;
2023-10-29 06:33:43 +01:00
app('log')->debug(sprintf('Found %d triggers to update.', $triggers->count()));
2023-12-20 19:35:52 +01:00
2023-06-21 12:34:58 +02:00
/** @var RuleTrigger $trigger */
foreach ($triggers as $trigger) {
$trigger->trigger_value = $newName;
$trigger->save();
2023-10-29 06:33:43 +01:00
app('log')->debug(sprintf('Updated trigger %d: %s', $trigger->id, $trigger->trigger_value));
2023-05-29 13:56:55 +02:00
}
2020-03-20 15:52:20 +01:00
}
2021-03-12 18:31:19 +01:00
private function updateRuleActions(string $oldName, string $newName): void
2020-04-08 06:43:58 +02:00
{
2023-12-20 19:35:52 +01:00
$types = ['set_category'];
2021-03-12 18:31:19 +01:00
$actions = RuleAction::leftJoin('rules', 'rules.id', '=', 'rule_actions.rule_id')
2023-12-20 19:35:52 +01:00
->where('rules.user_id', $this->user->id)
->whereIn('rule_actions.action_type', $types)
->where('rule_actions.action_value', $oldName)
->get(['rule_actions.*'])
;
2023-10-29 06:33:43 +01:00
app('log')->debug(sprintf('Found %d actions to update.', $actions->count()));
2023-12-20 19:35:52 +01:00
2021-03-12 18:31:19 +01:00
/** @var RuleAction $action */
foreach ($actions as $action) {
$action->action_value = $newName;
$action->save();
2023-10-29 06:33:43 +01:00
app('log')->debug(sprintf('Updated action %d: %s', $action->id, $action->action_value));
2021-03-12 18:31:19 +01:00
}
2020-04-08 06:43:58 +02:00
}
2023-06-21 12:34:58 +02:00
private function updateRecurrences(string $oldName, string $newName): void
2020-11-05 06:25:56 +01:00
{
2023-06-21 12:34:58 +02:00
RecurrenceTransactionMeta::leftJoin('recurrences_transactions', 'rt_meta.rt_id', '=', 'recurrences_transactions.id')
2023-12-20 19:35:52 +01:00
->leftJoin('recurrences', 'recurrences.id', '=', 'recurrences_transactions.recurrence_id')
->where('recurrences.user_id', $this->user->id)
->where('rt_meta.name', 'category_name')
->where('rt_meta.value', $oldName)
->update(['rt_meta.value' => $newName])
;
2023-06-21 12:34:58 +02:00
}
/**
2023-12-20 19:35:52 +01:00
* @throws \Exception
2023-06-21 12:34:58 +02:00
*/
private function updateNotes(Category $category, array $data): void
{
$note = array_key_exists('notes', $data) ? $data['notes'] : null;
2023-06-21 12:34:58 +02:00
if (null === $note) {
return;
2020-11-05 06:25:56 +01:00
}
2023-06-21 12:34:58 +02:00
if ('' === $note) {
$dbNote = $category->notes()->first();
if (null !== $dbNote) {
$dbNote->delete();
}
return;
}
$dbNote = $category->notes()->first();
2023-06-21 12:34:58 +02:00
if (null === $dbNote) {
$dbNote = new Note();
$dbNote->noteable()->associate($category);
}
$dbNote->text = trim($note);
$dbNote->save();
2020-11-05 06:25:56 +01:00
}
}