diff --git a/app/controllers/AccountController.php b/app/controllers/AccountController.php index da88631243..08ddca55e8 100644 --- a/app/controllers/AccountController.php +++ b/app/controllers/AccountController.php @@ -312,7 +312,7 @@ class AccountController extends BaseController default: throw new FireflyException('Cannot handle post_submit_action "' . e($data['post_submit_action']) . '"'); break; - case 'create_another': + case 'return_to_edit': case 'store': $messages = $acct->validate($data); /** @var MessageBag $messages ['errors'] */ diff --git a/app/controllers/BudgetController.php b/app/controllers/BudgetController.php index 54e095461d..6156d76c2d 100644 --- a/app/controllers/BudgetController.php +++ b/app/controllers/BudgetController.php @@ -263,7 +263,7 @@ class BudgetController extends BaseController default: throw new FireflyException('Cannot handle post_submit_action "' . e(Input::get('post_submit_action')) . '"'); break; - case 'create_another': + case 'return_to_edit': case 'update': $messages = $repos->validate($data); /** @var MessageBag $messages ['errors'] */ @@ -277,7 +277,7 @@ class BudgetController extends BaseController $repos->update($budget, $data); Session::flash('success', 'Budget updated!'); - if ($data['post_submit_action'] == 'create_another') { + if ($data['post_submit_action'] == 'return_to_edit') { return Redirect::route('budgets.edit', $budget->id); } else { return Redirect::route('budgets.index'); diff --git a/app/controllers/CategoryController.php b/app/controllers/CategoryController.php index 6b1d7a4e44..55a6e629d3 100644 --- a/app/controllers/CategoryController.php +++ b/app/controllers/CategoryController.php @@ -1,26 +1,17 @@ _repository = $repository; - $this->_category = $category; View::share('title', 'Categories'); View::share('mainTitleIcon', 'fa-bar-chart'); } @@ -40,8 +31,7 @@ class CategoryController extends BaseController */ public function delete(Category $category) { - return View::make('categories.delete')->with('category', $category) - ->with('subTitle', 'Delete category "' . $category->name . '"'); + return View::make('categories.delete')->with('category', $category)->with('subTitle', 'Delete category "' . $category->name . '"'); } /** @@ -51,7 +41,10 @@ class CategoryController extends BaseController */ public function destroy(Category $category) { - $this->_repository->destroy($category); + /** @var \FireflyIII\Database\Category $repos */ + $repos = App::make('FireflyIII\Database\Category'); + + $repos->destroy($category); Session::flash('success', 'The category was deleted.'); return Redirect::route('categories.index'); } @@ -72,10 +65,7 @@ class CategoryController extends BaseController */ public function index() { - $categories = $this->_repository->get(); - - return View::make('categories.index')->with('categories', $categories) - ->with('subTitle', 'All your categories'); + return View::make('categories.index'); } /** @@ -124,15 +114,40 @@ class CategoryController extends BaseController */ public function update(Category $category) { - $category = $this->_repository->update($category, Input::all()); - if ($category->validate()) { - Session::flash('success', 'Category "' . $category->name . '" updated.'); + /** @var \FireflyIII\Database\Category $repos */ + $repos = App::make('FireflyIII\Database\Category'); + $data = Input::except('_token'); - return Redirect::route('categories.index'); - } else { - Session::flash('success', 'Could not update category "' . $category->name . '".'); + 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!'); - return Redirect::route('categories.edit')->withErrors($category->errors())->withInput(); + 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; } diff --git a/app/controllers/GoogleTableController.php b/app/controllers/GoogleTableController.php index 8f51270363..80b652fe4b 100644 --- a/app/controllers/GoogleTableController.php +++ b/app/controllers/GoogleTableController.php @@ -8,6 +8,38 @@ use Firefly\Exception\FireflyException; class GoogleTableController extends BaseController { + /** + * @return \Illuminate\Http\JsonResponse + */ + public function categoryList() + { + + /** @var \FireflyIII\Database\Category $repos */ + $repos = App::make('FireflyIII\Database\Category'); + + /** @var \Grumpydictator\Gchart\GChart $chart */ + $chart = App::make('gchart'); + $chart->addColumn('ID', 'number'); + $chart->addColumn('ID_Edit', 'string'); + $chart->addColumn('ID_Delete', 'string'); + $chart->addColumn('Name_URL', 'string'); + $chart->addColumn('Name', 'string'); + + $list = $repos->get(); + + /** @var Category $entry */ + foreach ($list as $entry) { + $chart->addRow( + $entry->id, route('categories.edit', $entry->id), route('categories.delete', $entry->id), route('categories.show', $entry->id), $entry->name + ); + } + + + $chart->generate(); + return Response::json($chart->getData()); + + } + /** * @param $what * @@ -84,8 +116,8 @@ class GoogleTableController extends BaseController if (is_null($repetition)) { $journals = $budget->transactionjournals()->with(['budgets', 'categories', 'transactions', 'transactions.account'])->orderBy('date', 'DESC')->get(); } else { - $journals = $budget->transactionjournals()->with(['budgets', 'categories', 'transactions', 'transactions.account'])-> - after($repetition->startdate)->before($repetition->enddate)->orderBy('date', 'DESC')->get(); + $journals = $budget->transactionjournals()->with(['budgets', 'categories', 'transactions', 'transactions.account'])->after($repetition->startdate) + ->before($repetition->enddate)->orderBy('date', 'DESC')->get(); } /** @var TransactionJournal $transaction */ foreach ($journals as $journal) { diff --git a/app/lib/FireflyIII/Database/Budget.php b/app/lib/FireflyIII/Database/Budget.php index ba8d22fcf4..934941adf7 100644 --- a/app/lib/FireflyIII/Database/Budget.php +++ b/app/lib/FireflyIII/Database/Budget.php @@ -9,6 +9,7 @@ use Illuminate\Support\Collection; use FireflyIII\Database\Ifaces\CommonDatabaseCalls; use FireflyIII\Database\Ifaces\CUD; use FireflyIII\Database\Ifaces\BudgetInterface; + /** * Class Budget * @@ -80,11 +81,11 @@ class Budget implements CUD, CommonDatabaseCalls, BudgetInterface $successes = new MessageBag; $errors = new MessageBag; - if(isset($model['name'])) { - if(strlen($model['name']) < 1) { + if (isset($model['name'])) { + if (strlen($model['name']) < 1) { $errors->add('name', 'Name is too short'); } - if(strlen($model['name']) > 200) { + if (strlen($model['name']) > 200) { $errors->add('name', 'Name is too long'); } @@ -98,8 +99,8 @@ class Budget implements CUD, CommonDatabaseCalls, BudgetInterface } - if(!$errors->has('name')) { - $successes->add('name','OK'); + if (!$errors->has('name')) { + $successes->add('name', 'OK'); } return [ @@ -118,7 +119,7 @@ class Budget implements CUD, CommonDatabaseCalls, BudgetInterface { $data['user_id'] = $this->getUser()->id; - $budget = new \Budget($data); + $budget = new \Budget($data); $budget->class = 'Budget'; if (!$budget->validate()) { @@ -155,10 +156,12 @@ class Budget implements CUD, CommonDatabaseCalls, BudgetInterface /** * @param \Budget $budget - * @param Carbon $date + * @param Carbon $date + * * @return float */ - public function spentInMonth(\Budget $budget, Carbon $date) { + public function spentInMonth(\Budget $budget, Carbon $date) + { $end = clone $date; $date->startOfMonth(); $end->endOfMonth(); @@ -220,7 +223,7 @@ class Budget implements CUD, CommonDatabaseCalls, BudgetInterface */ public function update(Ardent $model, array $data) { - $model->name = $data['name']; + $model->name = $data['name']; if (!$model->validate()) { var_dump($model->errors()->all()); exit; diff --git a/app/lib/FireflyIII/Database/Category.php b/app/lib/FireflyIII/Database/Category.php index 47349ac021..9e9ec6c93b 100644 --- a/app/lib/FireflyIII/Database/Category.php +++ b/app/lib/FireflyIII/Database/Category.php @@ -2,6 +2,7 @@ namespace FireflyIII\Database; use Carbon\Carbon; +use FireflyIII\Exception\NotImplementedException; use Illuminate\Support\MessageBag; use LaravelBook\Ardent\Ardent; use Illuminate\Support\Collection; @@ -33,7 +34,8 @@ class Category implements CUD, CommonDatabaseCalls, CategoryInterface */ public function destroy(Ardent $model) { - // TODO: Implement destroy() method. + $model->delete(); + return true; } /** @@ -59,7 +61,37 @@ class Category implements CUD, CommonDatabaseCalls, CategoryInterface */ public function validate(array $model) { - // TODO: Implement validate() method. + $warnings = new MessageBag; + $successes = new MessageBag; + $errors = new MessageBag; + + if (isset($model['name'])) { + if (strlen($model['name']) < 1) { + $errors->add('name', 'Name is too short'); + } + if (strlen($model['name']) > 200) { + $errors->add('name', 'Name is too long'); + + } + } else { + $errors->add('name', 'Name is mandatory'); + } + $validator = \Validator::make($model, \Component::$rules); + + if ($validator->invalid()) { + $errors->merge($validator->errors()); + } + + + if (!$errors->has('name')) { + $successes->add('name', 'OK'); + } + + return [ + 'errors' => $errors, + 'warnings' => $warnings, + 'successes' => $successes + ]; } /** @@ -91,7 +123,7 @@ class Category implements CUD, CommonDatabaseCalls, CategoryInterface */ public function get() { - // TODO: Implement get() method. + return $this->getUser()->categories()->orderBy('name', 'ASC')->get(); } /** @@ -124,6 +156,15 @@ class Category implements CUD, CommonDatabaseCalls, CategoryInterface */ public function update(Ardent $model, array $data) { - // TODO: Implement update() method. + $model->name = $data['name']; + if (!$model->validate()) { + var_dump($model->errors()->all()); + exit; + } + + + $model->save(); + + return true; } } \ No newline at end of file diff --git a/app/routes.php b/app/routes.php index 4d85b37703..8b804a5d77 100644 --- a/app/routes.php +++ b/app/routes.php @@ -174,6 +174,7 @@ Route::group( // google table controller Route::get('/table/account/{account}/transactions', ['uses' => 'GoogleTableController@transactionsByAccount']); Route::get('/table/accounts/{what}', ['uses' => 'GoogleTableController@accountList']); + Route::get('/table/categories', ['uses' => 'GoogleTableController@categoryList']); Route::get('/table/budget/{budget}/{limitrepetition?}/transactions', ['uses' => 'GoogleTableController@transactionsByBudget']); diff --git a/app/views/accounts/edit.blade.php b/app/views/accounts/edit.blade.php index 39e14486ca..47e3ee60c2 100644 --- a/app/views/accounts/edit.blade.php +++ b/app/views/accounts/edit.blade.php @@ -1,13 +1,5 @@ @extends('layouts.default') @section('content') -
- Bla text here. -
-- Remember that deleting something is permanent. -
-+
+ Are you sure? +
- Account "{{{$category->name}}}" still has {{$category->transactionjournals()->count()}} transaction(s) associated to it. - These will NOT be deleted but will lose their connection to the category. - - @endif - -- Press "Delete permanently" If you are sure you want to delete "{{{$category->name}}}". -
++ + Cancel +
+Use categories to group your expenses
++ +
{{$errors->first('name')}}
- @else - For example: bike, utilities, daily groceries - @endif + +Use categories to group your expenses
-- Use categories to group expenses by hobby, for certain types of groceries or what bills are for. - Expenses grouped in categories do not have to reoccur every month or every week, like budgets. -
- -- {{{$category->name}}} - | -- | -