2015-02-07 18:15:15 -06:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace FireflyIII\Support;
|
|
|
|
|
2015-03-21 15:33:52 -05:00
|
|
|
use Amount as Amt;
|
2015-02-07 18:15:15 -06:00
|
|
|
use FireflyIII\Models\TransactionCurrency;
|
2015-02-24 14:10:25 -06:00
|
|
|
use Illuminate\Support\Collection;
|
2015-02-07 18:15:15 -06:00
|
|
|
use Illuminate\Support\MessageBag;
|
|
|
|
use Input;
|
|
|
|
use Session;
|
|
|
|
use View;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class ExpandedForm
|
|
|
|
*
|
|
|
|
* @package FireflyIII\Support
|
|
|
|
*/
|
|
|
|
class ExpandedForm
|
|
|
|
{
|
2015-02-25 08:19:14 -06:00
|
|
|
|
2015-02-07 18:15:15 -06:00
|
|
|
/**
|
|
|
|
* @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 = [])
|
2015-02-07 18:15:15 -06:00
|
|
|
{
|
|
|
|
$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();
|
2015-02-07 18:15:15 -06:00
|
|
|
$currencies = TransactionCurrency::orderBy('code', 'ASC')->get();
|
2015-02-24 14:10:25 -06:00
|
|
|
$html = View::make('form.amount', compact('defaultCurrency', 'currencies', 'classes', 'name', 'label', 'value', 'options'))->render();
|
2015-02-07 18:15:15 -06:00
|
|
|
|
|
|
|
return $html;
|
2015-02-24 14:10:25 -06:00
|
|
|
|
2015-02-07 18:15:15 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $name
|
|
|
|
* @param $options
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function label($name, $options)
|
|
|
|
{
|
|
|
|
if (isset($options['label'])) {
|
|
|
|
return $options['label'];
|
|
|
|
}
|
2015-03-02 13:05:28 -06:00
|
|
|
$labels = [
|
|
|
|
'amount_min' => 'Amount (min)',
|
|
|
|
'amount_max' => 'Amount (max)',
|
|
|
|
'match' => 'Matches on',
|
|
|
|
'repeat_freq' => 'Repetition',
|
|
|
|
'account_from_id' => 'Account from',
|
|
|
|
'account_to_id' => 'Account to',
|
|
|
|
'account_id' => 'Asset account',
|
|
|
|
'budget_id' => 'Budget',
|
|
|
|
'openingBalance' => 'Opening balance',
|
2015-04-01 12:42:14 -05:00
|
|
|
'virtualBalance' => 'Virtual balance',
|
|
|
|
'targetamount' => 'Target amount',
|
2015-03-02 13:05:28 -06:00
|
|
|
'accountRole' => 'Account role',
|
|
|
|
'openingBalanceDate' => 'Opening balance date',
|
|
|
|
'piggy_bank_id' => 'Piggy bank'];
|
2015-02-07 18:15:15 -06:00
|
|
|
|
|
|
|
|
|
|
|
return isset($labels[$name]) ? $labels[$name] : str_replace('_', ' ', ucfirst($name));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $name
|
|
|
|
* @param $label
|
|
|
|
* @param array $options
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public 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
|
|
|
|
*/
|
|
|
|
public function getHolderClasses($name)
|
|
|
|
{
|
|
|
|
/*
|
2015-03-21 15:33:52 -05:00
|
|
|
* Get errors from session:
|
2015-02-07 18:15:15 -06:00
|
|
|
*/
|
|
|
|
/** @var MessageBag $errors */
|
2015-03-21 15:33:52 -05:00
|
|
|
$errors = Session::get('errors');
|
|
|
|
$classes = 'form-group';
|
|
|
|
|
|
|
|
if (!is_null($errors) && $errors->has($name)) {
|
|
|
|
$classes = 'form-group has-error has-feedback';
|
2015-02-07 18:15:15 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
return $classes;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $name
|
|
|
|
* @param $value
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function fillFieldValue($name, $value)
|
|
|
|
{
|
|
|
|
if (Session::has('preFilled')) {
|
2015-03-07 02:21:06 -06:00
|
|
|
$preFilled = Session::get('preFilled');
|
2015-02-07 18:15:15 -06:00
|
|
|
$value = isset($preFilled[$name]) && is_null($value) ? $preFilled[$name] : $value;
|
|
|
|
}
|
|
|
|
if (!is_null(Input::old($name))) {
|
|
|
|
$value = Input::old($name);
|
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
2015-02-24 14:10:25 -06:00
|
|
|
$currencies = TransactionCurrency::orderBy('code', 'ASC')->get();
|
|
|
|
$html = View::make('form.balance', compact('defaultCurrency', 'currencies', 'classes', 'name', 'label', 'value', 'options'))->render();
|
|
|
|
|
|
|
|
return $html;
|
|
|
|
}
|
|
|
|
|
2015-02-07 18:15:15 -06:00
|
|
|
/**
|
|
|
|
* @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']);
|
|
|
|
|
|
|
|
$html = View::make('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);
|
|
|
|
$html = View::make('form.date', compact('classes', 'name', 'label', 'value', 'options'))->render();
|
|
|
|
|
|
|
|
return $html;
|
|
|
|
}
|
|
|
|
|
2015-03-02 13:05:28 -06:00
|
|
|
/**
|
|
|
|
* @param $name
|
|
|
|
* @param null $value
|
|
|
|
* @param array $options
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function integer($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'] = '1';
|
2015-03-07 02:21:06 -06:00
|
|
|
$html = View::make('form.integer', 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
|
|
|
/**
|
|
|
|
* @SuppressWarnings("CyclomaticComplexity") // It's exactly 5. So I don't mind.
|
|
|
|
*
|
|
|
|
* Takes any collection and tries to make a sensible select list compatible array of it.
|
|
|
|
*
|
|
|
|
* @param Collection $set
|
|
|
|
* @param bool $addEmpty
|
|
|
|
*
|
|
|
|
* @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) {
|
|
|
|
$id = intval($entry->id);
|
|
|
|
$title = null;
|
|
|
|
|
|
|
|
foreach ($fields as $field) {
|
|
|
|
if (isset($entry->$field)) {
|
|
|
|
$title = $entry->$field;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$selectList[$id] = $title;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $selectList;
|
|
|
|
}
|
|
|
|
|
2015-02-07 18:15:15 -06:00
|
|
|
/**
|
|
|
|
* @param $type
|
|
|
|
* @param $name
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function optionsList($type, $name)
|
|
|
|
{
|
2015-03-07 02:21:06 -06:00
|
|
|
$previousValue = Input::old('post_submit_action');
|
2015-02-07 18:15:15 -06:00
|
|
|
$previousValue = is_null($previousValue) ? 'store' : $previousValue;
|
2015-03-07 02:21:06 -06:00
|
|
|
$html = View::make('form.options', compact('type', 'name', 'previousValue'))->render();
|
2015-02-07 18:15:15 -06:00
|
|
|
|
|
|
|
return $html;
|
|
|
|
}
|
|
|
|
|
2015-02-11 00:35:10 -06:00
|
|
|
/**
|
|
|
|
* @param $name
|
|
|
|
* @param array $list
|
|
|
|
* @param null $selected
|
|
|
|
* @param array $options
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2015-02-07 18:15:15 -06:00
|
|
|
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-03-07 02:21:06 -06:00
|
|
|
$html = View::make('form.select', compact('classes', 'name', 'label', 'selected', 'options', 'list'))->render();
|
2015-02-07 18:15:15 -06:00
|
|
|
|
|
|
|
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-03-07 02:21:06 -06:00
|
|
|
$html = View::make('form.tags', compact('classes', 'name', 'label', 'value', 'options'))->render();
|
2015-03-02 13:05:28 -06:00
|
|
|
|
|
|
|
return $html;
|
|
|
|
}
|
|
|
|
|
2015-02-07 18:15:15 -06:00
|
|
|
/**
|
|
|
|
* @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);
|
|
|
|
$html = View::make('form.text', compact('classes', 'name', 'label', 'value', 'options'))->render();
|
|
|
|
|
|
|
|
return $html;
|
|
|
|
|
|
|
|
}
|
2015-03-29 01:14:32 -05:00
|
|
|
}
|