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

115 lines
3.4 KiB
PHP
Raw Normal View History

2018-07-20 14:34:56 +02:00
<?php
/**
* IndexController.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;
use FireflyIII\Http\Controllers\Controller;
use FireflyIII\Models\Rule;
2020-06-26 07:28:25 +02:00
use FireflyIII\Models\RuleGroup;
2018-07-20 14:34:56 +02:00
use FireflyIII\Repositories\Rule\RuleRepositoryInterface;
use FireflyIII\Repositories\RuleGroup\RuleGroupRepositoryInterface;
use FireflyIII\Support\Http\Controllers\RuleManagement;
use FireflyIII\User;
use Illuminate\Contracts\View\Factory;
2018-07-20 14:34:56 +02:00
use Illuminate\Http\JsonResponse;
2020-08-27 06:35:53 +02:00
use Illuminate\Http\RedirectResponse;
2018-07-20 14:34:56 +02:00
use Illuminate\Http\Request;
use Illuminate\View\View;
2018-07-20 14:34:56 +02:00
/**
* Class IndexController
*/
class IndexController extends Controller
{
use RuleManagement;
2020-08-27 06:35:53 +02:00
private RuleGroupRepositoryInterface $ruleGroupRepos;
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) {
app('view')->share('title', (string) trans('firefly.rules'));
2018-07-20 14:34:56 +02:00
app('view')->share('mainTitleIcon', 'fa-random');
$this->ruleGroupRepos = app(RuleGroupRepositoryInterface::class);
$this->ruleRepos = app(RuleRepositoryInterface::class);
return $next($request);
}
);
}
/**
2018-07-21 08:55:32 +02:00
* Index of all rules and groups.
*
* @return Factory|View
2018-07-20 14:34:56 +02:00
*/
public function index()
{
/** @var User $user */
$user = auth()->user();
$this->createDefaultRuleGroup();
$this->createDefaultRule();
$this->ruleGroupRepos->resetOrder();
2021-02-12 20:15:23 +01:00
$ruleGroups = $this->ruleGroupRepos->getRuleGroupsWithRules(null);
2018-07-20 14:34:56 +02:00
return prefixView('rules.index', compact('ruleGroups'));
2018-07-20 14:34:56 +02:00
}
2020-08-27 06:35:53 +02:00
/**
* @param Rule $rule
* @return RedirectResponse
* @throws \FireflyIII\Exceptions\FireflyException
*/
public function search(Rule $rule): RedirectResponse
{
2020-10-17 08:53:32 +02:00
$route = route('search.index');
2020-08-27 07:12:44 +02:00
$query = $this->ruleRepos->getSearchQuery($rule);
2020-08-27 06:35:53 +02:00
$route = sprintf('%s?%s', $route, http_build_query(['search' => $query, 'rule' => $rule->id]));
return redirect($route);
}
2018-07-20 14:34:56 +02:00
/**
2020-06-26 07:28:25 +02:00
* @param Request $request
* @param Rule $rule
* @param RuleGroup $ruleGroup
2018-08-06 19:14:30 +02:00
*
2020-06-26 07:28:25 +02:00
* @return JsonResponse
2018-07-20 14:34:56 +02:00
*/
2020-06-26 07:28:25 +02:00
public function moveRule(Request $request, Rule $rule, RuleGroup $ruleGroup): JsonResponse
2018-07-20 14:34:56 +02:00
{
2020-06-26 07:28:25 +02:00
$order = (int) $request->get('order');
$this->ruleRepos->moveRule($rule, $ruleGroup, (int) $order);
2018-07-20 14:34:56 +02:00
2020-06-26 07:28:25 +02:00
return response()->json([]);
2018-07-20 14:34:56 +02:00
}
}