firefly-iii/app/controllers/LimitController.php

152 lines
4.1 KiB
PHP
Raw Normal View History

<?php
use Firefly\Storage\Budget\BudgetRepositoryInterface as BRI;
use Firefly\Storage\Limit\LimitRepositoryInterface as LRI;
/**
* Class LimitController
*/
class LimitController extends BaseController
{
protected $_budgets;
protected $_limits;
/**
* @param BRI $budgets
* @param LRI $limits
*/
public function __construct(BRI $budgets, LRI $limits)
{
$this->_budgets = $budgets;
$this->_limits = $limits;
}
/**
2014-08-10 08:01:46 -05:00
* @param Budget $budget
*
2014-08-10 08:01:46 -05:00
* @return $this
*/
2014-07-30 15:31:35 -05:00
public function create(\Budget $budget = null)
{
$periods = \Config::get('firefly.periods_to_text');
$prefilled = [
2014-08-19 06:24:11 -05:00
'startdate' => \Input::get('startdate') ? : date('Y-m-d'),
'repeat_freq' => \Input::get('repeat_freq') ? : 'monthly',
'budget_id' => $budget ? $budget->id : null
];
$budgets = $this->_budgets->getAsSelectList();
2014-07-28 14:33:32 -05:00
2014-07-30 15:31:35 -05:00
return View::make('limits.create')->with('budgets', $budgets)->with(
'periods', $periods
)->with('prefilled', $prefilled);
}
2014-08-10 08:01:46 -05:00
/**
* @param Limit $limit
*
* @return $this
*/
2014-07-30 15:31:35 -05:00
public function delete(\Limit $limit)
{
return View::make('limits.delete')->with('limit', $limit);
}
2014-08-10 08:01:46 -05:00
/**
* @param Limit $limit
*
* @return \Illuminate\Http\RedirectResponse
*/
2014-08-09 03:01:37 -05:00
public function destroy(\Limit $limit)
2014-07-30 15:31:35 -05:00
{
2014-08-28 00:53:54 -05:00
Event::fire('limits.destroy', [$limit]); // before
2014-08-09 03:01:37 -05:00
$success = $this->_limits->destroy($limit);
2014-07-30 15:31:35 -05:00
2014-08-09 03:01:37 -05:00
if ($success) {
Session::flash('success', 'The envelope was deleted.');
2014-07-30 15:31:35 -05:00
} else {
2014-08-09 03:01:37 -05:00
Session::flash('error', 'Could not delete the envelope. Check the logs to be sure.');
}
if (Input::get('from') == 'date') {
return Redirect::route('budgets.index');
} else {
return Redirect::route('budgets.index.budget');
2014-07-30 15:31:35 -05:00
}
}
/**
2014-08-10 08:01:46 -05:00
* @param Limit $limit
*
2014-08-10 08:01:46 -05:00
* @return $this
*/
2014-07-30 15:31:35 -05:00
public function edit(Limit $limit)
{
$budgets = $this->_budgets->getAsSelectList();
2014-07-30 15:31:35 -05:00
$periods = \Config::get('firefly.periods_to_text');
2014-07-30 15:31:35 -05:00
return View::make('limits.edit')->with('limit', $limit)->with('budgets', $budgets)->with(
'periods', $periods
);
}
2014-08-10 08:01:46 -05:00
/**
* @param Budget $budget
*
* @return $this|\Illuminate\Http\RedirectResponse
*/
2014-07-30 15:31:35 -05:00
public function store(Budget $budget = null)
{
2014-07-28 14:33:32 -05:00
2014-07-30 15:31:35 -05:00
// find a limit with these properties, as we might already have one:
$limit = $this->_limits->store(Input::all());
2014-08-10 04:30:14 -05:00
if ($limit->validate()) {
Session::flash('success', 'Envelope created!');
2014-08-28 00:53:54 -05:00
Event::fire('limits.store', [$limit]);
2014-07-30 15:31:35 -05:00
if (Input::get('from') == 'date') {
return Redirect::route('budgets.index');
} else {
return Redirect::route('budgets.index.budget');
}
} else {
Session::flash('error', 'Could not save new envelope.');
2014-07-30 15:31:35 -05:00
$budgetId = $budget ? $budget->id : null;
2014-08-10 04:30:14 -05:00
$parameters = [$budgetId, 'from' => Input::get('from')];
2014-08-10 04:30:14 -05:00
return Redirect::route('budgets.limits.create', $parameters)->withInput()
->withErrors($limit->errors());
2014-07-30 15:31:35 -05:00
}
}
/**
2014-08-10 08:01:46 -05:00
* @param Limit $limit
*
2014-08-10 08:01:46 -05:00
* @return $this|\Illuminate\Http\RedirectResponse
*/
2014-08-09 03:01:37 -05:00
public function update(\Limit $limit)
{
2014-08-19 06:24:11 -05:00
2014-08-28 00:53:54 -05:00
$limit = $this->_limits->update($limit, Input::all());
2014-08-19 06:24:11 -05:00
if ($limit->validate()) {
2014-08-28 00:53:54 -05:00
Event::fire('limits.update', [$limit]);
2014-08-09 03:01:37 -05:00
Session::flash('success', 'Limit saved!');
if (Input::get('from') == 'date') {
return Redirect::route('budgets.index');
} else {
2014-08-09 03:01:37 -05:00
return Redirect::route('budgets.index.budget');
}
2014-08-10 04:30:14 -05:00
} else {
Session::flash('error', 'Could not save new limit: ' . $limit->errors()->first());
return Redirect::route('budgets.limits.edit', [$limit->id, 'from' => Input::get('from')])->withInput()
->withErrors($limit->errors());
}
2014-07-28 14:33:32 -05:00
}
2014-07-23 01:16:04 -05:00
}