firefly-iii/app/Support/ExpandedForm.php

636 lines
19 KiB
PHP
Raw Normal View History

<?php
/**
* ExpandedForm.php
2017-10-21 01:40:00 -05:00
* Copyright (c) 2017 thegrumpydictator@gmail.com
*
2017-10-21 01:40:00 -05:00
* This file is part of Firefly III.
*
2017-10-21 01:40:00 -05:00
* Firefly III is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Firefly III 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
2017-12-17 07:44:05 -06:00
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace FireflyIII\Support;
use Amount as Amt;
2016-04-29 10:29:13 -05:00
use Carbon\Carbon;
2015-05-26 01:17:58 -05:00
use Eloquent;
2015-02-24 14:10:25 -06:00
use Illuminate\Support\Collection;
use Illuminate\Support\MessageBag;
use RuntimeException;
use Session;
/**
2017-11-15 05:25:49 -06:00
* Class ExpandedForm.
*/
class ExpandedForm
{
2016-04-24 00:18:39 -05:00
/**
2017-12-29 02:05:35 -06:00
* @param string $name
* @param null $value
* @param array $options
2016-04-24 00:18:39 -05:00
*
* @return string
2017-12-29 02:05:35 -06:00
* @throws \FireflyIII\Exceptions\FireflyException
2016-04-24 00:18:39 -05:00
*/
2016-04-27 12:21:47 -05:00
public function amount(string $name, $value = null, array $options = []): string
2016-04-24 00:18:39 -05:00
{
2016-12-06 09:58:39 -06:00
return $this->currencyField($name, 'amount', $value, $options);
2016-04-24 00:18:39 -05:00
}
/**
* @param $name
* @param null $value
* @param array $options
*
* @return string
2017-12-29 02:05:35 -06:00
* @throws \FireflyIII\Exceptions\FireflyException
*/
2016-04-27 12:21:47 -05:00
public function amountSmall(string $name, $value = null, array $options = []): string
{
2016-12-06 09:58:39 -06:00
return $this->currencyField($name, 'amount-small', $value, $options);
}
2015-02-24 14:10:25 -06:00
/**
* @param $name
* @param null $value
* @param array $options
*
* @return string
2017-12-29 02:05:35 -06:00
* @throws \FireflyIII\Exceptions\FireflyException
*
2015-02-24 14:10:25 -06:00
*/
2016-02-06 08:00:57 -06:00
public function balance(string $name, $value = null, array $options = []): string
2015-02-24 14:10:25 -06:00
{
2016-12-06 09:58:39 -06:00
return $this->currencyField($name, 'balance', $value, $options);
2015-02-24 14:10:25 -06:00
}
/**
* @param $name
* @param int $value
* @param null $checked
* @param array $options
*
* @return string
2017-12-22 11:32:43 -06:00
*
2017-12-17 07:30:53 -06:00
* @throws \Throwable
*/
2016-02-06 08:00:57 -06:00
public function checkbox(string $name, $value = 1, $checked = null, $options = []): string
{
2017-11-15 05:25:49 -06:00
$options['checked'] = true === $checked ? true : null;
$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']);
2015-07-12 10:59:13 -05:00
$html = view('form.checkbox', compact('classes', 'name', 'label', 'value', 'options'))->render();
return $html;
}
/**
* @param $name
* @param null $value
* @param array $options
*
* @return string
2017-12-22 11:32:43 -06:00
*
2017-12-17 07:30:53 -06:00
* @throws \Throwable
*/
2016-02-06 08:00:57 -06:00
public function date(string $name, $value = null, array $options = []): string
{
$label = $this->label($name, $options);
$options = $this->expandOptionArray($name, $label, $options);
$classes = $this->getHolderClasses($name);
$value = $this->fillFieldValue($name, $value);
2015-06-22 11:50:54 -05:00
unset($options['placeholder']);
2015-07-12 10:59:13 -05:00
$html = view('form.date', compact('classes', 'name', 'label', 'value', 'options'))->render();
return $html;
}
2016-01-20 08:23:36 -06:00
/**
* @param $name
* @param array $options
*
* @return string
2017-12-22 11:32:43 -06:00
*
2017-12-17 07:30:53 -06:00
* @throws \Throwable
2016-01-20 08:23:36 -06:00
*/
2016-02-06 08:00:57 -06:00
public function file(string $name, array $options = []): string
2016-01-20 08:23:36 -06:00
{
$label = $this->label($name, $options);
$options = $this->expandOptionArray($name, $label, $options);
$classes = $this->getHolderClasses($name);
$html = view('form.file', compact('classes', 'name', 'label', 'options'))->render();
return $html;
}
2015-04-03 15:54:21 -05:00
/**
* @param $name
* @param null $value
* @param array $options
*
* @return string
2017-12-22 11:32:43 -06:00
*
2017-12-17 07:30:53 -06:00
* @throws \Throwable
2015-04-03 15:54:21 -05:00
*/
2016-02-06 08:00:57 -06:00
public function integer(string $name, $value = null, array $options = []): string
2015-04-03 15:54:21 -05:00
{
$label = $this->label($name, $options);
$options = $this->expandOptionArray($name, $label, $options);
$classes = $this->getHolderClasses($name);
$value = $this->fillFieldValue($name, $value);
$options['step'] = '1';
2015-07-12 10:59:13 -05:00
$html = view('form.integer', compact('classes', 'name', 'label', 'value', 'options'))->render();
2015-04-03 15:54:21 -05:00
return $html;
}
2015-03-02 13:05:28 -06:00
/**
* @param $name
* @param null $value
* @param array $options
*
* @return string
2017-12-22 11:32:43 -06:00
*
2017-12-17 07:30:53 -06:00
* @throws \Throwable
2015-03-02 13:05:28 -06:00
*/
2016-02-06 08:00:57 -06:00
public function location(string $name, $value = null, array $options = []): string
2015-03-02 13:05:28 -06:00
{
$label = $this->label($name, $options);
$options = $this->expandOptionArray($name, $label, $options);
$classes = $this->getHolderClasses($name);
$value = $this->fillFieldValue($name, $value);
2015-07-12 10:59:13 -05:00
$html = view('form.location', compact('classes', 'name', 'label', 'value', 'options'))->render();
2015-03-02 13:05:28 -06:00
return $html;
}
2015-02-24 14:10:25 -06:00
/**
* Takes any collection and tries to make a sensible select list compatible array of it.
*
2015-05-05 03:23:01 -05:00
* @param \Illuminate\Support\Collection $set
2015-02-24 14:10:25 -06:00
*
2016-02-06 08:00:57 -06:00
* @return array
2015-02-24 14:10:25 -06:00
*/
2016-04-27 12:21:47 -05:00
public function makeSelectList(Collection $set): array
2015-02-24 14:10:25 -06:00
{
$selectList = [];
2016-04-27 12:21:47 -05:00
$fields = ['title', 'name', 'description'];
/** @var Eloquent $entry */
foreach ($set as $entry) {
$entryId = intval($entry->id);
$title = null;
foreach ($fields as $field) {
2017-11-15 05:25:49 -06:00
if (isset($entry->$field) && null === $title) {
2016-04-27 12:21:47 -05:00
$title = $entry->$field;
}
}
$selectList[$entryId] = $title;
2015-02-24 14:10:25 -06:00
}
2016-04-27 12:21:47 -05:00
return $selectList;
}
/**
2016-04-29 10:29:13 -05:00
* @param \Illuminate\Support\Collection $set
2016-04-27 12:21:47 -05:00
*
* @return array
*/
public function makeSelectListWithEmpty(Collection $set): array
{
2016-05-20 01:00:35 -05:00
$selectList = [];
2016-04-27 12:21:47 -05:00
$selectList[0] = '(none)';
$fields = ['title', 'name', 'description'];
2015-03-07 02:21:06 -06:00
/** @var Eloquent $entry */
2015-02-24 14:10:25 -06:00
foreach ($set as $entry) {
2015-05-25 00:14:04 -05:00
$entryId = intval($entry->id);
$title = null;
2015-02-24 14:10:25 -06:00
foreach ($fields as $field) {
2017-11-15 05:25:49 -06:00
if (isset($entry->$field) && null === $title) {
2015-02-24 14:10:25 -06:00
$title = $entry->$field;
}
}
2015-05-25 00:14:04 -05:00
$selectList[$entryId] = $title;
2015-02-24 14:10:25 -06:00
}
return $selectList;
}
2015-07-06 13:21:55 -05:00
/**
* @param $name
* @param array $list
* @param null $selected
* @param array $options
*
* @return string
2017-12-22 11:32:43 -06:00
*
2017-12-17 07:30:53 -06:00
* @throws \Throwable
2015-07-06 13:21:55 -05:00
*/
2016-02-06 08:00:57 -06:00
public function multiCheckbox(string $name, array $list = [], $selected = null, array $options = []): string
2015-07-06 13:21:55 -05:00
{
$label = $this->label($name, $options);
$options = $this->expandOptionArray($name, $label, $options);
$classes = $this->getHolderClasses($name);
$selected = $this->fillFieldValue($name, $selected);
unset($options['class']);
2015-07-12 10:59:13 -05:00
$html = view('form.multiCheckbox', compact('classes', 'name', 'label', 'selected', 'options', 'list'))->render();
2015-07-06 13:21:55 -05:00
return $html;
}
/**
* @param $name
2015-05-05 03:23:01 -05:00
* @param array $list
* @param null $selected
* @param array $options
*
* @return string
2017-12-22 11:32:43 -06:00
*
2017-12-17 07:30:53 -06:00
* @throws \Throwable
*/
2016-02-06 08:00:57 -06:00
public function multiRadio(string $name, array $list = [], $selected = null, array $options = []): string
{
$label = $this->label($name, $options);
$options = $this->expandOptionArray($name, $label, $options);
$classes = $this->getHolderClasses($name);
$selected = $this->fillFieldValue($name, $selected);
unset($options['class']);
2015-07-12 10:59:13 -05:00
$html = view('form.multiRadio', compact('classes', 'name', 'label', 'selected', 'options', 'list'))->render();
return $html;
}
/**
* @param string $name
* @param null $value
* @param array $options
*
* @return string
2017-12-22 11:32:43 -06:00
*
2017-12-17 07:30:53 -06:00
* @throws \Throwable
*/
public function nonSelectableAmount(string $name, $value = null, array $options = []): string
{
$label = $this->label($name, $options);
$options = $this->expandOptionArray($name, $label, $options);
$classes = $this->getHolderClasses($name);
$value = $this->fillFieldValue($name, $value);
$options['step'] = 'any';
$selectedCurrency = isset($options['currency']) ? $options['currency'] : Amt::getDefaultCurrency();
unset($options['currency']);
unset($options['placeholder']);
// make sure value is formatted nicely:
2017-11-15 05:25:49 -06:00
if (null !== $value && '' !== $value) {
$value = round($value, $selectedCurrency->decimal_places);
}
$html = view('form.non-selectable-amount', compact('selectedCurrency', 'classes', 'name', 'label', 'value', 'options'))->render();
return $html;
}
/**
* @param string $name
* @param null $value
* @param array $options
*
* @return string
2017-12-22 11:32:43 -06:00
*
2017-12-17 07:30:53 -06:00
* @throws \Throwable
*/
public function nonSelectableBalance(string $name, $value = null, array $options = []): string
{
$label = $this->label($name, $options);
$options = $this->expandOptionArray($name, $label, $options);
$classes = $this->getHolderClasses($name);
$value = $this->fillFieldValue($name, $value);
$options['step'] = 'any';
$selectedCurrency = isset($options['currency']) ? $options['currency'] : Amt::getDefaultCurrency();
unset($options['currency']);
unset($options['placeholder']);
// make sure value is formatted nicely:
2017-11-15 05:25:49 -06:00
if (null !== $value && '' !== $value) {
$decimals = $selectedCurrency->decimal_places ?? 2;
$value = round($value, $decimals);
}
$html = view('form.non-selectable-amount', compact('selectedCurrency', 'classes', 'name', 'label', 'value', 'options'))->render();
return $html;
}
2017-11-03 00:58:39 -05:00
/**
* @param string $name
* @param null $value
* @param array $options
*
* @return string
2017-12-22 11:32:43 -06:00
*
2017-12-17 07:30:53 -06:00
* @throws \Throwable
2017-11-03 00:58:39 -05:00
*/
public function number(string $name, $value = null, array $options = []): string
{
$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']);
$html = view('form.number', compact('classes', 'name', 'label', 'value', 'options'))->render();
2017-11-03 00:58:39 -05:00
return $html;
}
/**
* @param $type
* @param $name
*
* @return string
2017-12-22 11:32:43 -06:00
*
2017-12-17 07:30:53 -06:00
* @throws \Throwable
*/
2016-04-05 15:00:03 -05:00
public function optionsList(string $type, string $name): string
{
$previousValue = null;
try {
$previousValue = request()->old('post_submit_action');
} catch (RuntimeException $e) {
// don't care
}
2017-11-15 05:25:49 -06:00
$previousValue = null === $previousValue ? 'store' : $previousValue;
2015-07-12 10:59:13 -05:00
$html = view('form.options', compact('type', 'name', 'previousValue'))->render();
return $html;
}
2016-04-25 11:43:09 -05:00
2016-12-20 10:14:43 -06:00
/**
* @param $name
* @param array $options
*
* @return string
2017-12-22 11:32:43 -06:00
*
2017-12-17 07:30:53 -06:00
* @throws \Throwable
2016-12-20 10:14:43 -06:00
*/
public function password(string $name, array $options = []): string
{
$label = $this->label($name, $options);
$options = $this->expandOptionArray($name, $label, $options);
$classes = $this->getHolderClasses($name);
$html = view('form.password', compact('classes', 'name', 'label', 'value', 'options'))->render();
return $html;
}
2015-02-11 00:35:10 -06:00
/**
* @param $name
* @param array $list
* @param null $selected
* @param array $options
*
* @return string
2017-12-22 11:32:43 -06:00
*
2017-12-17 07:30:53 -06:00
* @throws \Throwable
2015-02-11 00:35:10 -06:00
*/
2016-02-06 08:00:57 -06:00
public function select(string $name, array $list = [], $selected = null, array $options = []): string
{
$label = $this->label($name, $options);
$options = $this->expandOptionArray($name, $label, $options);
$classes = $this->getHolderClasses($name);
$selected = $this->fillFieldValue($name, $selected);
2015-06-22 11:50:54 -05:00
unset($options['autocomplete']);
unset($options['placeholder']);
2015-07-12 10:59:13 -05:00
$html = view('form.select', compact('classes', 'name', 'label', 'selected', 'options', 'list'))->render();
return $html;
}
2015-03-02 13:05:28 -06:00
/**
* @param $name
* @param null $value
* @param array $options
*
* @return string
2017-12-22 11:32:43 -06:00
*
2017-12-17 07:30:53 -06:00
* @throws \Throwable
2015-03-02 13:05:28 -06:00
*/
2016-02-06 08:00:57 -06:00
public function staticText(string $name, $value, array $options = []): string
2015-03-02 13:05:28 -06:00
{
2016-01-20 08:23:36 -06:00
$label = $this->label($name, $options);
$options = $this->expandOptionArray($name, $label, $options);
$classes = $this->getHolderClasses($name);
$html = view('form.static', compact('classes', 'name', 'label', 'value', 'options'))->render();
2015-03-02 13:05:28 -06:00
return $html;
}
/**
* @param $name
2016-01-20 08:23:36 -06:00
* @param null $value
* @param array $options
*
* @return string
2017-12-22 11:32:43 -06:00
*
2017-12-17 07:30:53 -06:00
* @throws \Throwable
*/
2016-02-06 08:00:57 -06:00
public function tags(string $name, $value = null, array $options = []): string
{
2016-01-20 08:23:36 -06:00
$label = $this->label($name, $options);
$options = $this->expandOptionArray($name, $label, $options);
$classes = $this->getHolderClasses($name);
$value = $this->fillFieldValue($name, $value);
$options['data-role'] = 'tagsinput';
$html = view('form.tags', compact('classes', 'name', 'label', 'value', 'options'))->render();
return $html;
}
/**
* @param $name
* @param null $value
* @param array $options
*
* @return string
2017-12-22 11:32:43 -06:00
*
2017-12-17 07:30:53 -06:00
* @throws \Throwable
*/
2016-02-06 08:00:57 -06:00
public function text(string $name, $value = null, array $options = []): string
{
$label = $this->label($name, $options);
$options = $this->expandOptionArray($name, $label, $options);
$classes = $this->getHolderClasses($name);
$value = $this->fillFieldValue($name, $value);
2015-07-12 10:59:13 -05:00
$html = view('form.text', compact('classes', 'name', 'label', 'value', 'options'))->render();
return $html;
}
/**
* @param $name
* @param null $value
* @param array $options
*
* @return string
2017-12-22 11:32:43 -06:00
*
2017-12-17 07:30:53 -06:00
* @throws \Throwable
*/
2016-02-06 08:00:57 -06:00
public function textarea(string $name, $value = null, array $options = []): string
{
$label = $this->label($name, $options);
$options = $this->expandOptionArray($name, $label, $options);
$classes = $this->getHolderClasses($name);
$value = $this->fillFieldValue($name, $value);
$options['rows'] = 4;
2015-07-12 10:59:13 -05:00
$html = view('form.textarea', compact('classes', 'name', 'label', 'value', 'options'))->render();
return $html;
}
2016-01-20 08:23:36 -06:00
/**
* @param $name
* @param $label
* @param array $options
*
* @return array
*/
2016-02-06 08:00:57 -06:00
protected function expandOptionArray(string $name, $label, array $options): array
2016-01-20 08:23:36 -06:00
{
$name = str_replace('[]', '', $name);
2016-01-20 08:23:36 -06:00
$options['class'] = 'form-control';
$options['id'] = 'ffInput_' . $name;
$options['autocomplete'] = 'off';
$options['placeholder'] = ucfirst($label);
return $options;
}
/**
* @param $name
* @param $value
*
* @return mixed
*/
2016-02-05 02:30:06 -06:00
protected function fillFieldValue(string $name, $value)
2016-01-20 08:23:36 -06:00
{
if (Session::has('preFilled')) {
2016-02-04 00:27:03 -06:00
$preFilled = session('preFilled');
2017-11-15 05:25:49 -06:00
$value = isset($preFilled[$name]) && null === $value ? $preFilled[$name] : $value;
2016-01-20 08:23:36 -06:00
}
try {
2017-11-15 05:25:49 -06:00
if (null !== request()->old($name)) {
$value = request()->old($name);
2016-01-20 08:23:36 -06:00
}
} catch (RuntimeException $e) {
// don't care about session errors.
}
2016-04-29 10:29:13 -05:00
if ($value instanceof Carbon) {
$value = $value->format('Y-m-d');
}
2016-01-20 08:23:36 -06:00
return $value;
}
/**
* @param $name
*
* @return string
*/
2016-02-06 08:00:57 -06:00
protected function getHolderClasses(string $name): string
2016-01-20 08:23:36 -06:00
{
2017-11-15 05:25:49 -06:00
// Get errors from session:
2016-01-20 08:23:36 -06:00
/** @var MessageBag $errors */
2016-02-04 00:27:03 -06:00
$errors = session('errors');
2016-01-20 08:23:36 -06:00
$classes = 'form-group';
2017-11-15 05:25:49 -06:00
if (null !== $errors && $errors->has($name)) {
2016-01-20 08:23:36 -06:00
$classes = 'form-group has-error has-feedback';
}
return $classes;
}
/**
* @param $name
* @param $options
*
* @return mixed
*/
2016-02-06 08:00:57 -06:00
protected function label(string $name, array $options): string
2016-01-20 08:23:36 -06:00
{
if (isset($options['label'])) {
return $options['label'];
}
$name = str_replace('[]', '', $name);
2016-01-20 08:23:36 -06:00
2016-02-06 08:00:57 -06:00
return strval(trans('form.' . $name));
2016-01-20 08:23:36 -06:00
}
2016-12-06 09:58:39 -06:00
/**
* @param string $name
* @param string $view
* @param null $value
* @param array $options
*
* @return string
2017-12-22 11:32:43 -06:00
*
2017-12-29 02:05:35 -06:00
* @throws \FireflyIII\Exceptions\FireflyException
2016-12-06 09:58:39 -06:00
*/
private function currencyField(string $name, string $view, $value = null, array $options = []): string
{
$label = $this->label($name, $options);
$options = $this->expandOptionArray($name, $label, $options);
$classes = $this->getHolderClasses($name);
$value = $this->fillFieldValue($name, $value);
$options['step'] = 'any';
$defaultCurrency = isset($options['currency']) ? $options['currency'] : Amt::getDefaultCurrency();
$currencies = app('amount')->getAllCurrencies();
2016-12-06 09:58:39 -06:00
unset($options['currency']);
unset($options['placeholder']);
2016-12-22 11:19:50 -06:00
2017-07-07 10:51:14 -05:00
// perhaps the currency has been sent to us in the field $amount_currency_id_$name (amount_currency_id_amount)
$preFilled = session('preFilled');
$key = 'amount_currency_id_' . $name;
$sentCurrencyId = isset($preFilled[$key]) ? intval($preFilled[$key]) : $defaultCurrency->id;
// find this currency in set of currencies:
foreach ($currencies as $currency) {
if ($currency->id === $sentCurrencyId) {
$defaultCurrency = $currency;
break;
}
}
2016-12-22 11:19:50 -06:00
// make sure value is formatted nicely:
2017-11-15 05:25:49 -06:00
if (null !== $value && '' !== $value) {
2016-12-30 06:45:02 -06:00
$value = round($value, $defaultCurrency->decimal_places);
2016-12-22 11:19:50 -06:00
}
2016-12-06 09:58:39 -06:00
$html = view('form.' . $view, compact('defaultCurrency', 'currencies', 'classes', 'name', 'label', 'value', 'options'))->render();
return $html;
}
2015-03-29 01:14:32 -05:00
}