2019-08-10 15:09:44 +02:00
|
|
|
<?php
|
2024-11-25 04:18:55 +01:00
|
|
|
|
2019-10-02 06:37:26 +02:00
|
|
|
/**
|
|
|
|
* FormSupport.php
|
2020-02-16 13:56:52 +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);
|
2019-08-10 15:09:44 +02:00
|
|
|
|
|
|
|
namespace FireflyIII\Support\Form;
|
|
|
|
|
|
|
|
use Carbon\Carbon;
|
2019-08-10 17:11:57 +02:00
|
|
|
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
|
2023-05-29 13:56:55 +02:00
|
|
|
use Illuminate\Support\MessageBag;
|
2025-05-27 16:57:36 +02:00
|
|
|
use Throwable;
|
2019-08-10 15:09:44 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Trait FormSupport
|
|
|
|
*/
|
|
|
|
trait FormSupport
|
|
|
|
{
|
2025-01-04 09:34:17 +01:00
|
|
|
public function multiSelect(string $name, ?array $list = null, mixed $selected = null, ?array $options = null): string
|
2019-08-10 15:09:44 +02:00
|
|
|
{
|
2025-09-07 11:04:11 +02:00
|
|
|
$list ??= [];
|
2019-08-10 15:09:44 +02:00
|
|
|
$label = $this->label($name, $options);
|
|
|
|
$options = $this->expandOptionArray($name, $label, $options);
|
|
|
|
$classes = $this->getHolderClasses($name);
|
|
|
|
$selected = $this->fillFieldValue($name, $selected);
|
2024-12-22 08:43:12 +01:00
|
|
|
|
2019-08-10 15:09:44 +02:00
|
|
|
unset($options['autocomplete'], $options['placeholder']);
|
2023-12-20 19:35:52 +01:00
|
|
|
|
2019-08-10 15:09:44 +02:00
|
|
|
try {
|
2024-12-22 08:43:12 +01:00
|
|
|
$html = view('form.multi-select', compact('classes', 'name', 'label', 'selected', 'options', 'list'))->render();
|
2025-05-27 16:57:36 +02:00
|
|
|
} catch (Throwable $e) {
|
2024-12-22 08:43:12 +01:00
|
|
|
app('log')->debug(sprintf('Could not render multi-select(): %s', $e->getMessage()));
|
|
|
|
$html = 'Could not render multi-select.';
|
2019-08-10 15:09:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $html;
|
|
|
|
}
|
|
|
|
|
2024-03-18 20:25:30 +01:00
|
|
|
protected function label(string $name, ?array $options = null): string
|
2023-06-21 12:34:58 +02:00
|
|
|
{
|
2023-12-10 06:45:59 +01:00
|
|
|
$options ??= [];
|
2023-06-21 12:34:58 +02:00
|
|
|
if (array_key_exists('label', $options)) {
|
|
|
|
return $options['label'];
|
|
|
|
}
|
|
|
|
$name = str_replace('[]', '', $name);
|
|
|
|
|
2025-09-07 11:04:11 +02:00
|
|
|
return (string)trans('form.'.$name);
|
2023-06-21 12:34:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-12-20 19:35:52 +01:00
|
|
|
* @param mixed $label
|
2019-08-10 15:09:44 +02:00
|
|
|
*/
|
2024-03-18 20:25:30 +01:00
|
|
|
protected function expandOptionArray(string $name, $label, ?array $options = null): array
|
2019-08-10 15:09:44 +02:00
|
|
|
{
|
2025-09-07 11:04:11 +02:00
|
|
|
$options ??= [];
|
2019-08-10 15:09:44 +02:00
|
|
|
$name = str_replace('[]', '', $name);
|
|
|
|
$options['class'] = 'form-control';
|
2025-09-07 11:04:11 +02:00
|
|
|
$options['id'] = 'ffInput_'.$name;
|
2019-08-10 15:09:44 +02:00
|
|
|
$options['autocomplete'] = 'off';
|
2025-09-07 10:59:07 +02:00
|
|
|
$options['placeholder'] = ucfirst((string)$label);
|
2019-08-10 15:09:44 +02:00
|
|
|
|
|
|
|
return $options;
|
|
|
|
}
|
|
|
|
|
2023-06-21 12:34:58 +02:00
|
|
|
protected function getHolderClasses(string $name): string
|
|
|
|
{
|
|
|
|
// Get errors from session:
|
2023-12-20 19:35:52 +01:00
|
|
|
/** @var null|MessageBag $errors */
|
2025-05-27 17:06:15 +02:00
|
|
|
$errors = session('errors');
|
2023-06-21 12:34:58 +02:00
|
|
|
|
|
|
|
if (null !== $errors && $errors->has($name)) {
|
2025-05-27 17:06:15 +02:00
|
|
|
return 'form-group has-error has-feedback';
|
2023-06-21 12:34:58 +02:00
|
|
|
}
|
|
|
|
|
2025-05-27 17:06:15 +02:00
|
|
|
return 'form-group';
|
2023-06-21 12:34:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-12-20 19:35:52 +01:00
|
|
|
* @param null|mixed $value
|
2019-08-10 15:09:44 +02:00
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
protected function fillFieldValue(string $name, $value = null)
|
|
|
|
{
|
|
|
|
if (app('session')->has('preFilled')) {
|
|
|
|
$preFilled = session('preFilled');
|
2021-04-06 17:00:16 +02:00
|
|
|
$value = array_key_exists($name, $preFilled) && null === $value ? $preFilled[$name] : $value;
|
2019-08-10 15:09:44 +02:00
|
|
|
}
|
|
|
|
|
2022-12-30 20:25:04 +01:00
|
|
|
if (null !== request()->old($name)) {
|
|
|
|
$value = request()->old($name);
|
2019-08-10 15:09:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($value instanceof Carbon) {
|
2025-05-27 17:06:15 +02:00
|
|
|
return $value->format('Y-m-d');
|
2019-08-10 15:09:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
|
2024-12-22 20:32:58 +01:00
|
|
|
/**
|
|
|
|
* @param mixed $selected
|
|
|
|
*/
|
|
|
|
public function select(string $name, ?array $list = null, $selected = null, ?array $options = null): string
|
|
|
|
{
|
2025-09-07 11:04:11 +02:00
|
|
|
$list ??= [];
|
2024-12-22 20:32:58 +01:00
|
|
|
$label = $this->label($name, $options);
|
|
|
|
$options = $this->expandOptionArray($name, $label, $options);
|
|
|
|
$classes = $this->getHolderClasses($name);
|
|
|
|
$selected = $this->fillFieldValue($name, $selected);
|
|
|
|
unset($options['autocomplete'], $options['placeholder']);
|
|
|
|
|
|
|
|
try {
|
|
|
|
$html = view('form.select', compact('classes', 'name', 'label', 'selected', 'options', 'list'))->render();
|
2025-05-27 16:57:36 +02:00
|
|
|
} catch (Throwable $e) {
|
2024-12-22 20:32:58 +01:00
|
|
|
app('log')->debug(sprintf('Could not render select(): %s', $e->getMessage()));
|
|
|
|
$html = 'Could not render select.';
|
|
|
|
}
|
|
|
|
|
|
|
|
return $html;
|
|
|
|
}
|
|
|
|
|
2021-03-21 09:15:40 +01:00
|
|
|
protected function getAccountRepository(): AccountRepositoryInterface
|
2019-08-10 15:09:44 +02:00
|
|
|
{
|
2021-03-21 09:15:40 +01:00
|
|
|
return app(AccountRepositoryInterface::class);
|
2019-08-10 15:09:44 +02:00
|
|
|
}
|
|
|
|
|
2021-03-21 09:15:40 +01:00
|
|
|
protected function getDate(): Carbon
|
2019-08-10 15:09:44 +02:00
|
|
|
{
|
2025-09-07 10:59:07 +02:00
|
|
|
return today(config('app.timezone'));
|
2019-08-10 15:09:44 +02:00
|
|
|
}
|
2019-08-17 12:09:03 +02:00
|
|
|
}
|