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

209 lines
6.5 KiB
PHP
Raw Normal View History

<?php
2016-02-05 05:08:25 -06:00
declare(strict_types = 1);
namespace FireflyIII\Http\Controllers;
2016-01-15 06:06:17 -06:00
use Auth;
use ExpandedForm;
use FireflyIII\Http\Requests\RuleGroupFormRequest;
use FireflyIII\Models\RuleGroup;
use FireflyIII\Repositories\RuleGroup\RuleGroupRepositoryInterface;
use Input;
use Preferences;
use Session;
use URL;
use View;
2016-01-15 06:06:17 -06:00
/**
* Class RuleGroupController
2016-01-15 06:06:17 -06:00
*
* @package FireflyIII\Http\Controllers
*/
class RuleGroupController extends Controller
{
2016-01-15 06:06:17 -06:00
/**
* RuleGroupController constructor.
*/
public function __construct()
{
parent::__construct();
View::share('title', trans('firefly.rules'));
View::share('mainTitleIcon', 'fa-random');
}
2016-01-15 06:06:17 -06:00
/**
* @return View
*/
2016-01-15 06:09:27 -06:00
public function create()
2016-01-15 06:06:17 -06:00
{
$subTitleIcon = 'fa-clone';
$subTitle = trans('firefly.make_new_rule_group');
// put previous url in session if not redirect from store (not "create another").
2016-02-04 00:27:03 -06:00
if (session('rules.rule-group.create.fromStore') !== true) {
2016-01-15 06:06:17 -06:00
Session::put('rules.rule-group.create.url', URL::previous());
}
Session::forget('rules.rule-group.create.fromStore');
Session::flash('gaEventCategory', 'rules');
Session::flash('gaEventAction', 'create-rule-group');
return view('rules.rule-group.create', compact('subTitleIcon', 'subTitle'));
2016-01-15 06:06:17 -06:00
}
2016-01-24 08:58:16 -06:00
/**
* @param RuleGroupRepositoryInterface $repository
* @param RuleGroup $ruleGroup
*
* @return View
*/
public function delete(RuleGroupRepositoryInterface $repository, RuleGroup $ruleGroup)
{
$subTitle = trans('firefly.delete_rule_group', ['title' => $ruleGroup->title]);
$ruleGroupList = Expandedform::makeSelectList($repository->get(), 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');
return view('rules.rule-group.delete', compact('ruleGroup', 'subTitle', 'ruleGroupList'));
}
2016-01-15 06:06:17 -06:00
/**
2016-01-15 06:08:25 -06:00
* @param RuleGroupRepositoryInterface $repository
2016-01-15 06:06:17 -06:00
*
2016-01-24 08:58:16 -06:00
* @param RuleGroup $ruleGroup
*
* @return \Illuminate\Http\RedirectResponse
2016-01-15 06:06:17 -06:00
*/
2016-01-24 08:58:16 -06:00
public function destroy(RuleGroupRepositoryInterface $repository, RuleGroup $ruleGroup)
2016-01-15 06:06:17 -06:00
{
2016-01-24 08:58:16 -06:00
$title = $ruleGroup->title;
$moveTo = Auth::user()->ruleGroups()->find(intval(Input::get('move_rules_before_delete')));
2016-01-15 06:06:17 -06:00
2016-01-24 08:58:16 -06:00
$repository->destroy($ruleGroup, $moveTo);
Session::flash('success', trans('firefly.deleted_rule_group', ['title' => $title]));
2016-01-15 06:06:17 -06:00
Preferences::mark();
2016-02-04 00:27:03 -06:00
return redirect(session('rules.rule-group.delete.url'));
2016-01-24 08:58:16 -06:00
}
/**
* @param RuleGroupRepositoryInterface $repository
* @param RuleGroup $ruleGroup
*
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
*/
public function down(RuleGroupRepositoryInterface $repository, RuleGroup $ruleGroup)
{
$repository->moveDown($ruleGroup);
return redirect(route('rules.index'));
2016-01-15 06:06:17 -06:00
}
/**
* @param RuleGroup $ruleGroup
*
* @return View
*/
2016-01-15 06:09:27 -06:00
public function edit(RuleGroup $ruleGroup)
2016-01-15 06:06:17 -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").
2016-02-04 00:27:03 -06:00
if (session('rules.rule-group.edit.fromUpdate') !== true) {
2016-01-15 06:06:17 -06:00
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-15 06:08:25 -06:00
* @param RuleGroupFormRequest $request
* @param RuleGroupRepositoryInterface $repository
2016-01-15 06:06:17 -06:00
*
* @return $this|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
*/
2016-01-24 08:58:16 -06:00
public function store(RuleGroupFormRequest $request, RuleGroupRepositoryInterface $repository)
2016-01-15 06:06:17 -06:00
{
$data = [
'title' => $request->input('title'),
'description' => $request->input('description'),
2016-01-24 08:58:16 -06:00
'user' => Auth::user()->id,
2016-01-15 06:06:17 -06:00
];
2016-01-24 08:58:16 -06:00
$ruleGroup = $repository->store($data);
2016-01-15 06:06:17 -06:00
2016-01-24 08:58:16 -06:00
Session::flash('success', trans('firefly.created_new_rule_group', ['title' => $ruleGroup->title]));
2016-01-15 06:06:17 -06:00
Preferences::mark();
2016-01-24 08:58:16 -06:00
if (intval(Input::get('create_another')) === 1) {
// set value so create routine will not overwrite URL:
Session::put('rules.rule-group.create.fromStore', true);
2016-01-15 06:06:17 -06:00
2016-01-24 08:58:16 -06:00
return redirect(route('rules.rule-group.create'))->withInput();
2016-01-15 06:06:17 -06:00
}
// redirect to previous URL.
2016-02-04 00:27:03 -06:00
return redirect(session('rules.rule-group.create.url'));
2016-01-15 06:06:17 -06:00
}
/**
2016-01-15 06:08:25 -06:00
* @param RuleGroupRepositoryInterface $repository
* @param RuleGroup $ruleGroup
2016-01-15 06:06:17 -06:00
*
2016-01-24 08:58:16 -06:00
* @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
2016-01-15 06:06:17 -06:00
*/
2016-01-24 08:58:16 -06:00
public function up(RuleGroupRepositoryInterface $repository, RuleGroup $ruleGroup)
2016-01-15 06:06:17 -06:00
{
2016-01-24 08:58:16 -06:00
$repository->moveUp($ruleGroup);
2016-01-15 06:06:17 -06:00
2016-01-24 08:58:16 -06:00
return redirect(route('rules.index'));
2016-01-15 06:06:17 -06:00
}
/**
2016-01-24 08:58:16 -06:00
* @param RuleGroupFormRequest $request
2016-01-15 06:08:25 -06:00
* @param RuleGroupRepositoryInterface $repository
* @param RuleGroup $ruleGroup
2016-01-15 06:06:17 -06:00
*
2016-01-24 08:58:16 -06:00
* @return $this|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
2016-01-15 06:06:17 -06:00
*/
2016-01-24 08:58:16 -06:00
public function update(RuleGroupFormRequest $request, RuleGroupRepositoryInterface $repository, RuleGroup $ruleGroup)
2016-01-15 06:06:17 -06:00
{
2016-01-24 08:58:16 -06:00
$data = [
'title' => $request->input('title'),
'description' => $request->input('description'),
'active' => intval($request->input('active')) == 1,
];
2016-01-15 06:06:17 -06:00
2016-01-24 08:58:16 -06:00
$repository->update($ruleGroup, $data);
2016-01-15 06:06:17 -06:00
2016-01-24 08:58:16 -06:00
Session::flash('success', trans('firefly.updated_rule_group', ['title' => $ruleGroup->title]));
2016-01-15 06:06:17 -06:00
Preferences::mark();
2016-01-24 08:58:16 -06:00
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);
2016-01-15 06:06:17 -06:00
2016-01-24 08:58:16 -06:00
return redirect(route('rules.rule-group.edit', [$ruleGroup->id]))->withInput(['return_to_edit' => 1]);
}
2016-01-15 06:06:17 -06:00
2016-01-24 08:58:16 -06:00
// redirect to previous URL.
2016-02-04 00:27:03 -06:00
return redirect(session('rules.rule-group.edit.url'));
2016-01-15 06:06:17 -06:00
}
}