|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Tinyissue package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Mohamed Alsharaf <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Tinyissue\Model\Traits\Project\Issue; |
|
13
|
|
|
|
|
14
|
|
|
use Illuminate\Database\Eloquent; |
|
15
|
|
|
use Illuminate\Database\Eloquent\Relations; |
|
16
|
|
|
use Tinyissue\Model; |
|
17
|
|
|
use Tinyissue\Model\Activity; |
|
18
|
|
|
use Tinyissue\Model\Project; |
|
19
|
|
|
use Tinyissue\Model\Project\Issue\Attachment; |
|
20
|
|
|
use Tinyissue\Model\User; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* CrudTrait is trait class containing the methods for adding/editing/deleting the Project\Issue model. |
|
24
|
|
|
* |
|
25
|
|
|
* @author Mohamed Alsharaf <[email protected]> |
|
26
|
|
|
* |
|
27
|
|
|
* @property int $id |
|
28
|
|
|
* @property int $created_by |
|
29
|
|
|
* @property int $project_id |
|
30
|
|
|
* @property string $title |
|
31
|
|
|
* @property string $body |
|
32
|
|
|
* @property int $assigned_to |
|
33
|
|
|
* @property int $time_quote |
|
34
|
|
|
* @property int $closed_by |
|
35
|
|
|
* @property int $closed_at |
|
36
|
|
|
* @property int status |
|
37
|
|
|
* @property int $updated_at |
|
38
|
|
|
* @property int $updated_by |
|
39
|
|
|
* @property Project $project |
|
40
|
|
|
* @property User $user |
|
41
|
|
|
* @property User $updatedBy |
|
42
|
|
|
* |
|
43
|
|
|
* @method Eloquent\Model save() |
|
44
|
|
|
* @method Eloquent\Model fill(array $attributes) |
|
45
|
|
|
* @method Relations\BelongsToMany tags() |
|
46
|
|
|
* @method Relations\HasMany activities() |
|
47
|
|
|
* @method Relations\HasMany comments() |
|
48
|
|
|
*/ |
|
49
|
|
|
trait CrudTrait |
|
50
|
|
|
{ |
|
51
|
|
|
/** |
|
52
|
|
|
* Set the issue is updated by a user. |
|
53
|
|
|
* |
|
54
|
|
|
* @param int $userId |
|
55
|
|
|
* |
|
56
|
|
|
* @return Eloquent\Model |
|
57
|
|
|
*/ |
|
58
|
10 |
|
public function changeUpdatedBy($userId) |
|
59
|
|
|
{ |
|
60
|
10 |
|
$this->updated_by = $userId; |
|
61
|
10 |
|
$this->touch(); |
|
|
|
|
|
|
62
|
|
|
|
|
63
|
10 |
|
return $this->save(); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Reassign the issue to a new user. |
|
68
|
|
|
* |
|
69
|
|
|
* @param int|User $assignTo |
|
70
|
|
|
* @param User $user |
|
71
|
|
|
* |
|
72
|
|
|
* @return Eloquent\Model |
|
73
|
|
|
*/ |
|
74
|
3 |
|
public function reassign($assignTo, User $user) |
|
75
|
|
|
{ |
|
76
|
3 |
|
$assignToId = !$assignTo instanceof User ? $assignTo : $assignTo->id; |
|
77
|
3 |
|
$this->assigned_to = $assignToId; |
|
78
|
|
|
|
|
79
|
|
|
// Add event on successful save |
|
80
|
|
|
static::saved(function (Project\Issue $issue) use ($user) { |
|
81
|
3 |
|
$this->queueAssign($issue, $user); |
|
|
|
|
|
|
82
|
3 |
|
}); |
|
83
|
|
|
|
|
84
|
3 |
|
$this->save(); |
|
85
|
|
|
|
|
86
|
3 |
|
return $this->activities()->save(new User\Activity([ |
|
87
|
3 |
|
'type_id' => Activity::TYPE_REASSIGN_ISSUE, |
|
88
|
3 |
|
'parent_id' => $this->project->id, |
|
89
|
3 |
|
'user_id' => $user->id, |
|
90
|
3 |
|
'action_id' => $this->assigned_to, |
|
91
|
|
|
])); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
/** |
|
95
|
|
|
* Update the given issue. |
|
96
|
|
|
* |
|
97
|
|
|
* @param array $input |
|
98
|
|
|
* |
|
99
|
|
|
* @return Eloquent\Model |
|
100
|
|
|
*/ |
|
101
|
6 |
|
public function updateIssue(array $input) |
|
102
|
|
|
{ |
|
103
|
6 |
|
$fill = array_only($input, [ |
|
104
|
6 |
|
'title', 'body', 'assigned_to', |
|
105
|
|
|
]); |
|
106
|
6 |
|
$fill['updated_by'] = $this->updatedBy->id; |
|
107
|
|
|
|
|
108
|
6 |
|
if (isset($input['time_quote']['lock'])) { |
|
109
|
|
|
$fill['lock_quote'] = $input['time_quote']['lock']; |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
// Only save quote if not locked or locked & user allowed to modify it |
|
113
|
6 |
|
if (array_key_exists('time_quote', $input) && |
|
114
|
6 |
|
(!$this->isQuoteLocked() || $this->user->permission(Model\Permission::PERM_ISSUE_LOCK_QUOTE)) |
|
|
|
|
|
|
115
|
|
|
) { |
|
116
|
6 |
|
$fill['time_quote'] = $input['time_quote']; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/* Add to activity log for assignment if changed */ |
|
120
|
6 |
|
if ($input['assigned_to'] != $this->assigned_to) { |
|
121
|
1 |
|
$this->activities()->save(new User\Activity([ |
|
122
|
1 |
|
'type_id' => Activity::TYPE_REASSIGN_ISSUE, |
|
123
|
1 |
|
'parent_id' => $this->project->id, |
|
124
|
1 |
|
'user_id' => $this->updatedBy->id, |
|
125
|
1 |
|
'action_id' => $this->assigned_to, |
|
126
|
|
|
])); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
6 |
|
$this->fill($fill); |
|
130
|
|
|
|
|
131
|
6 |
|
$this->syncTags($input, $this->tags()->with('parent')->get()); |
|
|
|
|
|
|
132
|
|
|
|
|
133
|
|
|
// Add event on successful save |
|
134
|
|
|
static::saved(function (Project\Issue $issue) { |
|
135
|
6 |
|
$this->queueUpdate($issue, $issue->updatedBy); |
|
|
|
|
|
|
136
|
6 |
|
}); |
|
137
|
|
|
|
|
138
|
6 |
|
return $this->save(); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* Create a new issue. |
|
143
|
|
|
* |
|
144
|
|
|
* @param array $input |
|
145
|
|
|
* |
|
146
|
|
|
* @return CrudTrait |
|
|
|
|
|
|
147
|
|
|
*/ |
|
148
|
37 |
|
public function createIssue(array $input) |
|
149
|
|
|
{ |
|
150
|
|
|
$fill = [ |
|
151
|
37 |
|
'created_by' => $this->user->id, |
|
152
|
37 |
|
'project_id' => $this->project->id, |
|
153
|
37 |
|
'title' => $input['title'], |
|
154
|
37 |
|
'body' => $input['body'], |
|
155
|
|
|
]; |
|
156
|
|
|
|
|
157
|
37 |
|
if ($this->user->permission('issue-modify')) { |
|
158
|
36 |
|
$fill['assigned_to'] = $input['assigned_to']; |
|
159
|
36 |
|
$fill['time_quote'] = $input['time_quote']; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
37 |
|
$this->fill($fill)->save(); |
|
163
|
|
|
|
|
164
|
|
|
// Add issue to messages queue |
|
165
|
37 |
|
$this->queueAdd($this, $this->user); |
|
|
|
|
|
|
166
|
|
|
|
|
167
|
|
|
/* Add to user's activity log */ |
|
168
|
37 |
|
$this->activities()->save(new User\Activity([ |
|
169
|
37 |
|
'type_id' => Activity::TYPE_CREATE_ISSUE, |
|
170
|
37 |
|
'parent_id' => $this->project->id, |
|
171
|
37 |
|
'user_id' => $this->user->id, |
|
172
|
|
|
])); |
|
173
|
|
|
|
|
174
|
|
|
/* Add attachments to issue */ |
|
175
|
37 |
|
Attachment::where('upload_token', '=', $input['upload_token']) |
|
|
|
|
|
|
176
|
37 |
|
->where('uploaded_by', '=', $this->user->id) |
|
177
|
37 |
|
->update(['issue_id' => $this->id]); |
|
178
|
|
|
|
|
179
|
|
|
// Add default tag to newly created issue |
|
180
|
37 |
|
$defaultTag = app('tinyissue.settings')->getFirstStatusTagId(); |
|
181
|
37 |
|
if ($defaultTag > 0 && empty($input['tag_status'])) { |
|
182
|
|
|
$input['tag_status'] = $defaultTag; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
37 |
|
$this->syncTags($input); |
|
|
|
|
|
|
186
|
|
|
|
|
187
|
37 |
|
return $this; |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
/** |
|
191
|
|
|
* Move the issue (comments & activities) to another project. |
|
192
|
|
|
* |
|
193
|
|
|
* @param int $projectId |
|
194
|
|
|
* |
|
195
|
|
|
* @return $this |
|
196
|
|
|
*/ |
|
197
|
1 |
|
public function changeProject($projectId) |
|
198
|
|
|
{ |
|
199
|
1 |
|
$this->project_id = $projectId; |
|
200
|
1 |
|
$this->save(); |
|
201
|
1 |
|
$comments = $this->comments()->get(); |
|
202
|
1 |
|
foreach ($comments as $comment) { |
|
203
|
1 |
|
$comment->project_id = $projectId; |
|
204
|
1 |
|
$comment->save(); |
|
205
|
|
|
} |
|
206
|
|
|
|
|
207
|
1 |
|
$activities = $this->activities()->get(); |
|
208
|
1 |
|
foreach ($activities as $activity) { |
|
209
|
1 |
|
$activity->parent_id = $projectId; |
|
210
|
1 |
|
$activity->save(); |
|
211
|
|
|
} |
|
212
|
|
|
|
|
213
|
1 |
|
return $this; |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
/** |
|
217
|
|
|
* Delete an issue. |
|
218
|
|
|
* |
|
219
|
|
|
* @return bool |
|
220
|
|
|
* |
|
221
|
|
|
* @throws \Exception |
|
222
|
|
|
*/ |
|
223
|
|
|
public function delete() |
|
224
|
|
|
{ |
|
225
|
|
|
$id = $this->id; |
|
226
|
|
|
$projectId = $this->project_id; |
|
227
|
|
|
$comments = $this->comments; |
|
|
|
|
|
|
228
|
|
|
$attachments = $this->attachments; |
|
|
|
|
|
|
229
|
|
|
|
|
230
|
|
|
$status = parent::delete(); |
|
231
|
|
|
|
|
232
|
|
|
if ($status) { |
|
233
|
|
|
$attachments->each(function (Attachment $attachment) use ($projectId) { |
|
234
|
|
|
$path = config('filesystems.disks.local.root') |
|
235
|
|
|
. '/' . config('tinyissue.uploads_dir') |
|
236
|
|
|
. '/' . $projectId |
|
237
|
|
|
. '/' . $attachment->upload_token; |
|
238
|
|
|
$attachment->deleteFile($path, $attachment->filename); |
|
239
|
|
|
$attachment->delete(); |
|
240
|
|
|
}); |
|
241
|
|
|
$comments->each(function (Project\Issue\Comment $comment) { |
|
242
|
|
|
$comment->deleteComment(auth()->user()); |
|
|
|
|
|
|
243
|
|
|
}); |
|
244
|
|
|
User\Activity::where('parent_id', '=', $projectId)->where('item_id', '=', $id)->delete(); |
|
245
|
|
|
\DB::table('projects_issues_tags')->where('issue_id', '=', $id)->delete(); |
|
246
|
|
|
} |
|
247
|
|
|
|
|
248
|
|
|
return $status; |
|
249
|
|
|
} |
|
250
|
|
|
} |
|
251
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idableprovides a methodequalsIdthat in turn relies on the methodgetId(). If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()as an abstract method to the trait will make sure it is available.