1 | <?php |
||
29 | trait CrudTrait |
||
30 | { |
||
31 | /** |
||
32 | * Set the issue is updated by a user. |
||
33 | * |
||
34 | * @param int $userId |
||
35 | * |
||
36 | * @return Eloquent\Model |
||
37 | */ |
||
38 | 10 | public function changeUpdatedBy($userId) |
|
39 | { |
||
40 | 10 | $this->updated_by = $userId; |
|
|
|||
41 | 10 | $this->touch(); |
|
42 | |||
43 | 10 | return $this->save(); |
|
44 | } |
||
45 | |||
46 | /** |
||
47 | * Reassign the issue to a new user. |
||
48 | * |
||
49 | * @param int|User $assignTo |
||
50 | * @param User $user |
||
51 | * |
||
52 | * @return Eloquent\Model |
||
53 | */ |
||
54 | 3 | public function reassign($assignTo, User $user) |
|
55 | { |
||
56 | 3 | $assignToId = !$assignTo instanceof User ? $assignTo : $assignTo->id; |
|
57 | 3 | $this->assigned_to = $assignToId; |
|
58 | |||
59 | // Add event on successful save |
||
60 | static::saved(function (Project\Issue $issue) use ($user) { |
||
61 | 3 | $this->queueAssign($issue, $user); |
|
62 | 3 | }); |
|
63 | |||
64 | 3 | $this->save(); |
|
65 | |||
66 | 3 | return $this->activities()->save(new User\Activity([ |
|
67 | 3 | 'type_id' => Activity::TYPE_REASSIGN_ISSUE, |
|
68 | 3 | 'parent_id' => $this->project->id, |
|
69 | 3 | 'user_id' => $user->id, |
|
70 | 3 | 'action_id' => $this->assigned_to, |
|
71 | ])); |
||
72 | } |
||
73 | |||
74 | /** |
||
75 | * Update the given issue. |
||
76 | * |
||
77 | * @param array $input |
||
78 | * |
||
79 | * @return Eloquent\Model |
||
80 | */ |
||
81 | 6 | public function updateIssue(array $input) |
|
82 | { |
||
83 | 6 | $fill = array_only($input, [ |
|
84 | 6 | 'title', 'body', 'assigned_to', |
|
85 | ]); |
||
86 | 6 | $fill['updated_by'] = $this->updatedBy->id; |
|
87 | |||
88 | 6 | if (isset($input['time_quote']['lock'])) { |
|
89 | $fill['lock_quote'] = $input['time_quote']['lock']; |
||
90 | } |
||
91 | |||
92 | // Only save quote if not locked or locked & user allowed to modify it |
||
93 | 6 | if (array_key_exists('time_quote', $input) && |
|
94 | 6 | (!$this->isQuoteLocked() || $this->user->permission(Model\Permission::PERM_ISSUE_LOCK_QUOTE)) |
|
95 | ) { |
||
96 | 6 | $fill['time_quote'] = $input['time_quote']; |
|
97 | } |
||
98 | |||
99 | /* Add to activity log for assignment if changed */ |
||
100 | 6 | $assignToId = (int) array_get($input, 'assigned_to'); |
|
101 | 6 | if ($assignToId > 0 && $assignToId !== (int) $this->assigned_to) { |
|
102 | 1 | $this->activities()->save(new User\Activity([ |
|
103 | 1 | 'type_id' => Activity::TYPE_REASSIGN_ISSUE, |
|
104 | 1 | 'parent_id' => $this->project->id, |
|
105 | 1 | 'user_id' => $this->updatedBy->id, |
|
106 | 1 | 'action_id' => $this->assigned_to, |
|
107 | ])); |
||
108 | } |
||
109 | |||
110 | 6 | $this->fill($fill); |
|
111 | |||
112 | 6 | $this->syncTags($input, $this->tags()->with('parent')->get()); |
|
113 | |||
114 | // Add event on successful save |
||
115 | static::saved(function (Project\Issue $issue) { |
||
116 | 6 | $this->queueUpdate($issue, $issue->updatedBy); |
|
117 | 6 | }); |
|
118 | |||
119 | 6 | return $this->save(); |
|
120 | } |
||
121 | |||
122 | /** |
||
123 | * Create a new issue. |
||
124 | * |
||
125 | * @param array $input |
||
126 | * |
||
127 | * @return static |
||
128 | */ |
||
129 | 37 | public function createIssue(array $input) |
|
130 | { |
||
131 | $fill = [ |
||
132 | 37 | 'created_by' => $this->user->id, |
|
133 | 37 | 'project_id' => $this->project->id, |
|
134 | 37 | 'title' => $input['title'], |
|
135 | 37 | 'body' => $input['body'], |
|
136 | ]; |
||
137 | |||
138 | 37 | if ($this->user->permission('issue-modify')) { |
|
139 | 36 | $fill['assigned_to'] = $input['assigned_to']; |
|
140 | 36 | $fill['time_quote'] = $input['time_quote']; |
|
141 | } |
||
142 | |||
143 | 37 | $this->fill($fill)->save(); |
|
144 | |||
145 | // Add issue to messages queue |
||
146 | 37 | $this->queueAdd($this, $this->user); |
|
147 | |||
148 | /* Add to user's activity log */ |
||
149 | 37 | $this->activities()->save(new User\Activity([ |
|
150 | 37 | 'type_id' => Activity::TYPE_CREATE_ISSUE, |
|
151 | 37 | 'parent_id' => $this->project->id, |
|
152 | 37 | 'user_id' => $this->user->id, |
|
153 | ])); |
||
154 | |||
155 | /* Add attachments to issue */ |
||
156 | 37 | Attachment::where('upload_token', '=', $input['upload_token']) |
|
157 | 37 | ->where('uploaded_by', '=', $this->user->id) |
|
158 | 37 | ->update(['issue_id' => $this->id]); |
|
159 | |||
160 | // Add default tag to newly created issue |
||
161 | 37 | $defaultTag = app('tinyissue.settings')->getFirstStatusTagId(); |
|
162 | 37 | if ($defaultTag > 0 && empty($input['tag_status'])) { |
|
163 | $input['tag_status'] = $defaultTag; |
||
164 | } |
||
165 | |||
166 | 37 | $this->syncTags($input); |
|
167 | |||
168 | 37 | return $this; |
|
169 | } |
||
170 | |||
171 | /** |
||
172 | * Move the issue (comments & activities) to another project. |
||
173 | * |
||
174 | * @param int $projectId |
||
175 | * |
||
176 | * @return $this |
||
177 | */ |
||
178 | 1 | public function changeProject($projectId) |
|
179 | { |
||
180 | 1 | $this->project_id = $projectId; |
|
181 | 1 | $this->save(); |
|
182 | 1 | $comments = $this->comments()->get(); |
|
183 | 1 | foreach ($comments as $comment) { |
|
184 | 1 | $comment->project_id = $projectId; |
|
185 | 1 | $comment->save(); |
|
186 | } |
||
187 | |||
188 | 1 | $activities = $this->activities()->get(); |
|
189 | 1 | foreach ($activities as $activity) { |
|
190 | 1 | $activity->parent_id = $projectId; |
|
191 | 1 | $activity->save(); |
|
192 | } |
||
193 | |||
194 | 1 | return $this; |
|
195 | } |
||
196 | |||
197 | /** |
||
198 | * Delete an issue. |
||
199 | * |
||
200 | * @return bool |
||
201 | * |
||
202 | * @throws \Exception |
||
203 | */ |
||
204 | public function delete() |
||
205 | { |
||
206 | $id = $this->id; |
||
207 | $projectId = $this->project_id; |
||
208 | $comments = $this->comments; |
||
209 | $attachments = $this->attachments; |
||
210 | |||
211 | $status = parent::delete(); |
||
212 | |||
213 | if ($status) { |
||
214 | $attachments->each(function (Attachment $attachment) use ($projectId) { |
||
215 | $path = config('filesystems.disks.local.root') |
||
216 | . '/' . config('tinyissue.uploads_dir') |
||
217 | . '/' . $projectId |
||
218 | . '/' . $attachment->upload_token; |
||
219 | $attachment->deleteFile($path, $attachment->filename); |
||
220 | $attachment->delete(); |
||
221 | }); |
||
222 | $comments->each(function (Project\Issue\Comment $comment) { |
||
223 | $comment->deleteComment($this->getLoggedUser()->user()); |
||
224 | }); |
||
225 | User\Activity::where('parent_id', '=', $projectId)->where('item_id', '=', $id)->delete(); |
||
226 | \DB::table('projects_issues_tags')->where('issue_id', '=', $id)->delete(); |
||
227 | } |
||
228 | |||
229 | return $status; |
||
230 | } |
||
231 | |||
232 | abstract public function touch(); |
||
233 | abstract public function getLoggedUser(); |
||
240 | abstract public function tags(); |
||
241 | abstract public function isQuoteLocked(); |
||
242 | abstract public function fill(array $attributes); |
||
243 | abstract public function syncTags(array $input, Collection $currentTags = null); |
||
244 | } |
||
245 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: