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
|
4 |
|
public function updateIssue(array $input) |
102
|
|
|
{ |
103
|
|
|
$fill = [ |
104
|
4 |
|
'title' => $input['title'], |
105
|
4 |
|
'body' => $input['body'], |
106
|
4 |
|
'assigned_to' => $input['assigned_to'], |
107
|
4 |
|
'time_quote' => $input['time_quote'], |
108
|
4 |
|
'updated_by' => $this->updatedBy->id, |
109
|
|
|
]; |
110
|
|
|
|
111
|
|
|
/* Add to activity log for assignment if changed */ |
112
|
4 |
|
if ($input['assigned_to'] != $this->assigned_to) { |
113
|
1 |
|
$this->activities()->save(new User\Activity([ |
114
|
1 |
|
'type_id' => Activity::TYPE_REASSIGN_ISSUE, |
115
|
1 |
|
'parent_id' => $this->project->id, |
116
|
1 |
|
'user_id' => $this->updatedBy->id, |
117
|
1 |
|
'action_id' => $this->assigned_to, |
118
|
|
|
])); |
119
|
|
|
} |
120
|
|
|
|
121
|
4 |
|
$this->fill($fill); |
122
|
|
|
|
123
|
4 |
|
$this->syncTags($input, $this->tags()->with('parent')->get()); |
|
|
|
|
124
|
|
|
|
125
|
|
|
// Add event on successful save |
126
|
4 |
|
static::saved(function (Project\Issue $issue) { |
127
|
4 |
|
$this->queueUpdate($issue, $issue->updatedBy); |
|
|
|
|
128
|
4 |
|
}); |
129
|
|
|
|
130
|
4 |
|
return $this->save(); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Create a new issue. |
135
|
|
|
* |
136
|
|
|
* @param array $input |
137
|
|
|
* |
138
|
|
|
* @return CrudTrait |
|
|
|
|
139
|
|
|
*/ |
140
|
32 |
|
public function createIssue(array $input) |
141
|
|
|
{ |
142
|
|
|
$fill = [ |
143
|
32 |
|
'created_by' => $this->user->id, |
144
|
32 |
|
'project_id' => $this->project->id, |
145
|
32 |
|
'title' => $input['title'], |
146
|
32 |
|
'body' => $input['body'], |
147
|
|
|
]; |
148
|
|
|
|
149
|
32 |
|
if ($this->user->permission('issue-modify')) { |
150
|
31 |
|
$fill['assigned_to'] = $input['assigned_to']; |
151
|
31 |
|
$fill['time_quote'] = $input['time_quote']; |
152
|
|
|
} |
153
|
|
|
|
154
|
32 |
|
$this->fill($fill)->save(); |
155
|
|
|
|
156
|
|
|
// Add issue to messages queue |
157
|
32 |
|
$this->queueAdd($this, $this->user); |
|
|
|
|
158
|
|
|
|
159
|
|
|
/* Add to user's activity log */ |
160
|
32 |
|
$this->activities()->save(new User\Activity([ |
161
|
32 |
|
'type_id' => Activity::TYPE_CREATE_ISSUE, |
162
|
32 |
|
'parent_id' => $this->project->id, |
163
|
32 |
|
'user_id' => $this->user->id, |
164
|
|
|
])); |
165
|
|
|
|
166
|
|
|
/* Add attachments to issue */ |
167
|
32 |
|
Attachment::where('upload_token', '=', $input['upload_token']) |
|
|
|
|
168
|
32 |
|
->where('uploaded_by', '=', $this->user->id) |
169
|
32 |
|
->update(['issue_id' => $this->id]); |
170
|
|
|
|
171
|
|
|
// Add default tag to newly created issue |
172
|
32 |
|
$defaultTag = app('tinyissue.settings')->getFirstStatusTagId(); |
173
|
32 |
|
if ($defaultTag > 0 && empty($input['tag_status'])) { |
174
|
|
|
$input['tag_status'] = $defaultTag; |
175
|
|
|
} |
176
|
|
|
|
177
|
32 |
|
$this->syncTags($input); |
|
|
|
|
178
|
|
|
|
179
|
32 |
|
return $this; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
/** |
183
|
|
|
* Move the issue (comments & activities) to another project. |
184
|
|
|
* |
185
|
|
|
* @param int $projectId |
186
|
|
|
* |
187
|
|
|
* @return $this |
188
|
|
|
*/ |
189
|
1 |
|
public function changeProject($projectId) |
190
|
|
|
{ |
191
|
1 |
|
$this->project_id = $projectId; |
192
|
1 |
|
$this->save(); |
193
|
1 |
|
$comments = $this->comments()->get(); |
194
|
1 |
|
foreach ($comments as $comment) { |
195
|
1 |
|
$comment->project_id = $projectId; |
196
|
1 |
|
$comment->save(); |
197
|
|
|
} |
198
|
|
|
|
199
|
1 |
|
$activities = $this->activities()->get(); |
200
|
1 |
|
foreach ($activities as $activity) { |
201
|
1 |
|
$activity->parent_id = $projectId; |
202
|
1 |
|
$activity->save(); |
203
|
|
|
} |
204
|
|
|
|
205
|
1 |
|
return $this; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* Delete an issue. |
210
|
|
|
* |
211
|
|
|
* @return bool |
212
|
|
|
* |
213
|
|
|
* @throws \Exception |
214
|
|
|
*/ |
215
|
|
|
public function delete() |
216
|
|
|
{ |
217
|
|
|
$id = $this->id; |
218
|
|
|
$projectId = $this->project_id; |
219
|
|
|
$comments = $this->comments; |
|
|
|
|
220
|
|
|
$attachments = $this->attachments; |
|
|
|
|
221
|
|
|
|
222
|
|
|
$status = parent::delete(); |
223
|
|
|
|
224
|
|
|
if ($status) { |
225
|
|
|
$attachments->each(function (Attachment $attachment) use ($projectId) { |
226
|
|
|
$path = config('filesystems.disks.local.root') |
227
|
|
|
. '/' . config('tinyissue.uploads_dir') |
228
|
|
|
. '/' . $projectId |
229
|
|
|
. '/' . $attachment->upload_token; |
230
|
|
|
$attachment->deleteFile($path, $attachment->filename); |
231
|
|
|
$attachment->delete(); |
232
|
|
|
}); |
233
|
|
|
$comments->each(function (Project\Issue\Comment $comment) { |
234
|
|
|
$comment->deleteComment(auth()->user()); |
|
|
|
|
235
|
|
|
}); |
236
|
|
|
User\Activity::where('parent_id', '=', $projectId)->where('item_id', '=', $id)->delete(); |
237
|
|
|
\DB::table('projects_issues_tags')->where('issue_id', '=', $id)->delete(); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
return $status; |
241
|
|
|
} |
242
|
|
|
} |
243
|
|
|
|
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
Idable
provides a methodequalsId
that 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.