Some more rule things before I merge a change to the CSV importer.

This commit is contained in:
James Cole 2016-01-14 19:20:02 +01:00
parent 42203ba872
commit 9703439a4c
6 changed files with 86 additions and 26 deletions

View File

@ -98,6 +98,15 @@ class RuleController extends Controller
} }
unset($key, $ruleTriggers); unset($key, $ruleTriggers);
// array of valid values for actions
$ruleActions = array_keys(Config::get('firefly.rule-actions'));
$possibleActions = [];
foreach ($ruleActions as $key) {
$possibleActions[$key] = trans('firefly.rule_action_' . $key . '_choice');
}
unset($key, $ruleActions);
// has old input? // has old input?
if (Input::old()) { if (Input::old()) {
// process old triggers. // process old triggers.
@ -107,7 +116,7 @@ class RuleController extends Controller
$triggerCount++; $triggerCount++;
$oldTrigger = $entry; $oldTrigger = $entry;
$oldValue = Input::old('rule-trigger-value')[$index]; $oldValue = Input::old('rule-trigger-value')[$index];
$oldChecked = isset(Input::old('rule-action-value')[$index]) ? true : false; $oldChecked = isset(Input::old('rule-trigger-stop')[$index]) ? true : false;
$oldTriggers[] = view( $oldTriggers[] = view(
'rules.partials.trigger', 'rules.partials.trigger',
[ [
@ -120,12 +129,28 @@ class RuleController extends Controller
)->render(); )->render();
$newIndex++; $newIndex++;
} }
// echo '<pre>';
// var_dump(Input::old());
// var_dump($oldTriggers);
// exit;
}
// process old actions
$newIndex = 0;
foreach (Input::old('rule-action') as $index => $entry) {
$count = ($newIndex + 1);
$actionCount++;
$oldAction = $entry;
$oldValue = Input::old('rule-action-value')[$index];
$oldChecked = isset(Input::old('rule-action-stop')[$index]) ? true : false;
$oldActions[] = view(
'rules.partials.action',
[
'oldTrigger' => $oldAction,
'oldValue' => $oldValue,
'oldChecked' => $oldChecked,
'actions' => $possibleActions,
'count' => $count
]
)->render();
$newIndex++;
}
}
$subTitleIcon = 'fa-clone'; $subTitleIcon = 'fa-clone';
$subTitle = trans('firefly.make_new_rule', ['title' => $ruleGroup->title]); $subTitle = trans('firefly.make_new_rule', ['title' => $ruleGroup->title]);
@ -143,9 +168,11 @@ class RuleController extends Controller
} }
Session::forget('rules.rule.create.fromStore'); Session::forget('rules.rule.create.fromStore');
Session::flash('gaEventCategory', 'rules'); Session::flash('gaEventCategory', 'rules');
Session::flash('gaEventAction', 'create-rule-group'); Session::flash('gaEventAction', 'create-rule');
return view('rules.rule.create', compact('subTitleIcon','oldTriggers', 'triggerCount', 'actionCount', 'ruleGroup', 'subTitle', 'journalTriggers')); return view(
'rules.rule.create', compact('subTitleIcon', 'oldTriggers', 'oldActions', 'triggerCount', 'actionCount', 'ruleGroup', 'subTitle', 'journalTriggers')
);
} }
/** /**

View File

@ -38,21 +38,26 @@ class RuleFormRequest extends Request
{ {
$validTriggers = array_keys(Config::get('firefly.rule-triggers')); $validTriggers = array_keys(Config::get('firefly.rule-triggers'));
$validActions = array_keys(Config::get('firefly.rule-actions'));
// some actions require text:
$contextActions = Config::get('firefly.rule-actions-text');
$titleRule = 'required|between:1,100|uniqueObjectForUser:rule_groups,title'; $titleRule = 'required|between:1,100|uniqueObjectForUser:rule_groups,title';
if (RuleGroup::find(Input::get('id'))) { if (RuleGroup::find(Input::get('id'))) {
$titleRule = 'required|between:1,100|uniqueObjectForUser:rule_groups,title,' . intval(Input::get('id')); $titleRule = 'required|between:1,100|uniqueObjectForUser:rule_groups,title,' . intval(Input::get('id'));
} }
return [ $rules = [
'title' => $titleRule, 'title' => $titleRule,
'description' => 'between:1,5000', 'description' => 'between:1,5000',
'stop_processing' => 'boolean', 'stop_processing' => 'boolean',
'trigger' => 'required|in:store-journal,update-journal', 'trigger' => 'required|in:store-journal,update-journal',
'rule-trigger.*' => 'required|in:' . join(',', $validTriggers), 'rule-trigger.*' => 'required|in:' . join(',', $validTriggers),
'rule-trigger-value.*' => 'required|min:1' 'rule-trigger-value.*' => 'required|min:1',
'rule-action.*' => 'required|in:' . join(',', $validActions),
'rule-action-value.*' => 'required_if:rule-action.*,' . join(',', $contextActions)
]; ];
} }
} }

View File

@ -201,5 +201,15 @@ return [
'append_description' => 'FireflyIII\Rules\Actions\AppendDescription', 'append_description' => 'FireflyIII\Rules\Actions\AppendDescription',
'prepend_description' => 'FireflyIII\Rules\Actions\PrependDescription', 'prepend_description' => 'FireflyIII\Rules\Actions\PrependDescription',
], ],
// all rule actions that require text input:
'rule-actions-text' => [
'set_category',
'set_budget',
'add_tag',
'remove_tag',
'set_description',
'append_description',
'prepend_description',
]
]; ];

View File

@ -14,8 +14,11 @@ $(function () {
if (triggerCount == 0) { if (triggerCount == 0) {
addNewTrigger(); addNewTrigger();
} }
if (actionCount == 0) {
addNewAction(); addNewAction();
}
$('.add_rule_trigger').click(function () { $('.add_rule_trigger').click(function () {
addNewTrigger(); addNewTrigger();

View File

@ -3,24 +3,37 @@
<a href="#" class="btn btn-danger btn-sm remove-action"><i class="fa fa-trash"></i></a> <a href="#" class="btn btn-danger btn-sm remove-action"><i class="fa fa-trash"></i></a>
</td> </td>
<td style="width:30%;"> <td style="width:30%;">
{% if errors.has('XXX') %} {% if errors.has('rule-action.'~count) %}
<span class="form-control-feedback"><i class="fa fa-fw fa-remove"></i></span> <span class="form-control-feedback"><i class="fa fa-fw fa-remove"></i></span>
<p class="text-danger">{{ errors.first('xxxx') }}</p> <p class="text-danger">{{ errors.first('rule-action.'~count) }}</p>
{% endif %} {% endif %}
<select name="rule-action[{{ count }}]" class="form-control"> <select name="rule-action[{{ count }}]" class="form-control">
{% for key,name in actions %} {% for key,name in actions %}
<option value="{{ key }}" label="{{ name }}">{{ name }}</option> <option value="{{ key }}" label="{{ name }}"
{% if key == oldTrigger %}
selected
{% endif %}
>{{ name }}</option>
{% endfor %} {% endfor %}
</select> </select>
</td> </td>
<td> <td>
<input type="text" name="rule-action-value[{{ count }}]" class="form-control">
<input type="text" value="{{ oldValue }}" name="rule-action-value[{{ count }}]"
class="form-control">
{% if errors.has(('rule-action-value.'~count)) %}
<p class="text-danger">
{{ errors.first('rule-action-value.'~count) }}
</p>
{% endif %}
</td> </td>
<td style="width:20%;"> <td style="width:20%;">
<div class="checkbox"> <div class="checkbox">
<label> <label>
<input type="checkbox" name="rule-action-stop[{{ count }}]" value="1"/> <input type="checkbox" name="rule-action-stop[{{ count }}]" value="1"
{% if oldChecked %}checked{% endif %}
/>
</label> </label>
</div> </div>
</td> </td>

View File

@ -84,7 +84,9 @@
</tr> </tr>
</thead> </thead>
<tbody class="rule-action-tbody"> <tbody class="rule-action-tbody">
{% for action in oldActions %}
{{ action|raw }}
{% endfor %}
</tbody> </tbody>
</table> </table>