2019-08-10 08:09:44 -05:00
|
|
|
<?php
|
2019-10-01 23:37:26 -05:00
|
|
|
/**
|
|
|
|
* FormSupport.php
|
2020-02-16 06:56:52 -06:00
|
|
|
* Copyright (c) 2019 james@firefly-iii.org
|
2019-10-01 23:37:26 -05: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 05:09:03 -05:00
|
|
|
declare(strict_types=1);
|
2019-08-10 08:09:44 -05:00
|
|
|
|
|
|
|
namespace FireflyIII\Support\Form;
|
|
|
|
|
|
|
|
use Carbon\Carbon;
|
2022-12-30 13:25:04 -06:00
|
|
|
use Carbon\Exceptions\InvalidDateException;
|
2019-08-10 10:11:57 -05:00
|
|
|
use FireflyIII\Repositories\Account\AccountRepositoryInterface;
|
2019-08-10 08:09:44 -05:00
|
|
|
use Illuminate\Support\MessageBag;
|
|
|
|
use Log;
|
|
|
|
use Throwable;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Trait FormSupport
|
|
|
|
*/
|
|
|
|
trait FormSupport
|
|
|
|
{
|
|
|
|
/**
|
2022-12-29 12:42:26 -06:00
|
|
|
* @param string $name
|
|
|
|
* @param array|null $list
|
|
|
|
* @param mixed $selected
|
|
|
|
* @param array|null $options
|
2019-08-10 08:09:44 -05:00
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function select(string $name, array $list = null, $selected = null, array $options = null): string
|
|
|
|
{
|
|
|
|
$list = $list ?? [];
|
|
|
|
$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 {
|
2022-01-29 07:11:12 -06:00
|
|
|
$html = view('form.select', compact('classes', 'name', 'label', 'selected', 'options', 'list'))->render();
|
2022-10-30 23:53:36 -05:00
|
|
|
} catch (Throwable $e) {
|
2019-08-10 08:09:44 -05:00
|
|
|
Log::debug(sprintf('Could not render select(): %s', $e->getMessage()));
|
|
|
|
$html = 'Could not render select.';
|
|
|
|
}
|
|
|
|
|
|
|
|
return $html;
|
|
|
|
}
|
|
|
|
|
2021-03-21 03:15:40 -05:00
|
|
|
/**
|
2022-12-29 12:42:26 -06:00
|
|
|
* @param string $name
|
|
|
|
* @param array|null $options
|
2021-03-21 03:15:40 -05:00
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function label(string $name, array $options = null): string
|
|
|
|
{
|
|
|
|
$options = $options ?? [];
|
2021-04-06 10:00:16 -05:00
|
|
|
if (array_key_exists('label', $options)) {
|
2021-03-21 03:15:40 -05:00
|
|
|
return $options['label'];
|
|
|
|
}
|
|
|
|
$name = str_replace('[]', '', $name);
|
|
|
|
|
2022-12-29 12:42:26 -06:00
|
|
|
return (string)trans('form.'.$name);
|
2021-03-21 03:15:40 -05:00
|
|
|
}
|
|
|
|
|
2019-08-10 08:09:44 -05:00
|
|
|
/**
|
2022-12-29 12:42:26 -06:00
|
|
|
* @param string $name
|
|
|
|
* @param mixed $label
|
|
|
|
* @param array|null $options
|
2019-08-10 08:09:44 -05:00
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function expandOptionArray(string $name, $label, array $options = null): array
|
|
|
|
{
|
|
|
|
$options = $options ?? [];
|
|
|
|
$name = str_replace('[]', '', $name);
|
|
|
|
$options['class'] = 'form-control';
|
2022-12-29 12:42:26 -06:00
|
|
|
$options['id'] = 'ffInput_'.$name;
|
2019-08-10 08:09:44 -05:00
|
|
|
$options['autocomplete'] = 'off';
|
|
|
|
$options['placeholder'] = ucfirst($label);
|
|
|
|
|
|
|
|
return $options;
|
|
|
|
}
|
|
|
|
|
2021-03-21 03:15:40 -05:00
|
|
|
/**
|
2022-12-29 12:42:26 -06:00
|
|
|
* @param string $name
|
2021-03-21 03:15:40 -05:00
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function getHolderClasses(string $name): string
|
|
|
|
{
|
|
|
|
// Get errors from session:
|
|
|
|
/** @var MessageBag $errors */
|
|
|
|
$errors = session('errors');
|
|
|
|
$classes = 'form-group';
|
|
|
|
|
|
|
|
if (null !== $errors && $errors->has($name)) {
|
|
|
|
$classes = 'form-group has-error has-feedback';
|
|
|
|
}
|
|
|
|
|
|
|
|
return $classes;
|
|
|
|
}
|
|
|
|
|
2019-08-10 08:09:44 -05:00
|
|
|
/**
|
2022-12-29 12:42:26 -06:00
|
|
|
* @param string $name
|
|
|
|
* @param mixed|null $value
|
2019-08-10 08:09:44 -05:00
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
protected function fillFieldValue(string $name, $value = null)
|
|
|
|
{
|
|
|
|
if (app('session')->has('preFilled')) {
|
|
|
|
$preFilled = session('preFilled');
|
2021-04-06 10:00:16 -05:00
|
|
|
$value = array_key_exists($name, $preFilled) && null === $value ? $preFilled[$name] : $value;
|
2019-08-10 08:09:44 -05:00
|
|
|
}
|
|
|
|
|
2022-12-30 13:25:04 -06:00
|
|
|
if (null !== request()->old($name)) {
|
|
|
|
$value = request()->old($name);
|
2019-08-10 08:09:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($value instanceof Carbon) {
|
|
|
|
$value = $value->format('Y-m-d');
|
|
|
|
}
|
|
|
|
|
|
|
|
return $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-03-21 03:15:40 -05:00
|
|
|
* @return AccountRepositoryInterface
|
2019-08-10 08:09:44 -05:00
|
|
|
*/
|
2021-03-21 03:15:40 -05:00
|
|
|
protected function getAccountRepository(): AccountRepositoryInterface
|
2019-08-10 08:09:44 -05:00
|
|
|
{
|
2021-03-21 03:15:40 -05:00
|
|
|
return app(AccountRepositoryInterface::class);
|
2019-08-10 08:09:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-03-21 03:15:40 -05:00
|
|
|
* @return Carbon
|
2019-08-10 08:09:44 -05:00
|
|
|
*/
|
2021-03-21 03:15:40 -05:00
|
|
|
protected function getDate(): Carbon
|
2019-08-10 08:09:44 -05:00
|
|
|
{
|
2021-03-21 03:15:40 -05:00
|
|
|
/** @var Carbon $date */
|
|
|
|
$date = null;
|
|
|
|
try {
|
|
|
|
$date = today(config('app.timezone'));
|
2022-12-30 13:25:04 -06:00
|
|
|
} catch (InvalidDateException $e) {
|
|
|
|
Log::error($e->getMessage());
|
2019-08-10 08:09:44 -05:00
|
|
|
}
|
|
|
|
|
2021-03-21 03:15:40 -05:00
|
|
|
return $date;
|
2019-08-10 08:09:44 -05:00
|
|
|
}
|
2019-08-17 05:09:03 -05:00
|
|
|
}
|