/** * @param array $data * * @return Rule */ public function store(array $data) { /** @var RuleGroup $ruleGroup */ $ruleGroup = Auth::user()->ruleGroups()->find($data['rule_group_id']); // get max order: $order = $this->getHighestOrderInRuleGroup($ruleGroup); // start by creating a new rule: $rule = new Rule(); $rule->user()->associate($data['user_id']); $rule->rule_group_id = $data['rule_group_id']; $rule->order = $order + 1; $rule->active = 1; $rule->stop_processing = intval($data['stop_processing']) == 1; $rule->title = $data['title']; $rule->description = strlen($data['description']) > 0 ? $data['description'] : null; $rule->save(); // start storing triggers: $order = 1; $stopProcessing = false; $triggerValues = ['action' => 'user_action', 'value' => $data['trigger'], 'stopProcessing' => $stopProcessing, 'order' => $order]; $this->storeTrigger($rule, $triggerValues); foreach ($data['rule-triggers'] as $index => $trigger) { $value = $data['rule-trigger-values'][$index]; $stopProcessing = isset($data['rule-trigger-stop'][$index]) ? true : false; $triggerValues = ['action' => $trigger, 'value' => $value, 'stopProcessing' => $stopProcessing, 'order' => $order]; $this->storeTrigger($rule, $triggerValues); $order++; } // same for actions. $order = 1; foreach ($data['rule-actions'] as $index => $action) { $value = $data['rule-action-values'][$index]; $stopProcessing = isset($data['rule-action-stop'][$index]) ? true : false; $actionValues = ['action' => $action, 'value' => $value, 'stopProcessing' => $stopProcessing, 'order' => $order]; $this->storeAction($rule, $actionValues); } return $rule; }
/** * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ public function createRules() { // three rule groups to get started. $ruleGroup = new RuleGroup(); $ruleGroup->user()->associate($this->user); $ruleGroup->order = 1; $ruleGroup->active = 1; $ruleGroup->title = 'Default rules'; $ruleGroup->description = 'All your rules not in a particular group.'; $ruleGroup->save(); unset($ruleGroup); $ruleGroup = new RuleGroup(); $ruleGroup->user()->associate($this->user); $ruleGroup->order = 2; $ruleGroup->active = 1; $ruleGroup->title = 'Empty rule group'; $ruleGroup->description = 'Intentionally has no rules.'; $ruleGroup->save(); unset($ruleGroup); $ruleGroup = new RuleGroup(); $ruleGroup->user()->associate($this->user); $ruleGroup->order = 3; $ruleGroup->active = 1; $ruleGroup->title = 'Rules for bills'; $ruleGroup->description = 'All rules for bills and recurring costs.'; $ruleGroup->save(); unset($ruleGroup); // move groceries to correct budget/category $rule = new Rule(); $rule->user()->associate($this->user); $rule->ruleGroup()->associate(RuleGroup::find(1)); $rule->order = 1; $rule->active = 1; $rule->stop_processing = 0; $rule->title = 'Move groceries'; $rule->description = 'Move groceries to correct category and budget.'; $rule->save(); // initial trigger for this rule: $ruleTrigger = new RuleTrigger(); $ruleTrigger->rule()->associate($rule); $ruleTrigger->order = 1; $ruleTrigger->active = 1; $ruleTrigger->stop_processing = 0; $ruleTrigger->trigger_type = 'user_action'; $ruleTrigger->trigger_value = 'store-journal'; $ruleTrigger->save(); unset($ruleTrigger); // content trigger for this rule. $ruleTrigger = new RuleTrigger(); $ruleTrigger->rule()->associate($rule); $ruleTrigger->order = 2; $ruleTrigger->active = 1; $ruleTrigger->stop_processing = 0; $ruleTrigger->trigger_type = 'description_contains'; $ruleTrigger->trigger_value = 'groceries'; $ruleTrigger->save(); unset($ruleTrigger); // another $ruleTrigger = new RuleTrigger(); $ruleTrigger->rule()->associate($rule); $ruleTrigger->order = 3; $ruleTrigger->active = 1; $ruleTrigger->stop_processing = 0; $ruleTrigger->trigger_type = 'from_account_is'; $ruleTrigger->trigger_value = 'TestData Checking Account'; $ruleTrigger->save(); unset($ruleTrigger); // actions for this rule. one, set category $ruleAction = new RuleAction(); $ruleAction->rule()->associate($rule); $ruleAction->order = 1; $ruleAction->active = 1; $ruleAction->stop_processing = 0; $ruleAction->action_type = 'set_category'; $ruleAction->action_value = 'Groceries'; $ruleAction->save(); unset($ruleAction); // actions for this rule. one, set budget $ruleAction = new RuleAction(); $ruleAction->rule()->associate($rule); $ruleAction->order = 2; $ruleAction->active = 1; $ruleAction->stop_processing = 0; $ruleAction->action_type = 'set_budget'; $ruleAction->action_value = 'Groceries'; $ruleAction->save(); unset($ruleAction); // move "gas" to "Car" and "Car" $rule = new Rule(); $rule->user()->associate($this->user); $rule->ruleGroup()->associate(RuleGroup::find(1)); $rule->order = 2; $rule->active = 1; $rule->stop_processing = 0; $rule->title = 'Move gas'; $rule->description = null; $rule->save(); // initial trigger for this rule: $ruleTrigger = new RuleTrigger(); $ruleTrigger->rule()->associate($rule); $ruleTrigger->order = 1; $ruleTrigger->active = 1; $ruleTrigger->stop_processing = 0; $ruleTrigger->trigger_type = 'user_action'; $ruleTrigger->trigger_value = 'store-journal'; $ruleTrigger->save(); unset($ruleTrigger); // content trigger for this rule. $ruleTrigger = new RuleTrigger(); $ruleTrigger->rule()->associate($rule); $ruleTrigger->order = 2; $ruleTrigger->active = 1; $ruleTrigger->stop_processing = 0; $ruleTrigger->trigger_type = 'description_contains'; $ruleTrigger->trigger_value = 'gas'; $ruleTrigger->save(); unset($ruleTrigger); // another $ruleTrigger = new RuleTrigger(); $ruleTrigger->rule()->associate($rule); $ruleTrigger->order = 3; $ruleTrigger->active = 1; $ruleTrigger->stop_processing = 0; $ruleTrigger->trigger_type = 'from_account_is'; $ruleTrigger->trigger_value = 'TestData Checking Account'; $ruleTrigger->save(); unset($ruleTrigger); // actions for this rule. one, set category $ruleAction = new RuleAction(); $ruleAction->rule()->associate($rule); $ruleAction->order = 1; $ruleAction->active = 1; $ruleAction->stop_processing = 0; $ruleAction->action_type = 'set_category'; $ruleAction->action_value = 'Car'; $ruleAction->save(); unset($ruleAction); // actions for this rule. one, set budget $ruleAction = new RuleAction(); $ruleAction->rule()->associate($rule); $ruleAction->order = 2; $ruleAction->active = 1; $ruleAction->stop_processing = 0; $ruleAction->action_type = 'set_budget'; $ruleAction->action_value = 'Car'; $ruleAction->save(); unset($ruleAction); // move savings to money management $rule = new Rule(); $rule->user()->associate($this->user); $rule->ruleGroup()->associate(RuleGroup::find(1)); $rule->order = 3; $rule->active = 1; $rule->stop_processing = 0; $rule->title = 'Move savings'; $rule->description = null; $rule->save(); // initial trigger for this rule: $ruleTrigger = new RuleTrigger(); $ruleTrigger->rule()->associate($rule); $ruleTrigger->order = 1; $ruleTrigger->active = 1; $ruleTrigger->stop_processing = 0; $ruleTrigger->trigger_type = 'user_action'; $ruleTrigger->trigger_value = 'store-journal'; $ruleTrigger->save(); unset($ruleTrigger); // is transfer $ruleTrigger = new RuleTrigger(); $ruleTrigger->rule()->associate($rule); $ruleTrigger->order = 2; $ruleTrigger->active = 1; $ruleTrigger->stop_processing = 0; $ruleTrigger->trigger_type = 'transaction_type'; $ruleTrigger->trigger_value = 'Transfer'; $ruleTrigger->save(); unset($ruleTrigger); // content trigger for this rule. $ruleTrigger = new RuleTrigger(); $ruleTrigger->rule()->associate($rule); $ruleTrigger->order = 3; $ruleTrigger->active = 1; $ruleTrigger->stop_processing = 0; $ruleTrigger->trigger_type = 'description_is'; $ruleTrigger->trigger_value = 'Save money'; $ruleTrigger->save(); unset($ruleTrigger); // another $ruleTrigger = new RuleTrigger(); $ruleTrigger->rule()->associate($rule); $ruleTrigger->order = 4; $ruleTrigger->active = 1; $ruleTrigger->stop_processing = 0; $ruleTrigger->trigger_type = 'from_account_is'; $ruleTrigger->trigger_value = 'TestData Checking Account'; $ruleTrigger->save(); unset($ruleTrigger); // another $ruleTrigger = new RuleTrigger(); $ruleTrigger->rule()->associate($rule); $ruleTrigger->order = 5; $ruleTrigger->active = 1; $ruleTrigger->stop_processing = 0; $ruleTrigger->trigger_type = 'to_account_is'; $ruleTrigger->trigger_value = 'TestData Savings'; $ruleTrigger->save(); unset($ruleTrigger); // actions for this rule. one, set category $ruleAction = new RuleAction(); $ruleAction->rule()->associate($rule); $ruleAction->order = 1; $ruleAction->active = 1; $ruleAction->stop_processing = 0; $ruleAction->action_type = 'set_category'; $ruleAction->action_value = 'Money Management'; $ruleAction->save(); unset($ruleAction); // move TV bill to "Bills" and "House" $rule = new Rule(); $rule->user()->associate($this->user); $rule->ruleGroup()->associate(RuleGroup::find(3)); $rule->order = 1; $rule->active = 1; $rule->stop_processing = 0; $rule->title = 'TV Bill'; $rule->description = null; $rule->save(); // initial trigger for this rule: $ruleTrigger = new RuleTrigger(); $ruleTrigger->rule()->associate($rule); $ruleTrigger->order = 1; $ruleTrigger->active = 1; $ruleTrigger->stop_processing = 0; $ruleTrigger->trigger_type = 'user_action'; $ruleTrigger->trigger_value = 'store-journal'; $ruleTrigger->save(); unset($ruleTrigger); // content trigger for this rule. $ruleTrigger = new RuleTrigger(); $ruleTrigger->rule()->associate($rule); $ruleTrigger->order = 2; $ruleTrigger->active = 1; $ruleTrigger->stop_processing = 0; $ruleTrigger->trigger_type = 'description_contains'; $ruleTrigger->trigger_value = 'tv bill'; $ruleTrigger->save(); unset($ruleTrigger); // another $ruleTrigger = new RuleTrigger(); $ruleTrigger->rule()->associate($rule); $ruleTrigger->order = 3; $ruleTrigger->active = 1; $ruleTrigger->stop_processing = 0; $ruleTrigger->trigger_type = 'from_account_is'; $ruleTrigger->trigger_value = 'TestData Checking Account'; $ruleTrigger->save(); unset($ruleTrigger); // actions for this rule. one, set category $ruleAction = new RuleAction(); $ruleAction->rule()->associate($rule); $ruleAction->order = 1; $ruleAction->active = 1; $ruleAction->stop_processing = 0; $ruleAction->action_type = 'set_category'; $ruleAction->action_value = 'House'; $ruleAction->save(); unset($ruleAction); // actions for this rule. one, set budget $ruleAction = new RuleAction(); $ruleAction->rule()->associate($rule); $ruleAction->order = 2; $ruleAction->active = 1; $ruleAction->stop_processing = 0; $ruleAction->action_type = 'set_budget'; $ruleAction->action_value = 'Bills'; $ruleAction->save(); unset($ruleAction); // move rent to bills, rent. $rule = new Rule(); $rule->user()->associate($this->user); $rule->ruleGroup()->associate(RuleGroup::find(3)); $rule->order = 2; $rule->active = 1; $rule->stop_processing = 1; $rule->title = 'Rent'; $rule->description = 'Do something with rent.'; $rule->save(); // initial trigger for this rule: $ruleTrigger = new RuleTrigger(); $ruleTrigger->rule()->associate($rule); $ruleTrigger->order = 1; $ruleTrigger->active = 1; $ruleTrigger->stop_processing = 0; $ruleTrigger->trigger_type = 'user_action'; $ruleTrigger->trigger_value = 'update-journal'; $ruleTrigger->save(); unset($ruleTrigger); // content trigger for this rule. $ruleTrigger = new RuleTrigger(); $ruleTrigger->rule()->associate($rule); $ruleTrigger->order = 2; $ruleTrigger->active = 1; $ruleTrigger->stop_processing = 0; $ruleTrigger->trigger_type = 'description_contains'; $ruleTrigger->trigger_value = 'rent'; $ruleTrigger->save(); unset($ruleTrigger); // another $ruleTrigger = new RuleTrigger(); $ruleTrigger->rule()->associate($rule); $ruleTrigger->order = 3; $ruleTrigger->active = 1; $ruleTrigger->stop_processing = 1; $ruleTrigger->trigger_type = 'from_account_is'; $ruleTrigger->trigger_value = 'TestData Checking Account'; $ruleTrigger->save(); unset($ruleTrigger); // actions for this rule. one, set category $ruleAction = new RuleAction(); $ruleAction->rule()->associate($rule); $ruleAction->order = 1; $ruleAction->active = 1; $ruleAction->stop_processing = 0; $ruleAction->action_type = 'set_category'; $ruleAction->action_value = 'House'; $ruleAction->save(); unset($ruleAction); // actions for this rule. one, set budget $ruleAction = new RuleAction(); $ruleAction->rule()->associate($rule); $ruleAction->order = 2; $ruleAction->active = 1; $ruleAction->stop_processing = 1; $ruleAction->action_type = 'set_budget'; $ruleAction->action_value = 'Bills'; $ruleAction->save(); unset($ruleAction); }
/** * @param array $data * * @return Rule */ public function store(array $data) : Rule { /** @var RuleGroup $ruleGroup */ $ruleGroup = $this->user->ruleGroups()->find($data['rule_group_id']); // get max order: $order = $this->getHighestOrderInRuleGroup($ruleGroup); // start by creating a new rule: $rule = new Rule(); $rule->user()->associate($data['user_id']); $rule->rule_group_id = $data['rule_group_id']; $rule->order = $order + 1; $rule->active = 1; $rule->stop_processing = intval($data['stop_processing']) == 1; $rule->title = $data['title']; $rule->description = strlen($data['description']) > 0 ? $data['description'] : null; $rule->save(); // start storing triggers: $this->storeTriggers($rule, $data); // same for actions. $this->storeActions($rule, $data); return $rule; }