2016-01-13 11:34:56 -06:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* RuleRepository.php
|
2016-04-01 09:44:46 -05:00
|
|
|
* Copyright (C) 2016 thegrumpydictator@gmail.com
|
2016-01-13 11:34:56 -06:00
|
|
|
*
|
2016-10-04 23:52:15 -05:00
|
|
|
* This software may be modified and distributed under the terms of the
|
|
|
|
* Creative Commons Attribution-ShareAlike 4.0 International License.
|
|
|
|
*
|
|
|
|
* See the LICENSE file for details.
|
2016-01-13 11:34:56 -06:00
|
|
|
*/
|
|
|
|
|
2016-05-20 05:41:23 -05:00
|
|
|
declare(strict_types = 1);
|
|
|
|
|
2016-01-13 11:34:56 -06:00
|
|
|
namespace FireflyIII\Repositories\Rule;
|
|
|
|
|
2016-02-17 10:27:41 -06:00
|
|
|
use FireflyIII\Exceptions\FireflyException;
|
2016-01-13 14:44:26 -06:00
|
|
|
use FireflyIII\Models\Rule;
|
2016-01-14 14:34:17 -06:00
|
|
|
use FireflyIII\Models\RuleAction;
|
2016-01-13 11:34:56 -06:00
|
|
|
use FireflyIII\Models\RuleGroup;
|
2016-01-14 04:27:15 -06:00
|
|
|
use FireflyIII\Models\RuleTrigger;
|
2016-03-03 02:02:04 -06:00
|
|
|
use FireflyIII\User;
|
2016-01-13 11:34:56 -06:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class RuleRepository
|
|
|
|
*
|
|
|
|
* @package FireflyIII\Repositories\Rule
|
|
|
|
*/
|
|
|
|
class RuleRepository implements RuleRepositoryInterface
|
|
|
|
{
|
2016-03-03 02:02:04 -06:00
|
|
|
/** @var User */
|
|
|
|
private $user;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* BillRepository constructor.
|
|
|
|
*
|
|
|
|
* @param User $user
|
|
|
|
*/
|
|
|
|
public function __construct(User $user)
|
|
|
|
{
|
|
|
|
$this->user = $user;
|
|
|
|
}
|
2016-01-13 14:44:26 -06:00
|
|
|
|
2016-01-24 09:50:55 -06:00
|
|
|
/**
|
|
|
|
* @return int
|
|
|
|
*/
|
2016-04-06 02:27:45 -05:00
|
|
|
public function count(): int
|
2016-01-24 09:50:55 -06:00
|
|
|
{
|
2016-03-03 02:02:04 -06:00
|
|
|
return $this->user->rules()->count();
|
2016-01-24 09:50:55 -06:00
|
|
|
}
|
|
|
|
|
2016-01-14 04:27:15 -06:00
|
|
|
/**
|
2016-01-20 08:23:36 -06:00
|
|
|
* @param Rule $rule
|
2016-01-14 14:34:17 -06:00
|
|
|
*
|
2016-01-14 04:27:15 -06:00
|
|
|
* @return bool
|
|
|
|
*/
|
2016-04-06 02:27:45 -05:00
|
|
|
public function destroy(Rule $rule): bool
|
2016-01-14 04:27:15 -06:00
|
|
|
{
|
2016-01-20 08:23:36 -06:00
|
|
|
foreach ($rule->ruleTriggers as $trigger) {
|
|
|
|
$trigger->delete();
|
|
|
|
}
|
|
|
|
foreach ($rule->ruleActions as $action) {
|
|
|
|
$action->delete();
|
2016-01-14 04:27:15 -06:00
|
|
|
}
|
2016-01-20 08:23:36 -06:00
|
|
|
$rule->delete();
|
2016-01-14 04:27:15 -06:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-01-24 09:50:55 -06:00
|
|
|
/**
|
2016-05-15 09:13:05 -05:00
|
|
|
* FIxXME can return null
|
2016-04-06 02:27:45 -05:00
|
|
|
*
|
2016-01-24 09:50:55 -06:00
|
|
|
* @return RuleGroup
|
|
|
|
*/
|
2016-04-06 02:27:45 -05:00
|
|
|
public function getFirstRuleGroup(): RuleGroup
|
2016-01-24 09:50:55 -06:00
|
|
|
{
|
2016-03-03 02:02:04 -06:00
|
|
|
return $this->user->ruleGroups()->first();
|
2016-01-24 09:50:55 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param RuleGroup $ruleGroup
|
|
|
|
*
|
|
|
|
* @return int
|
|
|
|
*/
|
2016-04-06 02:27:45 -05:00
|
|
|
public function getHighestOrderInRuleGroup(RuleGroup $ruleGroup): int
|
2016-01-24 09:50:55 -06:00
|
|
|
{
|
|
|
|
return intval($ruleGroup->rules()->max('order'));
|
|
|
|
}
|
|
|
|
|
2016-02-17 10:27:41 -06:00
|
|
|
/**
|
|
|
|
* @param Rule $rule
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
* @throws FireflyException
|
|
|
|
*/
|
|
|
|
public function getPrimaryTrigger(Rule $rule): string
|
|
|
|
{
|
|
|
|
$count = $rule->ruleTriggers()->count();
|
|
|
|
if ($count === 0) {
|
|
|
|
throw new FireflyException('Rules should have more than zero triggers, rule #' . $rule->id . ' has none!');
|
|
|
|
}
|
|
|
|
|
|
|
|
return $rule->ruleTriggers()->where('trigger_type', 'user_action')->first()->trigger_value;
|
|
|
|
}
|
|
|
|
|
2016-01-20 08:23:36 -06:00
|
|
|
/**
|
|
|
|
* @param Rule $rule
|
|
|
|
*
|
2016-01-15 04:27:27 -06:00
|
|
|
* @return bool
|
|
|
|
*/
|
2016-04-06 02:27:45 -05:00
|
|
|
public function moveDown(Rule $rule): bool
|
2016-01-15 04:27:27 -06:00
|
|
|
{
|
2016-01-20 08:23:36 -06:00
|
|
|
$order = $rule->order;
|
2016-01-15 04:27:27 -06:00
|
|
|
|
2016-01-20 08:23:36 -06:00
|
|
|
// find the rule with order+1 and give it order-1
|
|
|
|
$other = $rule->ruleGroup->rules()->where('order', ($order + 1))->first();
|
|
|
|
if ($other) {
|
|
|
|
$other->order = $other->order - 1;
|
|
|
|
$other->save();
|
2016-01-15 04:27:27 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-01-20 08:23:36 -06:00
|
|
|
$rule->order = ($rule->order + 1);
|
|
|
|
$rule->save();
|
|
|
|
$this->resetRulesInGroupOrder($rule->ruleGroup);
|
2016-04-25 11:43:09 -05:00
|
|
|
|
2016-04-06 02:27:45 -05:00
|
|
|
return true;
|
2016-01-20 08:23:36 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Rule $rule
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2016-04-06 02:27:45 -05:00
|
|
|
public function moveUp(Rule $rule): bool
|
2016-01-20 08:23:36 -06:00
|
|
|
{
|
|
|
|
$order = $rule->order;
|
|
|
|
|
|
|
|
// find the rule with order-1 and give it order+1
|
|
|
|
$other = $rule->ruleGroup->rules()->where('order', ($order - 1))->first();
|
|
|
|
if ($other) {
|
|
|
|
$other->order = ($other->order + 1);
|
|
|
|
$other->save();
|
|
|
|
}
|
|
|
|
|
|
|
|
$rule->order = ($rule->order - 1);
|
|
|
|
$rule->save();
|
|
|
|
$this->resetRulesInGroupOrder($rule->ruleGroup);
|
2016-04-25 11:43:09 -05:00
|
|
|
|
2016-04-06 02:27:45 -05:00
|
|
|
return true;
|
2016-01-15 04:27:27 -06:00
|
|
|
}
|
|
|
|
|
2016-01-14 04:27:15 -06:00
|
|
|
/**
|
|
|
|
* @param Rule $rule
|
|
|
|
* @param array $ids
|
2016-01-14 14:34:17 -06:00
|
|
|
*
|
2016-01-14 04:27:15 -06:00
|
|
|
* @return bool
|
|
|
|
*/
|
2016-04-06 02:27:45 -05:00
|
|
|
public function reorderRuleActions(Rule $rule, array $ids): bool
|
2016-01-14 04:27:15 -06:00
|
|
|
{
|
|
|
|
$order = 1;
|
|
|
|
foreach ($ids as $actionId) {
|
|
|
|
/** @var RuleTrigger $trigger */
|
|
|
|
$action = $rule->ruleActions()->find($actionId);
|
|
|
|
if (!is_null($action)) {
|
|
|
|
$action->order = $order;
|
|
|
|
$action->save();
|
|
|
|
$order++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-01-14 02:38:48 -06:00
|
|
|
/**
|
2016-01-20 08:23:36 -06:00
|
|
|
* @param Rule $rule
|
|
|
|
* @param array $ids
|
2016-01-14 14:34:17 -06:00
|
|
|
*
|
2016-01-14 02:38:48 -06:00
|
|
|
* @return bool
|
|
|
|
*/
|
2016-04-06 02:27:45 -05:00
|
|
|
public function reorderRuleTriggers(Rule $rule, array $ids): bool
|
2016-01-14 02:38:48 -06:00
|
|
|
{
|
2016-01-20 08:23:36 -06:00
|
|
|
$order = 1;
|
|
|
|
foreach ($ids as $triggerId) {
|
|
|
|
/** @var RuleTrigger $trigger */
|
|
|
|
$trigger = $rule->ruleTriggers()->find($triggerId);
|
|
|
|
if (!is_null($trigger)) {
|
|
|
|
$trigger->order = $order;
|
|
|
|
$trigger->save();
|
|
|
|
$order++;
|
|
|
|
}
|
2016-01-14 02:38:48 -06:00
|
|
|
}
|
|
|
|
|
2016-01-20 08:23:36 -06:00
|
|
|
return true;
|
2016-01-14 02:38:48 -06:00
|
|
|
}
|
|
|
|
|
2016-01-24 09:50:55 -06:00
|
|
|
/**
|
|
|
|
* @param RuleGroup $ruleGroup
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
2016-04-06 02:27:45 -05:00
|
|
|
public function resetRulesInGroupOrder(RuleGroup $ruleGroup): bool
|
2016-01-24 09:50:55 -06:00
|
|
|
{
|
|
|
|
$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;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-01-14 14:34:17 -06:00
|
|
|
/**
|
|
|
|
* @param array $data
|
|
|
|
*
|
|
|
|
* @return Rule
|
|
|
|
*/
|
2016-04-06 02:27:45 -05:00
|
|
|
public function store(array $data): Rule
|
2016-01-14 14:34:17 -06:00
|
|
|
{
|
|
|
|
/** @var RuleGroup $ruleGroup */
|
2016-03-03 02:02:04 -06:00
|
|
|
$ruleGroup = $this->user->ruleGroups()->find($data['rule_group_id']);
|
2016-01-14 14:34:17 -06:00
|
|
|
|
|
|
|
// get max order:
|
|
|
|
$order = $this->getHighestOrderInRuleGroup($ruleGroup);
|
|
|
|
|
|
|
|
// start by creating a new rule:
|
|
|
|
$rule = new Rule;
|
2016-10-23 05:19:32 -05:00
|
|
|
$rule->user()->associate($this->user->id);
|
2016-01-14 14:34:17 -06:00
|
|
|
|
|
|
|
$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:
|
2016-02-12 10:41:12 -06:00
|
|
|
$this->storeTriggers($rule, $data);
|
2016-01-14 14:34:17 -06:00
|
|
|
|
|
|
|
// same for actions.
|
2016-02-12 10:41:12 -06:00
|
|
|
$this->storeActions($rule, $data);
|
2016-01-14 14:34:17 -06:00
|
|
|
|
|
|
|
return $rule;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-01-24 09:50:55 -06:00
|
|
|
* @param Rule $rule
|
|
|
|
* @param array $values
|
2016-01-14 14:34:17 -06:00
|
|
|
*
|
2016-01-24 09:50:55 -06:00
|
|
|
* @return RuleAction
|
2016-01-14 14:34:17 -06:00
|
|
|
*/
|
2016-04-06 02:27:45 -05:00
|
|
|
public function storeAction(Rule $rule, array $values): RuleAction
|
2016-01-14 14:34:17 -06:00
|
|
|
{
|
2016-01-24 09:50:55 -06:00
|
|
|
$ruleAction = new RuleAction;
|
|
|
|
$ruleAction->rule()->associate($rule);
|
|
|
|
$ruleAction->order = $values['order'];
|
|
|
|
$ruleAction->active = 1;
|
|
|
|
$ruleAction->stop_processing = $values['stopProcessing'];
|
|
|
|
$ruleAction->action_type = $values['action'];
|
|
|
|
$ruleAction->action_value = $values['value'];
|
|
|
|
$ruleAction->save();
|
|
|
|
|
|
|
|
|
|
|
|
return $ruleAction;
|
2016-01-14 14:34:17 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-01-22 00:35:28 -06:00
|
|
|
* @param Rule $rule
|
|
|
|
* @param array $values
|
2016-01-14 14:34:17 -06:00
|
|
|
*
|
2016-01-20 08:23:36 -06:00
|
|
|
* @return RuleTrigger
|
2016-01-14 14:34:17 -06:00
|
|
|
*/
|
2016-04-06 02:27:45 -05:00
|
|
|
public function storeTrigger(Rule $rule, array $values): RuleTrigger
|
2016-01-14 14:34:17 -06:00
|
|
|
{
|
2016-01-20 08:23:36 -06:00
|
|
|
$ruleTrigger = new RuleTrigger;
|
|
|
|
$ruleTrigger->rule()->associate($rule);
|
2016-01-22 00:35:28 -06:00
|
|
|
$ruleTrigger->order = $values['order'];
|
2016-01-20 08:23:36 -06:00
|
|
|
$ruleTrigger->active = 1;
|
2016-01-22 00:35:28 -06:00
|
|
|
$ruleTrigger->stop_processing = $values['stopProcessing'];
|
|
|
|
$ruleTrigger->trigger_type = $values['action'];
|
|
|
|
$ruleTrigger->trigger_value = $values['value'];
|
2016-01-20 08:23:36 -06:00
|
|
|
$ruleTrigger->save();
|
2016-01-14 14:34:17 -06:00
|
|
|
|
2016-01-20 08:23:36 -06:00
|
|
|
return $ruleTrigger;
|
2016-01-14 14:34:17 -06:00
|
|
|
}
|
2016-01-15 02:25:32 -06:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Rule $rule
|
|
|
|
* @param array $data
|
2016-01-15 06:13:33 -06:00
|
|
|
*
|
2016-01-15 02:25:32 -06:00
|
|
|
* @return Rule
|
|
|
|
*/
|
2016-04-06 02:27:45 -05:00
|
|
|
public function update(Rule $rule, array $data): Rule
|
2016-01-15 02:25:32 -06:00
|
|
|
{
|
|
|
|
// update rule:
|
|
|
|
$rule->active = $data['active'];
|
|
|
|
$rule->stop_processing = $data['stop_processing'];
|
|
|
|
$rule->title = $data['title'];
|
|
|
|
$rule->description = $data['description'];
|
|
|
|
$rule->save();
|
|
|
|
|
|
|
|
// delete triggers:
|
|
|
|
$rule->ruleTriggers()->delete();
|
|
|
|
|
|
|
|
// delete actions:
|
|
|
|
$rule->ruleActions()->delete();
|
|
|
|
|
|
|
|
// recreate triggers:
|
2016-02-12 10:41:12 -06:00
|
|
|
$this->storeTriggers($rule, $data);
|
|
|
|
|
|
|
|
// recreate actions:
|
|
|
|
$this->storeActions($rule, $data);
|
|
|
|
|
|
|
|
|
|
|
|
return $rule;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Rule $rule
|
|
|
|
* @param array $data
|
2016-04-06 02:27:45 -05:00
|
|
|
*
|
|
|
|
* @return bool
|
2016-02-12 10:41:12 -06:00
|
|
|
*/
|
2016-04-06 02:27:45 -05:00
|
|
|
private function storeActions(Rule $rule, array $data): bool
|
2016-02-12 10:41:12 -06:00
|
|
|
{
|
|
|
|
$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);
|
|
|
|
}
|
2016-04-25 11:43:09 -05:00
|
|
|
|
2016-04-06 02:27:45 -05:00
|
|
|
return true;
|
2016-02-12 10:41:12 -06:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Rule $rule
|
|
|
|
* @param array $data
|
2016-04-25 11:43:09 -05:00
|
|
|
*
|
2016-04-06 02:27:45 -05:00
|
|
|
* @return bool
|
2016-02-12 10:41:12 -06:00
|
|
|
*/
|
2016-04-06 02:27:45 -05:00
|
|
|
private function storeTriggers(Rule $rule, array $data): bool
|
2016-02-12 10:41:12 -06:00
|
|
|
{
|
2016-01-15 02:25:32 -06:00
|
|
|
$order = 1;
|
|
|
|
$stopProcessing = false;
|
2016-01-22 00:35:28 -06:00
|
|
|
|
|
|
|
$triggerValues = [
|
|
|
|
'action' => 'user_action',
|
|
|
|
'value' => $data['trigger'],
|
|
|
|
'stopProcessing' => $stopProcessing,
|
|
|
|
'order' => $order,
|
|
|
|
];
|
|
|
|
|
2016-01-24 09:50:55 -06:00
|
|
|
$this->storeTrigger($rule, $triggerValues);
|
2016-01-15 02:25:32 -06:00
|
|
|
foreach ($data['rule-triggers'] as $index => $trigger) {
|
|
|
|
$value = $data['rule-trigger-values'][$index];
|
|
|
|
$stopProcessing = isset($data['rule-trigger-stop'][$index]) ? true : false;
|
2016-01-22 00:35:28 -06:00
|
|
|
|
|
|
|
$triggerValues = [
|
|
|
|
'action' => $trigger,
|
|
|
|
'value' => $value,
|
|
|
|
'stopProcessing' => $stopProcessing,
|
|
|
|
'order' => $order,
|
|
|
|
];
|
|
|
|
|
2016-01-24 09:50:55 -06:00
|
|
|
$this->storeTrigger($rule, $triggerValues);
|
2016-01-15 02:25:32 -06:00
|
|
|
$order++;
|
|
|
|
}
|
2016-04-25 11:43:09 -05:00
|
|
|
|
2016-04-06 02:27:45 -05:00
|
|
|
return true;
|
2016-01-15 02:25:32 -06:00
|
|
|
}
|
2016-01-22 00:35:28 -06:00
|
|
|
}
|