Files
firefly-iii/app/Support/ExpandedForm.php

418 lines
13 KiB
PHP
Raw Normal View History

<?php
2022-12-29 19:42:26 +01:00
/**
* ExpandedForm.php
2020-02-16 13:56:52 +01:00
* Copyright (c) 2019 james@firefly-iii.org
*
* 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.
2017-10-21 08:40:00 +02:00
*
* This program is distributed in the hope that it will be useful,
2017-10-21 08:40:00 +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.
2017-10-21 08:40:00 +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/>.
*/
declare(strict_types=1);
namespace FireflyIII\Support;
2015-05-26 08:17:58 +02:00
use Eloquent;
use FireflyIII\Exceptions\FireflyException;
use FireflyIII\Support\Form\FormSupport;
2015-02-24 21:10:25 +01:00
use Illuminate\Support\Collection;
/**
2017-11-15 12:25:49 +01:00
* Class ExpandedForm.
*/
class ExpandedForm
{
use FormSupport;
2021-03-21 09:15:40 +01:00
/**
2023-12-20 19:35:52 +01:00
* @param mixed $value
2018-04-07 22:23:16 +02:00
*
2023-02-22 18:03:31 +01:00
* @throws FireflyException
2018-04-07 22:23:16 +02:00
*/
public function amountNoCurrency(string $name, $value = null, ?array $options = null): string
2018-04-07 22:23:16 +02:00
{
$options ??= [];
2018-04-07 22:23:16 +02:00
$label = $this->label($name, $options);
$options = $this->expandOptionArray($name, $label, $options);
$classes = $this->getHolderClasses($name);
$value = $this->fillFieldValue($name, $value);
$options['step'] = 'any';
unset($options['currency'], $options['placeholder']);
// make sure value is formatted nicely:
2023-12-20 19:35:52 +01:00
// if (null !== $value && '' !== $value) {
// $value = round((float)$value, 8);
// }
try {
$html = view('form.amount-no-currency', compact('classes', 'name', 'label', 'value', 'options'))->render();
} catch (\Throwable $e) {
2023-10-29 06:32:00 +01:00
app('log')->error(sprintf('Could not render amountNoCurrency(): %s', $e->getMessage()));
$html = 'Could not render amountNoCurrency.';
2023-12-20 19:35:52 +01:00
throw new FireflyException($html, 0, $e);
}
2018-04-07 22:23:16 +02:00
return $html;
}
/**
2023-12-20 19:35:52 +01:00
* @param mixed $checked
*
2023-02-22 18:03:31 +01:00
* @throws FireflyException
*/
public function checkbox(string $name, ?int $value = null, $checked = null, ?array $options = null): string
{
$options ??= [];
$value ??= 1;
2018-07-27 05:03:37 +02:00
$options['checked'] = true === $checked;
2018-04-22 11:29:20 +02:00
2018-08-05 18:59:15 +02:00
if (app('session')->has('preFilled')) {
2018-04-22 11:29:20 +02:00
$preFilled = session('preFilled');
$options['checked'] = $preFilled[$name] ?? $options['checked'];
}
$label = $this->label($name, $options);
$options = $this->expandOptionArray($name, $label, $options);
$classes = $this->getHolderClasses($name);
$value = $this->fillFieldValue($name, $value);
unset($options['placeholder'], $options['autocomplete'], $options['class']);
2023-12-20 19:35:52 +01:00
try {
$html = view('form.checkbox', compact('classes', 'name', 'label', 'value', 'options'))->render();
} catch (\Throwable $e) {
2023-10-29 06:33:43 +01:00
app('log')->debug(sprintf('Could not render checkbox(): %s', $e->getMessage()));
$html = 'Could not render checkbox.';
2023-12-20 19:35:52 +01:00
throw new FireflyException($html, 0, $e);
}
return $html;
}
/**
2023-12-20 19:35:52 +01:00
* @param mixed $value
*
2023-02-22 18:03:31 +01:00
* @throws FireflyException
*/
public function date(string $name, $value = null, ?array $options = null): string
{
$label = $this->label($name, $options);
$options = $this->expandOptionArray($name, $label, $options);
$classes = $this->getHolderClasses($name);
$value = $this->fillFieldValue($name, $value);
2015-06-22 18:50:54 +02:00
unset($options['placeholder']);
2023-12-20 19:35:52 +01:00
try {
$html = view('form.date', compact('classes', 'name', 'label', 'value', 'options'))->render();
} catch (\Throwable $e) {
2023-10-29 06:33:43 +01:00
app('log')->debug(sprintf('Could not render date(): %s', $e->getMessage()));
$html = 'Could not render date.';
2023-12-20 19:35:52 +01:00
throw new FireflyException($html, 0, $e);
}
return $html;
}
2016-01-20 15:23:36 +01:00
/**
2023-02-22 18:03:31 +01:00
* @throws FireflyException
2016-01-20 15:23:36 +01:00
*/
public function file(string $name, ?array $options = null): string
2016-01-20 15:23:36 +01:00
{
2023-12-10 06:45:59 +01:00
$options ??= [];
2016-01-20 15:23:36 +01:00
$label = $this->label($name, $options);
$options = $this->expandOptionArray($name, $label, $options);
$classes = $this->getHolderClasses($name);
2023-12-20 19:35:52 +01:00
try {
$html = view('form.file', compact('classes', 'name', 'label', 'options'))->render();
} catch (\Throwable $e) {
2023-10-29 06:33:43 +01:00
app('log')->debug(sprintf('Could not render file(): %s', $e->getMessage()));
$html = 'Could not render file.';
2023-12-20 19:35:52 +01:00
throw new FireflyException($html, 0, $e);
}
2016-01-20 15:23:36 +01:00
return $html;
}
2015-04-03 22:54:21 +02:00
/**
2023-12-20 19:35:52 +01:00
* @param mixed $value
2015-04-03 22:54:21 +02:00
*
2023-02-22 18:03:31 +01:00
* @throws FireflyException
2015-04-03 22:54:21 +02:00
*/
public function integer(string $name, $value = null, ?array $options = null): string
2015-04-03 22:54:21 +02:00
{
2023-12-10 06:51:59 +01:00
$options ??= [];
$label = $this->label($name, $options);
$options = $this->expandOptionArray($name, $label, $options);
$classes = $this->getHolderClasses($name);
$value = $this->fillFieldValue($name, $value);
2023-12-10 06:45:59 +01:00
$options['step'] ??= '1';
2023-12-20 19:35:52 +01:00
try {
$html = view('form.integer', compact('classes', 'name', 'label', 'value', 'options'))->render();
} catch (\Throwable $e) {
2023-10-29 06:33:43 +01:00
app('log')->debug(sprintf('Could not render integer(): %s', $e->getMessage()));
$html = 'Could not render integer.';
2023-12-20 19:35:52 +01:00
throw new FireflyException($html, 0, $e);
}
2015-04-03 22:54:21 +02:00
return $html;
}
2015-03-02 20:05:28 +01:00
/**
2023-12-20 19:35:52 +01:00
* @param mixed $value
2015-03-02 20:05:28 +01:00
*
2023-02-22 18:03:31 +01:00
* @throws FireflyException
2015-03-02 20:05:28 +01:00
*/
public function location(string $name, $value = null, ?array $options = null): string
2015-03-02 20:05:28 +01:00
{
2023-12-10 06:45:59 +01:00
$options ??= [];
$label = $this->label($name, $options);
$options = $this->expandOptionArray($name, $label, $options);
$classes = $this->getHolderClasses($name);
$value = $this->fillFieldValue($name, $value);
2023-12-20 19:35:52 +01:00
try {
$html = view('form.location', compact('classes', 'name', 'label', 'value', 'options'))->render();
} catch (\Throwable $e) {
2023-10-29 06:33:43 +01:00
app('log')->debug(sprintf('Could not render location(): %s', $e->getMessage()));
$html = 'Could not render location.';
2023-12-20 19:35:52 +01:00
throw new FireflyException($html, 0, $e);
}
2015-03-02 20:05:28 +01:00
return $html;
}
2016-04-27 19:21:47 +02:00
public function makeSelectListWithEmpty(Collection $set): array
{
2016-05-20 08:00:35 +02:00
$selectList = [];
2016-04-27 19:21:47 +02:00
$selectList[0] = '(none)';
$fields = ['title', 'name', 'description'];
2023-12-20 19:35:52 +01:00
/** @var \Eloquent $entry */
2015-02-24 21:10:25 +01:00
foreach ($set as $entry) {
2022-12-31 13:32:42 +01:00
// All Eloquent models have an ID
2025-01-04 19:43:58 +01:00
$entryId = $entry->id;
$current = $entry->toArray();
$title = null;
2015-02-24 21:10:25 +01:00
foreach ($fields as $field) {
2021-04-12 06:16:00 +02:00
if (array_key_exists($field, $current) && null === $title) {
2022-10-31 05:53:36 +01:00
$title = $current[$field];
2015-02-24 21:10:25 +01:00
}
}
2015-05-25 07:14:04 +02:00
$selectList[$entryId] = $title;
2015-02-24 21:10:25 +01:00
}
2021-07-04 07:58:11 +02:00
2015-02-24 21:10:25 +01:00
return $selectList;
}
2021-03-21 09:15:40 +01:00
/**
2023-12-20 19:35:52 +01:00
* @param null $value
2021-03-21 09:15:40 +01:00
*
2023-02-22 18:03:31 +01:00
* @throws FireflyException
2021-03-21 09:15:40 +01:00
*/
public function objectGroup($value = null, ?array $options = null): string
2021-03-21 09:15:40 +01:00
{
$name = 'object_group';
$label = $this->label($name, $options);
$options = $this->expandOptionArray($name, $label, $options);
$classes = $this->getHolderClasses($name);
$value = $this->fillFieldValue($name, $value);
$options['rows'] = 4;
if (null === $value) {
$value = '';
}
try {
$html = view('form.object_group', compact('classes', 'name', 'label', 'value', 'options'))->render();
} catch (\Throwable $e) {
2023-10-29 06:33:43 +01:00
app('log')->debug(sprintf('Could not render objectGroup(): %s', $e->getMessage()));
2021-03-21 09:15:40 +01:00
$html = 'Could not render objectGroup.';
2023-12-20 19:35:52 +01:00
throw new FireflyException($html, 0, $e);
2021-03-21 09:15:40 +01:00
}
return $html;
}
2018-08-04 17:30:47 +02:00
/**
2023-02-22 18:03:31 +01:00
* @throws FireflyException
2018-08-04 17:30:47 +02:00
*/
2018-08-05 18:59:15 +02:00
public function optionsList(string $type, string $name): string
2018-08-04 17:30:47 +02:00
{
try {
$html = view('form.options', compact('type', 'name'))->render();
} catch (\Throwable $e) {
2023-10-29 06:33:43 +01:00
app('log')->debug(sprintf('Could not render select(): %s', $e->getMessage()));
2018-08-05 18:59:15 +02:00
$html = 'Could not render optionsList.';
2023-12-20 19:35:52 +01:00
throw new FireflyException($html, 0, $e);
2018-08-04 17:30:47 +02:00
}
return $html;
}
/**
2023-02-22 18:03:31 +01:00
* @throws FireflyException
*/
public function password(string $name, ?array $options = null): string
{
2018-08-05 18:59:15 +02:00
$label = $this->label($name, $options);
$options = $this->expandOptionArray($name, $label, $options);
$classes = $this->getHolderClasses($name);
2023-12-20 19:35:52 +01:00
try {
$html = view('form.password', compact('classes', 'name', 'label', 'options'))->render();
} catch (\Throwable $e) {
2023-10-29 06:33:43 +01:00
app('log')->debug(sprintf('Could not render password(): %s', $e->getMessage()));
2018-08-05 18:59:15 +02:00
$html = 'Could not render password.';
2023-12-20 19:35:52 +01:00
throw new FireflyException($html, 0, $e);
}
return $html;
}
2024-12-14 05:45:54 +01:00
2024-12-11 07:23:46 +01:00
/**
* @throws FireflyException
*/
public function passwordWithValue(string $name, string $value, ?array $options = null): string
{
$label = $this->label($name, $options);
$options = $this->expandOptionArray($name, $label, $options);
$classes = $this->getHolderClasses($name);
try {
2024-12-14 05:45:54 +01:00
$html = view('form.password', compact('classes', 'value', 'name', 'label', 'options'))->render();
} catch (\Throwable $e) {
2024-12-11 07:23:46 +01:00
app('log')->debug(sprintf('Could not render passwordWithValue(): %s', $e->getMessage()));
$html = 'Could not render passwordWithValue.';
throw new FireflyException($html, 0, $e);
}
return $html;
}
2016-04-25 18:43:09 +02:00
2016-12-20 17:14:43 +01:00
/**
2018-08-05 18:59:15 +02:00
* Function to render a percentage.
*
2023-12-20 19:35:52 +01:00
* @param mixed $value
2016-12-20 17:14:43 +01:00
*
2023-02-22 18:03:31 +01:00
* @throws FireflyException
2016-12-20 17:14:43 +01:00
*/
public function percentage(string $name, $value = null, ?array $options = null): string
2016-12-20 17:14:43 +01:00
{
2018-08-05 18:59:15 +02:00
$label = $this->label($name, $options);
$options = $this->expandOptionArray($name, $label, $options);
$classes = $this->getHolderClasses($name);
$value = $this->fillFieldValue($name, $value);
$options['step'] = 'any';
unset($options['placeholder']);
2023-12-20 19:35:52 +01:00
try {
$html = view('form.percentage', compact('classes', 'name', 'label', 'value', 'options'))->render();
} catch (\Throwable $e) {
2023-10-29 06:33:43 +01:00
app('log')->debug(sprintf('Could not render percentage(): %s', $e->getMessage()));
2018-08-05 18:59:15 +02:00
$html = 'Could not render percentage.';
2023-12-20 19:35:52 +01:00
throw new FireflyException($html, 0, $e);
}
2016-12-20 17:14:43 +01:00
return $html;
}
2015-03-02 20:05:28 +01:00
/**
2023-12-20 19:35:52 +01:00
* @param mixed $value
2015-03-02 20:05:28 +01:00
*
2023-02-22 18:03:31 +01:00
* @throws FireflyException
2015-03-02 20:05:28 +01:00
*/
public function staticText(string $name, $value, ?array $options = null): string
2015-03-02 20:05:28 +01:00
{
2016-01-20 15:23:36 +01:00
$label = $this->label($name, $options);
$options = $this->expandOptionArray($name, $label, $options);
$classes = $this->getHolderClasses($name);
2023-12-20 19:35:52 +01:00
try {
$html = view('form.static', compact('classes', 'name', 'label', 'value', 'options'))->render();
} catch (\Throwable $e) {
2023-10-29 06:33:43 +01:00
app('log')->debug(sprintf('Could not render staticText(): %s', $e->getMessage()));
$html = 'Could not render staticText.';
2023-12-20 19:35:52 +01:00
throw new FireflyException($html, 0, $e);
}
2015-03-02 20:05:28 +01:00
return $html;
}
/**
2023-12-20 19:35:52 +01:00
* @param mixed $value
*
2023-02-22 18:03:31 +01:00
* @throws FireflyException
*/
public function text(string $name, $value = null, ?array $options = null): string
{
$label = $this->label($name, $options);
$options = $this->expandOptionArray($name, $label, $options);
$classes = $this->getHolderClasses($name);
$value = $this->fillFieldValue($name, $value);
2023-12-20 19:35:52 +01:00
try {
$html = view('form.text', compact('classes', 'name', 'label', 'value', 'options'))->render();
} catch (\Throwable $e) {
2023-10-29 06:33:43 +01:00
app('log')->debug(sprintf('Could not render text(): %s', $e->getMessage()));
$html = 'Could not render text.';
2023-12-20 19:35:52 +01:00
throw new FireflyException($html, 0, $e);
}
return $html;
}
/**
2023-12-20 19:35:52 +01:00
* @param mixed $value
*
2023-02-22 18:03:31 +01:00
* @throws FireflyException
*/
public function textarea(string $name, $value = null, ?array $options = null): string
{
$label = $this->label($name, $options);
$options = $this->expandOptionArray($name, $label, $options);
$classes = $this->getHolderClasses($name);
$value = $this->fillFieldValue($name, $value);
$options['rows'] = 4;
2018-08-05 18:59:15 +02:00
if (null === $value) {
$value = '';
}
try {
$html = view('form.textarea', compact('classes', 'name', 'label', 'value', 'options'))->render();
} catch (\Throwable $e) {
2023-10-29 06:33:43 +01:00
app('log')->debug(sprintf('Could not render textarea(): %s', $e->getMessage()));
$html = 'Could not render textarea.';
2023-12-20 19:35:52 +01:00
throw new FireflyException($html, 0, $e);
}
return $html;
}
2015-03-29 08:14:32 +02:00
}