2018-07-20 14:34:56 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* CreateController.php
|
2020-01-31 07:32:04 +01:00
|
|
|
* Copyright (c) 2019 james@firefly-iii.org
|
2018-07-20 14:34:56 +02:00
|
|
|
*
|
2019-10-02 06:37:26 +02:00
|
|
|
* This file is part of Firefly III (https://github.com/firefly-iii).
|
2018-07-20 14:34:56 +02:00
|
|
|
*
|
2019-10-02 06:37:26 +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
|
|
|
*
|
2019-10-02 06:37:26 +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
|
2019-10-02 06:37:26 +02:00
|
|
|
* GNU Affero General Public License for more details.
|
2018-07-20 14:34:56 +02:00
|
|
|
*
|
2019-10-02 06:37:26 +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;
|
|
|
|
|
|
|
|
use FireflyIII\Http\Controllers\Controller;
|
|
|
|
use FireflyIII\Http\Requests\RuleFormRequest;
|
|
|
|
use FireflyIII\Models\Bill;
|
2020-03-14 21:01:21 +01:00
|
|
|
use FireflyIII\Models\Rule;
|
2018-07-20 14:34:56 +02:00
|
|
|
use FireflyIII\Models\RuleGroup;
|
2020-01-25 06:32:15 +01:00
|
|
|
use FireflyIII\Models\TransactionJournal;
|
2018-07-20 14:34:56 +02:00
|
|
|
use FireflyIII\Repositories\Rule\RuleRepositoryInterface;
|
2018-12-31 08:11:57 +01:00
|
|
|
use FireflyIII\Support\Http\Controllers\ModelInformation;
|
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\SearchInterface;
|
2020-03-17 15:01:00 +01:00
|
|
|
use Illuminate\Contracts\View\Factory;
|
2018-07-20 14:34:56 +02:00
|
|
|
use Illuminate\Http\RedirectResponse;
|
|
|
|
use Illuminate\Http\Request;
|
2020-03-17 15:01:00 +01:00
|
|
|
use Illuminate\Routing\Redirector;
|
|
|
|
use Illuminate\View\View;
|
2018-07-20 14:34:56 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class CreateController
|
|
|
|
*/
|
|
|
|
class CreateController extends Controller
|
|
|
|
{
|
2018-12-31 08:11:57 +01:00
|
|
|
use RuleManagement, ModelInformation;
|
2020-08-27 20:22:52 +02:00
|
|
|
|
2020-08-28 05:51:02 +02:00
|
|
|
private RuleRepositoryInterface $ruleRepos;
|
2018-07-20 14:34:56 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* RuleController constructor.
|
2020-01-25 06:32:15 +01:00
|
|
|
*
|
2019-07-01 20:22:35 +02:00
|
|
|
* @codeCoverageIgnore
|
2018-07-20 14:34:56 +02:00
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
parent::__construct();
|
|
|
|
|
|
|
|
$this->middleware(
|
|
|
|
function ($request, $next) {
|
2021-03-21 09:15:40 +01: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);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new rule. It will be stored under the given $ruleGroup.
|
|
|
|
*
|
2020-08-28 05:51:02 +02:00
|
|
|
* @param Request $request
|
|
|
|
* @param RuleGroup|null $ruleGroup
|
2018-07-20 14:34:56 +02:00
|
|
|
*
|
2020-03-17 15:01:00 +01:00
|
|
|
* @return Factory|View
|
2018-07-20 14:34:56 +02:00
|
|
|
*/
|
2018-08-05 15:34:20 +02:00
|
|
|
public function create(Request $request, RuleGroup $ruleGroup = null)
|
2018-07-20 14:34:56 +02:00
|
|
|
{
|
|
|
|
$this->createDefaultRuleGroup();
|
2018-08-05 15:34:20 +02:00
|
|
|
$preFilled = [
|
2018-07-20 14:34:56 +02:00
|
|
|
'strict' => true,
|
|
|
|
];
|
2018-08-05 15:34:20 +02:00
|
|
|
$oldTriggers = [];
|
|
|
|
$oldActions = [];
|
2018-07-20 14:34:56 +02:00
|
|
|
|
2020-08-27 20:22:52 +02:00
|
|
|
// build triggers from query, if present.
|
2021-03-21 09:15:40 +01: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]));
|
|
|
|
array_push($operators, ['type' => 'description_contains', 'value' => $words]);
|
|
|
|
}
|
|
|
|
$oldTriggers = $this->parseFromOperators($operators);
|
|
|
|
}
|
|
|
|
|
2018-08-05 15:34:20 +02:00
|
|
|
// restore actions and triggers from old input:
|
2018-07-20 14:34:56 +02:00
|
|
|
if ($request->old()) {
|
|
|
|
$oldTriggers = $this->getPreviousTriggers($request);
|
|
|
|
$oldActions = $this->getPreviousActions($request);
|
|
|
|
}
|
|
|
|
|
2019-05-30 12:31:19 +02:00
|
|
|
$triggerCount = count($oldTriggers);
|
|
|
|
$actionCount = count($oldActions);
|
2018-07-20 14:34:56 +02:00
|
|
|
$subTitleIcon = 'fa-clone';
|
|
|
|
|
2018-08-05 15:34:20 +02:00
|
|
|
// title depends on whether or not there is a rule group:
|
2021-03-21 09:15:40 +01:00
|
|
|
$subTitle = (string)trans('firefly.make_new_rule_no_group');
|
2018-08-05 15:34:20 +02:00
|
|
|
if (null !== $ruleGroup) {
|
2021-03-21 09:15:40 +01:00
|
|
|
$subTitle = (string)trans('firefly.make_new_rule', ['title' => $ruleGroup->title]);
|
2018-08-05 15:34:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// flash old data
|
2018-07-20 14:34:56 +02:00
|
|
|
$request->session()->flash('preFilled', $preFilled);
|
|
|
|
|
|
|
|
// put previous url in session if not redirect from store (not "create another").
|
|
|
|
if (true !== session('rules.create.fromStore')) {
|
|
|
|
$this->rememberPreviousUri('rules.create.uri');
|
|
|
|
}
|
|
|
|
session()->forget('rules.create.fromStore');
|
|
|
|
|
2021-01-31 20:35:54 +01:00
|
|
|
return prefixView(
|
2020-03-17 15:01:00 +01:00
|
|
|
'rules.rule.create',
|
|
|
|
compact('subTitleIcon', 'oldTriggers', 'preFilled', 'oldActions', 'triggerCount', 'actionCount', 'ruleGroup', 'subTitle')
|
2018-07-20 14:34:56 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-08-05 18:59:15 +02:00
|
|
|
/**
|
|
|
|
* Create a new rule. It will be stored under the given $ruleGroup.
|
|
|
|
*
|
|
|
|
* @param Request $request
|
|
|
|
* @param Bill $bill
|
|
|
|
*
|
2020-03-17 15:01:00 +01:00
|
|
|
* @return Factory|View
|
2018-08-05 18:59:15 +02:00
|
|
|
*/
|
|
|
|
public function createFromBill(Request $request, Bill $bill)
|
|
|
|
{
|
2021-03-21 09:15:40 +01:00
|
|
|
$request->session()->flash('info', (string)trans('firefly.instructions_rule_from_bill', ['name' => e($bill->name)]));
|
2018-08-05 18:59:15 +02:00
|
|
|
|
|
|
|
$this->createDefaultRuleGroup();
|
|
|
|
$preFilled = [
|
|
|
|
'strict' => true,
|
2021-03-21 09:15:40 +01:00
|
|
|
'title' => (string)trans('firefly.new_rule_for_bill_title', ['name' => $bill->name]),
|
|
|
|
'description' => (string)trans('firefly.new_rule_for_bill_description', ['name' => $bill->name]),
|
2018-08-05 18:59:15 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
// make triggers and actions from the bill itself.
|
|
|
|
|
|
|
|
// get triggers and actions for bill:
|
|
|
|
$oldTriggers = $this->getTriggersForBill($bill);
|
|
|
|
$oldActions = $this->getActionsForBill($bill);
|
|
|
|
|
2020-08-26 19:41:50 +02:00
|
|
|
// restore actions and triggers from old input:
|
|
|
|
if ($request->old()) {
|
|
|
|
$oldTriggers = $this->getPreviousTriggers($request);
|
|
|
|
$oldActions = $this->getPreviousActions($request);
|
|
|
|
}
|
|
|
|
|
2019-05-30 12:31:19 +02:00
|
|
|
$triggerCount = count($oldTriggers);
|
|
|
|
$actionCount = count($oldActions);
|
2018-08-05 18:59:15 +02:00
|
|
|
$subTitleIcon = 'fa-clone';
|
|
|
|
|
|
|
|
// title depends on whether or not there is a rule group:
|
2021-03-21 09:15:40 +01:00
|
|
|
$subTitle = (string)trans('firefly.make_new_rule_no_group');
|
2018-08-05 18:59:15 +02:00
|
|
|
|
|
|
|
// flash old data
|
|
|
|
$request->session()->flash('preFilled', $preFilled);
|
|
|
|
|
|
|
|
// put previous url in session if not redirect from store (not "create another").
|
|
|
|
if (true !== session('rules.create.fromStore')) {
|
|
|
|
$this->rememberPreviousUri('rules.create.uri');
|
|
|
|
}
|
|
|
|
session()->forget('rules.create.fromStore');
|
|
|
|
|
2021-01-31 20:35:54 +01:00
|
|
|
return prefixView(
|
2020-03-17 15:01:00 +01:00
|
|
|
'rules.rule.create',
|
|
|
|
compact('subTitleIcon', 'oldTriggers', 'preFilled', 'oldActions', 'triggerCount', 'actionCount', 'subTitle')
|
2018-08-05 18:59:15 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-01-25 06:32:15 +01:00
|
|
|
/**
|
|
|
|
* @param Request $request
|
|
|
|
* @param TransactionJournal $journal
|
|
|
|
*/
|
|
|
|
public function createFromJournal(Request $request, TransactionJournal $journal)
|
|
|
|
{
|
2021-04-07 07:53:05 +02:00
|
|
|
$request->session()->flash('info', (string)trans('firefly.instructions_rule_from_journal', ['name' => e($journal->description)]));
|
2020-01-25 06:32:15 +01:00
|
|
|
|
|
|
|
$subTitleIcon = 'fa-clone';
|
2021-03-21 09:15:40 +01:00
|
|
|
$subTitle = (string)trans('firefly.make_new_rule_no_group');
|
2020-01-25 06:32:15 +01:00
|
|
|
|
|
|
|
// get triggers and actions for journal.
|
2020-08-27 20:22:52 +02:00
|
|
|
$oldTriggers = $this->getTriggersForJournal($journal);
|
|
|
|
$oldActions = [];
|
2020-01-25 06:32:15 +01:00
|
|
|
|
|
|
|
$this->createDefaultRuleGroup();
|
|
|
|
|
|
|
|
// collect pre-filled information:
|
|
|
|
$preFilled = [
|
|
|
|
'strict' => true,
|
2021-03-21 09:15:40 +01:00
|
|
|
'title' => (string)trans('firefly.new_rule_for_journal_title', ['description' => $journal->description]),
|
|
|
|
'description' => (string)trans('firefly.new_rule_for_journal_description', ['description' => $journal->description]),
|
2020-01-25 06:32:15 +01:00
|
|
|
];
|
|
|
|
|
2020-08-26 19:41:50 +02:00
|
|
|
// restore actions and triggers from old input:
|
|
|
|
if ($request->old()) {
|
|
|
|
$oldTriggers = $this->getPreviousTriggers($request);
|
|
|
|
$oldActions = $this->getPreviousActions($request);
|
|
|
|
}
|
|
|
|
|
|
|
|
$triggerCount = count($oldTriggers);
|
|
|
|
$actionCount = count($oldActions);
|
|
|
|
|
2020-01-25 06:32:15 +01:00
|
|
|
// flash old data
|
|
|
|
$request->session()->flash('preFilled', $preFilled);
|
|
|
|
|
|
|
|
// put previous url in session if not redirect from store (not "create another").
|
|
|
|
if (true !== session('rules.create.fromStore')) {
|
|
|
|
$this->rememberPreviousUri('rules.create.uri');
|
|
|
|
}
|
|
|
|
session()->forget('rules.create.fromStore');
|
|
|
|
|
2021-01-31 20:35:54 +01:00
|
|
|
return prefixView(
|
2020-03-17 15:01:00 +01:00
|
|
|
'rules.rule.create',
|
|
|
|
compact('subTitleIcon', 'oldTriggers', 'preFilled', 'oldActions', 'triggerCount', 'actionCount', 'subTitle')
|
2020-01-25 06:32:15 +01:00
|
|
|
);
|
2020-03-17 15:01:00 +01:00
|
|
|
}
|
2020-01-25 06:32:15 +01:00
|
|
|
|
2020-03-17 15:01:00 +01:00
|
|
|
/**
|
|
|
|
* @param Rule $rule
|
|
|
|
*
|
|
|
|
* @return RedirectResponse
|
|
|
|
*/
|
|
|
|
public function duplicate(Rule $rule): RedirectResponse
|
|
|
|
{
|
|
|
|
/** @var Rule $newRule */
|
|
|
|
$newRule = $this->ruleRepos->duplicate($rule);
|
|
|
|
|
|
|
|
session()->flash('success', trans('firefly.duplicated_rule', ['title' => $rule->title, 'newTitle' => $newRule->title]));
|
|
|
|
|
|
|
|
return redirect(route('rules.index'));
|
2020-01-25 06:32:15 +01:00
|
|
|
}
|
|
|
|
|
2018-07-20 14:34:56 +02:00
|
|
|
/**
|
2018-07-21 08:55:32 +02:00
|
|
|
* Store the new rule.
|
|
|
|
*
|
2018-07-20 14:34:56 +02:00
|
|
|
* @param RuleFormRequest $request
|
|
|
|
*
|
2020-03-17 15:01:00 +01:00
|
|
|
* @return RedirectResponse|Redirector
|
2019-08-17 10:47:10 +02:00
|
|
|
*
|
2018-07-20 14:34:56 +02:00
|
|
|
*/
|
|
|
|
public function store(RuleFormRequest $request)
|
|
|
|
{
|
|
|
|
$data = $request->getRuleData();
|
|
|
|
$rule = $this->ruleRepos->store($data);
|
2021-03-21 09:15:40 +01:00
|
|
|
session()->flash('success', (string)trans('firefly.stored_new_rule', ['title' => $rule->title]));
|
2018-07-20 14:34:56 +02:00
|
|
|
app('preferences')->mark();
|
|
|
|
|
|
|
|
// redirect to show bill.
|
2021-03-21 09:15:40 +01:00
|
|
|
if ('true' === $request->get('return_to_bill') && (int)$request->get('bill_id') > 0) {
|
2021-04-07 07:28:43 +02:00
|
|
|
return redirect(route('bills.show', [(int)$request->get('bill_id')]));
|
2018-07-20 14:34:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// redirect to new bill creation.
|
2021-03-21 09:15:40 +01:00
|
|
|
if ((int)$request->get('bill_id') > 0) {
|
2021-04-07 07:28:43 +02:00
|
|
|
return redirect($this->getPreviousUri('bills.create.uri'));
|
2018-07-20 14:34:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$redirect = redirect($this->getPreviousUri('rules.create.uri'));
|
|
|
|
|
2021-03-21 09:15:40 +01:00
|
|
|
if (1 === (int)$request->get('create_another')) {
|
2021-04-07 07:28:43 +02:00
|
|
|
|
2018-07-20 14:34:56 +02:00
|
|
|
session()->put('rules.create.fromStore', true);
|
|
|
|
$redirect = redirect(route('rules.create', [$data['rule_group_id']]))->withInput();
|
2021-04-07 07:28:43 +02:00
|
|
|
|
2018-07-20 14:34:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $redirect;
|
|
|
|
}
|
2018-07-22 20:32:02 +02:00
|
|
|
}
|