firefly-iii/app/Http/Controllers/RuleController.php

630 lines
21 KiB
PHP
Raw Normal View History

2016-01-11 13:41:57 -06:00
<?php
/**
* RuleController.php
* Copyright (C) 2016 Sander Dorigo
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
namespace FireflyIII\Http\Controllers;
2016-01-13 08:59:45 -06:00
use Auth;
2016-01-14 11:09:20 -06:00
use Config;
2016-01-14 03:33:24 -06:00
use ExpandedForm;
2016-01-11 13:41:57 -06:00
use FireflyIII\Http\Requests;
2016-01-14 11:09:20 -06:00
use FireflyIII\Http\Requests\RuleFormRequest;
2016-01-13 11:34:56 -06:00
use FireflyIII\Http\Requests\RuleGroupFormRequest;
2016-01-13 09:05:39 -06:00
use FireflyIII\Models\Rule;
2016-01-15 01:45:39 -06:00
use FireflyIII\Models\RuleAction;
2016-01-13 09:08:05 -06:00
use FireflyIII\Models\RuleGroup;
2016-01-15 01:45:39 -06:00
use FireflyIII\Models\RuleTrigger;
2016-01-13 11:34:56 -06:00
use FireflyIII\Repositories\Rule\RuleRepositoryInterface;
use Input;
use Preferences;
use Response;
2016-01-13 11:34:56 -06:00
use Session;
use URL;
2016-01-11 13:41:57 -06:00
use View;
/**
* Class RuleController
*
* @package FireflyIII\Http\Controllers
*/
class RuleController extends Controller
{
2016-01-12 13:56:53 -06:00
/**
* RuleController constructor.
*/
2016-01-11 13:41:57 -06:00
public function __construct()
{
parent::__construct();
View::share('title', trans('firefly.rules'));
View::share('mainTitleIcon', 'fa-random');
}
2016-01-13 11:34:56 -06:00
/**
* @return View
*/
public function createRuleGroup()
{
$subTitleIcon = 'fa-clone';
$subTitle = trans('firefly.make_new_rule_group');
// put previous url in session if not redirect from store (not "create another").
2016-01-13 11:50:15 -06:00
if (Session::get('rules.rule-group.create.fromStore') !== true) {
Session::put('rules.rule-group.create.url', URL::previous());
2016-01-13 11:34:56 -06:00
}
2016-01-14 05:37:49 -06:00
Session::forget('rules.rule-group.create.fromStore');
2016-01-13 11:34:56 -06:00
Session::flash('gaEventCategory', 'rules');
Session::flash('gaEventAction', 'create-rule-group');
2016-01-13 11:50:15 -06:00
return view('rules.rule-group.create', compact('subTitleIcon', 'what', 'subTitle'));
2016-01-13 11:34:56 -06:00
}
2016-01-14 05:37:49 -06:00
/**
2016-01-14 14:34:17 -06:00
* @param RuleFormRequest $request
* @param RuleRepositoryInterface $repository
* @param RuleGroup $ruleGroup
2016-01-14 09:41:15 -06:00
*
2016-01-14 14:34:17 -06:00
* @return $this|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
2016-01-14 09:41:15 -06:00
*/
2016-01-14 14:34:17 -06:00
public function storeRule(RuleFormRequest $request, RuleRepositoryInterface $repository, RuleGroup $ruleGroup)
2016-01-14 09:41:15 -06:00
{
2016-01-14 14:34:17 -06:00
// process the rule itself:
$data = [
'rule_group_id' => intval($request->get('rule_group_id')),
'title' => $request->get('title'),
'trigger' => $request->get('trigger'),
'description' => $request->get('description'),
'rule-triggers' => $request->get('rule-trigger'),
'rule-trigger-values' => $request->get('rule-trigger-value'),
'rule-trigger-stop' => $request->get('rule-trigger-stop'),
'rule-actions' => $request->get('rule-action'),
'rule-action-values' => $request->get('rule-action-value'),
'rule-action-stop' => $request->get('rule-action-stop'),
2016-01-15 01:45:39 -06:00
'stop_processing' => $request->get('stop_processing'),
2016-01-14 14:34:17 -06:00
];
$rule = $repository->storeRule($data);
Session::flash('success', trans('firefly.stored_new_rule', ['title' => $rule->title]));
Preferences::mark();
if (intval(Input::get('create_another')) === 1) {
// set value so create routine will not overwrite URL:
Session::put('rules.rule.create.fromStore', true);
return redirect(route('rules.rule.create', [$request->input('what')]))->withInput();
}
// redirect to previous URL.
return redirect(Session::get('rules.rule.create.url'));
2016-01-14 09:41:15 -06:00
}
/**
* @param RuleGroup $ruleGroup
*
2016-01-14 05:37:49 -06:00
* @return View
*/
public function createRule(RuleGroup $ruleGroup)
{
2016-01-14 11:09:20 -06:00
// count for possible present previous entered triggers/actions.
$triggerCount = 0;
$actionCount = 0;
// collection of those triggers/actions.
$oldTriggers = [];
$oldActions = [];
// has old input?
if (Input::old()) {
// process old triggers.
2016-01-14 11:57:52 -06:00
$newIndex = 0;
2016-01-14 11:09:20 -06:00
foreach (Input::old('rule-trigger') as $index => $entry) {
2016-01-14 11:57:52 -06:00
$count = ($newIndex + 1);
2016-01-14 11:09:20 -06:00
$triggerCount++;
$oldTrigger = $entry;
$oldValue = Input::old('rule-trigger-value')[$index];
$oldChecked = isset(Input::old('rule-trigger-stop')[$index]) ? true : false;
2016-01-14 11:09:20 -06:00
$oldTriggers[] = view(
'rules.partials.trigger',
[
'oldTrigger' => $oldTrigger,
'oldValue' => $oldValue,
'oldChecked' => $oldChecked,
2016-01-15 01:45:39 -06:00
'count' => $count,
2016-01-14 11:09:20 -06:00
]
)->render();
2016-01-14 11:57:52 -06:00
$newIndex++;
2016-01-14 11:09:20 -06:00
}
// process old actions
$newIndex = 0;
foreach (Input::old('rule-action') as $index => $entry) {
$count = ($newIndex + 1);
$actionCount++;
$oldAction = $entry;
$oldValue = Input::old('rule-action-value')[$index];
$oldChecked = isset(Input::old('rule-action-stop')[$index]) ? true : false;
$oldActions[] = view(
'rules.partials.action',
[
'oldTrigger' => $oldAction,
'oldValue' => $oldValue,
'oldChecked' => $oldChecked,
2016-01-15 01:45:39 -06:00
'count' => $count,
]
)->render();
$newIndex++;
}
}
2016-01-14 11:09:20 -06:00
2016-01-14 05:37:49 -06:00
$subTitleIcon = 'fa-clone';
$subTitle = trans('firefly.make_new_rule', ['title' => $ruleGroup->title]);
// put previous url in session if not redirect from store (not "create another").
if (Session::get('rules.rule.create.fromStore') !== true) {
Session::put('rules.rule.create.url', URL::previous());
}
Session::forget('rules.rule.create.fromStore');
Session::flash('gaEventCategory', 'rules');
Session::flash('gaEventAction', 'create-rule');
2016-01-14 05:37:49 -06:00
return view(
2016-01-15 01:13:19 -06:00
'rules.rule.create', compact('subTitleIcon', 'oldTriggers', 'oldActions', 'triggerCount', 'actionCount', 'ruleGroup', 'subTitle')
);
2016-01-14 05:37:49 -06:00
}
2016-01-13 11:34:56 -06:00
/**
* @param RuleGroupFormRequest $request
* @param RuleRepositoryInterface $repository
*
* @return $this|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
*/
public function storeRuleGroup(RuleGroupFormRequest $request, RuleRepositoryInterface $repository)
{
$data = [
'title' => $request->input('title'),
'description' => $request->input('description'),
'user' => Auth::user()->id,
];
$ruleGroup = $repository->storeRuleGroup($data);
Session::flash('success', trans('firefly.created_new_rule_group', ['title' => $ruleGroup->title]));
Preferences::mark();
if (intval(Input::get('create_another')) === 1) {
// set value so create routine will not overwrite URL:
2016-01-13 11:50:15 -06:00
Session::put('rules.rule-group.create.fromStore', true);
2016-01-13 11:34:56 -06:00
return redirect(route('rules.rule-group.create'))->withInput();
}
// redirect to previous URL.
2016-01-13 11:50:15 -06:00
return redirect(Session::get('rules.rule-group.create.url'));
2016-01-13 11:34:56 -06:00
}
2016-01-11 13:41:57 -06:00
2016-01-15 01:45:39 -06:00
public function editRule(Rule $rule)
{
2016-01-15 01:04:57 -06:00
// count for current rule's triggers/actions.
$triggerCount = 0;
$actionCount = 0;
// collection of those triggers/actions.
2016-01-15 02:25:32 -06:00
$oldTriggers = [];
$oldActions = [];
2016-01-15 01:04:57 -06:00
// has old input?
if (Input::old()) {
// process old triggers.
$newIndex = 0;
foreach (Input::old('rule-trigger') as $index => $entry) {
$count = ($newIndex + 1);
$triggerCount++;
$oldTrigger = $entry;
$oldValue = Input::old('rule-trigger-value')[$index];
$oldChecked = isset(Input::old('rule-trigger-stop')[$index]) ? true : false;
$oldTriggers[] = view(
'rules.partials.trigger',
[
'oldTrigger' => $oldTrigger,
'oldValue' => $oldValue,
'oldChecked' => $oldChecked,
2016-01-15 01:45:39 -06:00
'count' => $count,
2016-01-15 01:04:57 -06:00
]
)->render();
$newIndex++;
}
// process old actions
$newIndex = 0;
foreach (Input::old('rule-action') as $index => $entry) {
$count = ($newIndex + 1);
$actionCount++;
$oldAction = $entry;
$oldValue = Input::old('rule-action-value')[$index];
$oldChecked = isset(Input::old('rule-action-stop')[$index]) ? true : false;
$oldActions[] = view(
'rules.partials.action',
[
'oldTrigger' => $oldAction,
'oldValue' => $oldValue,
'oldChecked' => $oldChecked,
2016-01-15 01:45:39 -06:00
'count' => $count,
]
)->render();
$newIndex++;
}
} else {
// get current triggers
$newIndex = 0;
/**
* @var int $index
* @var RuleTrigger $entry
*/
foreach ($rule->ruleTriggers as $index => $entry) {
if ($entry->trigger_type != 'user_action') {
$count = ($newIndex + 1);
$triggerCount++;
$oldTrigger = $entry->trigger_type;
$oldValue = $entry->trigger_value;
$oldChecked = $entry->stop_processing;
$oldTriggers[] = view(
'rules.partials.trigger',
[
'oldTrigger' => $oldTrigger,
'oldValue' => $oldValue,
'oldChecked' => $oldChecked,
'count' => $count,
]
)->render();
2016-01-15 02:25:32 -06:00
$newIndex++;
2016-01-15 01:45:39 -06:00
}
2016-01-15 02:25:32 -06:00
2016-01-15 01:45:39 -06:00
}
// get current actions
$newIndex = 0;
/**
2016-01-15 02:25:32 -06:00
* @var int $index
2016-01-15 01:45:39 -06:00
* @var RuleAction $entry
*/
foreach ($rule->ruleActions as $index => $entry) {
$count = ($newIndex + 1);
$actionCount++;
$oldAction = $entry->action_type;
$oldValue = $entry->action_value;
$oldChecked = $entry->stop_processing;
$oldActions[] = view(
'rules.partials.action',
[
'oldTrigger' => $oldAction,
'oldValue' => $oldValue,
'oldChecked' => $oldChecked,
'count' => $count,
2016-01-15 01:04:57 -06:00
]
)->render();
$newIndex++;
}
}
2016-01-15 01:45:39 -06:00
// get rule trigger for update / store-journal:
2016-01-15 02:25:32 -06:00
$primaryTrigger = $rule->ruleTriggers()->where('trigger_type', 'user_action')->first()->trigger_value;
2016-01-15 01:45:39 -06:00
2016-01-15 01:04:57 -06:00
2016-01-15 01:45:39 -06:00
$subTitle = trans('firefly.edit_rule', ['title' => $rule->title]);
2016-01-15 01:04:57 -06:00
// put previous url in session if not redirect from store (not "return_to_edit").
if (Session::get('rules.rule.edit.fromUpdate') !== true) {
Session::put('rules.rule.edit.url', URL::previous());
}
Session::forget('rules.rule.edit.fromUpdate');
Session::flash('gaEventCategory', 'rules');
Session::flash('gaEventAction', 'edit-rule');
2016-01-15 02:25:32 -06:00
return view('rules.rule.edit', compact('rule', 'subTitle', 'primaryTrigger',
2016-01-15 01:45:39 -06:00
'oldTriggers', 'oldActions', 'triggerCount', 'actionCount'));
2016-01-15 01:04:57 -06:00
}
2016-01-15 02:25:32 -06:00
/**
* @param RuleRepositoryInterface $repository
* @param RuleFormRequest $request
* @param Rule $rule
* @return $this|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
*/
public function updateRule(RuleRepositoryInterface $repository, RuleFormRequest $request, Rule $rule)
{
// process the rule itself:
$data = [
'title' => $request->get('title'),
'active' => intval($request->get('active')) == 1,
'trigger' => $request->get('trigger'),
'description' => $request->get('description'),
'rule-triggers' => $request->get('rule-trigger'),
'rule-trigger-values' => $request->get('rule-trigger-value'),
'rule-trigger-stop' => $request->get('rule-trigger-stop'),
'rule-actions' => $request->get('rule-action'),
'rule-action-values' => $request->get('rule-action-value'),
'rule-action-stop' => $request->get('rule-action-stop'),
'stop_processing' => intval($request->get('stop_processing')) == 1,
];
$repository->updateRule($rule, $data);
Session::flash('success', trans('firefly.updated_rule', ['title' => $rule->title]));
Preferences::mark();
if (intval(Input::get('return_to_edit')) === 1) {
// set value so edit routine will not overwrite URL:
Session::put('rules.rule.edit.fromUpdate', true);
return redirect(route('rules.rule.edit', [$rule->id]))->withInput(['return_to_edit' => 1]);
}
// redirect to previous URL.
return redirect(Session::get('rules.rule.edit.url'));
}
2016-01-13 11:50:15 -06:00
2016-01-12 13:56:53 -06:00
/**
2016-01-13 11:50:15 -06:00
* @param RuleGroup $ruleGroup
*
2016-01-12 13:56:53 -06:00
* @return View
*/
2016-01-13 11:50:15 -06:00
public function editRuleGroup(RuleGroup $ruleGroup)
2016-01-11 13:41:57 -06:00
{
2016-01-13 11:50:15 -06:00
$subTitle = trans('firefly.edit_rule_group', ['title' => $ruleGroup->title]);
// put previous url in session if not redirect from store (not "return_to_edit").
if (Session::get('rules.rule-group.edit.fromUpdate') !== true) {
Session::put('rules.rule-group.edit.url', URL::previous());
}
Session::forget('rules.rule-group.edit.fromUpdate');
Session::flash('gaEventCategory', 'rules');
Session::flash('gaEventAction', 'edit-rule-group');
return view('rules.rule-group.edit', compact('ruleGroup', 'subTitle'));
2016-01-13 08:59:45 -06:00
2016-01-11 13:41:57 -06:00
}
2016-01-13 09:05:39 -06:00
/**
2016-01-13 11:50:15 -06:00
* @param RuleGroupFormRequest $request
* @param RuleRepositoryInterface $repository
* @param RuleGroup $ruleGroup
*
* @return $this|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
2016-01-13 09:05:39 -06:00
*/
2016-01-13 11:50:15 -06:00
public function updateRuleGroup(RuleGroupFormRequest $request, RuleRepositoryInterface $repository, RuleGroup $ruleGroup)
2016-01-13 09:05:39 -06:00
{
2016-01-13 11:50:15 -06:00
$data = [
'title' => $request->input('title'),
'description' => $request->input('description'),
'active' => intval($request->input('active')) == 1,
];
$repository->updateRuleGroup($ruleGroup, $data);
2016-01-13 11:50:15 -06:00
Session::flash('success', trans('firefly.updated_rule_group', ['title' => $ruleGroup->title]));
Preferences::mark();
if (intval(Input::get('return_to_edit')) === 1) {
// set value so edit routine will not overwrite URL:
Session::put('rules.rule-group.edit.fromUpdate', true);
return redirect(route('rules.rule-group.edit', [$ruleGroup->id]))->withInput(['return_to_edit' => 1]);
}
// redirect to previous URL.
return redirect(Session::get('rules.rule-group.edit.url'));
2016-01-13 09:05:39 -06:00
}
2016-01-13 09:08:05 -06:00
/**
2016-01-14 14:34:17 -06:00
* @param RuleRepositoryInterface $repository
* @param Rule $rule
*
2016-01-14 14:34:17 -06:00
* @return View
*/
public function deleteRule(Rule $rule)
{
$subTitle = trans('firefly.delete_rule', ['title' => $rule->title]);
// put previous url in session
Session::put('rules.rule.delete.url', URL::previous());
Session::flash('gaEventCategory', 'rules');
Session::flash('gaEventAction', 'delete-rule');
return view('rules.rule.delete', compact('rule', 'subTitle'));
}
/**
* @param RuleRepositoryInterface $repository
* @param RuleGroup $ruleGroup
*
* @return View
*/
2016-01-14 03:33:24 -06:00
public function deleteRuleGroup(RuleRepositoryInterface $repository, RuleGroup $ruleGroup)
{
$subTitle = trans('firefly.delete_rule_group', ['title' => $ruleGroup->title]);
2016-01-14 03:33:24 -06:00
$ruleGroupList = Expandedform::makeSelectList($repository->getRuleGroups(), true);
unset($ruleGroupList[$ruleGroup->id]);
// put previous url in session
Session::put('rules.rule-group.delete.url', URL::previous());
Session::flash('gaEventCategory', 'rules');
Session::flash('gaEventAction', 'delete-rule-group');
2016-01-14 03:33:24 -06:00
return view('rules.rule-group.delete', compact('ruleGroup', 'subTitle', 'ruleGroupList'));
2016-01-14 14:34:17 -06:00
}
/**
* @param Rule $rule
* @param RuleRepositoryInterface $repository
*
* @return \Illuminate\Http\RedirectResponse
*/
public function destroyRule(RuleRepositoryInterface $repository, Rule $rule)
{
$title = $rule->title;
$repository->destroyRule($rule);
Session::flash('success', trans('firefly.deleted_rule', ['title' => $title]));
Preferences::mark();
return redirect(Session::get('rules.rule.delete.url'));
}
/**
* @param RuleGroup $ruleGroup
* @param RuleRepositoryInterface $repository
*
* @return \Illuminate\Http\RedirectResponse
*/
public function destroyRuleGroup(RuleRepositoryInterface $repository, RuleGroup $ruleGroup)
{
$title = $ruleGroup->title;
$moveTo = Auth::user()->ruleGroups()->find(intval(Input::get('move_rules_before_delete')));
2016-01-14 03:33:24 -06:00
$repository->destroyRuleGroup($ruleGroup, $moveTo);
Session::flash('success', trans('firefly.deleted_rule_group', ['title' => $title]));
Preferences::mark();
return redirect(Session::get('rules.rule-group.delete.url'));
}
/**
* @param RuleRepositoryInterface $repository
* @param Rule $rule
2016-01-14 09:41:15 -06:00
*
* @return \Illuminate\Http\JsonResponse
*/
public function reorderRuleTriggers(RuleRepositoryInterface $repository, Rule $rule)
{
$ids = Input::get('triggers');
if (is_array($ids)) {
$repository->reorderRuleTriggers($rule, $ids);
}
return Response::json(true);
}
/**
* @param RuleRepositoryInterface $repository
* @param Rule $rule
2016-01-14 09:41:15 -06:00
*
* @return \Illuminate\Http\JsonResponse
*/
public function reorderRuleActions(RuleRepositoryInterface $repository, Rule $rule)
{
$ids = Input::get('actions');
if (is_array($ids)) {
$repository->reorderRuleActions($rule, $ids);
}
return Response::json(true);
}
2016-01-13 11:50:15 -06:00
2016-01-13 09:08:05 -06:00
/**
2016-01-13 11:50:15 -06:00
* @return View
2016-01-13 09:08:05 -06:00
*/
2016-01-13 11:50:15 -06:00
public function index()
2016-01-13 11:34:56 -06:00
{
$ruleGroups = Auth::user()
->ruleGroups()
2016-01-15 03:52:35 -06:00
->orderBy('active', 'DESC')
->orderBy('order', 'ASC')
->with(
[
'rules' => function ($query) {
2016-01-15 03:52:35 -06:00
$query->orderBy('active', 'DESC');
$query->orderBy('order', 'ASC');
},
'rules.ruleTriggers' => function ($query) {
$query->orderBy('order', 'ASC');
},
'rules.ruleActions' => function ($query) {
$query->orderBy('order', 'ASC');
},
]
)->get();
2016-01-13 09:08:05 -06:00
2016-01-13 11:50:15 -06:00
return view('rules.index', compact('ruleGroups'));
2016-01-13 09:08:05 -06:00
}
2016-01-13 11:50:15 -06:00
2016-01-14 02:38:48 -06:00
/**
* @param RuleRepositoryInterface $repository
* @param Rule $rule
2016-01-14 09:41:15 -06:00
*
2016-01-14 02:38:48 -06:00
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
*/
public function upRule(RuleRepositoryInterface $repository, Rule $rule)
{
$repository->moveRuleUp($rule);
return redirect(route('rules.index'));
}
2016-01-13 11:50:15 -06:00
/**
2016-01-14 02:38:48 -06:00
* @param RuleRepositoryInterface $repository
* @param Rule $rule
2016-01-14 09:41:15 -06:00
*
2016-01-14 02:38:48 -06:00
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
2016-01-13 11:50:15 -06:00
*/
2016-01-14 02:38:48 -06:00
public function downRule(RuleRepositoryInterface $repository, Rule $rule)
2016-01-13 11:50:15 -06:00
{
2016-01-14 02:38:48 -06:00
$repository->moveRuleDown($rule);
return redirect(route('rules.index'));
2016-01-13 11:50:15 -06:00
}
2016-01-14 02:53:59 -06:00
/**
* @param RuleRepositoryInterface $repository
* @param RuleGroup $ruleGroup
2016-01-14 09:41:15 -06:00
*
2016-01-14 02:53:59 -06:00
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
*/
public function upRuleGroup(RuleRepositoryInterface $repository, RuleGroup $ruleGroup)
{
$repository->moveRuleGroupUp($ruleGroup);
return redirect(route('rules.index'));
}
/**
* @param RuleRepositoryInterface $repository
* @param RuleGroup $ruleGroup
2016-01-14 09:41:15 -06:00
*
2016-01-14 02:53:59 -06:00
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
*/
public function downRuleGroup(RuleRepositoryInterface $repository, RuleGroup $ruleGroup)
{
$repository->moveRuleGroupDown($ruleGroup);
return redirect(route('rules.index'));
}
2016-01-11 13:41:57 -06:00
}