Fixed some things in the rule controllers.

This commit is contained in:
James Cole 2016-01-24 16:50:55 +01:00
parent 0f12ebb31c
commit 94700f4064
5 changed files with 158 additions and 64 deletions

View File

@ -16,6 +16,7 @@ use FireflyIII\Models\RuleAction;
use FireflyIII\Models\RuleGroup; use FireflyIII\Models\RuleGroup;
use FireflyIII\Models\RuleTrigger; use FireflyIII\Models\RuleTrigger;
use FireflyIII\Repositories\Rule\RuleRepositoryInterface; use FireflyIII\Repositories\Rule\RuleRepositoryInterface;
use FireflyIII\Repositories\RuleGroup\RuleGroupRepositoryInterface;
use Input; use Input;
use Preferences; use Preferences;
use Response; use Response;
@ -173,6 +174,10 @@ class RuleController extends Controller
*/ */
public function index() public function index()
{ {
$this->createDefaultRuleGroup();
$this->createDefaultRule();
$ruleGroups = Auth::user() $ruleGroups = Auth::user()
->ruleGroups() ->ruleGroups()
->orderBy('active', 'DESC') ->orderBy('active', 'DESC')
@ -326,6 +331,56 @@ class RuleController extends Controller
return redirect(Session::get('rules.rule.edit.url')); return redirect(Session::get('rules.rule.edit.url'));
} }
private function createDefaultRule()
{
/** @var RuleRepositoryInterface $repository */
$repository = app('FireflyIII\Repositories\Rule\RuleRepositoryInterface');
if ($repository->count() === 0) {
$data = [
'rule_group_id' => $repository->getFirstRuleGroup()->id,
'stop_processing' => 0,
'title' => trans('firefly.default_rule_name'),
'description' => trans('firefly.default_rule_description'),
'trigger' => 'store-journal',
'rule-trigger-values' => [
trans('firefly.default_rule_trigger_description'),
trans('firefly.default_rule_trigger_from_account'),
],
'rule-action-values' => [
trans('firefly.default_rule_action_prepend'),
trans('firefly.default_rule_action_set_category'),
],
'rule-triggers' => ['description_is', 'from_account_is'],
'rule-actions' => ['prepend_description', 'set_category'],
];
$repository->store($data);
}
}
/**
*
*/
private function createDefaultRuleGroup()
{
/** @var RuleGroupRepositoryInterface $repository */
$repository = app('FireflyIII\Repositories\RuleGroup\RuleGroupRepositoryInterface');
if ($repository->count() === 0) {
$data = [
'user' => Auth::user()->id,
'title' => trans('firefly.default_rule_group_name'),
'description' => trans('firefly.default_rule_group_description'),
];
$repository->store($data);
}
}
/** /**
* @param Rule $rule * @param Rule $rule
* *

View File

@ -23,6 +23,14 @@ use FireflyIII\Models\RuleTrigger;
class RuleRepository implements RuleRepositoryInterface class RuleRepository implements RuleRepositoryInterface
{ {
/**
* @return int
*/
public function count()
{
return Auth::user()->rules()->count();
}
/** /**
* @param Rule $rule * @param Rule $rule
* *
@ -41,6 +49,24 @@ class RuleRepository implements RuleRepositoryInterface
return true; return true;
} }
/**
* @return RuleGroup
*/
public function getFirstRuleGroup()
{
return Auth::user()->ruleGroups()->first();
}
/**
* @param RuleGroup $ruleGroup
*
* @return int
*/
public function getHighestOrderInRuleGroup(RuleGroup $ruleGroup)
{
return intval($ruleGroup->rules()->max('order'));
}
/** /**
* @param Rule $rule * @param Rule $rule
* *
@ -63,31 +89,6 @@ class RuleRepository implements RuleRepositoryInterface
$this->resetRulesInGroupOrder($rule->ruleGroup); $this->resetRulesInGroupOrder($rule->ruleGroup);
} }
/**
* @param RuleGroup $ruleGroup
*
* @return bool
*/
public function resetRulesInGroupOrder(RuleGroup $ruleGroup)
{
$ruleGroup->rules()->whereNotNull('deleted_at')->update(['order' => 0]);
$set = $ruleGroup->rules()
->orderBy('order', 'ASC')
->orderBy('updated_at', 'DESC')
->get();
$count = 1;
/** @var Rule $entry */
foreach ($set as $entry) {
$entry->order = $count;
$entry->save();
$count++;
}
return true;
}
/** /**
* @param Rule $rule * @param Rule $rule
* *
@ -153,6 +154,31 @@ class RuleRepository implements RuleRepositoryInterface
return true; return true;
} }
/**
* @param RuleGroup $ruleGroup
*
* @return bool
*/
public function resetRulesInGroupOrder(RuleGroup $ruleGroup)
{
$ruleGroup->rules()->whereNotNull('deleted_at')->update(['order' => 0]);
$set = $ruleGroup->rules()
->orderBy('order', 'ASC')
->orderBy('updated_at', 'DESC')
->get();
$count = 1;
/** @var Rule $entry */
foreach ($set as $entry) {
$entry->order = $count;
$entry->save();
$count++;
}
return true;
}
/** /**
* @param array $data * @param array $data
* *
@ -168,7 +194,7 @@ class RuleRepository implements RuleRepositoryInterface
// start by creating a new rule: // start by creating a new rule:
$rule = new Rule; $rule = new Rule;
$rule->user()->associate(Auth::user()); $rule->user()->associate(Auth::user()); // TODO must be $data['user']
$rule->rule_group_id = $data['rule_group_id']; $rule->rule_group_id = $data['rule_group_id'];
$rule->order = ($order + 1); $rule->order = ($order + 1);
@ -190,7 +216,7 @@ class RuleRepository implements RuleRepositoryInterface
'order' => $order, 'order' => $order,
]; ];
$this->storeTrigger($rule, 'user_action', $data['trigger'], $stopProcessing, $order); $this->storeTrigger($rule, $triggerValues);
foreach ($data['rule-triggers'] as $index => $trigger) { foreach ($data['rule-triggers'] as $index => $trigger) {
$value = $data['rule-trigger-values'][$index]; $value = $data['rule-trigger-values'][$index];
$stopProcessing = isset($data['rule-trigger-stop'][$index]) ? true : false; $stopProcessing = isset($data['rule-trigger-stop'][$index]) ? true : false;
@ -202,7 +228,7 @@ class RuleRepository implements RuleRepositoryInterface
'order' => $order, 'order' => $order,
]; ];
$this->storeTrigger($rule, $trigger, $value, $stopProcessing, $order); $this->storeTrigger($rule, $triggerValues);
$order++; $order++;
} }
@ -219,42 +245,12 @@ class RuleRepository implements RuleRepositoryInterface
'order' => $order, 'order' => $order,
]; ];
$this->storeAction($rule, $action, $value, $stopProcessing, $order); $this->storeAction($rule, $actionValues);
} }
return $rule; return $rule;
} }
/**
* @param RuleGroup $ruleGroup
*
* @return int
*/
public function getHighestOrderInRuleGroup(RuleGroup $ruleGroup)
{
return intval($ruleGroup->rules()->max('order'));
}
/**
* @param Rule $rule
* @param array $values
*
* @return RuleTrigger
*/
public function storeTrigger(Rule $rule, array $values)
{
$ruleTrigger = new RuleTrigger;
$ruleTrigger->rule()->associate($rule);
$ruleTrigger->order = $values['order'];
$ruleTrigger->active = 1;
$ruleTrigger->stop_processing = $values['stopProcessing'];
$ruleTrigger->trigger_type = $values['action'];
$ruleTrigger->trigger_value = $values['value'];
$ruleTrigger->save();
return $ruleTrigger;
}
/** /**
* @param Rule $rule * @param Rule $rule
* @param array $values * @param array $values
@ -276,6 +272,26 @@ class RuleRepository implements RuleRepositoryInterface
return $ruleAction; return $ruleAction;
} }
/**
* @param Rule $rule
* @param array $values
*
* @return RuleTrigger
*/
public function storeTrigger(Rule $rule, array $values)
{
$ruleTrigger = new RuleTrigger;
$ruleTrigger->rule()->associate($rule);
$ruleTrigger->order = $values['order'];
$ruleTrigger->active = 1;
$ruleTrigger->stop_processing = $values['stopProcessing'];
$ruleTrigger->trigger_type = $values['action'];
$ruleTrigger->trigger_value = $values['value'];
$ruleTrigger->save();
return $ruleTrigger;
}
/** /**
* @param Rule $rule * @param Rule $rule
* @param array $data * @param array $data
@ -308,7 +324,7 @@ class RuleRepository implements RuleRepositoryInterface
'order' => $order, 'order' => $order,
]; ];
$this->storeTrigger($rule, 'user_action', $data['trigger'], $stopProcessing, $order); $this->storeTrigger($rule, $triggerValues);
foreach ($data['rule-triggers'] as $index => $trigger) { foreach ($data['rule-triggers'] as $index => $trigger) {
$value = $data['rule-trigger-values'][$index]; $value = $data['rule-trigger-values'][$index];
$stopProcessing = isset($data['rule-trigger-stop'][$index]) ? true : false; $stopProcessing = isset($data['rule-trigger-stop'][$index]) ? true : false;
@ -320,7 +336,7 @@ class RuleRepository implements RuleRepositoryInterface
'order' => $order, 'order' => $order,
]; ];
$this->storeTrigger($rule, $trigger, $value, $stopProcessing, $order); $this->storeTrigger($rule, $triggerValues);
$order++; $order++;
} }
@ -337,7 +353,7 @@ class RuleRepository implements RuleRepositoryInterface
'order' => $order, 'order' => $order,
]; ];
$this->storeAction($rule, $action, $value, $stopProcessing, $order); $this->storeAction($rule, $actionValues);
} }

View File

@ -21,6 +21,11 @@ use FireflyIII\Models\RuleTrigger;
*/ */
interface RuleRepositoryInterface interface RuleRepositoryInterface
{ {
/**
* @return int
*/
public function count();
/** /**
* @param Rule $rule * @param Rule $rule
* *
@ -28,6 +33,11 @@ interface RuleRepositoryInterface
*/ */
public function destroy(Rule $rule); public function destroy(Rule $rule);
/**
* @return RuleGroup
*/
public function getFirstRuleGroup();
/** /**
* @param RuleGroup $ruleGroup * @param RuleGroup $ruleGroup
* *

View File

@ -15,6 +15,14 @@ use Illuminate\Support\Collection;
*/ */
class RuleGroupRepository implements RuleGroupRepositoryInterface class RuleGroupRepository implements RuleGroupRepositoryInterface
{ {
/**
* @return int
*/
public function count()
{
return Auth::user()->ruleGroups()->count();
}
/** /**
* @param RuleGroup $ruleGroup * @param RuleGroup $ruleGroup
* @param RuleGroup $moveTo * @param RuleGroup $moveTo

View File

@ -13,6 +13,11 @@ use Illuminate\Support\Collection;
*/ */
interface RuleGroupRepositoryInterface interface RuleGroupRepositoryInterface
{ {
/**
* @return int
*/
public function count();
/** /**
* @param RuleGroup $ruleGroup * @param RuleGroup $ruleGroup
* @param RuleGroup $moveTo * @param RuleGroup $moveTo