2014-07-29 13:30:50 -05:00
|
|
|
<?php
|
2014-11-10 12:03:03 -06:00
|
|
|
use Firefly\Exception\FireflyException;
|
|
|
|
use Illuminate\Support\MessageBag;
|
2014-07-30 00:14:00 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class CategoryController
|
|
|
|
*/
|
|
|
|
class CategoryController extends BaseController
|
2014-07-29 13:30:50 -05:00
|
|
|
{
|
2014-11-10 11:39:50 -06:00
|
|
|
/**
|
2014-11-10 12:03:03 -06:00
|
|
|
*
|
2014-11-10 11:39:50 -06:00
|
|
|
*/
|
2014-11-10 12:03:03 -06:00
|
|
|
public function __construct()
|
2014-07-30 00:14:00 -05:00
|
|
|
{
|
2014-11-10 11:38:58 -06:00
|
|
|
View::share('title', 'Categories');
|
2014-09-15 10:57:19 -05:00
|
|
|
View::share('mainTitleIcon', 'fa-bar-chart');
|
2014-07-30 00:14:00 -05:00
|
|
|
}
|
|
|
|
|
2014-08-10 08:01:46 -05:00
|
|
|
/**
|
|
|
|
* @return \Illuminate\View\View
|
|
|
|
*/
|
2014-07-29 13:30:50 -05:00
|
|
|
public function create()
|
|
|
|
{
|
2014-09-12 10:30:12 -05:00
|
|
|
return View::make('categories.create')->with('subTitle', 'Create a new category');
|
2014-07-29 13:30:50 -05:00
|
|
|
}
|
|
|
|
|
2014-08-10 08:01:46 -05:00
|
|
|
/**
|
|
|
|
* @param Category $category
|
|
|
|
*
|
|
|
|
* @return $this
|
|
|
|
*/
|
2014-07-30 00:14:00 -05:00
|
|
|
public function delete(Category $category)
|
2014-07-29 13:30:50 -05:00
|
|
|
{
|
2014-11-10 12:03:03 -06:00
|
|
|
return View::make('categories.delete')->with('category', $category)->with('subTitle', 'Delete category "' . $category->name . '"');
|
2014-07-29 13:30:50 -05:00
|
|
|
}
|
|
|
|
|
2014-08-10 08:01:46 -05:00
|
|
|
/**
|
|
|
|
* @param Category $category
|
|
|
|
*
|
|
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
|
|
*/
|
2014-08-02 08:54:39 -05:00
|
|
|
public function destroy(Category $category)
|
2014-07-29 13:30:50 -05:00
|
|
|
{
|
2014-11-10 12:03:03 -06:00
|
|
|
/** @var \FireflyIII\Database\Category $repos */
|
|
|
|
$repos = App::make('FireflyIII\Database\Category');
|
|
|
|
|
|
|
|
$repos->destroy($category);
|
2014-09-03 09:50:53 -05:00
|
|
|
Session::flash('success', 'The category was deleted.');
|
2014-07-30 07:45:46 -05:00
|
|
|
return Redirect::route('categories.index');
|
2014-07-29 13:30:50 -05:00
|
|
|
}
|
|
|
|
|
2014-08-10 08:01:46 -05:00
|
|
|
/**
|
|
|
|
* @param Category $category
|
|
|
|
*
|
|
|
|
* @return $this
|
|
|
|
*/
|
2014-07-30 00:14:00 -05:00
|
|
|
public function edit(Category $category)
|
2014-07-29 13:30:50 -05:00
|
|
|
{
|
2014-09-03 09:50:53 -05:00
|
|
|
return View::make('categories.edit')->with('category', $category)
|
2014-11-10 11:38:58 -06:00
|
|
|
->with('subTitle', 'Edit category "' . $category->name . '"');
|
2014-07-29 13:30:50 -05:00
|
|
|
}
|
|
|
|
|
2014-08-10 08:01:46 -05:00
|
|
|
/**
|
|
|
|
* @return $this
|
|
|
|
*/
|
2014-07-29 13:30:50 -05:00
|
|
|
public function index()
|
|
|
|
{
|
2014-11-10 12:03:03 -06:00
|
|
|
return View::make('categories.index');
|
2014-07-29 13:30:50 -05:00
|
|
|
}
|
|
|
|
|
2014-08-10 08:01:46 -05:00
|
|
|
/**
|
|
|
|
* @param Category $category
|
|
|
|
*
|
|
|
|
* @return $this
|
|
|
|
*/
|
2014-07-30 00:14:00 -05:00
|
|
|
public function show(Category $category)
|
2014-07-29 13:30:50 -05:00
|
|
|
{
|
2014-11-10 14:55:22 -06:00
|
|
|
return View::make('categories.show', compact('category'));
|
2014-07-29 13:30:50 -05:00
|
|
|
}
|
|
|
|
|
2014-08-10 08:01:46 -05:00
|
|
|
/**
|
|
|
|
* @return $this|\Illuminate\Http\RedirectResponse
|
|
|
|
*/
|
2014-07-29 13:30:50 -05:00
|
|
|
public function store()
|
|
|
|
{
|
2014-07-30 07:45:46 -05:00
|
|
|
$category = $this->_repository->store(Input::all());
|
2014-08-10 04:30:14 -05:00
|
|
|
if ($category->validate()) {
|
|
|
|
Session::flash('success', 'Category "' . $category->name . '" created!');
|
2014-07-30 07:45:46 -05:00
|
|
|
|
|
|
|
if (Input::get('create') == '1') {
|
|
|
|
return Redirect::route('categories.create');
|
|
|
|
}
|
2014-08-02 00:34:38 -05:00
|
|
|
|
2014-07-30 07:45:46 -05:00
|
|
|
return Redirect::route('categories.index');
|
|
|
|
} else {
|
|
|
|
Session::flash('error', 'Could not save the new category!');
|
2014-08-02 00:34:38 -05:00
|
|
|
|
2014-07-30 07:45:46 -05:00
|
|
|
return Redirect::route('categories.create')->withInput();
|
|
|
|
}
|
2014-07-29 13:30:50 -05:00
|
|
|
}
|
|
|
|
|
2014-08-10 08:01:46 -05:00
|
|
|
/**
|
|
|
|
* @param Category $category
|
|
|
|
*
|
|
|
|
* @return $this|\Illuminate\Http\RedirectResponse
|
|
|
|
*/
|
2014-08-02 08:54:39 -05:00
|
|
|
public function update(Category $category)
|
2014-07-29 13:30:50 -05:00
|
|
|
{
|
2014-11-10 12:03:03 -06:00
|
|
|
/** @var \FireflyIII\Database\Category $repos */
|
|
|
|
$repos = App::make('FireflyIII\Database\Category');
|
|
|
|
$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 'return_to_edit':
|
|
|
|
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 category: ' . $messages['errors']->first());
|
|
|
|
return Redirect::route('categories.edit', $category->id)->withInput()->withErrors($messages['errors']);
|
|
|
|
}
|
|
|
|
// store!
|
|
|
|
$repos->update($category, $data);
|
|
|
|
Session::flash('success', 'Category updated!');
|
|
|
|
|
|
|
|
if ($data['post_submit_action'] == 'return_to_edit') {
|
|
|
|
return Redirect::route('categories.edit', $category->id);
|
|
|
|
} else {
|
|
|
|
return Redirect::route('categories.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('categories.edit', $category->id)->withInput();
|
|
|
|
break;
|
2014-08-02 08:54:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-07-29 13:30:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|