firefly-iii/app/controllers/RecurringController.php

182 lines
5.7 KiB
PHP
Raw Normal View History

<?php
2014-09-28 01:47:51 -05:00
use Firefly\Exception\FireflyException;
use Firefly\Storage\RecurringTransaction\RecurringTransactionRepositoryInterface as RTR;
2014-08-10 08:01:46 -05:00
/**
* Class RecurringController
*
* @SuppressWarnings(PHPMD.CamelCasePropertyName)
2014-08-10 08:01:46 -05:00
*/
class RecurringController extends BaseController
{
protected $_repository;
2014-08-10 08:01:46 -05:00
/**
* @param RTR $repository
*/
public function __construct(RTR $repository)
{
$this->_repository = $repository;
View::share('title', 'Recurring transactions');
View::share('mainTitleIcon', 'fa-rotate-right');
}
2014-08-10 08:01:46 -05:00
/**
* @return $this
*/
public function create()
{
View::share('subTitle', 'Create new');
$periods = \Config::get('firefly.periods_to_text');
return View::make('recurring.create')->with('periods', $periods);
}
2014-08-10 08:01:46 -05:00
/**
* @param RecurringTransaction $recurringTransaction
*
* @return $this
*/
public function delete(RecurringTransaction $recurringTransaction)
{
View::share('subTitle', 'Delete "' . $recurringTransaction->name . '"');
return View::make('recurring.delete')->with('recurringTransaction', $recurringTransaction);
}
2014-08-10 08:01:46 -05:00
/**
* @param RecurringTransaction $recurringTransaction
*
* @return \Illuminate\Http\RedirectResponse
*/
public function destroy(RecurringTransaction $recurringTransaction)
{
2014-09-28 01:47:51 -05:00
//Event::fire('recurring.destroy', [$recurringTransaction]);
$result = $this->_repository->destroy($recurringTransaction);
if ($result === true) {
Session::flash('success', 'The recurring transaction was deleted.');
} else {
Session::flash('error', 'Could not delete the recurring transaction. Check the logs to be sure.');
}
return Redirect::route('recurring.index');
}
2014-08-10 08:01:46 -05:00
/**
* @param RecurringTransaction $recurringTransaction
*
* @return $this
*/
public function edit(RecurringTransaction $recurringTransaction)
{
$periods = \Config::get('firefly.periods_to_text');
View::share('subTitle', 'Edit "' . $recurringTransaction->name . '"');
return View::make('recurring.edit')->with('periods', $periods)->with(
'recurringTransaction', $recurringTransaction
);
}
2014-08-10 08:01:46 -05:00
/**
* @return $this
*/
public function index()
{
2014-09-28 01:47:51 -05:00
return View::make('recurring.index');
}
2014-08-10 08:01:46 -05:00
/**
*
*/
public function show(RecurringTransaction $recurringTransaction)
{
View::share('subTitle', $recurringTransaction->name);
return View::make('recurring.show')->with('recurring', $recurringTransaction);
}
public function store()
{
2014-09-28 01:47:51 -05:00
switch (Input::get('post_submit_action')) {
default:
throw new FireflyException('Method ' . Input::get('post_submit_action') . ' not implemented yet.');
break;
case 'store':
case 'create_another':
/*
* Try to store:
*/
$messageBag = $this->_repository->store(Input::all());
/*
* Failure!
*/
if ($messageBag->count() > 0) {
Session::flash('error', 'Could not save recurring transaction: ' . $messageBag->first());
return Redirect::route('recurring.create')->withInput()->withErrors($messageBag);
}
/*
* Success!
*/
Session::flash('success', 'Recurring transaction "' . e(Input::get('name')) . '" saved!');
/*
* Redirect to original location or back to the form.
*/
if (Input::get('post_submit_action') == 'create_another') {
return Redirect::route('recurring.create')->withInput();
} else {
return Redirect::route('recurring.index');
}
break;
2014-09-28 01:47:51 -05:00
}
//
//
// 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());
// }
}
2014-08-10 08:01:46 -05:00
/**
* @param RecurringTransaction $recurringTransaction
*
* @return $this|\Illuminate\Http\RedirectResponse
2014-08-10 08:01:46 -05:00
*/
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.');
2014-09-28 01:47:51 -05:00
//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()
);
}
}
}