Various updates to facilitate validating things.

This commit is contained in:
Sander Dorigo 2014-10-06 19:32:09 +02:00
parent 28aaea1aa3
commit d5bcf5497f
13 changed files with 304 additions and 315 deletions

View File

@ -2,6 +2,7 @@
use Firefly\Exception\FireflyException;
use Firefly\Storage\RecurringTransaction\RecurringTransactionRepositoryInterface as RTR;
use Firefly\Helper\Controllers\RecurringInterface as RI;
/**
* Class RecurringController
@ -11,13 +12,16 @@ use Firefly\Storage\RecurringTransaction\RecurringTransactionRepositoryInterface
class RecurringController extends BaseController
{
protected $_repository;
protected $_helper;
/**
* @param RTR $repository
* @param RI $helper
*/
public function __construct(RTR $repository)
public function __construct(RTR $repository, RI $helper)
{
$this->_repository = $repository;
$this->_helper = $helper;
View::share('title', 'Recurring transactions');
View::share('mainTitleIcon', 'fa-rotate-right');
@ -100,7 +104,7 @@ class RecurringController extends BaseController
public function store()
{
$data = Input::except(['_token', 'post_submit_action']);
switch (Input::get('post_submit_action')) {
default:
throw new FireflyException('Method ' . Input::get('post_submit_action') . ' not implemented yet.');
@ -110,7 +114,7 @@ class RecurringController extends BaseController
/*
* Try to store:
*/
$messageBag = $this->_repository->store(Input::all());
$messageBag = $this->_repository->store($data);
/*
* Failure!
@ -134,49 +138,76 @@ class RecurringController extends BaseController
return Redirect::route('recurring.index');
}
break;
case 'validate_only':
$messageBags = $this->_helper->validate($data);
Session::flash('warnings', $messageBags['warnings']);
Session::flash('successes', $messageBags['successes']);
Session::flash('errors', $messageBags['errors']);
return Redirect::route('recurring.create')->withInput();
break;
}
//
//
// if ($recurringTransaction->errors()->count() == 0) {
// Session::flash('success', 'Recurring transaction "' . $recurringTransaction->name . '" saved!');
// //Event::fire('recurring.store', [$recurringTransaction]);
// if (Input::get('create') == '1') {
// return Redirect::route('recurring.create')->withInput();
// } else {
// return Redirect::route('recurring.index');
// }
// } else {
// Session::flash(
// 'error', 'Could not save the recurring transaction: ' . $recurringTransaction->errors()->first()
// );
//
// return Redirect::route('recurring.create')->withInput()->withErrors($recurringTransaction->errors());
// }
}
/**
* @param RecurringTransaction $recurringTransaction
*
* @return $this|\Illuminate\Http\RedirectResponse
*/
public function update(RecurringTransaction $recurringTransaction)
{
/** @var \RecurringTransaction $recurringTransaction */
$recurringTransaction = $this->_repository->update($recurringTransaction, Input::all());
if ($recurringTransaction->errors()->count() == 0) {
Session::flash('success', 'The recurring transaction has been updated.');
//Event::fire('recurring.update', [$recurringTransaction]);
$data = Input::except(['_token', 'post_submit_action']);
switch (Input::get('post_submit_action')) {
case 'update':
case 'return_to_edit':
$messageBag = $this->_repository->update($recurringTransaction, $data);
if ($messageBag->count() == 0) {
// has been saved, return to index:
Session::flash('success', 'Recurring transaction updated!');
return Redirect::route('recurring.index');
} else {
Session::flash(
'error', 'Could not update the recurring transaction: ' . $recurringTransaction->errors()->first()
);
if (Input::get('post_submit_action') == 'return_to_edit') {
return Redirect::route('recurring.edit', $recurringTransaction->id)->withInput();
} else {
return Redirect::route('recurring.index');
}
} else {
Session::flash('error', 'Could not update recurring transaction: ' . $messageBag->first());
return Redirect::route('recurring.edit', $recurringTransaction->id)->withInput()->withErrors(
$recurringTransaction->errors()
);
return Redirect::route('transactions.edit', $recurringTransaction->id)->withInput()
->withErrors($messageBag);
}
break;
case 'validate_only':
$data = Input::all();
$data['id'] = $recurringTransaction->id;
$messageBags = $this->_helper->validate($data);
Session::flash('warnings', $messageBags['warnings']);
Session::flash('successes', $messageBags['successes']);
Session::flash('errors', $messageBags['errors']);
return Redirect::route('recurring.edit', $recurringTransaction->id)->withInput();
break;
// update
default:
throw new FireflyException('Method ' . Input::get('post_submit_action') . ' not implemented yet.');
break;
}
// /** @var \RecurringTransaction $recurringTransaction */
// $recurringTransaction = $this->_repository->update($recurringTransaction, Input::all());
// if ($recurringTransaction->errors()->count() == 0) {
// Session::flash('success', 'The recurring transaction has been updated.');
// //Event::fire('recurring.update', [$recurringTransaction]);
//
// return Redirect::route('recurring.index');
// } else {
// Session::flash(
// 'error', 'Could not update the recurring transaction: ' . $recurringTransaction->errors()->first()
// );
//
// return Redirect::route('recurring.edit', $recurringTransaction->id)->withInput()->withErrors(
// $recurringTransaction->errors()
// );
// }
}
}

View File

@ -323,7 +323,7 @@ class TransactionController extends BaseController
public function update(TransactionJournal $journal)
{
switch (Input::get('post_submit_action')) {
case 'store':
case 'update':
case 'return_to_edit':
$what = strtolower($journal->transactionType->type);
$messageBag = $this->_helper->update($journal, Input::all());
@ -333,7 +333,7 @@ class TransactionController extends BaseController
Event::fire('journals.update', [$journal]);
if (Input::get('post_submit_action') == 'return_to_edit') {
return Redirect::route('transactions.edit', $journal->id);
return Redirect::route('transactions.edit', $journal->id)->withInput();
} else {
return Redirect::route('transactions.index.' . $what);
}

View File

@ -100,10 +100,73 @@ class Form
'account_id' => 'Asset account'
];
return isset($labels[$name]) ? $labels[$name] : str_replace('_',' ',ucfirst($name));
return isset($labels[$name]) ? $labels[$name] : str_replace('_', ' ', ucfirst($name));
}
/**
* Return buttons for update/validate/return.
*
* @param $type
* @param $name
*/
public static function ffOptionsList($type, $name)
{
$previousValue = \Input::old('post_submit_action');
$previousValue = is_null($previousValue) ? 'store' : $previousValue;
/*
* Store.
*/
$store = '';
switch ($type) {
case 'create':
$store = '<div class="form-group"><label for="default" class="col-sm-4 control-label">Store</label>';
$store .= '<div class="col-sm-8"><div class="radio"><label>';
$store .= \Form::radio('post_submit_action', 'store', $previousValue == 'store');
$store .= 'Store ' . $name . '</label></div></div></div>';
break;
case 'update':
$store = '<div class="form-group"><label for="default" class="col-sm-4 control-label">Store</label>';
$store .= '<div class="col-sm-8"><div class="radio"><label>';
$store .= \Form::radio('post_submit_action', 'update', $previousValue == 'store');
$store .= 'Update ' . $name . '</label></div></div></div>';
break;
default:
throw new FireflyException('Cannot create ffOptionsList for option (store) ' . $type);
break;
}
/*
* validate is always the same:
*/
$validate = '<div class="form-group"><label for="validate_only" class="col-sm-4 control-label">Validate only';
$validate .= '</label><div class="col-sm-8"><div class="radio"><label>';
$validate .= \Form::radio('post_submit_action', 'validate_only', $previousValue == 'validate_only');
$validate .= 'Only validate, do not save</label></div></div></div>';
/*
* Store & return:
*/
switch ($type) {
case 'create':
$return = '<div class="form-group"><label for="return_to_form" class="col-sm-4 control-label">';
$return .= 'Return here</label><div class="col-sm-8"><div class="radio"><label>';
$return .= \Form::radio('post_submit_action','create_another', $previousValue == 'create_another');
$return .= 'After storing, return here to create another one.</label></div></div></div>';
break;
case 'update':
$return = '<div class="form-group"><label for="return_to_edit" class="col-sm-4 control-label">';
$return .= 'Return here</label><div class="col-sm-8"><div class="radio"><label>';
$return .= \Form::radio('post_submit_action','return_to_edit', $previousValue == 'return_to_edit');
$return .= 'After updating, return here.</label></div></div></div>';
break;
default:
throw new FireflyException('Cannot create ffOptionsList for option (store+return) ' . $type);
break;
}
return $store.$validate.$return;
}
/**
* @param $type
* @param $name

View File

@ -0,0 +1,120 @@
<?php
namespace Firefly\Helper\Controllers;
use Carbon\Carbon;
use Exception;
use Illuminate\Support\MessageBag;
class Recurring implements RecurringInterface
{
/**
* Returns messages about the validation.
*
* @param array $data
*
* @return array
*/
public function validate(array $data)
{
$errors = new MessageBag;
$warnings = new MessageBag;
$successes = new MessageBag;
/*
* Name:
*/
if (strlen($data['name']) == 0) {
$errors->add('name', 'The name should not be this short.');
}
if (strlen($data['name']) > 250) {
$errors->add('name', 'The name should not be this long.');
}
if (! isset($data['id'])) {
$count = \Auth::user()->recurringtransactions()->whereName($data['name'])->count();
} else {
$count = \Auth::user()->recurringtransactions()->whereName($data['name'])->where('id', '!=', $data['id'])->count();
}
if ($count > 0) {
$errors->add('name', 'A recurring transaction with this name already exists.');
}
if (count($errors->get('name')) == 0) {
$successes->add('name', 'OK!');
}
/*
* Match
*/
if (count(explode(',', $data['match'])) > 10) {
$warnings->add('match', 'This many matches is pretty pointless');
}
if (strlen($data['match']) == 0) {
$errors->add('match', 'Cannot match on nothing.');
}
if (count($errors->get('match')) == 0) {
$successes->add('match', 'OK!');
}
/*
* Amount
*/
if (floatval($data['amount_max']) == 0 && floatval($data['amount_min']) == 0) {
$errors->add('amount_min', 'Amount max and min cannot both be zero.');
$errors->add('amount_max', 'Amount max and min cannot both be zero.');
}
if (floatval($data['amount_max']) < floatval($data['amount_min'])) {
$errors->add('amount_max', 'Amount max must be more than amount min.');
}
if (floatval($data['amount_min']) > floatval($data['amount_max'])) {
$errors->add('amount_max', 'Amount min must be less than amount max.');
}
if (count($errors->get('amount_min')) == 0) {
$successes->add('amount_min', 'OK!');
}
if (count($errors->get('amount_max')) == 0) {
$successes->add('amount_max', 'OK!');
}
/*
* Date
*/
try {
$date = new Carbon($data['date']);
} catch (Exception $e) {
$errors->add('date', 'The date entered was invalid');
}
if (strlen($data['date']) == 0) {
$errors->add('date', 'The date entered was invalid');
}
if (!$errors->has('date')) {
$successes->add('date', 'OK!');
}
$successes->add('active', 'OK!');
$successes->add('automatch', 'OK!');
if (intval($data['skip']) < 0) {
$errors->add('skip', 'Cannot be below zero.');
} else if (intval($data['skip']) > 31) {
$errors->add('skip', 'Cannot be above 31.');
}
if (count($errors->get('skip')) == 0) {
$successes->add('skip', 'OK!');
}
$set = \Config::get('firefly.budget_periods');
if (!in_array($data['repeat_freq'], $set)) {
$errors->add('repeat_freq', 'Invalid value.');
}
if (count($errors->get('repeat_freq')) == 0) {
$successes->add('repeat_freq', 'OK!');
}
return ['errors' => $errors, 'warnings' => $warnings, 'successes' => $successes];
}
}

View File

@ -0,0 +1,15 @@
<?php
namespace Firefly\Helper\Controllers;
interface RecurringInterface {
/**
* Returns messages about the validation.
*
* @param array $data
*
* @return array
*/
public function validate(array $data);
}

View File

@ -32,6 +32,11 @@ class HelperServiceProvider extends ServiceProvider
'Firefly\Helper\Controllers\Json'
);
$this->app->bind(
'Firefly\Helper\Controllers\RecurringInterface',
'Firefly\Helper\Controllers\Recurring'
);
$this->app->bind(
'Firefly\Helper\Controllers\SearchInterface',
'Firefly\Helper\Controllers\Search'

View File

@ -154,7 +154,7 @@ class EloquentRecurringTransactionRepository implements RecurringTransactionRepo
// unique name?
$count = $this->_user->recurringtransactions()->whereName($data['name'])->count();
if($count > 0) {
if ($count > 0) {
$messageBag->add('name', 'A recurring transaction with this name already exists.');
return $messageBag;
}
@ -183,6 +183,8 @@ class EloquentRecurringTransactionRepository implements RecurringTransactionRepo
if ($recurringTransaction->validate()) {
$recurringTransaction->save();
} else {
$messageBag = $recurringTransaction->errors();
}
return $messageBag;
@ -192,10 +194,11 @@ class EloquentRecurringTransactionRepository implements RecurringTransactionRepo
* @param \RecurringTransaction $recurringTransaction
* @param $data
*
* @return mixed|void
* @return MessageBag
*/
public function update(\RecurringTransaction $recurringTransaction, $data)
{
$messageBag = new MessageBag;
$recurringTransaction->name = $data['name'];
$recurringTransaction->match = join(' ', explode(',', $data['match']));
$recurringTransaction->amount_max = floatval($data['amount_max']);
@ -203,9 +206,9 @@ class EloquentRecurringTransactionRepository implements RecurringTransactionRepo
// both amounts zero:
if ($recurringTransaction->amount_max == 0 && $recurringTransaction->amount_min == 0) {
$recurringTransaction->errors()->add('amount_max', 'Amount max and min cannot both be zero.');
$messageBag->add('amount_max', 'Amount max and min cannot both be zero.');
return $recurringTransaction;
return $messageBag;
}
$recurringTransaction->date = new Carbon($data['date']);
$recurringTransaction->active = isset($data['active']) ? intval($data['active']) : 0;
@ -215,9 +218,11 @@ class EloquentRecurringTransactionRepository implements RecurringTransactionRepo
if ($recurringTransaction->validate()) {
$recurringTransaction->save();
} else {
$messageBag = $recurringTransaction->errors();
}
return $recurringTransaction;
return $messageBag;
}

View File

@ -50,7 +50,7 @@ interface RecurringTransactionRepositoryInterface
* @param \RecurringTransaction $recurringTransaction
* @param $data
*
* @return mixed
* @return MessageBag
*/
public function update(\RecurringTransaction $recurringTransaction, $data);

View File

@ -93,6 +93,10 @@ App::down(
\Form::macro('ffCheckbox',function ($name, $value = 1, $checked = null, $options = []) {
return \Firefly\Form\Form::ffCheckbox($name, $value, $checked, $options);
});
\Form::macro('ffOptionsList',function ($type, $name) {
return \Firefly\Form\Form::ffOptionsList($type, $name);
});
/*
|--------------------------------------------------------------------------

View File

@ -43,45 +43,7 @@
<i class="fa fa-bolt"></i> Options
</div>
<div class="panel-body">
<div class="form-group">
<label for="default" class="col-sm-4 control-label">
Store
</label>
<div class="col-sm-8">
<div class="radio">
<label>
{{Form::radio('post_submit_action','store',true)}}
Store the recurring transaction
</label>
</div>
</div>
</div>
<div class="form-group">
<label for="validate_only" class="col-sm-4 control-label">
Validate only
</label>
<div class="col-sm-8">
<div class="radio">
<label>
{{Form::radio('post_submit_action','validate_only')}}
Only validate, do not save
</label>
</div>
</div>
</div>
<div class="form-group">
<label for="return_to_form" class="col-sm-4 control-label">
Return here
</label>
<div class="col-sm-8">
<div class="radio">
<label>
{{Form::radio('post_submit_action','create_another')}}
After storing, return here to create another one.
</label>
</div>
</div>
</div>
{{Form::ffOptionsList('create','recurring transaction')}}
</div>
</div>

View File

@ -10,82 +10,13 @@
<i class="fa fa-exclamation-circle"></i> Mandatory fields
</div>
<div class="panel-body">
<!-- name -->
<div class="form-group">
<label for="name" class="col-sm-4 control-label">Name</label>
<div class="col-sm-8">
<input type="text" name="name" class="form-control" id="name"
value="{{{Input::old('name') ?: $recurringTransaction->name}}}" placeholder="Name">
@if($errors->has('name'))
<p class="text-danger">{{$errors->first('name')}}</p>
@endif
</div>
{{Form::ffText('name',$recurringTransaction->name)}}
{{Form::ffTags('match',join(',',explode(' ',$recurringTransaction->match)))}}
{{Form::ffAmount('amount_min',$recurringTransaction->amount_min)}}
{{Form::ffAmount('amount_max',$recurringTransaction->amount_max)}}
{{Form::ffDate('date',$recurringTransaction->date->format('Y-m-d'))}}
{{Form::ffSelect('repeat_freq',$periods,$recurringTransaction->repeat_freq)}}
</div>
<div class="form-group">
<label for="match" class="col-sm-4 control-label">Matches on</label>
<div class="col-sm-8">
<input type="text" name="match" class="form-control" id="match"
value="{{Input::old('match') ?: join(',',explode(' ',$recurringTransaction->match))}}"
data-role="tagsinput">
@if($errors->has('match'))
<p class="text-danger">{{$errors->first('match')}}</p>
@endif
</div>
</div>
<div class="form-group">
{{ Form::label('amount_min', 'Minimum amount', ['class' => 'col-sm-4 control-label'])}}
<div class="col-sm-8">
<div class="input-group">
<span class="input-group-addon">&euro;</span>
{{Form::input('number','amount_min', Input::old('amount_min') ?: $recurringTransaction->amount_min,
['step' => 'any', 'class' => 'form-control'])}}
</div>
@if($errors->has('amount_min'))
<p class="text-danger">{{$errors->first('amount_min')}}</p>
@endif
</div>
</div>
<div class="form-group">
{{ Form::label('amount_max', 'Maximum amount', ['class' => 'col-sm-4 control-label'])}}
<div class="col-sm-8">
<div class="input-group">
<span class="input-group-addon">&euro;</span>
{{Form::input('number','amount_max', Input::old('amount_max') ?: $recurringTransaction->amount_max,
['step' => 'any', 'class' => 'form-control'])}}
</div>
@if($errors->has('amount_max'))
<p class="text-danger">{{$errors->first('amount_max')}}</p>
@endif
</div>
</div>
<div class="form-group">
{{ Form::label('date', 'Date', ['class' => 'col-sm-4 control-label'])}}
<div class="col-sm-8">
{{ Form::input('date','date', Input::old('date') ?: $recurringTransaction->date->format('Y-m-d'),
['class' => 'form-control']) }}
@if($errors->has('date'))
<p class="text-danger">{{$errors->first('date')}}</p>
@endif
</div>
</div>
<div class="form-group">
<label for="period" class="col-sm-4 control-label">Recurrence</label>
<div class="col-sm-8">
{{Form::select('repeat_freq',$periods,Input::old('repeat_freq') ?: $recurringTransaction->repeat_freq,
['class' => 'form-control'])}}
@if($errors->has('repeat_freq'))
<p class="text-danger">{{$errors->first('repeat_freq')}}</p>
@endif
</div>
</div>
</div>
</div>
<p>
@ -101,94 +32,21 @@
<i class="fa fa-smile-o"></i> Optional fields
</div>
<div class="panel-body">
<div class="form-group">
{{ Form::label('skip', 'Skip', ['class' => 'col-sm-4 control-label'])}}
<div class="col-sm-8">
{{Form::input('number','skip', Input::old('skip') ?: $recurringTransaction->skip,
['class' => 'form-control'])}}
{{Form::ffInteger('skip',$recurringTransaction->skip)}}
{{Form::ffCheckbox('automatch',1,$recurringTransaction->automatch)}}
{{Form::ffCheckbox('active',1,$recurringTransaction->active)}}
@if($errors->has('skip'))
<p class="text-danger">{{$errors->first('skip')}}</p>
@endif
</div>
</div>
<div class="form-group">
<label for="automatch" class="col-sm-4 control-label">Auto-match</label>
<div class="col-sm-8">
<div class="checkbox">
<label>
{{Form::checkbox('automatch',1,Input::old('automatch') == '1' ||
(is_null(Input::old('automatch')) && $recurringTransaction->automatch == 1))}}
Yes
</label>
</div>
<span class="help-block">Firefly will automatically match transactions.</span>
</div>
</div>
<div class="form-group">
<label for="active" class="col-sm-4 control-label">Active</label>
<div class="col-sm-8">
<div class="checkbox">
<label>
{{Form::checkbox('active',1,Input::old('active') == '1' ||
(is_null(Input::old('active')) && $recurringTransaction->active == 1))}}
Yes
</label>
</div>
<span class="help-block">This recurring transaction is actually active.</span>
</div>
</div>
</div>
</div>
<!-- panel for options -->
<div class="panel panel-default">
<div class="panel-heading">
<i class="fa fa-bolt"></i> Options
</div>
<div class="panel-body">
<div class="form-group">
<label for="default" class="col-sm-4 control-label">
Update
</label>
<div class="col-sm-8">
<div class="radio">
<label>
{{Form::radio('post_submit_action','store',true)}}
Update the recurring transaction
</label>
</div>
</div>
{{Form::ffOptionsList('update','recurring transaction')}}
</div>
<div class="form-group">
<label for="validate_only" class="col-sm-4 control-label">
Validate only
</label>
<div class="col-sm-8">
<div class="radio">
<label>
{{Form::radio('post_submit_action','validate_only')}}
Only validate, do not save changes
</label>
</div>
</div>
</div>
<div class="form-group">
<label for="return_to_form" class="col-sm-4 control-label">
Return here
</label>
<div class="col-sm-8">
<div class="radio">
<label>
{{Form::radio('post_submit_action','return_to_edit')}}
After update, return here again.
</label>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
{{Form::close()}}

View File

@ -79,49 +79,11 @@
<i class="fa fa-bolt"></i> Options
</div>
<div class="panel-body">
<div class="form-group">
<label for="default" class="col-sm-4 control-label">
Store
</label>
<div class="col-sm-8">
<div class="radio">
<label>
{{Form::radio('post_submit_action','store',true)}}
Store the {{{$what}}}
</label>
</div>
</div>
{{Form::ffOptionsList('create','transaction')}}
</div>
<div class="form-group">
<label for="validate_only" class="col-sm-4 control-label">
Validate only
</label>
<div class="col-sm-8">
<div class="radio">
<label>
{{Form::radio('post_submit_action','validate_only')}}
Only validate, do not save
</label>
</div>
</div>
</div>
<div class="form-group">
<label for="return_to_form" class="col-sm-4 control-label">
Return here
</label>
<div class="col-sm-8">
<div class="radio">
<label>
{{Form::radio('post_submit_action','create_another')}}
After storing, return here to create another one.
</label>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
{{Form::close()}}

View File

@ -78,43 +78,7 @@
<i class="fa fa-bolt"></i> Options
</div>
<div class="panel-body">
<div class="form-group">
<label for="default" class="col-sm-4 control-label">
Update
</label>
<div class="col-sm-8">
<div class="radio">
<label>
{{Form::radio('post_submit_action','store',true)}}
Update the {{{$what}}}
</label>
</div>
</div>
</div>
<div class="form-group">
<label for="validate_only" class="col-sm-4 control-label">
Validate only
</label>
<div class="col-sm-8">
<div class="radio">
<label>
{{Form::radio('post_submit_action','validate_only')}}
Only validate, do not save changes
</label>
</div>
</div>
</div>
<div class="form-group">
<label for="return_to_form" class="col-sm-4 control-label">
Return here
</label>
<div class="col-sm-8">
<div class="radio">
<label>
{{Form::radio('post_submit_action','return_to_edit')}}
After update, return here again.
</label>
</div>
{{Form::ffOptionsList('update','transaction')}}
</div>
</div>
</div>