firefly-iii/app/controllers/CategoryController.php

190 lines
5.3 KiB
PHP
Raw Normal View History

<?php
use Carbon\Carbon;
2014-12-13 15:54:52 -06:00
use FireflyIII\Database\Category\Category as CategoryRepository;
use FireflyIII\Exception\FireflyException;
2014-07-30 00:14:00 -05:00
/**
2014-12-12 09:22:16 -06:00
*
* @SuppressWarnings("CamelCase") // I'm fine with this.
2014-12-12 09:22:16 -06:00
*
2014-07-30 00:14:00 -05:00
* Class CategoryController
*/
class CategoryController extends BaseController
{
2014-12-12 00:42:38 -06:00
/** @var CategoryRepository */
protected $_repository;
/**
2014-12-12 00:42:38 -06:00
* @param CategoryRepository $repository
*/
2014-12-12 00:42:38 -06:00
public function __construct(CategoryRepository $repository)
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-12-12 00:42:38 -06:00
$this->_repository = $repository;
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');
}
/**
* @return \Illuminate\View\View
*/
public function noCategory()
{
$start = \Session::get('start', Carbon::now()->startOfMonth());
$end = \Session::get('end', Carbon::now()->startOfMonth());
$list = $this->_repository->journalsNoCategory($start, $end);
$subTitle = 'Transactions without a category in ' . $start->format('F Y');
return View::make('categories.noCategory', compact('list', 'subTitle'));
}
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)
{
2015-01-01 16:12:12 -06:00
return View::make('categories.delete')->with('category', $category)->with('subTitle', 'Delete category "' . e($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)
{
2014-12-12 00:42:38 -06:00
Session::flash('success', 'Category "' . e($category->name) . '" was deleted.');
$this->_repository->destroy($category);
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)
{
2015-01-01 16:12:12 -06:00
return View::make('categories.edit')->with('category', $category)->with('subTitle', 'Edit category "' . e($category->name) . '"');
}
2014-08-10 08:01:46 -05:00
/**
* @return $this
*/
public function index()
{
2014-12-12 00:42:38 -06:00
$categories = $this->_repository->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-12-12 00:42:38 -06:00
$hideCategory = true; // used in list.
$journals = $this->_repository->getTransactionJournals($category, 50);
2014-11-14 04:56:45 -06:00
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
/**
2015-01-17 03:05:43 -06:00
*
2014-12-06 10:53:25 -06:00
* @return $this
* @throws FireflyException
2014-08-10 08:01:46 -05:00
*/
public function store()
{
2014-12-20 09:53:32 -06:00
$data = Input::except('_token');
$data['user_id'] = Auth::user()->id;
2014-12-12 09:22:16 -06:00
// 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 category: ' . $messages['errors']->first());
2015-01-17 03:05:43 -06:00
return Redirect::route('categories.create')->withInput();
2014-12-12 09:22:16 -06:00
}
// return to create screen:
2015-01-17 03:05:43 -06:00
if ($data['post_submit_action'] == 'validate_only') {
2014-12-12 09:22:16 -06:00
return Redirect::route('categories.create')->withInput();
}
2015-01-01 22:52:38 -06:00
// store
2014-12-12 09:22:16 -06:00
$this->_repository->store($data);
Session::flash('success', 'Category "' . e($data['name']) . '" stored.');
if ($data['post_submit_action'] == 'store') {
return Redirect::route('categories.index');
}
2014-12-20 09:53:32 -06:00
return Redirect::route('categories.create')->withInput();
}
2014-08-10 08:01:46 -05:00
/**
2015-01-17 03:05:43 -06:00
*
2014-08-10 08:01:46 -05:00
* @param Category $category
*
2014-12-06 10:53:25 -06:00
* @return $this
* @throws FireflyException
2014-08-10 08:01:46 -05:00
*/
2014-08-02 08:54:39 -05:00
public function update(Category $category)
{
2014-12-20 09:53:32 -06:00
$data = Input::except('_token');
$data['user_id'] = Auth::user()->id;
2014-12-12 09:22:16 -06:00
// 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 category: ' . $messages['errors']->first());
2015-01-17 03:05:43 -06:00
return Redirect::route('categories.edit', $category->id)->withInput();
2014-12-12 09:22:16 -06:00
}
// return to update screen:
2015-01-17 03:05:43 -06:00
if ($data['post_submit_action'] == 'validate_only') {
2014-12-12 09:22:16 -06:00
return Redirect::route('categories.edit', $category->id)->withInput();
}
// update
$this->_repository->update($category, $data);
Session::flash('success', 'Category "' . e($data['name']) . '" updated.');
// go back to list
if ($data['post_submit_action'] == 'update') {
return Redirect::route('categories.index');
}
2014-08-02 08:54:39 -05:00
2014-12-20 09:53:32 -06:00
// go back to update screen.
return Redirect::route('categories.edit', $category->id)->withInput(['post_submit_action' => 'return_to_edit']);
2014-12-12 09:22:16 -06:00
2014-08-02 08:54:39 -05:00
}
2015-01-01 23:16:49 -06:00
}