/**
  * @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'];
 }
 /**
  * @return array
  */
 public function rules()
 {
     $validTriggers = array_keys(config('firefly.rule-triggers'));
     $validActions = array_keys(config('firefly.rule-actions'));
     // some actions require text:
     $contextActions = join(',', config('firefly.rule-actions-text'));
     $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'));
     }
     $rules = ['title' => $titleRule, 'description' => 'between:1,5000', 'stop_processing' => 'boolean', 'rule_group_id' => 'required|belongsToUser:rule_groups', 'trigger' => 'required|in:store-journal,update-journal', 'rule-trigger.*' => 'required|in:' . join(',', $validTriggers), 'rule-trigger-value.*' => 'required|min:1|ruleTriggerValue', 'rule-action.*' => 'required|in:' . join(',', $validActions)];
     // since Laravel does not support this stuff yet, here's a trick.
     for ($i = 0; $i < 10; $i++) {
         $rules['rule-action-value.' . $i] = 'required_if:rule-action.' . $i . ',' . $contextActions . '|ruleActionValue';
     }
     return $rules;
 }
 /**
  * @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);
 }