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
|
8 |
|
public function changeUpdatedBy($userId) |
59
|
|
|
{ |
60
|
8 |
|
$this->updated_by = $userId; |
61
|
8 |
|
$this->touch(); |
|
|
|
|
62
|
|
|
|
63
|
8 |
|
return $this->save(); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Reassign the issue to a new user. |
68
|
|
|
* |
69
|
|
|
* @param int|User $assignTo |
70
|
|
|
* @param int|User $user |
71
|
|
|
* |
72
|
|
|
* @return Eloquent\Model |
73
|
|
|
*/ |
74
|
3 |
|
public function reassign($assignTo, $user) |
75
|
1 |
|
{ |
76
|
3 |
|
$assignToId = !$assignTo instanceof User ? $assignTo : $assignTo->id; |
77
|
3 |
|
$userId = !$user instanceof User ? $user : $user->id; |
78
|
3 |
|
$this->assigned_to = $assignToId; |
79
|
|
|
|
80
|
|
|
// Add event on successful save |
81
|
|
|
static::saved(function(Project\Issue $issue) use ($userId) { |
82
|
3 |
|
$this->queueAssign($issue, $userId); |
|
|
|
|
83
|
3 |
|
}); |
84
|
|
|
|
85
|
3 |
|
$this->save(); |
86
|
|
|
|
87
|
3 |
|
return $this->activities()->save(new User\Activity([ |
88
|
3 |
|
'type_id' => Activity::TYPE_REASSIGN_ISSUE, |
89
|
3 |
|
'parent_id' => $this->project->id, |
90
|
3 |
|
'user_id' => $userId, |
91
|
3 |
|
'action_id' => $this->assigned_to, |
92
|
3 |
|
])); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Update the given issue. |
97
|
|
|
* |
98
|
|
|
* @param array $input |
99
|
|
|
* |
100
|
|
|
* @return Eloquent\Model |
101
|
|
|
*/ |
102
|
1 |
|
public function updateIssue(array $input) |
103
|
|
|
{ |
104
|
|
|
$fill = [ |
105
|
1 |
|
'title' => $input['title'], |
106
|
1 |
|
'body' => $input['body'], |
107
|
1 |
|
'assigned_to' => $input['assigned_to'], |
108
|
1 |
|
'time_quote' => $input['time_quote'], |
109
|
1 |
|
'updated_by' => $this->updatedBy->id, |
110
|
1 |
|
]; |
111
|
|
|
|
112
|
|
|
/* Add to activity log for assignment if changed */ |
113
|
1 |
|
if ($input['assigned_to'] != $this->assigned_to) { |
114
|
1 |
|
$this->activities()->save(new User\Activity([ |
115
|
1 |
|
'type_id' => Activity::TYPE_REASSIGN_ISSUE, |
116
|
1 |
|
'parent_id' => $this->project->id, |
117
|
1 |
|
'user_id' => $this->updatedBy->id, |
118
|
1 |
|
'action_id' => $this->assigned_to, |
119
|
1 |
|
])); |
120
|
1 |
|
} |
121
|
|
|
|
122
|
1 |
|
$this->fill($fill); |
123
|
|
|
|
124
|
1 |
|
$this->syncTags($input, $this->tags()->with('parent')->get()); |
|
|
|
|
125
|
|
|
|
126
|
|
|
// Add event on successful save |
127
|
1 |
|
static::saved(function(Project\Issue $issue) { |
128
|
1 |
|
$this->queueUpdate($issue, $issue->updatedBy); |
|
|
|
|
129
|
1 |
|
}); |
130
|
|
|
|
131
|
1 |
|
return $this->save(); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Create a new issue. |
136
|
|
|
* |
137
|
|
|
* @param array $input |
138
|
|
|
* |
139
|
|
|
* @return CrudTrait |
|
|
|
|
140
|
|
|
*/ |
141
|
29 |
|
public function createIssue(array $input) |
142
|
2 |
|
{ |
143
|
|
|
$fill = [ |
144
|
29 |
|
'created_by' => $this->user->id, |
145
|
29 |
|
'project_id' => $this->project->id, |
146
|
29 |
|
'title' => $input['title'], |
147
|
29 |
|
'body' => $input['body'], |
148
|
29 |
|
]; |
149
|
|
|
|
150
|
29 |
|
if ($this->user->permission('issue-modify')) { |
151
|
28 |
|
$fill['assigned_to'] = $input['assigned_to']; |
152
|
28 |
|
$fill['time_quote'] = $input['time_quote']; |
153
|
28 |
|
} |
154
|
|
|
|
155
|
29 |
|
$this->fill($fill)->save(); |
156
|
|
|
|
157
|
|
|
// Add issue to messages queue |
158
|
29 |
|
$this->queueAdd($this, $this->user); |
|
|
|
|
159
|
|
|
|
160
|
|
|
/* Add to user's activity log */ |
161
|
29 |
|
$this->activities()->save(new User\Activity([ |
162
|
29 |
|
'type_id' => Activity::TYPE_CREATE_ISSUE, |
163
|
29 |
|
'parent_id' => $this->project->id, |
164
|
29 |
|
'user_id' => $this->user->id, |
165
|
29 |
|
])); |
166
|
|
|
|
167
|
|
|
/* Add attachments to issue */ |
168
|
29 |
|
Attachment::where('upload_token', '=', $input['upload_token']) |
|
|
|
|
169
|
29 |
|
->where('uploaded_by', '=', $this->user->id) |
170
|
29 |
|
->update(['issue_id' => $this->id]); |
171
|
|
|
|
172
|
|
|
// Add default tag to newly created issue |
173
|
29 |
|
$defaultTag = app('tinyissue.settings')->getFirstStatusTagId(); |
174
|
29 |
|
if ($defaultTag > 0 && empty($input['tag_status'])) { |
175
|
|
|
$input['tag_status'] = $defaultTag; |
176
|
|
|
} |
177
|
|
|
|
178
|
29 |
|
$this->syncTags($input); |
|
|
|
|
179
|
|
|
|
180
|
29 |
|
return $this; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* Move the issue (comments & activities) to another project. |
185
|
|
|
* |
186
|
|
|
* @param int $projectId |
187
|
|
|
* |
188
|
|
|
* @return $this |
189
|
|
|
*/ |
190
|
29 |
|
public function changeProject($projectId) |
191
|
|
|
{ |
192
|
1 |
|
$this->project_id = $projectId; |
193
|
1 |
|
$this->save(); |
194
|
1 |
|
$comments = $this->comments()->get(); |
195
|
1 |
|
foreach ($comments as $comment) { |
196
|
29 |
|
$comment->project_id = $projectId; |
197
|
29 |
|
$comment->save(); |
198
|
1 |
|
} |
199
|
|
|
|
200
|
1 |
|
$activities = $this->activities()->get(); |
201
|
1 |
|
foreach ($activities as $activity) { |
202
|
1 |
|
$activity->parent_id = $projectId; |
203
|
1 |
|
$activity->save(); |
204
|
1 |
|
} |
205
|
|
|
|
206
|
1 |
|
return $this; |
207
|
|
|
} |
208
|
|
|
} |
209
|
|
|
|
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.