2016-05-20 08:57:45 +02:00
|
|
|
<?php
|
2024-11-25 04:18:55 +01:00
|
|
|
|
2019-10-02 06:37:26 +02:00
|
|
|
/**
|
|
|
|
* RuleController.php
|
2020-01-31 07:32:04 +01:00
|
|
|
* Copyright (c) 2019 james@firefly-iii.org
|
2019-10-02 06:37:26 +02:00
|
|
|
*
|
|
|
|
* This file is part of Firefly III (https://github.com/firefly-iii).
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2019-08-17 12:09:03 +02:00
|
|
|
declare(strict_types=1);
|
2017-04-09 07:44:22 +02:00
|
|
|
|
2019-07-21 17:15:06 +02:00
|
|
|
namespace FireflyIII\Http\Controllers\Json;
|
2021-03-28 11:46:23 +02:00
|
|
|
|
2022-11-02 06:25:37 +01:00
|
|
|
use FireflyIII\Exceptions\FireflyException;
|
2019-07-21 17:15:06 +02:00
|
|
|
use FireflyIII\Http\Controllers\Controller;
|
2018-07-08 12:28:42 +02:00
|
|
|
use Illuminate\Http\JsonResponse;
|
2016-12-28 13:02:56 +01:00
|
|
|
use Illuminate\Http\Request;
|
2015-03-29 07:51:56 +02:00
|
|
|
|
2015-02-24 22:53:38 +01:00
|
|
|
/**
|
2019-07-21 17:15:06 +02:00
|
|
|
* Class RuleController
|
2015-02-24 22:53:38 +01:00
|
|
|
*/
|
2019-07-21 17:15:06 +02:00
|
|
|
class RuleController extends Controller
|
2015-03-06 08:20:27 +01:00
|
|
|
{
|
2016-01-14 16:41:15 +01:00
|
|
|
/**
|
2018-07-22 08:10:16 +02:00
|
|
|
* Render HTML form for rule action.
|
|
|
|
*
|
2023-02-22 18:03:31 +01:00
|
|
|
* @throws FireflyException
|
2016-01-14 16:41:15 +01:00
|
|
|
*/
|
2018-07-08 12:28:42 +02:00
|
|
|
public function action(Request $request): JsonResponse
|
2016-01-14 16:41:15 +01:00
|
|
|
{
|
2024-12-22 08:43:12 +01:00
|
|
|
$count = (int) $request->get('count') > 0 ? (int) $request->get('count') : 1;
|
2016-04-26 21:40:15 +02:00
|
|
|
$keys = array_keys(config('firefly.rule-actions'));
|
2016-01-14 16:41:15 +01:00
|
|
|
$actions = [];
|
|
|
|
foreach ($keys as $key) {
|
2024-12-22 08:43:12 +01:00
|
|
|
$actions[$key] = (string) trans('firefly.rule_action_'.$key.'_choice');
|
2016-01-14 16:41:15 +01:00
|
|
|
}
|
2023-12-20 19:35:52 +01:00
|
|
|
|
2018-08-04 17:30:06 +02:00
|
|
|
try {
|
2022-01-29 14:11:12 +01:00
|
|
|
$view = view('rules.partials.action', compact('actions', 'count'))->render();
|
2023-12-20 19:35:52 +01:00
|
|
|
} catch (\Throwable $e) {
|
2023-10-29 06:32:00 +01:00
|
|
|
app('log')->error(sprintf('Cannot render rules.partials.action: %s', $e->getMessage()));
|
|
|
|
app('log')->error($e->getTraceAsString());
|
2018-08-04 17:30:06 +02:00
|
|
|
$view = 'Could not render view.';
|
2023-12-20 19:35:52 +01:00
|
|
|
|
2022-11-02 06:25:37 +01:00
|
|
|
throw new FireflyException($view, 0, $e);
|
2018-08-04 17:30:06 +02:00
|
|
|
}
|
2021-09-18 10:26:12 +02:00
|
|
|
|
2018-03-10 20:30:09 +01:00
|
|
|
return response()->json(['html' => $view]);
|
2016-01-14 16:41:15 +01:00
|
|
|
}
|
|
|
|
|
2016-01-24 15:58:16 +01:00
|
|
|
/**
|
2018-07-22 08:10:16 +02:00
|
|
|
* Render HTML for rule trigger.
|
|
|
|
*
|
2023-02-22 18:03:31 +01:00
|
|
|
* @throws FireflyException
|
2016-01-24 15:58:16 +01:00
|
|
|
*/
|
2018-07-08 12:28:42 +02:00
|
|
|
public function trigger(Request $request): JsonResponse
|
2016-01-24 15:58:16 +01:00
|
|
|
{
|
2024-12-22 08:43:12 +01:00
|
|
|
$count = (int) $request->get('count') > 0 ? (int) $request->get('count') : 1;
|
2022-03-20 07:42:39 +01:00
|
|
|
$operators = config('search.operators');
|
2020-08-26 20:18:27 +02:00
|
|
|
$triggers = [];
|
|
|
|
foreach ($operators as $key => $operator) {
|
|
|
|
if ('user_action' !== $key && false === $operator['alias']) {
|
2024-12-22 08:43:12 +01:00
|
|
|
$triggers[$key] = (string) trans(sprintf('firefly.rule_trigger_%s_choice', $key));
|
2016-01-24 15:58:16 +01:00
|
|
|
}
|
|
|
|
}
|
2017-09-03 10:16:45 +02:00
|
|
|
asort($triggers);
|
2016-01-24 15:58:16 +01:00
|
|
|
|
2018-08-04 17:30:06 +02:00
|
|
|
try {
|
2022-01-29 14:11:12 +01:00
|
|
|
$view = view('rules.partials.trigger', compact('triggers', 'count'))->render();
|
2023-12-20 19:35:52 +01:00
|
|
|
} catch (\Throwable $e) {
|
2023-10-29 06:32:00 +01:00
|
|
|
app('log')->error(sprintf('Cannot render rules.partials.trigger: %s', $e->getMessage()));
|
|
|
|
app('log')->error($e->getTraceAsString());
|
2018-08-04 17:30:06 +02:00
|
|
|
$view = 'Could not render view.';
|
2023-12-20 19:35:52 +01:00
|
|
|
|
2022-11-02 06:25:37 +01:00
|
|
|
throw new FireflyException($view, 0, $e);
|
2018-08-04 17:30:06 +02:00
|
|
|
}
|
2021-03-28 11:46:23 +02:00
|
|
|
|
2018-03-10 20:30:09 +01:00
|
|
|
return response()->json(['html' => $view]);
|
2016-01-24 15:58:16 +01:00
|
|
|
}
|
2019-08-17 12:09:03 +02:00
|
|
|
}
|