1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Rule.php |
4
|
|
|
* Copyright (c) 2017 [email protected] |
5
|
|
|
* |
6
|
|
|
* This file is part of Firefly III. |
7
|
|
|
* |
8
|
|
|
* Firefly III is free software: you can redistribute it and/or modify |
9
|
|
|
* it under the terms of the GNU General Public License as published by |
10
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
11
|
|
|
* (at your option) any later version. |
12
|
|
|
* |
13
|
|
|
* Firefly III is distributed in the hope that it will be useful, |
14
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
15
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16
|
|
|
* GNU General Public License for more details. |
17
|
|
|
* |
18
|
|
|
* You should have received a copy of the GNU General Public License |
19
|
|
|
* along with Firefly III. If not, see <http://www.gnu.org/licenses/>. |
20
|
|
|
*/ |
21
|
|
|
declare(strict_types=1); |
22
|
|
|
|
23
|
|
|
namespace FireflyIII\Models; |
24
|
|
|
|
25
|
|
|
use Illuminate\Database\Eloquent\Model; |
26
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes; |
27
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Class Rule. |
31
|
|
|
*/ |
32
|
|
|
class Rule extends Model |
33
|
|
|
{ |
34
|
|
|
use SoftDeletes; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* The attributes that should be casted to native types. |
38
|
|
|
* |
39
|
|
|
* @var array |
40
|
|
|
*/ |
41
|
|
|
protected $casts |
42
|
|
|
= [ |
43
|
|
|
'created_at' => 'datetime', |
44
|
|
|
'updated_at' => 'datetime', |
45
|
|
|
'deleted_at' => 'datetime', |
46
|
|
|
'active' => 'boolean', |
47
|
|
|
'order' => 'int', |
48
|
|
|
'stop_processing' => 'boolean', |
49
|
|
|
]; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param string $value |
53
|
|
|
* |
54
|
|
|
* @return Rule |
55
|
|
|
* @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException |
56
|
|
|
*/ |
57
|
|
|
public static function routeBinder(string $value): Rule |
58
|
|
|
{ |
59
|
|
|
if (auth()->check()) { |
60
|
|
|
$ruleId = (int)$value; |
61
|
|
|
$rule = auth()->user()->rules()->find($ruleId); |
62
|
|
|
if (null !== $rule) { |
63
|
|
|
return $rule; |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
throw new NotFoundHttpException; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @codeCoverageIgnore |
71
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany |
72
|
|
|
*/ |
73
|
|
|
public function ruleActions() |
74
|
|
|
{ |
75
|
|
|
return $this->hasMany('FireflyIII\Models\RuleAction'); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @codeCoverageIgnore |
80
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
81
|
|
|
*/ |
82
|
|
|
public function ruleGroup() |
83
|
|
|
{ |
84
|
|
|
return $this->belongsTo('FireflyIII\Models\RuleGroup'); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @codeCoverageIgnore |
89
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany |
90
|
|
|
*/ |
91
|
|
|
public function ruleTriggers() |
92
|
|
|
{ |
93
|
|
|
return $this->hasMany('FireflyIII\Models\RuleTrigger'); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param $value |
98
|
|
|
*/ |
99
|
|
|
public function setDescriptionAttribute($value) |
100
|
|
|
{ |
101
|
|
|
$this->attributes['description'] = e($value); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @codeCoverageIgnore |
106
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
107
|
|
|
*/ |
108
|
|
|
public function user() |
109
|
|
|
{ |
110
|
|
|
return $this->belongsTo('FireflyIII\User'); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|