Files
firefly-iii/app/controllers/CategoryController.php

190 lines
5.3 KiB
PHP
Raw Normal View History

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