firefly-iii/app/Support/ExpandedForm.php

445 lines
13 KiB
PHP
Raw Normal View History

<?php
namespace FireflyIII\Support;
use Amount as Amt;
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 Input;
use RuntimeException;
use Session;
/**
* Class ExpandedForm
*
* @package FireflyIII\Support
2015-05-26 01:17:58 -05:00
*
* @SuppressWarnings(PHPMD.TooManyMethods)
*/
class ExpandedForm
{
2015-02-25 08:19:14 -06:00
2015-07-18 16:51:51 -05:00
/**
* @param $name
* @param null $value
* @param array $options
*
* @return string
*/
public function staticText($name, $value, array $options = [])
{
$label = $this->label($name, $options);
$options = $this->expandOptionArray($name, $label, $options);
$classes = $this->getHolderClasses($name);
$value = $this->fillFieldValue($name, $value);
$html = view('form.static', compact('classes', 'name', 'label', 'value', 'options'))->render();
return $html;
}
/**
* @param $name
* @param null $value
* @param array $options
*
* @return string
*/
2015-02-24 14:10:25 -06:00
public function amount($name, $value = null, array $options = [])
{
$label = $this->label($name, $options);
$options = $this->expandOptionArray($name, $label, $options);
$classes = $this->getHolderClasses($name);
$value = $this->fillFieldValue($name, $value);
$options['step'] = 'any';
2015-02-24 14:10:25 -06:00
$options['min'] = '0.01';
2015-03-07 02:21:06 -06:00
$defaultCurrency = isset($options['currency']) ? $options['currency'] : Amt::getDefaultCurrency();
$currencies = Amt::getAllCurrencies();
2015-06-22 11:50:54 -05:00
unset($options['currency']);
unset($options['placeholder']);
2015-07-12 10:59:13 -05:00
$html = view('form.amount', compact('defaultCurrency', 'currencies', 'classes', 'name', 'label', 'value', 'options'))->render();
return $html;
2015-02-24 14:10:25 -06:00
}
/**
* @param $name
* @param $options
*
* @return mixed
*/
2015-05-26 01:17:58 -05:00
protected function label($name, $options)
{
if (isset($options['label'])) {
return $options['label'];
}
2015-05-20 12:55:53 -05:00
return trans('form.' . $name);
}
/**
* @param $name
* @param $label
* @param array $options
*
* @return array
*/
2015-05-26 01:17:58 -05:00
protected function expandOptionArray($name, $label, array $options)
{
$options['class'] = 'form-control';
$options['id'] = 'ffInput_' . $name;
$options['autocomplete'] = 'off';
$options['placeholder'] = ucfirst($label);
return $options;
}
/**
* @param $name
*
* @return string
*/
2015-05-26 01:17:58 -05:00
protected function getHolderClasses($name)
{
/*
* Get errors from session:
*/
/** @var MessageBag $errors */
$errors = Session::get('errors');
$classes = 'form-group';
if (!is_null($errors) && $errors->has($name)) {
$classes = 'form-group has-error has-feedback';
}
return $classes;
}
/**
* @param $name
* @param $value
*
* @return mixed
*/
2015-05-26 01:17:58 -05:00
protected function fillFieldValue($name, $value)
{
if (Session::has('preFilled')) {
2015-03-07 02:21:06 -06:00
$preFilled = Session::get('preFilled');
$value = isset($preFilled[$name]) && is_null($value) ? $preFilled[$name] : $value;
}
// @codeCoverageIgnoreStart
try {
if (!is_null(Input::old($name))) {
$value = Input::old($name);
}
} catch (RuntimeException $e) {
// don't care about session errors.
}
// @codeCoverageIgnoreEnd
return $value;
}
2015-02-24 14:10:25 -06:00
/**
* @param $name
* @param null $value
* @param array $options
*
* @return string
*/
public function balance($name, $value = null, array $options = [])
{
$label = $this->label($name, $options);
$options = $this->expandOptionArray($name, $label, $options);
$classes = $this->getHolderClasses($name);
$value = $this->fillFieldValue($name, $value);
$options['step'] = 'any';
2015-03-07 02:21:06 -06:00
$defaultCurrency = isset($options['currency']) ? $options['currency'] : Amt::getDefaultCurrency();
$currencies = Amt::getAllCurrencies();
2015-06-22 11:50:54 -05:00
unset($options['currency']);
unset($options['placeholder']);
2015-07-12 10:59:13 -05:00
$html = view('form.balance', compact('defaultCurrency', 'currencies', 'classes', 'name', 'label', 'value', 'options'))->render();
2015-02-24 14:10:25 -06:00
return $html;
}
/**
* @param $name
* @param int $value
* @param null $checked
* @param array $options
*
* @return string
*/
public function checkbox($name, $value = 1, $checked = null, $options = [])
{
$options['checked'] = $checked === true ? 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
*/
public function date($name, $value = null, array $options = [])
{
$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;
}
2015-04-03 15:54:21 -05:00
/**
* @param $name
* @param null $value
* @param array $options
*
* @return string
*/
public function integer($name, $value = null, array $options = [])
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-04-03 15:54:21 -05:00
}
2015-03-02 13:05:28 -06:00
/**
* @param $name
* @param null $value
* @param array $options
*
* @return string
*/
public function location($name, $value = null, array $options = [])
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
* @param bool $addEmpty
2015-02-24 14:10:25 -06:00
*
* @return mixed
*/
public function makeSelectList(Collection $set, $addEmpty = false)
{
$selectList = [];
if ($addEmpty) {
$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) {
if (isset($entry->$field)) {
$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
*/
public function multiCheckbox($name, array $list = [], $selected = null, array $options = [])
{
$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
*/
public function multiRadio($name, array $list = [], $selected = null, array $options = [])
{
$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 $type
* @param $name
*
* @return string
*/
public function optionsList($type, $name)
{
$previousValue = null;
// @codeCoverageIgnoreStart
try {
$previousValue = Input::old('post_submit_action');
} catch (RuntimeException $e) {
// don't care
}
// @codeCoverageIgnoreEnd
$previousValue = is_null($previousValue) ? 'store' : $previousValue;
2015-07-12 10:59:13 -05:00
$html = view('form.options', compact('type', 'name', 'previousValue'))->render();
return $html;
}
2015-02-11 00:35:10 -06:00
/**
* @param $name
* @param array $list
* @param null $selected
* @param array $options
*
* @return string
*/
public function select($name, array $list = [], $selected = null, array $options = [])
{
$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
*/
public function tags($name, $value = null, array $options = [])
{
$label = $this->label($name, $options);
$options = $this->expandOptionArray($name, $label, $options);
$classes = $this->getHolderClasses($name);
$value = $this->fillFieldValue($name, $value);
$options['data-role'] = 'tagsinput';
2015-07-12 10:59:13 -05:00
$html = view('form.tags', compact('classes', 'name', 'label', 'value', 'options'))->render();
2015-03-02 13:05:28 -06:00
return $html;
}
/**
* @param $name
* @param array $options
*
* @return string
*/
public function file($name, array $options = [])
{
$label = $this->label($name, $options);
$options = $this->expandOptionArray($name, $label, $options);
$classes = $this->getHolderClasses($name);
2015-07-12 10:59:13 -05:00
$html = view('form.file', compact('classes', 'name', 'label', 'options'))->render();
return $html;
}
/**
* @param $name
* @param null $value
* @param array $options
*
* @return string
*/
public function text($name, $value = null, array $options = [])
{
$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
*/
public function textarea($name, $value = null, array $options = [])
{
$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;
}
2015-03-29 01:14:32 -05:00
}