1 | <?php |
||
29 | trait CrudTagTrait |
||
30 | { |
||
31 | /** |
||
32 | * Change the status of an issue. |
||
33 | * |
||
34 | * @param int $status |
||
35 | * @param User $user |
||
36 | * |
||
37 | * @return Eloquent\Model |
||
38 | */ |
||
39 | 7 | public function changeStatus($status, User $user) |
|
40 | { |
||
41 | 7 | if ($status == 0) { |
|
42 | 7 | $this->closed_by = $user->id; |
|
|
|||
43 | 7 | $this->closed_at = (new \DateTime())->format('Y-m-d H:i:s'); |
|
44 | 7 | $activityType = Activity::TYPE_CLOSE_ISSUE; |
|
45 | } else { |
||
46 | 2 | $this->closed_by = 0; |
|
47 | 2 | $this->closed_at = null; |
|
48 | 2 | $activityType = Activity::TYPE_REOPEN_ISSUE; |
|
49 | } |
||
50 | |||
51 | /* Add to activity log */ |
||
52 | 7 | $this->activities()->save(new User\Activity([ |
|
53 | 7 | 'type_id' => $activityType, |
|
54 | 7 | 'parent_id' => $this->project->id, |
|
55 | 7 | 'user_id' => $user->id, |
|
56 | ])); |
||
57 | |||
58 | 7 | $this->status = $status; |
|
59 | |||
60 | // Add event on successful save |
||
61 | static::saved(function (Project\Issue $issue) use ($user) { |
||
62 | 7 | $this->queueUpdate($issue, $user); |
|
63 | 7 | }); |
|
64 | |||
65 | 7 | return $this->save(); |
|
66 | } |
||
67 | |||
68 | /** |
||
69 | * Sync the issue tags. |
||
70 | * |
||
71 | * @param array $input |
||
72 | * @param Collection $currentTags |
||
73 | * |
||
74 | * @return bool |
||
75 | */ |
||
76 | 37 | public function syncTags(array $input, Collection $currentTags = null) |
|
77 | { |
||
78 | 37 | $tagIds = array_only($input, [ |
|
79 | 37 | 'tag_type', 'tag_status', 'tag_resolution', |
|
80 | ]); |
||
81 | |||
82 | // User can edit their own role and can only change issue type |
||
83 | 37 | if ($this->updatedBy instanceof User && $this->updatedBy->isUser()) { |
|
84 | $currentTagIds = $currentTags->pluck('id', 'parent.name')->toArray(); |
||
85 | $tagIds['tag_status'] = array_key_exists('status', $currentTagIds)? $currentTagIds['status'] : 0; |
||
86 | $tagIds['tag_resolution'] = array_key_exists('resolution', $currentTagIds)? $currentTagIds['resolution'] : 0; |
||
87 | } |
||
88 | |||
89 | 37 | $tags = (new Tag())->whereIn('id', $tagIds)->get(); |
|
90 | |||
91 | 37 | $removedTags = []; |
|
92 | 37 | if (null === $currentTags) { |
|
93 | // Add the following tags except for open status |
||
94 | $addedTags = $tags |
||
95 | ->map(function (Tag $tag) { |
||
96 | 2 | return $tag->toShortArray(); |
|
97 | 37 | }) |
|
98 | 37 | ->toArray(); |
|
99 | } else { |
||
100 | // Tags remove from the issue |
||
101 | $removedTags = $currentTags |
||
102 | 6 | ->diff($tags) |
|
103 | ->map(function (Tag $tag) { |
||
104 | 2 | return $tag->toShortArray(); |
|
105 | 6 | }) |
|
106 | 6 | ->toArray(); |
|
107 | |||
108 | // Check if we are adding new tags |
||
109 | $addedTags = $tags |
||
110 | ->filter(function (Tag $tag) use ($currentTags) { |
||
111 | 5 | return $currentTags->where('id', $tag->id)->count() === 0; |
|
112 | 6 | }) |
|
113 | 6 | ->map(function (Tag $tag) { |
|
114 | 5 | return $tag->toShortArray(); |
|
115 | 6 | }) |
|
116 | 6 | ->toArray(); |
|
117 | |||
118 | // No new tags to add or remove |
||
119 | 6 | if (empty($removedTags) && empty($addedTags)) { |
|
120 | 1 | return true; |
|
121 | } |
||
122 | } |
||
123 | |||
124 | // Save relation |
||
125 | 37 | $this->tags()->sync($tags->lists('id')->all()); |
|
126 | |||
127 | // Activity is added when new issue create with tags or updated with tags excluding the open status tag |
||
128 | 37 | if (!empty($removedTags) || !empty($addedTags)) { |
|
129 | // Add this change to messages queue |
||
130 | 7 | $this->queueChangeTags($this, $addedTags, $removedTags, $this->user); |
|
131 | |||
132 | // Add to activity log for tags if changed |
||
133 | 7 | $this->activities()->save(new User\Activity([ |
|
134 | 7 | 'type_id' => Activity::TYPE_ISSUE_TAG, |
|
135 | 7 | 'parent_id' => $this->project->id, |
|
136 | 7 | 'user_id' => $this->user->id, |
|
137 | 7 | 'data' => ['added_tags' => $addedTags, 'removed_tags' => $removedTags], |
|
138 | ])); |
||
139 | } |
||
140 | |||
141 | 37 | return true; |
|
142 | } |
||
143 | |||
144 | /** |
||
145 | * Add tag to the issue & close issue if added tag is Closed. |
||
146 | * |
||
147 | * @param Tag $newTag |
||
148 | * @param Tag $oldTag |
||
149 | * |
||
150 | * @return $this |
||
151 | */ |
||
152 | 1 | public function changeKanbanTag(Tag $newTag, Tag $oldTag) |
|
153 | { |
||
154 | // skip if there is no change in status tags |
||
155 | 1 | if ($oldTag->name === $newTag->name) { |
|
156 | return $this; |
||
157 | } |
||
158 | |||
159 | // Open issue |
||
160 | 1 | $data = ['added_tags' => [], 'removed_tags' => []]; |
|
161 | |||
162 | // Remove previous status tag |
||
163 | 1 | $this->tags()->detach($oldTag); |
|
164 | 1 | $data['removed_tags'][] = $oldTag->toShortArray(); |
|
165 | |||
166 | // Add new tag |
||
167 | 1 | if (!$this->tags->contains($newTag)) { |
|
168 | 1 | $this->tags()->attach($newTag); |
|
169 | |||
170 | 1 | $data['added_tags'][] = $newTag->toShortArray(); |
|
171 | } |
||
172 | |||
173 | 1 | if (!empty($data)) { |
|
174 | // Add this change to messages queue |
||
175 | 1 | $this->queueChangeTags($this, $data['added_tags'], $data['removed_tags'], $this->user); |
|
176 | |||
177 | // Add to activity log for tags if changed |
||
178 | 1 | $this->activities()->save(new User\Activity([ |
|
179 | 1 | 'type_id' => Activity::TYPE_ISSUE_TAG, |
|
180 | 1 | 'parent_id' => $this->project->id, |
|
181 | 1 | 'user_id' => $this->user->id, |
|
182 | 1 | 'data' => $data, |
|
183 | ])); |
||
184 | } |
||
185 | |||
186 | 1 | return $this; |
|
187 | } |
||
188 | |||
189 | abstract public function activities(); |
||
192 | abstract public function save(array $options = []); |
||
193 | abstract public function tags(); |
||
194 | } |
||
195 |
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: