From 06174d6afb284923a862ea7f4bb0ccd6f4428bd9 Mon Sep 17 00:00:00 2001 From: James Cole Date: Wed, 13 Jan 2016 18:50:15 +0100 Subject: [PATCH] Edit rule group. --- app/Http/Controllers/RuleController.php | 72 +++++++++++++++---- app/Http/routes.php | 2 +- app/Repositories/Rule/RuleRepository.php | 17 +++++ .../Rule/RuleRepositoryInterface.php | 9 +++ resources/lang/en_US/firefly.php | 3 + .../create.twig} | 0 resources/views/rules/rule-group/edit.twig | 52 ++++++++++++++ 7 files changed, 142 insertions(+), 13 deletions(-) rename resources/views/rules/{create-rule-group.twig => rule-group/create.twig} (100%) create mode 100644 resources/views/rules/rule-group/edit.twig diff --git a/app/Http/Controllers/RuleController.php b/app/Http/Controllers/RuleController.php index 728891eaa0..cb4d63f2ce 100644 --- a/app/Http/Controllers/RuleController.php +++ b/app/Http/Controllers/RuleController.php @@ -47,14 +47,14 @@ class RuleController extends Controller $subTitle = trans('firefly.make_new_rule_group'); // put previous url in session if not redirect from store (not "create another"). - if (Session::get('rule-groups.create.fromStore') !== true) { - Session::put('rule-groups.create.url', URL::previous()); + if (Session::get('rules.rule-group.create.fromStore') !== true) { + Session::put('rules.rule-group.create.url', URL::previous()); } Session::forget('accounts.create.fromStore'); Session::flash('gaEventCategory', 'rules'); Session::flash('gaEventAction', 'create-rule-group'); - return view('rules.create-rule-group', compact('subTitleIcon', 'what', 'subTitle')); + return view('rules.rule-group.create', compact('subTitleIcon', 'what', 'subTitle')); } /** @@ -78,15 +78,70 @@ class RuleController extends Controller if (intval(Input::get('create_another')) === 1) { // set value so create routine will not overwrite URL: - Session::put('rule-groups.create.fromStore', true); + Session::put('rules.rule-group.create.fromStore', true); return redirect(route('rules.rule-group.create'))->withInput(); } // redirect to previous URL. - return redirect(Session::get('rule-groups.create.url')); + return redirect(Session::get('rules.rule-group.create.url')); } + + /** + * @param RuleGroup $ruleGroup + * + * @return View + */ + public function editRuleGroup(RuleGroup $ruleGroup) + { + $subTitle = trans('firefly.edit_rule_group', ['title' => $ruleGroup->title]); + + // put previous url in session if not redirect from store (not "return_to_edit"). + if (Session::get('rules.rule-group.edit.fromUpdate') !== true) { + Session::put('rules.rule-group.edit.url', URL::previous()); + } + Session::forget('rules.rule-group.edit.fromUpdate'); + Session::flash('gaEventCategory', 'rules'); + Session::flash('gaEventAction', 'edit-rule-group'); + + return view('rules.rule-group.edit', compact('ruleGroup', 'subTitle')); + + } + + /** + * @param RuleGroupFormRequest $request + * @param RuleRepositoryInterface $repository + * @param RuleGroup $ruleGroup + * + * @return $this|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector + */ + public function updateRuleGroup(RuleGroupFormRequest $request, RuleRepositoryInterface $repository, RuleGroup $ruleGroup) + { + $data = [ + 'title' => $request->input('title'), + 'description' => $request->input('description'), + 'active' => intval($request->input('active')) == 1, + ]; + + $repository->update($ruleGroup, $data); + + Session::flash('success', trans('firefly.updated_rule_group', ['title' => $ruleGroup->title])); + Preferences::mark(); + + if (intval(Input::get('return_to_edit')) === 1) { + // set value so edit routine will not overwrite URL: + Session::put('rules.rule-group.edit.fromUpdate', true); + + return redirect(route('rules.rule-group.edit', [$ruleGroup->id]))->withInput(['return_to_edit' => 1]); + } + + // redirect to previous URL. + return redirect(Session::get('rules.rule-group.edit.url')); + + } + + /** * @return View */ @@ -105,11 +160,4 @@ class RuleController extends Controller } - /** - * @param RuleGroup $ruleGroup - */ - public function editRuleGroup(RuleGroup $ruleGroup) - { - - } } diff --git a/app/Http/routes.php b/app/Http/routes.php index d567211762..a454ddbf92 100644 --- a/app/Http/routes.php +++ b/app/Http/routes.php @@ -248,7 +248,7 @@ Route::group( Route::get('/rules/groups/delete/{ruleGroup}', ['uses' => 'RuleController@deleteRuleGroup', 'as' => 'rules.rule-group.delete']); Route::post('/rules/groups/store', ['uses' => 'RuleController@storeRuleGroup', 'as' => 'rules.rule-group.store']); - + Route::post('/rules/groups/update/{ruleGroup}', ['uses' => 'RuleController@updateRuleGroup', 'as' => 'rules.rule-group.update']); /** * Search Controller diff --git a/app/Repositories/Rule/RuleRepository.php b/app/Repositories/Rule/RuleRepository.php index 1735039246..9dba43eaf7 100644 --- a/app/Repositories/Rule/RuleRepository.php +++ b/app/Repositories/Rule/RuleRepository.php @@ -53,4 +53,21 @@ class RuleRepository implements RuleRepositoryInterface return $newRuleGroup; } + + /** + * @param RuleGroup $ruleGroup + * @param array $data + * + * @return RuleGroup + */ + public function update(RuleGroup $ruleGroup, array $data) + { + // update the account: + $ruleGroup->title = $data['title']; + $ruleGroup->description = $data['description']; + $ruleGroup->active = $data['active']; + $ruleGroup->save(); + + return $ruleGroup; + } } \ No newline at end of file diff --git a/app/Repositories/Rule/RuleRepositoryInterface.php b/app/Repositories/Rule/RuleRepositoryInterface.php index 1130b49f2f..e1b910cef2 100644 --- a/app/Repositories/Rule/RuleRepositoryInterface.php +++ b/app/Repositories/Rule/RuleRepositoryInterface.php @@ -30,4 +30,13 @@ interface RuleRepositoryInterface */ public function getHighestOrderRuleGroup(); + + /** + * @param RuleGroup $ruleGroup + * @param array $data + * + * @return RuleGroup + */ + public function update(RuleGroup $ruleGroup, array $data); + } \ No newline at end of file diff --git a/resources/lang/en_US/firefly.php b/resources/lang/en_US/firefly.php index e530067696..90d7bcd70b 100644 --- a/resources/lang/en_US/firefly.php +++ b/resources/lang/en_US/firefly.php @@ -51,6 +51,9 @@ return [ 'make_new_rule_group' => 'Make new rule group', 'store_new_rule_group' => 'Store new rule group', 'created_new_rule_group' => 'New rule group ":title" stored!', + 'updated_rule_group' => 'Successfully updated rule group ":title".', + 'edit_rule_group' => 'Edit rule group ":title"', + 'update_rule_group' => 'Update rule group', 'no_rules_in_group' => 'There are no rules in this group', // actions and triggers diff --git a/resources/views/rules/create-rule-group.twig b/resources/views/rules/rule-group/create.twig similarity index 100% rename from resources/views/rules/create-rule-group.twig rename to resources/views/rules/rule-group/create.twig diff --git a/resources/views/rules/rule-group/edit.twig b/resources/views/rules/rule-group/edit.twig new file mode 100644 index 0000000000..a589c65715 --- /dev/null +++ b/resources/views/rules/rule-group/edit.twig @@ -0,0 +1,52 @@ +{% extends "./layout/default.twig" %} + +{% block breadcrumbs %} + {{ Breadcrumbs.renderIfExists(Route.getCurrentRoute.getName, ruleGroup) }} +{% endblock %} + +{% block content %} + {{ Form.model(ruleGroup, {'class' : 'form-horizontal','id' : 'update','url' : route('rules.rule-group.update',ruleGroup.id) } ) }} + +
+
+
+
+

{{ 'mandatoryFields'|_ }}

+
+
+ {{ ExpandedForm.checkbox('active') }} + {{ ExpandedForm.text('title') }} +
+
+ +
+
+ + +
+
+

{{ 'optionalFields'|_ }}

+
+
+ {{ ExpandedForm.textarea('description') }} +
+
+ + +
+
+

{{ 'options'|_ }}

+
+
+ {{ ExpandedForm.optionsList('update','budget') }} +
+ +
+
+
+ + {{ Form.close|raw }} + +{% endblock %}