Allow piggy bank edit/update.

This commit is contained in:
Sander Dorigo 2014-11-08 11:36:20 +01:00
parent 8196313ac0
commit fe05d218fc
3 changed files with 57 additions and 28 deletions

View File

@ -529,34 +529,49 @@ class PiggybankController extends BaseController
//
// }
// /**
// * @param Piggybank $piggyBank
// *
// * @return $this|\Illuminate\Http\RedirectResponse
// */
/**
* @param Piggybank $piggyBank
*
* @return $this|\Illuminate\Http\RedirectResponse
*/
public function update(Piggybank $piggyBank)
{
throw new NotImplementedException;
// $piggyBank = $this->_repository->update($piggyBank, Input::all());
// if ($piggyBank->validate()) {
// if ($piggyBank->repeats == 1) {
// $route = 'piggybanks.index.repeated';
// $message = 'Repeated expense';
// } else {
// $route = 'piggybanks.index.piggybanks';
// $message = 'Piggy bank';
// }
//
//
// Session::flash('success', $message . ' "' . $piggyBank->name . '" updated.');
// Event::fire('piggybanks.update', [$piggyBank]);
//
// return Redirect::route($route);
// } else {
// Session::flash('error', 'Could not update piggy bank: ' . $piggyBank->errors()->first());
//
// return Redirect::route('piggybanks.edit', $piggyBank->id)->withErrors($piggyBank->errors())->withInput();
// }
/** @var \FireflyIII\Database\Piggybank $repos */
$repos = App::make('FireflyIII\Database\Piggybank');
$data = Input::except('_token');
switch (Input::get('post_submit_action')) {
default:
throw new FireflyException('Cannot handle post_submit_action "' . e(Input::get('post_submit_action')) . '"');
break;
case 'create_another':
case 'update':
$messages = $repos->validate($data);
/** @var MessageBag $messages ['errors'] */
if ($messages['errors']->count() > 0) {
Session::flash('warnings', $messages['warnings']);
Session::flash('successes', $messages['successes']);
Session::flash('error', 'Could not save piggy bank: ' . $messages['errors']->first());
return Redirect::route('piggybanks.edit', $piggyBank->id)->withInput()->withErrors($messages['errors']);
}
// store!
$repos->update($piggyBank, $data);
Session::flash('success', 'Piggy bank updated!');
if ($data['post_submit_action'] == 'create_another') {
return Redirect::route('piggybanks.edit', $piggyBank->id);
} else {
return Redirect::route('piggybanks.index');
}
case 'validate_only':
$messageBags = $repos->validate($data);
Session::flash('warnings', $messageBags['warnings']);
Session::flash('successes', $messageBags['successes']);
Session::flash('errors', $messageBags['errors']);
return Redirect::route('piggybanks.edit', $piggyBank->id)->withInput();
break;
}
}

View File

@ -231,6 +231,20 @@ class Piggybank implements CUD, CommonDatabaseCalls, PiggybankInterface
*/
public function update(Ardent $model, array $data)
{
// TODO: Implement update() method.
/** @var \Piggybank $model */
$model->name = $data['name'];
$model->account_id = intval($data['account_id']);
$model->targetamount = floatval($data['targetamount']);
$model->targetdate = isset($data['targetdate']) && $data['targetdate'] != '' ? $data['targetdate'] : null;
$model->rep_every = isset($data['rep_every']) ? $data['rep_every'] : 0;
$model->reminder_skip = isset($data['reminder_skip']) ? $data['reminder_skip'] : 0;
$model->order = isset($data['order']) ? $data['order'] : 0;
$model->remind_me = isset($data['remind_me']) ? intval($data['remind_me']) : 0;
if(!$model->validate()) {
var_dump($model->errors());
exit();
}
$model->save();
return true;
}
}

View File

@ -40,7 +40,7 @@
<i class="fa fa-bolt"></i> Options
</div>
<div class="panel-body">
{{Form::ffOptionsList('create','piggy bank')}}
{{Form::ffOptionsList('update','piggy bank')}}
</div>
</div>