More code for rules.

This commit is contained in:
James Cole 2016-01-13 18:34:56 +01:00
parent e9ee93beb7
commit 27aae279e6
9 changed files with 255 additions and 1 deletions

View File

@ -11,8 +11,14 @@ namespace FireflyIII\Http\Controllers;
use Auth;
use FireflyIII\Http\Requests;
use FireflyIII\Http\Requests\RuleGroupFormRequest;
use FireflyIII\Models\Rule;
use FireflyIII\Models\RuleGroup;
use FireflyIII\Repositories\Rule\RuleRepositoryInterface;
use Input;
use Preferences;
use Session;
use URL;
use View;
/**
@ -32,6 +38,54 @@ class RuleController extends Controller
View::share('mainTitleIcon', 'fa-random');
}
/**
* @return View
*/
public function createRuleGroup()
{
$subTitleIcon = 'fa-clone';
$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());
}
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'));
}
/**
* @param RuleGroupFormRequest $request
* @param RuleRepositoryInterface $repository
*
* @return $this|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
*/
public function storeRuleGroup(RuleGroupFormRequest $request, RuleRepositoryInterface $repository)
{
$data = [
'title' => $request->input('title'),
'description' => $request->input('description'),
'user' => Auth::user()->id,
];
$ruleGroup = $repository->storeRuleGroup($data);
Session::flash('success', trans('firefly.created_new_rule_group', ['title' => $ruleGroup->title]));
Preferences::mark();
if (intval(Input::get('create_another')) === 1) {
// set value so create routine will not overwrite URL:
Session::put('rule-groups.create.fromStore', true);
return redirect(route('rules.rule-group.create'))->withInput();
}
// redirect to previous URL.
return redirect(Session::get('rule-groups.create.url'));
}
/**
* @return View
@ -54,7 +108,8 @@ class RuleController extends Controller
/**
* @param RuleGroup $ruleGroup
*/
public function editRuleGroup(RuleGroup $ruleGroup) {
public function editRuleGroup(RuleGroup $ruleGroup)
{
}
}

View File

@ -0,0 +1,49 @@
<?php
/**
* RuleGroupFormRequest.php
* Copyright (C) 2016 Sander Dorigo
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
namespace FireflyIII\Http\Requests;
use Auth;
use FireflyIII\Models\RuleGroup;
use Input;
/**
* Class RuleGroupFormRequest
*
* @codeCoverageIgnore
* @package FireflyIII\Http\Requests
*/
class RuleGroupFormRequest extends Request
{
/**
* @return bool
*/
public function authorize()
{
// Only allow logged in users
return Auth::check();
}
/**
* @return array
*/
public function rules()
{
$titleRule = 'required|between:1,100|uniqueObjectForUser:rule_groups,title';
if (RuleGroup::find(Input::get('id'))) {
$titleRule = 'required|between:1,100|uniqueObjectForUser:rule_groups,title,' . intval(Input::get('id'));
}
return [
'title' => $titleRule,
'description' => 'between:1,5000',
];
}
}

View File

