Files
firefly-iii/app/Http/Controllers/Rule/EditController.php

208 lines
7.0 KiB
PHP
Raw Normal View History

2018-07-20 14:34:56 +02:00
<?php
/**
* EditController.php
2020-01-31 07:32:04 +01:00
* Copyright (c) 2019 james@firefly-iii.org
2018-07-20 14:34:56 +02:00
*
* This file is part of Firefly III (https://github.com/firefly-iii).
2018-07-20 14:34:56 +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-07-20 14:34:56 +02:00
*
* This program is distributed in the hope that it will be useful,
2018-07-20 14:34:56 +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-07-20 14:34:56 +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-07-20 14:34:56 +02:00
*/
declare(strict_types=1);
namespace FireflyIII\Http\Controllers\Rule;
2021-03-28 11:46:23 +02:00
2018-07-20 14:34:56 +02:00
use FireflyIII\Http\Controllers\Controller;
use FireflyIII\Http\Requests\RuleFormRequest;
use FireflyIII\Models\Rule;
use FireflyIII\Repositories\Rule\RuleRepositoryInterface;
2018-12-31 08:11:57 +01:00
use FireflyIII\Support\Http\Controllers\RenderPartialViews;
2018-07-20 14:34:56 +02:00
use FireflyIII\Support\Http\Controllers\RuleManagement;
2020-08-27 20:22:52 +02:00
use FireflyIII\Support\Search\OperatorQuerySearch;
use FireflyIII\Support\Search\SearchInterface;
use Illuminate\Contracts\View\Factory;
2018-07-20 14:34:56 +02:00
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Routing\Redirector;
use Illuminate\View\View;
2020-10-01 16:52:01 +02:00
use Log;
2021-03-21 09:15:40 +01:00
use Throwable;
2018-07-20 14:34:56 +02:00
/**
* Class EditController
*/
class EditController extends Controller
{
2018-12-31 08:11:57 +01:00
use RuleManagement, RenderPartialViews;
2018-07-20 14:34:56 +02:00
2020-10-01 16:52:01 +02:00
private RuleRepositoryInterface $ruleRepos;
2018-07-20 14:34:56 +02:00
/**
* RuleController constructor.
*
* @codeCoverageIgnore
2018-07-20 14:34:56 +02:00
*/
public function __construct()
{
parent::__construct();
$this->middleware(
function ($request, $next) {
2022-03-29 14:58:06 +02:00
app('view')->share('title', (string) trans('firefly.rules'));
2018-07-20 14:34:56 +02:00
app('view')->share('mainTitleIcon', 'fa-random');
$this->ruleRepos = app(RuleRepositoryInterface::class);
return $next($request);
}
);
}
/**
2018-07-21 08:55:32 +02:00
* Edit a rule.
*
2018-07-20 14:34:56 +02:00
* @param Request $request
* @param Rule $rule
*
* @return Factory|View
2018-07-20 14:34:56 +02:00
*/
public function edit(Request $request, Rule $rule)
{
$triggerCount = 0;
$actionCount = 0;
$oldActions = [];
$oldTriggers = [];
2020-08-27 20:22:52 +02:00
// build triggers from query, if present.
2022-03-29 14:58:06 +02:00
$query = (string) $request->get('from_query');
2020-08-27 20:22:52 +02:00
if ('' !== $query) {
$search = app(SearchInterface::class);
$search->parseQuery($query);
$words = $search->getWordsAsString();
$operators = $search->getOperators()->toArray();
if ('' !== $words) {
session()->flash('warning', trans('firefly.rule_from_search_words', ['string' => $words]));
2022-03-29 15:10:05 +02:00
$operators[] = ['type' => 'description_contains', 'value' => $words];
2020-08-27 20:22:52 +02:00
}
$oldTriggers = $this->parseFromOperators($operators);
}
2018-07-20 14:34:56 +02:00
// has old input?
if (count($request->old()) > 0) {
2021-03-21 09:15:40 +01:00
$oldTriggers = $this->getPreviousTriggers($request);
$oldActions = $this->getPreviousActions($request);
2018-07-20 14:34:56 +02:00
}
2020-08-27 20:22:52 +02:00
$triggerCount = count($oldTriggers);
$actionCount = count($oldActions);
2018-07-20 14:34:56 +02:00
2020-08-27 20:22:52 +02:00
// overrule old input and query data when it has no rule data:
2018-07-20 14:34:56 +02:00
if (0 === $triggerCount && 0 === $actionCount) {
$oldTriggers = $this->getCurrentTriggers($rule);
$triggerCount = count($oldTriggers);
2018-07-20 14:34:56 +02:00
$oldActions = $this->getCurrentActions($rule);
$actionCount = count($oldActions);
2018-07-20 14:34:56 +02:00
}
$hasOldInput = null !== $request->old('_token');
$preFilled = [
2022-03-29 14:58:06 +02:00
'active' => $hasOldInput ? (bool) $request->old('active') : $rule->active,
'stop_processing' => $hasOldInput ? (bool) $request->old('stop_processing') : $rule->stop_processing,
'strict' => $hasOldInput ? (bool) $request->old('strict') : $rule->strict,
2018-07-20 14:34:56 +02:00
];
// get rule trigger for update / store-journal:
$primaryTrigger = $this->ruleRepos->getPrimaryTrigger($rule);
2022-03-29 14:58:06 +02:00
$subTitle = (string) trans('firefly.edit_rule', ['title' => $rule->title]);
2018-07-20 14:34:56 +02:00
// put previous url in session if not redirect from store (not "return_to_edit").
if (true !== session('rules.edit.fromUpdate')) {
2022-04-12 18:19:30 +02:00
$this->rememberPreviousUrl('rules.edit.url');
2018-07-20 14:34:56 +02:00
}
session()->forget('rules.edit.fromUpdate');
$request->session()->flash('preFilled', $preFilled);
return view('rules.rule.edit', compact('rule', 'subTitle', 'primaryTrigger', 'oldTriggers', 'oldActions', 'triggerCount', 'actionCount'));
2018-07-20 14:34:56 +02:00
}
2020-08-27 20:22:52 +02:00
/**
* @param array $submittedOperators
2021-03-21 09:15:40 +01:00
*
2020-08-27 20:22:52 +02:00
* @return array
*/
private function parseFromOperators(array $submittedOperators): array
{
// See reference nr. 65
$operators = config('search.operators');
2020-08-27 20:22:52 +02:00
$renderedEntries = [];
$triggers = [];
foreach ($operators as $key => $operator) {
if ('user_action' !== $key && false === $operator['alias']) {
2022-03-29 14:58:06 +02:00
$triggers[$key] = (string) trans(sprintf('firefly.rule_trigger_%s_choice', $key));
2020-08-27 20:22:52 +02:00
}
}
asort($triggers);
$index = 0;
foreach ($submittedOperators as $operator) {
try {
$renderedEntries[] = view(
2020-08-27 20:22:52 +02:00
'rules.partials.trigger',
[
'oldTrigger' => OperatorQuerySearch::getRootOperator($operator['type']),
'oldValue' => $operator['value'],
2020-10-01 17:02:15 +02:00
'oldChecked' => false,
2020-08-27 20:22:52 +02:00
'count' => $index + 1,
'triggers' => $triggers,
]
)->render();
2021-04-07 07:28:43 +02:00
} catch (Throwable $e) { // @phpstan-ignore-line
2020-08-27 20:22:52 +02:00
Log::debug(sprintf('Throwable was thrown in getPreviousTriggers(): %s', $e->getMessage()));
Log::error($e->getTraceAsString());
}
$index++;
}
return $renderedEntries;
}
2021-03-21 09:15:40 +01:00
/**
* Update the rule.
*
* @param RuleFormRequest $request
* @param Rule $rule
*
* @return RedirectResponse|Redirector
*/
public function update(RuleFormRequest $request, Rule $rule)
{
$data = $request->getRuleData();
$this->ruleRepos->update($rule, $data);
2022-03-29 14:58:06 +02:00
session()->flash('success', (string) trans('firefly.updated_rule', ['title' => $rule->title]));
2021-03-21 09:15:40 +01:00
app('preferences')->mark();
2022-04-12 18:19:30 +02:00
$redirect = redirect($this->getPreviousUrl('rules.edit.url'));
2022-03-29 14:58:06 +02:00
if (1 === (int) $request->get('return_to_edit')) {
2021-04-07 07:28:43 +02:00
2021-03-21 09:15:40 +01:00
session()->put('rules.edit.fromUpdate', true);
$redirect = redirect(route('rules.edit', [$rule->id]))->withInput(['return_to_edit' => 1]);
2021-04-07 07:28:43 +02:00
2021-03-21 09:15:40 +01:00
}
return $redirect;
}
}