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