2014-07-29 20:30:50 +02:00
|
|
|
<?php
|
2014-07-31 22:01:52 +02:00
|
|
|
|
2014-12-22 07:10:18 +01:00
|
|
|
use Carbon\Carbon;
|
2014-12-21 18:40:37 +01:00
|
|
|
use FireflyIII\Database\PiggyBank\PiggyBank as Repository;
|
2014-11-12 07:31:48 +01:00
|
|
|
use FireflyIII\Exception\FireflyException;
|
2014-10-31 07:32:43 +01:00
|
|
|
use Illuminate\Support\Collection;
|
2014-07-31 22:01:52 +02:00
|
|
|
|
|
|
|
/**
|
2014-12-21 18:40:37 +01:00
|
|
|
*
|
|
|
|
* @SuppressWarnings("CamelCase") // I'm fine with this.
|
|
|
|
* @SuppressWarnings("CyclomaticComplexity") // It's all 5. So ok.
|
|
|
|
* @SuppressWarnings("TooManyMethods") // I'm also fine with this.
|
|
|
|
* @SuppressWarnings("CouplingBetweenObjects") // There's only so much I can remove.
|
|
|
|
*
|
|
|
|
*
|
2014-12-24 20:55:42 +01:00
|
|
|
* Class PiggyBankController
|
2014-08-16 12:13:50 +02:00
|
|
|
*
|
2014-07-31 22:01:52 +02:00
|
|
|
*/
|
2014-12-24 20:55:42 +01:00
|
|
|
class PiggyBankController extends BaseController
|
2014-07-31 22:01:52 +02:00
|
|
|
{
|
|
|
|
|
2014-12-21 18:40:37 +01:00
|
|
|
/** @var Repository */
|
|
|
|
protected $_repository;
|
|
|
|
|
2014-08-17 08:45:22 +02:00
|
|
|
/**
|
2014-12-21 18:40:37 +01:00
|
|
|
* @param Repository $repository
|
2014-08-17 08:45:22 +02:00
|
|
|
*/
|
2014-12-21 18:40:37 +01:00
|
|
|
public function __construct(Repository $repository)
|
2014-08-17 08:45:22 +02:00
|
|
|
{
|
2014-12-21 18:40:37 +01:00
|
|
|
$this->_repository = $repository;
|
2014-12-22 07:10:18 +01:00
|
|
|
View::share('title', 'Piggy banks');
|
|
|
|
View::share('mainTitleIcon', 'fa-sort-amount-asc');
|
2014-08-17 08:45:22 +02:00
|
|
|
}
|
|
|
|
|
2014-11-12 22:36:02 +01:00
|
|
|
/**
|
2014-11-15 09:32:25 +01:00
|
|
|
* Add money to piggy bank
|
2014-11-15 11:36:27 +01:00
|
|
|
*
|
2014-12-24 20:55:42 +01:00
|
|
|
* @param PiggyBank $piggyBank
|
2014-11-12 22:36:02 +01:00
|
|
|
*
|
|
|
|
* @return $this
|
|
|
|
*/
|
2014-12-24 20:55:42 +01:00
|
|
|
public function add(PiggyBank $piggyBank)
|
2014-11-12 22:36:02 +01:00
|
|
|
{
|
2014-12-24 19:13:15 +01:00
|
|
|
$leftOnAccount = $this->_repository->leftOnAccount($piggyBank->account);
|
2014-12-25 08:07:17 +01:00
|
|
|
$savedSoFar = $piggyBank->currentRelevantRep()->currentamount;
|
|
|
|
$leftToSave = $piggyBank->targetamount - $savedSoFar;
|
|
|
|
$maxAmount = min($leftOnAccount, $leftToSave);
|
2014-11-12 22:36:02 +01:00
|
|
|
|
|
|
|
|
2014-12-24 19:13:15 +01:00
|
|
|
\Log::debug('Now going to view for piggy bank #' . $piggyBank->id . ' (' . $piggyBank->name . ')');
|
2014-12-23 21:13:42 +01:00
|
|
|
|
2014-12-24 20:55:42 +01:00
|
|
|
return View::make('piggy_banks.add', compact('piggyBank', 'maxAmount'));
|
2014-11-12 22:36:02 +01:00
|
|
|
}
|
|
|
|
|
2014-08-16 07:07:42 +02:00
|
|
|
/**
|
2014-11-13 16:13:32 +01:00
|
|
|
* @return mixed
|
2014-08-16 07:07:42 +02:00
|
|
|
*/
|
2014-10-30 19:26:52 +01:00
|
|
|
public function create()
|
2014-07-29 20:30:50 +02:00
|
|
|
{
|
2014-11-02 14:58:12 +01:00
|
|
|
|
2014-12-13 22:54:52 +01:00
|
|
|
/** @var \FireflyIII\Database\Account\Account $acct */
|
|
|
|
$acct = App::make('FireflyIII\Database\Account\Account');
|
2014-11-02 14:58:12 +01:00
|
|
|
|
2014-12-24 20:55:42 +01:00
|
|
|
$periods = Config::get('firefly.piggy_bank_periods');
|
2014-12-22 07:10:18 +01:00
|
|
|
$accounts = FFForm::makeSelectList($acct->getAssetAccounts());
|
|
|
|
$subTitle = 'Create new piggy bank';
|
|
|
|
$subTitleIcon = 'fa-plus';
|
2014-11-02 14:58:12 +01:00
|
|
|
|
2014-12-24 20:55:42 +01:00
|
|
|
return View::make('piggy_banks.create', compact('accounts', 'periods', 'subTitle', 'subTitleIcon'));
|
2014-07-29 20:30:50 +02:00
|
|
|
}
|
|
|
|
|
2014-08-10 15:01:46 +02:00
|
|
|
/**
|
2014-12-24 20:55:42 +01:00
|
|
|
* @param PiggyBank $piggyBank
|
2014-08-10 15:01:46 +02:00
|
|
|
*
|
|
|
|
* @return $this
|
|
|
|
*/
|
2014-12-24 20:55:42 +01:00
|
|
|
public function delete(PiggyBank $piggyBank)
|
2014-07-29 20:30:50 +02:00
|
|
|
{
|
2014-12-24 19:13:15 +01:00
|
|
|
$subTitle = 'Delete "' . e($piggyBank->name) . '"';
|
2014-12-22 07:10:18 +01:00
|
|
|
|
2014-12-24 20:55:42 +01:00
|
|
|
return View::make('piggy_banks.delete', compact('piggyBank', 'subTitle'));
|
2014-07-29 20:30:50 +02:00
|
|
|
}
|
|
|
|
|
2014-08-10 15:01:46 +02:00
|
|
|
/**
|
2014-12-24 20:55:42 +01:00
|
|
|
* @param PiggyBank $piggyBank
|
2014-08-10 15:01:46 +02:00
|
|
|
*
|
|
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
|
|
*/
|
2014-12-24 20:55:42 +01:00
|
|
|
public function destroy(PiggyBank $piggyBank)
|
2014-07-29 20:30:50 +02:00
|
|
|
{
|
2014-12-22 07:10:18 +01:00
|
|
|
|
|
|
|
Session::flash('success', 'Piggy bank "' . e($piggyBank->name) . '" deleted.');
|
|
|
|
$this->_repository->destroy($piggyBank);
|
2014-11-02 16:47:01 +01:00
|
|
|
|
2014-12-24 20:55:42 +01:00
|
|
|
return Redirect::route('piggy_banks.index');
|
2014-07-29 20:30:50 +02:00
|
|
|
}
|
|
|
|
|
2014-08-10 15:01:46 +02:00
|
|
|
/**
|
2014-12-24 20:55:42 +01:00
|
|
|
* @param PiggyBank $piggyBank
|
2014-08-10 15:01:46 +02:00
|
|
|
*
|
|
|
|
* @return $this
|
|
|
|
*/
|
2014-12-24 20:55:42 +01:00
|
|
|
public function edit(PiggyBank $piggyBank)
|
2014-07-29 20:30:50 +02:00
|
|
|
{
|
2014-11-02 14:58:12 +01:00
|
|
|
|
2014-12-13 22:54:52 +01:00
|
|
|
/** @var \FireflyIII\Database\Account\Account $acct */
|
|
|
|
$acct = App::make('FireflyIII\Database\Account\Account');
|
2014-11-02 14:58:12 +01:00
|
|
|
|
2014-12-24 20:55:42 +01:00
|
|
|
$periods = Config::get('firefly.piggy_bank_periods');
|
2014-12-22 07:10:18 +01:00
|
|
|
$accounts = FFForm::makeSelectList($acct->getAssetAccounts());
|
2014-12-24 19:13:15 +01:00
|
|
|
$subTitle = 'Edit piggy bank "' . e($piggyBank->name) . '"';
|
2014-12-22 07:10:18 +01:00
|
|
|
$subTitleIcon = 'fa-pencil';
|
2014-11-02 14:58:12 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Flash some data to fill the form.
|
|
|
|
*/
|
2014-12-24 19:13:15 +01:00
|
|
|
if (is_null($piggyBank->targetdate) || $piggyBank->targetdate == '') {
|
2014-12-22 07:10:18 +01:00
|
|
|
$targetDate = null;
|
|
|
|
} else {
|
2014-12-24 19:13:15 +01:00
|
|
|
$targetDate = new Carbon($piggyBank->targetdate);
|
2014-12-22 07:10:18 +01:00
|
|
|
$targetDate = $targetDate->format('Y-m-d');
|
|
|
|
}
|
2014-12-24 19:13:15 +01:00
|
|
|
$preFilled = ['name' => $piggyBank->name,
|
|
|
|
'account_id' => $piggyBank->account_id,
|
|
|
|
'targetamount' => $piggyBank->targetamount,
|
2014-12-22 07:10:18 +01:00
|
|
|
'targetdate' => $targetDate,
|
2014-12-24 19:13:15 +01:00
|
|
|
'reminder' => $piggyBank->reminder,
|
|
|
|
'remind_me' => intval($piggyBank->remind_me) == 1 || !is_null($piggyBank->reminder) ? true : false
|
2014-11-17 22:32:55 +01:00
|
|
|
];
|
2014-12-07 15:37:53 +01:00
|
|
|
Session::flash('preFilled', $preFilled);
|
2014-11-02 14:58:12 +01:00
|
|
|
|
2014-12-24 20:55:42 +01:00
|
|
|
return View::make('piggy_banks.edit', compact('subTitle', 'subTitleIcon', 'piggyBank', 'accounts', 'periods', 'preFilled'));
|
2014-09-17 08:55:51 +02:00
|
|
|
}
|
|
|
|
|
2014-12-06 17:53:25 +01:00
|
|
|
/**
|
|
|
|
* @return $this
|
|
|
|
*/
|
2014-11-12 22:36:02 +01:00
|
|
|
public function index()
|
2014-10-31 07:32:43 +01:00
|
|
|
{
|
2014-12-24 19:13:15 +01:00
|
|
|
/** @var Collection $piggyBanks */
|
|
|
|
$piggyBanks = $this->_repository->get();
|
2014-10-31 07:32:43 +01:00
|
|
|
|
2014-11-12 22:36:02 +01:00
|
|
|
$accounts = [];
|
2014-12-24 20:55:42 +01:00
|
|
|
/** @var PiggyBank $piggyBank */
|
2014-12-24 19:13:15 +01:00
|
|
|
foreach ($piggyBanks as $piggyBank) {
|
|
|
|
$piggyBank->savedSoFar = floatval($piggyBank->currentRelevantRep()->currentamount);
|
|
|
|
$piggyBank->percentage = intval($piggyBank->savedSoFar / $piggyBank->targetamount * 100);
|
|
|
|
$piggyBank->leftToSave = $piggyBank->targetamount - $piggyBank->savedSoFar;
|
2014-10-31 07:32:43 +01:00
|
|
|
|
2014-11-12 22:36:02 +01:00
|
|
|
/*
|
|
|
|
* Fill account information:
|
|
|
|
*/
|
2014-12-24 19:13:15 +01:00
|
|
|
$account = $piggyBank->account;
|
2014-11-12 22:36:02 +01:00
|
|
|
if (!isset($accounts[$account->id])) {
|
2014-12-23 21:13:42 +01:00
|
|
|
$accounts[$account->id] = [
|
|
|
|
'name' => $account->name,
|
|
|
|
'balance' => Steam::balance($account),
|
2014-12-24 21:20:47 +01:00
|
|
|
'leftForPiggyBanks' => $this->_repository->leftOnAccount($account),
|
2014-12-24 19:13:15 +01:00
|
|
|
'sumOfSaved' => $piggyBank->savedSoFar,
|
|
|
|
'sumOfTargets' => floatval($piggyBank->targetamount),
|
|
|
|
'leftToSave' => $piggyBank->leftToSave
|
2014-12-23 21:13:42 +01:00
|
|
|
];
|
2014-11-12 22:36:02 +01:00
|
|
|
} else {
|
2014-12-24 19:13:15 +01:00
|
|
|
$accounts[$account->id]['sumOfSaved'] += $piggyBank->savedSoFar;
|
|
|
|
$accounts[$account->id]['sumOfTargets'] += floatval($piggyBank->targetamount);
|
|
|
|
$accounts[$account->id]['leftToSave'] += $piggyBank->leftToSave;
|
2014-11-12 22:36:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-24 20:55:42 +01:00
|
|
|
return View::make('piggy_banks.index', compact('piggyBanks', 'accounts'));
|
2014-10-31 07:32:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-11-15 09:32:25 +01:00
|
|
|
* POST add money to piggy bank
|
2014-11-15 11:36:27 +01:00
|
|
|
*
|
2014-12-24 20:55:42 +01:00
|
|
|
* @param PiggyBank $piggyBank
|
2014-10-31 07:32:43 +01:00
|
|
|
*
|
|
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
|
|
*/
|
2014-12-24 20:55:42 +01:00
|
|
|
public function postAdd(PiggyBank $piggyBank)
|
2014-10-31 07:32:43 +01:00
|
|
|
{
|
2014-11-08 11:38:50 +01:00
|
|
|
$amount = round(floatval(Input::get('amount')), 2);
|
2014-10-31 07:32:43 +01:00
|
|
|
|
2014-12-13 22:54:52 +01:00
|
|
|
/** @var \FireflyIII\Database\PiggyBank\PiggyBank $acct */
|
2014-12-25 08:07:17 +01:00
|
|
|
$piggyRepository = App::make('FireflyIII\Database\PiggyBank\PiggyBank');
|
2014-10-31 07:32:43 +01:00
|
|
|
|
2014-12-25 08:07:17 +01:00
|
|
|
$leftOnAccount = $piggyRepository->leftOnAccount($piggyBank->account);
|
2014-12-24 19:13:15 +01:00
|
|
|
$savedSoFar = $piggyBank->currentRelevantRep()->currentamount;
|
|
|
|
$leftToSave = $piggyBank->targetamount - $savedSoFar;
|
2014-11-08 11:38:50 +01:00
|
|
|
$maxAmount = round(min($leftOnAccount, $leftToSave), 2);
|
2014-10-31 07:32:43 +01:00
|
|
|
|
|
|
|
if ($amount <= $maxAmount) {
|
2014-12-24 19:13:15 +01:00
|
|
|
$repetition = $piggyBank->currentRelevantRep();
|
2014-10-31 07:32:43 +01:00
|
|
|
$repetition->currentamount += $amount;
|
|
|
|
$repetition->save();
|
2014-11-15 09:32:25 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Create event!
|
|
|
|
*/
|
2014-12-24 20:55:42 +01:00
|
|
|
Event::fire('piggy_bank.addMoney', [$piggyBank, $amount]); // new and used.
|
2014-11-15 09:32:25 +01:00
|
|
|
|
2014-12-24 19:13:15 +01:00
|
|
|
Session::flash('success', 'Added ' . mf($amount, false) . ' to "' . e($piggyBank->name) . '".');
|
2014-10-31 07:32:43 +01:00
|
|
|
} else {
|
2014-12-24 19:13:15 +01:00
|
|
|
Session::flash('error', 'Could not add ' . mf($amount, false) . ' to "' . e($piggyBank->name) . '".');
|
2014-10-31 07:32:43 +01:00
|
|
|
}
|
|
|
|
|
2014-12-24 20:55:42 +01:00
|
|
|
return Redirect::route('piggy_banks.index');
|
2014-10-31 07:32:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2014-12-24 20:55:42 +01:00
|
|
|
* @param PiggyBank $piggyBank
|
2014-10-31 07:32:43 +01:00
|
|
|
*
|
|
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
|
|
*/
|
2014-12-24 20:55:42 +01:00
|
|
|
public function postRemove(PiggyBank $piggyBank)
|
2014-10-31 07:32:43 +01:00
|
|
|
{
|
|
|
|
$amount = floatval(Input::get('amount'));
|
|
|
|
|
2014-12-24 19:13:15 +01:00
|
|
|
$savedSoFar = $piggyBank->currentRelevantRep()->currentamount;
|
2014-10-31 07:32:43 +01:00
|
|
|
|
|
|
|
if ($amount <= $savedSoFar) {
|
2014-12-24 19:13:15 +01:00
|
|
|
$repetition = $piggyBank->currentRelevantRep();
|
2014-10-31 07:32:43 +01:00
|
|
|
$repetition->currentamount -= $amount;
|
|
|
|
$repetition->save();
|
2014-11-15 09:32:25 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Create event!
|
|
|
|
*/
|
2014-12-24 20:55:42 +01:00
|
|
|
Event::fire('piggy_bank.removeMoney', [$piggyBank, $amount]); // new and used.
|
2014-11-15 09:32:25 +01:00
|
|
|
|
2014-12-24 19:13:15 +01:00
|
|
|
Session::flash('success', 'Removed ' . mf($amount, false) . ' from "' . e($piggyBank->name) . '".');
|
2014-10-31 07:32:43 +01:00
|
|
|
} else {
|
2014-12-24 19:13:15 +01:00
|
|
|
Session::flash('error', 'Could not remove ' . mf($amount, false) . ' from "' . e($piggyBank->name) . '".');
|
2014-10-31 07:32:43 +01:00
|
|
|
}
|
2014-11-12 22:36:02 +01:00
|
|
|
|
2014-12-24 20:55:42 +01:00
|
|
|
return Redirect::route('piggy_banks.index');
|
2014-10-31 07:32:43 +01:00
|
|
|
}
|
|
|
|
|
2014-11-12 22:36:02 +01:00
|
|
|
/**
|
2014-12-24 20:55:42 +01:00
|
|
|
* @param PiggyBank $piggyBank
|
2014-11-12 22:36:02 +01:00
|
|
|
*
|
|
|
|
* @return \Illuminate\View\View
|
|
|
|
*/
|
2014-12-24 20:55:42 +01:00
|
|
|
public function remove(PiggyBank $piggyBank)
|
2014-07-29 20:30:50 +02:00
|
|
|
{
|
2014-12-25 08:07:17 +01:00
|
|
|
return View::make('piggy_banks.remove', compact('piggyBank'));
|
2014-07-29 20:30:50 +02:00
|
|
|
}
|
|
|
|
|
2014-12-06 17:53:25 +01:00
|
|
|
/**
|
2014-12-24 20:55:42 +01:00
|
|
|
* @param PiggyBank $piggyBank
|
2014-12-06 17:53:25 +01:00
|
|
|
*
|
|
|
|
* @return $this
|
|
|
|
*/
|
2014-12-24 20:55:42 +01:00
|
|
|
public function show(PiggyBank $piggyBank)
|
2014-07-29 20:30:50 +02:00
|
|
|
{
|
2014-11-13 07:50:55 +01:00
|
|
|
|
2014-12-24 20:55:42 +01:00
|
|
|
$events = $piggyBank->piggyBankEvents()->orderBy('date', 'DESC')->orderBy('id', 'DESC')->get();
|
2014-11-15 11:36:27 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Number of reminders:
|
|
|
|
*/
|
2014-11-17 20:18:14 +01:00
|
|
|
|
2014-12-24 19:13:15 +01:00
|
|
|
$amountPerReminder = $piggyBank->amountPerReminder();
|
|
|
|
$remindersCount = $piggyBank->countFutureReminders();
|
|
|
|
$subTitle = e($piggyBank->name);
|
2014-11-15 11:36:27 +01:00
|
|
|
|
2014-12-24 20:55:42 +01:00
|
|
|
return View::make('piggy_banks.show', compact('amountPerReminder', 'remindersCount', 'piggyBank', 'events', 'subTitle'));
|
2014-11-08 19:11:51 +01:00
|
|
|
|
2014-08-13 20:36:32 +02:00
|
|
|
}
|
|
|
|
|
2014-11-02 14:58:12 +01:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
2014-10-30 19:26:52 +01:00
|
|
|
public function store()
|
2014-07-29 20:30:50 +02:00
|
|
|
{
|
2014-11-02 14:58:12 +01:00
|
|
|
$data = Input::all();
|
|
|
|
$data['repeats'] = 0;
|
2014-12-21 18:40:37 +01:00
|
|
|
$data['user_id'] = Auth::user()->id;
|
|
|
|
|
|
|
|
|
|
|
|
// always validate:
|
|
|
|
$messages = $this->_repository->validate($data);
|
|
|
|
|
|
|
|
// flash messages:
|
|
|
|
Session::flash('warnings', $messages['warnings']);
|
|
|
|
Session::flash('successes', $messages['successes']);
|
|
|
|
Session::flash('errors', $messages['errors']);
|
|
|
|
if ($messages['errors']->count() > 0) {
|
|
|
|
Session::flash('error', 'Could not store piggy bank: ' . $messages['errors']->first());
|
|
|
|
}
|
2014-11-02 14:58:12 +01:00
|
|
|
|
2014-12-21 18:40:37 +01:00
|
|
|
|
|
|
|
// return to create screen:
|
|
|
|
if ($data['post_submit_action'] == 'validate_only' || $messages['errors']->count() > 0) {
|
2014-12-24 20:55:42 +01:00
|
|
|
return Redirect::route('piggy_banks.create')->withInput();
|
2014-12-21 18:40:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// store:
|
|
|
|
$piggyBank = $this->_repository->store($data);
|
2014-12-24 20:55:42 +01:00
|
|
|
Event::fire('piggy_bank.store', [$piggyBank]); // new and used.
|
2014-12-21 18:40:37 +01:00
|
|
|
Session::flash('success', 'Piggy bank "' . e($data['name']) . '" stored.');
|
|
|
|
if ($data['post_submit_action'] == 'store') {
|
2014-12-24 20:55:42 +01:00
|
|
|
return Redirect::route('piggy_banks.index');
|
2014-11-02 14:58:12 +01:00
|
|
|
}
|
2014-12-21 18:40:37 +01:00
|
|
|
|
2014-12-24 20:55:42 +01:00
|
|
|
return Redirect::route('piggy_banks.create')->withInput();
|
2014-07-29 20:30:50 +02:00
|
|
|
}
|
|
|
|
|
2014-11-08 11:36:20 +01:00
|
|
|
/**
|
2014-12-24 20:55:42 +01:00
|
|
|
* @param PiggyBank $piggyBank
|
2014-11-08 11:36:20 +01:00
|
|
|
*
|
2014-12-06 17:53:25 +01:00
|
|
|
* @return $this
|
|
|
|
* @throws FireflyException
|
2014-11-08 11:36:20 +01:00
|
|
|
*/
|
2014-12-24 20:55:42 +01:00
|
|
|
public function update(PiggyBank $piggyBank)
|
2014-07-29 20:30:50 +02:00
|
|
|
{
|
2014-11-08 11:36:20 +01:00
|
|
|
|
2014-12-14 20:40:02 +01:00
|
|
|
$data = Input::except('_token');
|
|
|
|
$data['rep_every'] = 0;
|
|
|
|
$data['reminder_skip'] = 0;
|
|
|
|
$data['order'] = 0;
|
2014-12-14 21:24:20 +01:00
|
|
|
$data['remind_me'] = isset($data['remind_me']) ? 1 : 0;
|
2014-12-21 18:40:37 +01:00
|
|
|
$data['user_id'] = Auth::user()->id;
|
|
|
|
|
|
|
|
// always validate:
|
|
|
|
$messages = $this->_repository->validate($data);
|
|
|
|
|
|
|
|
// flash messages:
|
|
|
|
Session::flash('warnings', $messages['warnings']);
|
|
|
|
Session::flash('successes', $messages['successes']);
|
|
|
|
Session::flash('errors', $messages['errors']);
|
|
|
|
if ($messages['errors']->count() > 0) {
|
|
|
|
Session::flash('error', 'Could not update piggy bank: ' . $messages['errors']->first());
|
|
|
|
}
|
|
|
|
|
|
|
|
// return to update screen:
|
|
|
|
if ($data['post_submit_action'] == 'validate_only' || $messages['errors']->count() > 0) {
|
2014-12-24 20:55:42 +01:00
|
|
|
return Redirect::route('piggy_banks.edit', $piggyBank->id)->withInput();
|
2014-12-21 18:40:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// update
|
|
|
|
$this->_repository->update($piggyBank, $data);
|
|
|
|
Session::flash('success', 'Piggy bank "' . e($data['name']) . '" updated.');
|
2014-11-08 11:36:20 +01:00
|
|
|
|
2014-12-21 18:40:37 +01:00
|
|
|
// go back to list
|
|
|
|
if ($data['post_submit_action'] == 'update') {
|
2014-12-24 20:55:42 +01:00
|
|
|
return Redirect::route('piggy_banks.index');
|
2014-11-08 11:36:20 +01:00
|
|
|
}
|
2014-08-10 11:30:14 +02:00
|
|
|
|
2014-12-21 18:40:37 +01:00
|
|
|
// go back to update screen.
|
2014-12-24 20:55:42 +01:00
|
|
|
return Redirect::route('piggy_banks.edit', $piggyBank->id)->withInput(['post_submit_action' => 'return_to_edit']);
|
2014-08-02 07:34:38 +02:00
|
|
|
|
|
|
|
}
|
2014-11-08 19:11:51 +01:00
|
|
|
}
|