2015-02-22 09:19:32 -06:00
|
|
|
<?php namespace FireflyIII\Http\Controllers;
|
|
|
|
|
|
|
|
use Auth;
|
2015-02-25 08:19:14 -06:00
|
|
|
use Carbon\Carbon;
|
2015-02-22 09:19:32 -06:00
|
|
|
use FireflyIII\Http\Requests\CategoryFormRequest;
|
|
|
|
use FireflyIII\Models\Category;
|
|
|
|
use FireflyIII\Repositories\Category\CategoryRepositoryInterface;
|
2015-02-24 15:53:38 -06:00
|
|
|
use Illuminate\Pagination\LengthAwarePaginator;
|
2015-03-04 02:42:47 -06:00
|
|
|
use Input;
|
2015-06-02 10:44:50 -05:00
|
|
|
use Preferences;
|
2015-02-22 09:19:32 -06:00
|
|
|
use Session;
|
2015-04-01 02:43:19 -05:00
|
|
|
use URL;
|
2015-04-05 12:43:20 -05:00
|
|
|
use View;
|
2015-02-22 09:19:32 -06:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class CategoryController
|
|
|
|
*
|
|
|
|
* @package FireflyIII\Http\Controllers
|
|
|
|
*/
|
|
|
|
class CategoryController extends Controller
|
|
|
|
{
|
|
|
|
|
2015-02-24 15:53:38 -06:00
|
|
|
/**
|
2015-05-23 13:49:57 -05:00
|
|
|
* @codeCoverageIgnore
|
2015-02-24 15:53:38 -06:00
|
|
|
*/
|
2015-02-22 09:19:32 -06:00
|
|
|
public function __construct()
|
|
|
|
{
|
2015-04-28 08:26:30 -05:00
|
|
|
parent::__construct();
|
2015-05-14 06:17:53 -05:00
|
|
|
View::share('title', trans('firefly.categories'));
|
2015-02-22 09:19:32 -06:00
|
|
|
View::share('mainTitleIcon', 'fa-bar-chart');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-05-26 13:57:31 -05:00
|
|
|
* @return \Illuminate\View\View
|
2015-02-22 09:19:32 -06:00
|
|
|
*/
|
|
|
|
public function create()
|
|
|
|
{
|
2015-04-01 02:43:19 -05:00
|
|
|
// put previous url in session if not redirect from store (not "create another").
|
|
|
|
if (Session::get('categories.create.fromStore') !== true) {
|
|
|
|
Session::put('categories.create.url', URL::previous());
|
|
|
|
}
|
|
|
|
Session::forget('categories.create.fromStore');
|
2015-05-25 01:12:31 -05:00
|
|
|
Session::flash('gaEventCategory', 'categories');
|
|
|
|
Session::flash('gaEventAction', 'create');
|
2015-06-09 10:56:08 -05:00
|
|
|
$subTitle = trans('firefly.create_new_category');
|
2015-04-01 02:43:19 -05:00
|
|
|
|
2015-05-05 03:23:01 -05:00
|
|
|
return view('categories.create', compact('subTitle'));
|
2015-02-22 09:19:32 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Category $category
|
|
|
|
*
|
|
|
|
* @return \Illuminate\View\View
|
|
|
|
*/
|
|
|
|
public function delete(Category $category)
|
|
|
|
{
|
2015-05-25 15:16:00 -05:00
|
|
|
$subTitle = trans('firefly.delete_category', ['name' => $category->name]);
|
2015-02-22 09:19:32 -06:00
|
|
|
|
2015-04-01 02:43:19 -05:00
|
|
|
// put previous url in session
|
|
|
|
Session::put('categories.delete.url', URL::previous());
|
2015-05-25 01:12:31 -05:00
|
|
|
Session::flash('gaEventCategory', 'categories');
|
|
|
|
Session::flash('gaEventAction', 'delete');
|
2015-04-01 02:43:19 -05:00
|
|
|
|
2015-02-22 09:19:32 -06:00
|
|
|
return view('categories.delete', compact('category', 'subTitle'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-05-03 05:54:39 -05:00
|
|
|
* @param CategoryRepositoryInterface $repository
|
|
|
|
* @param Category $category
|
2015-02-22 09:19:32 -06:00
|
|
|
*
|
|
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
|
|
*/
|
2015-05-03 05:54:39 -05:00
|
|
|
public function destroy(CategoryRepositoryInterface $repository, Category $category)
|
2015-02-22 09:19:32 -06:00
|
|
|
{
|
|
|
|
|
|
|
|
$name = $category->name;
|
|
|
|
$repository->destroy($category);
|
|
|
|
|
|
|
|
Session::flash('success', 'The category "' . e($name) . '" was deleted.');
|
2015-06-02 10:44:50 -05:00
|
|
|
Preferences::mark();
|
2015-02-22 09:19:32 -06:00
|
|
|
|
2015-07-06 09:27:21 -05:00
|
|
|
return redirect(Session::get('categories.delete.url'));
|
2015-02-22 09:19:32 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Category $category
|
|
|
|
*
|
2015-05-26 13:57:31 -05:00
|
|
|
* @return \Illuminate\View\View
|
2015-02-22 09:19:32 -06:00
|
|
|
*/
|
|
|
|
public function edit(Category $category)
|
|
|
|
{
|
2015-06-09 10:56:08 -05:00
|
|
|
$subTitle = trans('firefly.edit_category', ['name' => $category->name]);
|
2015-02-22 09:19:32 -06:00
|
|
|
|
2015-04-01 02:43:19 -05:00
|
|
|
// put previous url in session if not redirect from store (not "return_to_edit").
|
|
|
|
if (Session::get('categories.edit.fromUpdate') !== true) {
|
|
|
|
Session::put('categories.edit.url', URL::previous());
|
|
|
|
}
|
|
|
|
Session::forget('categories.edit.fromUpdate');
|
2015-05-25 01:12:31 -05:00
|
|
|
Session::flash('gaEventCategory', 'categories');
|
|
|
|
Session::flash('gaEventAction', 'edit');
|
2015-04-01 02:43:19 -05:00
|
|
|
|
2015-02-22 09:19:32 -06:00
|
|
|
return view('categories.edit', compact('category', 'subTitle'));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-05-03 05:58:55 -05:00
|
|
|
* @param CategoryRepositoryInterface $repository
|
2015-04-05 12:43:20 -05:00
|
|
|
*
|
2015-05-26 13:57:31 -05:00
|
|
|
* @return \Illuminate\View\View
|
2015-02-22 09:19:32 -06:00
|
|
|
*/
|
2015-04-05 12:43:20 -05:00
|
|
|
public function index(CategoryRepositoryInterface $repository)
|
2015-02-22 09:19:32 -06:00
|
|
|
{
|
2015-04-05 12:43:20 -05:00
|
|
|
$categories = $repository->getCategories();
|
2015-03-04 02:42:47 -06:00
|
|
|
|
|
|
|
$categories->each(
|
2015-06-06 16:09:12 -05:00
|
|
|
function (Category $category) use ($repository) {
|
2015-04-05 12:43:20 -05:00
|
|
|
$category->lastActivity = $repository->getLatestActivity($category);
|
2015-03-04 02:42:47 -06:00
|
|
|
}
|
|
|
|
);
|
2015-02-22 09:19:32 -06:00
|
|
|
|
|
|
|
return view('categories.index', compact('categories'));
|
|
|
|
}
|
|
|
|
|
2015-03-04 02:42:47 -06:00
|
|
|
/**
|
2015-05-03 05:58:55 -05:00
|
|
|
* @param CategoryRepositoryInterface $repository
|
|
|
|
*
|
2015-03-04 02:42:47 -06:00
|
|
|
* @return \Illuminate\View\View
|
|
|
|
*/
|
2015-04-05 12:43:20 -05:00
|
|
|
public function noCategory(CategoryRepositoryInterface $repository)
|
2015-03-04 02:42:47 -06:00
|
|
|
{
|
2015-04-05 12:43:20 -05:00
|
|
|
$start = Session::get('start', Carbon::now()->startOfMonth());
|
|
|
|
$end = Session::get('end', Carbon::now()->startOfMonth());
|
|
|
|
$list = $repository->getWithoutCategory($start, $end);
|
2015-06-09 10:56:08 -05:00
|
|
|
$subTitle = trans(
|
|
|
|
'firefly.without_category_between',
|
|
|
|
['start' => $start->formatLocalized($this->monthAndDayFormat), 'end' => $end->formatLocalized($this->monthAndDayFormat)]
|
|
|
|
);
|
2015-03-04 02:42:47 -06:00
|
|
|
|
|
|
|
return view('categories.noCategory', compact('list', 'subTitle'));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-05-03 05:54:39 -05:00
|
|
|
* @param CategoryRepositoryInterface $repository
|
|
|
|
* @param Category $category
|
2015-03-04 02:42:47 -06:00
|
|
|
*
|
2015-05-26 13:57:31 -05:00
|
|
|
* @return \Illuminate\View\View
|
2015-03-04 02:42:47 -06:00
|
|
|
*/
|
2015-05-03 05:54:39 -05:00
|
|
|
public function show(CategoryRepositoryInterface $repository, Category $category)
|
2015-03-04 02:42:47 -06:00
|
|
|
{
|
|
|
|
$hideCategory = true; // used in list.
|
|
|
|
$page = intval(Input::get('page'));
|
2015-04-05 12:43:20 -05:00
|
|
|
$set = $repository->getJournals($category, $page);
|
|
|
|
$count = $repository->countJournals($category);
|
2015-08-09 09:56:38 -05:00
|
|
|
$totalSum = $repository->journalsSum($category);
|
2015-08-09 10:01:12 -05:00
|
|
|
$periodSum = $repository->journalsSum($category, Session::get('start'), Session::get('end'));
|
2015-04-05 12:43:20 -05:00
|
|
|
$journals = new LengthAwarePaginator($set, $count, 50, $page);
|
2015-04-12 03:19:37 -05:00
|
|
|
$journals->setPath('categories/show/' . $category->id);
|
2015-03-04 02:42:47 -06:00
|
|
|
|
2015-08-09 10:01:12 -05:00
|
|
|
return view('categories.show', compact('category', 'journals', 'hideCategory', 'totalSum', 'periodSum'));
|
2015-03-04 02:42:47 -06:00
|
|
|
}
|
|
|
|
|
2015-02-22 09:19:32 -06:00
|
|
|
/**
|
|
|
|
* @param CategoryFormRequest $request
|
|
|
|
* @param CategoryRepositoryInterface $repository
|
|
|
|
*
|
2015-05-26 13:57:31 -05:00
|
|
|
* @return \Illuminate\Http\RedirectResponse
|
2015-02-22 09:19:32 -06:00
|
|
|
*/
|
|
|
|
public function store(CategoryFormRequest $request, CategoryRepositoryInterface $repository)
|
|
|
|
{
|
|
|
|
$categoryData = [
|
|
|
|
'name' => $request->input('name'),
|
|
|
|
'user' => Auth::user()->id,
|
|
|
|
];
|
2015-06-06 16:09:12 -05:00
|
|
|
$category = $repository->store($categoryData);
|
2015-02-22 09:19:32 -06:00
|
|
|
|
|
|
|
Session::flash('success', 'New category "' . $category->name . '" stored!');
|
2015-06-02 10:44:50 -05:00
|
|
|
Preferences::mark();
|
2015-03-29 14:27:51 -05:00
|
|
|
|
2015-03-25 10:14:28 -05:00
|
|
|
if (intval(Input::get('create_another')) === 1) {
|
2015-04-01 02:43:19 -05:00
|
|
|
Session::put('categories.create.fromStore', true);
|
2015-02-22 09:19:32 -06:00
|
|
|
|
2015-07-06 09:27:21 -05:00
|
|
|
return redirect(route('categories.create'))->withInput();
|
2015-03-25 10:13:39 -05:00
|
|
|
}
|
|
|
|
|
2015-07-06 09:27:21 -05:00
|
|
|
return redirect(route('categories.index'));
|
2015-02-22 09:19:32 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param CategoryFormRequest $request
|
|
|
|
* @param CategoryRepositoryInterface $repository
|
2015-05-03 05:54:39 -05:00
|
|
|
* @param Category $category
|
2015-02-22 09:19:32 -06:00
|
|
|
*
|
|
|
|
* @return \Illuminate\Http\RedirectResponse
|
|
|
|
*/
|
2015-05-03 05:54:39 -05:00
|
|
|
public function update(CategoryFormRequest $request, CategoryRepositoryInterface $repository, Category $category)
|
2015-02-22 09:19:32 -06:00
|
|
|
{
|
|
|
|
$categoryData = [
|
|
|
|
'name' => $request->input('name'),
|
|
|
|
];
|
|
|
|
|
|
|
|
$repository->update($category, $categoryData);
|
|
|
|
|
|
|
|
Session::flash('success', 'Category "' . $category->name . '" updated.');
|
2015-06-02 10:44:50 -05:00
|
|
|
Preferences::mark();
|
2015-02-22 09:19:32 -06:00
|
|
|
|
2015-03-26 11:45:03 -05:00
|
|
|
if (intval(Input::get('return_to_edit')) === 1) {
|
2015-04-01 02:43:19 -05:00
|
|
|
Session::put('categories.edit.fromUpdate', true);
|
2015-04-05 12:43:20 -05:00
|
|
|
|
2015-07-06 09:27:21 -05:00
|
|
|
return redirect(route('categories.edit', [$category->id]));
|
2015-03-26 11:45:03 -05:00
|
|
|
}
|
|
|
|
|
2015-04-01 02:43:19 -05:00
|
|
|
// redirect to previous URL.
|
2015-07-06 09:27:21 -05:00
|
|
|
return redirect(Session::get('categories.edit.url'));
|
2015-02-22 09:19:32 -06:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|