1 | <?php |
||
2 | /** |
||
3 | * Tag.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 Crypt; |
||
26 | use Illuminate\Database\Eloquent\Model; |
||
27 | use Illuminate\Database\Eloquent\SoftDeletes; |
||
28 | use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; |
||
29 | use Watson\Validating\ValidatingTrait; |
||
30 | |||
31 | /** |
||
32 | * Class Tag. |
||
33 | */ |
||
34 | class Tag extends Model |
||
35 | { |
||
36 | use ValidatingTrait, SoftDeletes; |
||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
37 | |||
38 | /** |
||
39 | * The attributes that should be casted to native types. |
||
40 | * |
||
41 | * @var array |
||
42 | */ |
||
43 | protected $casts |
||
44 | = [ |
||
45 | 'created_at' => 'datetime', |
||
46 | 'updated_at' => 'datetime', |
||
47 | 'deleted_at' => 'datetime', |
||
48 | 'date' => 'date', |
||
49 | 'zoomLevel' => 'int', |
||
50 | ]; |
||
51 | /** @var array */ |
||
52 | protected $dates = ['date']; |
||
53 | /** @var array */ |
||
54 | protected $fillable = ['user_id', 'tag', 'date', 'description', 'longitude', 'latitude', 'zoomLevel', 'tagMode']; |
||
55 | /** @var array */ |
||
56 | protected $rules = ['tag' => 'required|between:1,200']; |
||
57 | |||
58 | /** |
||
59 | * @param array $fields |
||
60 | * |
||
61 | * @return Tag|null |
||
62 | */ |
||
63 | public static function firstOrCreateEncrypted(array $fields) |
||
64 | { |
||
65 | // everything but the tag: |
||
66 | unset($fields['tagMode']); |
||
67 | $search = $fields; |
||
68 | unset($search['tag']); |
||
69 | |||
70 | $query = self::orderBy('id'); |
||
71 | foreach ($search as $name => $value) { |
||
72 | $query->where($name, $value); |
||
73 | } |
||
74 | $set = $query->get(['tags.*']); |
||
75 | /** @var Tag $tag */ |
||
76 | foreach ($set as $tag) { |
||
77 | if ($tag->tag === $fields['tag']) { |
||
78 | return $tag; |
||
79 | } |
||
80 | } |
||
81 | // create it! |
||
82 | $fields['tagMode'] = 'nothing'; |
||
83 | $fields['description'] = $fields['description'] ?? ''; |
||
84 | $tag = self::create($fields); |
||
85 | |||
86 | return $tag; |
||
87 | } |
||
88 | |||
89 | /** |
||
90 | * @param string $value |
||
91 | * |
||
92 | * @return Tag |
||
93 | * @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException |
||
94 | */ |
||
95 | public static function routeBinder(string $value): Tag |
||
96 | { |
||
97 | if (auth()->check()) { |
||
98 | $tagId = intval($value); |
||
99 | $tag = auth()->user()->tags()->find($tagId); |
||
100 | if (!is_null($tag)) { |
||
101 | return $tag; |
||
102 | } |
||
103 | } |
||
104 | throw new NotFoundHttpException; |
||
105 | } |
||
106 | |||
107 | /** |
||
108 | * @codeCoverageIgnore |
||
109 | * |
||
110 | * @param $value |
||
111 | * |
||
112 | * @return string |
||
113 | * @throws \Illuminate\Contracts\Encryption\DecryptException |
||
114 | */ |
||
115 | public function getDescriptionAttribute($value) |
||
116 | { |
||
117 | if (null === $value) { |
||
118 | return $value; |
||
119 | } |
||
120 | |||
121 | return Crypt::decrypt($value); |
||
122 | } |
||
123 | |||
124 | /** |
||
125 | * @codeCoverageIgnore |
||
126 | * |
||
127 | * @param $value |
||
128 | * |
||
129 | * @return string |
||
130 | * @throws \Illuminate\Contracts\Encryption\DecryptException |
||
131 | */ |
||
132 | public function getTagAttribute($value) |
||
133 | { |
||
134 | if (null === $value) { |
||
135 | return null; |
||
136 | } |
||
137 | |||
138 | return Crypt::decrypt($value); |
||
139 | } |
||
140 | |||
141 | /** |
||
142 | * @codeCoverageIgnore |
||
143 | * |
||
144 | * @param $value |
||
145 | * |
||
146 | * @throws \Illuminate\Contracts\Encryption\EncryptException |
||
147 | */ |
||
148 | public function setDescriptionAttribute($value) |
||
149 | { |
||
150 | $this->attributes['description'] = Crypt::encrypt($value); |
||
151 | } |
||
152 | |||
153 | /** |
||
154 | * @codeCoverageIgnore |
||
155 | * |
||
156 | * @param $value |
||
157 | * @throws \Illuminate\Contracts\Encryption\EncryptException |
||
158 | */ |
||
159 | public function setTagAttribute($value) |
||
160 | { |
||
161 | $this->attributes['tag'] = Crypt::encrypt($value); |
||
162 | } |
||
163 | |||
164 | /** |
||
165 | * @codeCoverageIgnore |
||
166 | * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany |
||
167 | */ |
||
168 | public function transactionJournals() |
||
169 | { |
||
170 | return $this->belongsToMany('FireflyIII\Models\TransactionJournal'); |
||
171 | } |
||
172 | |||
173 | /** |
||
174 | * @codeCoverageIgnore |
||
175 | * @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
||
176 | */ |
||
177 | public function user() |
||
178 | { |
||
179 | return $this->belongsTo('FireflyIII\User'); |
||
180 | } |
||
181 | } |
||
182 |