@ -247,6 +247,8 @@ Route::group(
Route::get('/rules/groups/edit/{ruleGroup}', ['uses' => 'RuleController@editRuleGroup', 'as' => 'rules.rule-group.edit']);
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']);
/**
* Search Controller

View File

@ -31,6 +31,9 @@ use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
*/
class RuleGroup extends Model
{
protected $fillable = ['user_id', 'order', 'title', 'description', 'active'];
/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
*/

View File

@ -90,6 +90,7 @@ class FireflyServiceProvider extends ServiceProvider
$this->app->bind('FireflyIII\Repositories\Currency\CurrencyRepositoryInterface', 'FireflyIII\Repositories\Currency\CurrencyRepository');
$this->app->bind('FireflyIII\Repositories\Tag\TagRepositoryInterface', 'FireflyIII\Repositories\Tag\TagRepository');
$this->app->bind('FireflyIII\Repositories\Attachment\AttachmentRepositoryInterface', 'FireflyIII\Repositories\Attachment\AttachmentRepository');
$this->app->bind('FireflyIII\Repositories\Rule\RuleRepositoryInterface', 'FireflyIII\Repositories\Rule\RuleRepository');
$this->app->bind('FireflyIII\Support\Search\SearchInterface', 'FireflyIII\Support\Search\Search');
// CSV import

View File

@ -0,0 +1,56 @@
<?php
/**
* RuleRepository.php
* Copyright (C) 2016 Sander Dorigo
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
namespace FireflyIII\Repositories\Rule;
use Auth;
use FireflyIII\Models\RuleGroup;
/**
* Class RuleRepository
*
* @package FireflyIII\Repositories\Rule
*/
class RuleRepository implements RuleRepositoryInterface
{
/**
* @return int
*/
public function getHighestOrderRuleGroup()
{
$entry = Auth::user()->ruleGroups()->max('order');
return intval($entry);
}
/**
* @param array $data
*
* @return RuleGroup
*/
public function storeRuleGroup(array $data)
{
$order = $this->getHighestOrderRuleGroup();
$newRuleGroup = new RuleGroup(
[
'user_id' => $data['user'],
'title' => $data['title'],
'description' => $data['description'],
'order' => ($order + 1),
'active' => 1,
]
);
$newRuleGroup->save();
return $newRuleGroup;
}
}

View File

@ -0,0 +1,33 @@
<?php
/**
* RuleRepositoryInterface.php
* Copyright (C) 2016 Sander Dorigo
*
* This software may be modified and distributed under the terms
* of the MIT license. See the LICENSE file for details.
*/
namespace FireflyIII\Repositories\Rule;
use FireflyIII\Models\RuleGroup;
/**
* Interface RuleRepositoryInterface
*
* @package FireflyIII\Repositories\Rule
*/
interface RuleRepositoryInterface
{
/**
* @param array $data
*
* @return RuleGroup
*/
public function storeRuleGroup(array $data);
/**
* @return int
*/
public function getHighestOrderRuleGroup();
}

View File

@ -48,6 +48,10 @@ return [
'new_rule_group' => 'New rule group',
'rule_priority_up' => 'Give rule more priority',
'rule_priority_down' => 'Give rule less priority',
'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!',
'no_rules_in_group' => 'There are no rules in this group',
// actions and triggers
'rule_trigger_user_action' => '',

View File

@ -0,0 +1,51 @@
{% extends "./layout/default.twig" %}
{% block breadcrumbs %}
{{ Breadcrumbs.renderIfExists(Route.getCurrentRoute.getName) }}
{% endblock %}
{% block content %}
{{ Form.open({'class' : 'form-horizontal','id' : 'store','url' : route('rules.rule-group.store')}) }}
<div class="row">
<div class="col-lg-6 col-md-6 col-sm-12">
<div class="box box-primary">
<div class="box-header with-border">
<h3 class="box-title">{{ 'mandatoryFields'|_ }}</h3>
</div>
<div class="box-body">
{{ ExpandedForm.text('title') }}
</div>
</div>
</div>
<div class="col-lg-6 col-md-6 col-sm-12">
<!-- optional fields -->
<div class="box">
<div class="box-header with-border">
<h3 class="box-title">{{ 'optionalFields'|_ }}</h3>
</div>
<div class="box-body">
{{ ExpandedForm.textarea('description') }}
</div>
</div>
<!-- panel for options -->
<div class="box">
<div class="box-header with-border">
<h3 class="box-title">{{ 'options'|_ }}</h3>
</div>
<div class="box-body">
{{ ExpandedForm.optionsList('create','rule-group') }}
</div>
<div class="box-footer">
<button type="submit" class="btn pull-right btn-success">{{ 'store_new_rule_group'|_ }}</button>
</div>
</div>
</div>
</div>
{{ Form.close|raw }}
{% endblock %}