firefly-iii/app/controllers/CategoryController.php

186 lines
6.0 KiB
PHP
Raw Normal View History

<?php
use FireflyIII\Exception\FireflyException;
use Illuminate\Support\MessageBag;
2014-07-30 00:14:00 -05:00
/**
* Class CategoryController
*/
class CategoryController extends BaseController
{
/**
*
*/
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
*/
public function create()
{
return View::make('categories.create')->with('subTitle', 'Create a new category');
}
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)
{
return View::make('categories.delete')->with('category', $category)->with('subTitle', 'Delete category "' . $category->name . '"');
}
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)
{
/** @var \FireflyIII\Database\Category $repos */
$repos = App::make('FireflyIII\Database\Category');
$repos->destroy($category);
Session::flash('success', 'The category was deleted.');
2014-11-12 15:36:02 -06:00
return Redirect::route('categories.index');
}
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-11-12 15:36:02 -06:00
return View::make('categories.edit')->with('category', $category)->with('subTitle', 'Edit category "' . $category->name . '"');
}
2014-08-10 08:01:46 -05:00
/**
* @return $this
*/
public function index()
{
/** @var \FireflyIII\Database\Category $repos */
2014-11-17 00:33:18 -06:00
$repos = App::make('FireflyIII\Database\Category');
$categories = $repos->get();
2014-11-17 00:33:18 -06:00
return View::make('categories.index', compact('categories'));
}
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-11-14 04:56:45 -06:00
$hideCategory = true;
/** @var \FireflyIII\Database\Category $repos */
$repos = App::make('FireflyIII\Database\Category');
$journals = $repos->getTransactionJournals($category, 50);
2014-11-17 00:33:18 -06:00
return View::make('categories.show', compact('category', 'journals', 'hideCategory'));
}
2014-08-10 08:01:46 -05:00
/**
* @return $this|\Illuminate\Http\RedirectResponse
*/
public function store()
{
2014-11-17 00:33:18 -06:00
$data = Input::all();
2014-11-15 00:46:01 -06:00
/** @var \FireflyIII\Database\Category $repos */
$repos = App::make('FireflyIII\Database\Category');
2014-11-15 00:46:01 -06:00
switch ($data['post_submit_action']) {
default:
throw new FireflyException('Cannot handle post_submit_action "' . e($data['post_submit_action']) . '"');
break;
case 'create_another':
case 'store':
$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());
2014-11-15 00:46:01 -06:00
return Redirect::route('categories.create')->withInput()->withErrors($messages['errors']);
}
// store!
$repos->store($data);
Session::flash('success', 'New category stored!');
if ($data['post_submit_action'] == 'create_another') {
return Redirect::route('categories.create')->withInput();
} else {
return Redirect::route('categories.index');
}
break;
case 'validate_only':
$messageBags = $repos->validate($data);
Session::flash('warnings', $messageBags['warnings']);
Session::flash('successes', $messageBags['successes']);
Session::flash('errors', $messageBags['errors']);
2014-11-15 00:46:01 -06:00
return Redirect::route('categories.create')->withInput();
break;
}
}
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)
{
/** @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());
2014-11-12 15:36:02 -06:00
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']);
2014-11-12 15:36:02 -06:00
return Redirect::route('categories.edit', $category->id)->withInput();
break;
2014-08-02 08:54:39 -05:00
}
}
}