diff --git a/app/lib/FireflyIII/Form/Form.php b/app/lib/FireflyIII/Form/Form.php index 2548b2d511..5d4bb91574 100644 --- a/app/lib/FireflyIII/Form/Form.php +++ b/app/lib/FireflyIII/Form/Form.php @@ -24,181 +24,15 @@ class Form */ public static function ffAmount($name, $value = null, array $options = []) { + $label = self::label($name, $options); + $options = self::expandOptionArray($name, $label, $options); + $classes = self::getHolderClasses($name); + $value = self::fillFieldValue($name, $value); $options['step'] = 'any'; $options['min'] = '0.01'; - - return self::ffInput('amount', $name, $value, $options); - - } - - /** - * @param $type - * @param $name - * @param null $value - * @param array $options - * @param array $list - * - * @return string - * @throws FireflyException - */ - public static function ffInput($type, $name, $value = null, array $options = [], $list = []) - { - /* - * add some defaults to this method: - */ - $options['class'] = 'form-control'; - $options['id'] = 'ffInput_' . $name; - $options['autocomplete'] = 'off'; - $label = self::label($name, $options); - - /* - * Make label and placeholder look nice. - */ - $options['placeholder'] = ucfirst($label); - - /* - * Get pre filled value: - */ - if (\Session::has('preFilled')) { - $preFilled = \Session::get('preFilled'); - $value = isset($preFilled[$name]) && is_null($value) ? $preFilled[$name] : $value; - - } - - /* - * Get the value. - */ - if (!is_null(\Input::old($name))) { - /* - * Old value overrules $value. - */ - $value = \Input::old($name); - } - - /* - * Get errors, warnings and successes from session: - */ - /** @var MessageBag $errors */ - $errors = \Session::get('errors'); - - /** @var MessageBag $warnings */ - $warnings = \Session::get('warnings'); - - /** @var MessageBag $successes */ - $successes = \Session::get('successes'); - - - /* - * If errors, add some more classes. - */ - switch (true) { - case (!is_null($errors) && $errors->has($name)): - $classes = 'form-group has-error has-feedback'; - break; - case (!is_null($warnings) && $warnings->has($name)): - $classes = 'form-group has-warning has-feedback'; - break; - case (!is_null($successes) && $successes->has($name)): - $classes = 'form-group has-success has-feedback'; - break; - default: - $classes = 'form-group'; - break; - } - - /* - * Add some HTML. - */ - $html = '
'; - $html .= ''; - $html .= '
'; - - - /* - * Switch input type: - */ - unset($options['label']); - switch ($type) { - case 'text': - $html .= \Form::input('text', $name, $value, $options); - break; - case 'amount': - // currency button: - $defaultCurrency = \Amount::getDefaultCurrency(); - $html .= - '
'; - // all currencies: - $list = \TransactionCurrency::where('name', '!=', $defaultCurrency->name)->get(); - $html .= '
'; - //$html .= '
' . \Amount::getCurrencySymbol() . '
'; - $html .= \Form::input('number', $name, $value, $options); - $html .= '
'; - break; - case 'number': - $html .= \Form::input('number', $name, $value, $options); - break; - case 'checkbox': - $checked = $options['checked']; - unset($options['placeholder'], $options['autocomplete'], $options['class']); - $html .= '
'; - - - break; - case 'date': - $html .= \Form::input('date', $name, $value, $options); - break; - case 'select': - $html .= \Form::select($name, $list, $value, $options); - break; - default: - throw new FireflyException('Cannot handle type "' . $type . '" in FFFormBuilder.'); - break; - } - - /* - * If errors, respond to them: - */ - - if (!is_null($errors)) { - if ($errors->has($name)) { - $html .= ''; - $html .= '

' . e($errors->first($name)) . '

'; - } - } - unset($errors); - /* - * If warnings, respond to them: - */ - - if (!is_null($warnings)) { - if ($warnings->has($name)) { - $html .= ''; - $html .= '

' . e($warnings->first($name)) . '

'; - } - } - unset($warnings); - - /* - * If successes, respond to them: - */ - - if (!is_null($successes)) { - if ($successes->has($name)) { - $html .= ''; - $html .= '

' . e($successes->first($name)) . '

'; - } - } - unset($successes); - - $html .= '
'; - $html .= '
'; + $defaultCurrency = \Amount::getDefaultCurrency(); + $currencies = \TransactionCurrency::where('name', '!=', $defaultCurrency->name)->get(); + $html = \View::make('form.amount', compact('defaultCurrency', 'currencies', 'classes', 'name', 'label', 'value', 'options'))->render(); return $html; @@ -216,14 +50,87 @@ class Form return $options['label']; } $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' - ,'piggy_bank_id' => 'Piggy bank']; + 'account_from_id' => 'Account from', 'account_to_id' => 'Account to', 'account_id' => 'Asset account', 'budget_id' => 'Budget' + , 'piggy_bank_id' => 'Piggy bank']; return isset($labels[$name]) ? $labels[$name] : str_replace('_', ' ', ucfirst($name)); } + /** + * @param $name + * @param $label + * @param array $options + * + * @return array + */ + public static 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 static function getHolderClasses($name) + { + /* + * Get errors, warnings and successes from session: + */ + /** @var MessageBag $errors */ + $errors = \Session::get('errors'); + + /** @var MessageBag $warnings */ + $warnings = \Session::get('warnings'); + + /** @var MessageBag $successes */ + $successes = \Session::get('successes'); + + switch (true) { + case (!is_null($errors) && $errors->has($name)): + $classes = 'form-group has-error has-feedback'; + break; + case (!is_null($warnings) && $warnings->has($name)): + $classes = 'form-group has-warning has-feedback'; + break; + case (!is_null($successes) && $successes->has($name)): + $classes = 'form-group has-success has-feedback'; + break; + default: + $classes = 'form-group'; + break; + } + + return $classes; + } + + /** + * @param $name + * @param $value + * + * @return mixed + */ + public static function fillFieldValue($name, $value) + { + if (\Session::has('preFilled')) { + $preFilled = \Session::get('preFilled'); + $value = isset($preFilled[$name]) && is_null($value) ? $preFilled[$name] : $value; + } + if (!is_null(\Input::old($name))) { + $value = \Input::old($name); + } + + return $value; + } + /** * @param $name * @param null $value @@ -234,10 +141,15 @@ class Form */ public static function ffBalance($name, $value = null, array $options = []) { + $label = self::label($name, $options); + $options = self::expandOptionArray($name, $label, $options); + $classes = self::getHolderClasses($name); + $value = self::fillFieldValue($name, $value); $options['step'] = 'any'; - - return self::ffInput('amount', $name, $value, $options); - + $defaultCurrency = \Amount::getDefaultCurrency(); + $currencies = \TransactionCurrency::where('name', '!=', $defaultCurrency->name)->get(); + $html = \View::make('form.balance', compact('defaultCurrency', 'currencies', 'classes', 'name', 'label', 'value', 'options'))->render(); + return $html; } /** @@ -252,8 +164,16 @@ class Form public static function ffCheckbox($name, $value = 1, $checked = null, $options = []) { $options['checked'] = $checked === true ? true : null; + $label = self::label($name, $options); + $options = self::expandOptionArray($name, $label, $options); + $classes = self::getHolderClasses($name); + $value = self::fillFieldValue($name, $value); - return self::ffInput('checkbox', $name, $value, $options); + unset($options['placeholder'], $options['autocomplete'], $options['class']); + + $html = \View::make('form.checkbox', compact('classes', 'name', 'label', 'value', 'options'))->render(); + + return $html; } /** @@ -266,7 +186,13 @@ class Form */ public static function ffDate($name, $value = null, array $options = []) { - return self::ffInput('date', $name, $value, $options); + $label = self::label($name, $options); + $options = self::expandOptionArray($name, $label, $options); + $classes = self::getHolderClasses($name); + $value = self::fillFieldValue($name, $value); + $html = \View::make('form.date', compact('classes', 'name', 'label', 'value', 'options'))->render(); + + return $html; } /** @@ -279,9 +205,14 @@ class Form */ public static function ffInteger($name, $value = null, array $options = []) { + $label = self::label($name, $options); + $options = self::expandOptionArray($name, $label, $options); + $classes = self::getHolderClasses($name); + $value = self::fillFieldValue($name, $value); $options['step'] = '1'; + $html = \View::make('form.integer', compact('classes', 'name', 'label', 'value', 'options'))->render(); - return self::ffInput('number', $name, $value, $options); + return $html; } @@ -298,57 +229,9 @@ class Form { $previousValue = \Input::old('post_submit_action'); $previousValue = is_null($previousValue) ? 'store' : $previousValue; - /* - * Store. - */ - switch ($type) { - case 'create': - $store = '
'; - $store .= '
'; - break; - case 'update': - $store = '
'; - $store .= '
'; - break; - default: - throw new FireflyException('Cannot create ffOptionsList for option (store) ' . $type); - break; - } + $html = \View::make('form.options', compact('type', 'name', 'previousValue'))->render(); - /* - * validate is always the same: - */ - $validate = '
'; - - /* - * Store & return: - */ - switch ($type) { - case 'create': - $return = '
'; - break; - case 'update': - $return = '
'; - break; - default: - throw new FireflyException('Cannot create ffOptionsList for option (store+return) ' . $type); - break; - } - - return $store . $validate . $return; + return $html; } /** @@ -358,11 +241,16 @@ class Form * @param array $options * * @return string - * @throws FireflyException */ public static function ffSelect($name, array $list = [], $selected = null, array $options = []) { - return self::ffInput('select', $name, $selected, $options, $list); + $label = self::label($name, $options); + $options = self::expandOptionArray($name, $label, $options); + $classes = self::getHolderClasses($name); + $selected = self::fillFieldValue($name, $selected); + $html = \View::make('form.select', compact('classes', 'name', 'label', 'selected', 'options', 'list'))->render(); + + return $html; } /** @@ -375,9 +263,14 @@ class Form */ public static function ffTags($name, $value = null, array $options = []) { + $label = self::label($name, $options); + $options = self::expandOptionArray($name, $label, $options); + $classes = self::getHolderClasses($name); + $value = self::fillFieldValue($name, $value); $options['data-role'] = 'tagsinput'; + $html = \View::make('form.tags', compact('classes', 'name', 'label', 'value', 'options'))->render(); - return self::ffInput('text', $name, $value, $options); + return $html; } /** @@ -390,7 +283,13 @@ class Form */ public static function ffText($name, $value = null, array $options = []) { - return self::ffInput('text', $name, $value, $options); + $label = self::label($name, $options); + $options = self::expandOptionArray($name, $label, $options); + $classes = self::getHolderClasses($name); + $value = self::fillFieldValue($name, $value); + $html = \View::make('form.text', compact('classes', 'name', 'label', 'value', 'options'))->render(); + + return $html; } } diff --git a/app/views/form/amount.blade.php b/app/views/form/amount.blade.php new file mode 100644 index 0000000000..3de0fcd955 --- /dev/null +++ b/app/views/form/amount.blade.php @@ -0,0 +1,19 @@ +
+ +
+
+
+ + +
+ {{Form::input('number', $name, $value, $options)}} + @include('form.feedback') +
+
+
\ No newline at end of file diff --git a/app/views/form/balance.blade.php b/app/views/form/balance.blade.php new file mode 100644 index 0000000000..3de0fcd955 --- /dev/null +++ b/app/views/form/balance.blade.php @@ -0,0 +1,19 @@ +
+ +
+
+
+ + +
+ {{Form::input('number', $name, $value, $options)}} + @include('form.feedback') +
+
+
\ No newline at end of file diff --git a/app/views/form/checkbox.blade.php b/app/views/form/checkbox.blade.php new file mode 100644 index 0000000000..5772bbf3f1 --- /dev/null +++ b/app/views/form/checkbox.blade.php @@ -0,0 +1,11 @@ +
+ +
+
+ +
+ @include('form.feedback') +
+
\ No newline at end of file diff --git a/app/views/form/date.blade.php b/app/views/form/date.blade.php new file mode 100644 index 0000000000..beb3228f9e --- /dev/null +++ b/app/views/form/date.blade.php @@ -0,0 +1,7 @@ +
+ +
+ {{Form::input('date', $name, $value, $options)}} + @include('form.feedback') +
+
\ No newline at end of file diff --git a/app/views/form/feedback.blade.php b/app/views/form/feedback.blade.php new file mode 100644 index 0000000000..72c3762e4b --- /dev/null +++ b/app/views/form/feedback.blade.php @@ -0,0 +1,12 @@ +@if(Session::has('errors') && Session::get('errors')->has($name)) + +

{{{Session::get('errors')->first($name)}}}

+@endif +@if(Session::has('warnings') && Session::get('warnings')->has($name)) + +

{{{Session::get('warnings')->first($name)}}}

+@endif +@if(Session::has('successes') && Session::get('successes')->has($name)) + +

{{{Session::get('successes')->first($name)}}}

+@endif \ No newline at end of file diff --git a/app/views/form/integer.blade.php b/app/views/form/integer.blade.php new file mode 100644 index 0000000000..fd10ca6ca6 --- /dev/null +++ b/app/views/form/integer.blade.php @@ -0,0 +1,9 @@ +
+ +
+
+ {{Form::input('number', $name, $value, $options)}} + @include('form.feedback') +
+
+
\ No newline at end of file diff --git a/app/views/form/options.blade.php b/app/views/form/options.blade.php new file mode 100644 index 0000000000..5373a0c60e --- /dev/null +++ b/app/views/form/options.blade.php @@ -0,0 +1,69 @@ +@if($type == 'create') +
+ +
+
+ +
+
+
+@endif +@if($type == 'update') +
+ +
+
+ +
+
+
+@endif + +
+ +
+
+ +
+
+
+ +@if($type == 'create') +
+ +
+
+ +
+
+
+@endif + +@if($type == 'update') +
+ +
+
+
+
+
+@endif \ No newline at end of file diff --git a/app/views/form/select.blade.php b/app/views/form/select.blade.php new file mode 100644 index 0000000000..cc35a7b4ea --- /dev/null +++ b/app/views/form/select.blade.php @@ -0,0 +1,8 @@ +
+ +
+ {{Form::select($name, $list, $selected , $options )}} + @include('form.feedback') + +
+
\ No newline at end of file diff --git a/app/views/form/tags.blade.php b/app/views/form/tags.blade.php new file mode 100644 index 0000000000..3094ba8e25 --- /dev/null +++ b/app/views/form/tags.blade.php @@ -0,0 +1,7 @@ +
+ +
+ {{Form::input('text', $name, $value, $options)}} + @include('form.feedback') +
+
\ No newline at end of file diff --git a/app/views/form/text.blade.php b/app/views/form/text.blade.php new file mode 100644 index 0000000000..3094ba8e25 --- /dev/null +++ b/app/views/form/text.blade.php @@ -0,0 +1,7 @@ +
+ +
+ {{Form::input('text', $name, $value, $options)}} + @include('form.feedback') +
+
\ No newline at end of file