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\Repository\Project; |
13
|
|
|
|
14
|
|
|
use Illuminate\Database\Eloquent\Collection; |
15
|
|
|
use Illuminate\Database\Eloquent\Relations\Relation; |
16
|
|
|
use Tinyissue\Contracts\Model\UserInterface; |
17
|
|
|
use Tinyissue\Model\Project; |
18
|
|
|
use Tinyissue\Model\User; |
19
|
|
|
use Tinyissue\Repository\Repository; |
20
|
|
|
|
21
|
|
|
class Fetcher extends Repository |
22
|
|
|
{ |
23
|
|
|
public function __construct(Project $model) |
24
|
|
|
{ |
25
|
|
|
$this->model = $model; |
|
|
|
|
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Returns collection of active projects. |
30
|
|
|
* |
31
|
|
|
* @return Eloquent\Collection |
32
|
|
|
*/ |
33
|
|
|
public function getActiveProjects() |
34
|
|
|
{ |
35
|
|
|
return $this->model->active()->orderBy('name', 'ASC')->get(); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function getNotes() |
39
|
|
|
{ |
40
|
|
|
return $this->model->notes()->with('createdBy')->get(); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Returns collection of public projects. |
45
|
|
|
* |
46
|
|
|
* @return Eloquent\Collection |
47
|
|
|
*/ |
48
|
|
|
public function getPublicProjects() |
49
|
|
|
{ |
50
|
|
|
return $this->model->public()->orderBy('name', 'ASC')->get(); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Returns all users that are not assigned in the current project. |
55
|
|
|
* |
56
|
|
|
* @return array |
57
|
|
|
*/ |
58
|
|
|
public function getNotMembers() |
59
|
|
|
{ |
60
|
|
|
return (new User())->notDeleted()->notMemberOfProject($this->model)->get(); |
|
|
|
|
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Fetch and filter issues in the project. |
65
|
|
|
* |
66
|
|
|
* @param int $status |
67
|
|
|
* @param array $filter |
68
|
|
|
* |
69
|
|
|
* @return \Illuminate\Database\Eloquent\Collection |
70
|
|
|
*/ |
71
|
|
|
public function getIssues($status = Project\Issue::STATUS_OPEN, array $filter = []) |
72
|
|
|
{ |
73
|
|
|
$sortOrder = array_get($filter, 'sort.sortorder', 'desc'); |
74
|
|
|
$sortBy = array_get($filter, 'sort.sortby', null); |
75
|
|
|
|
76
|
|
|
$query = $this->model->issues() |
77
|
|
|
->with('countComments', 'user', 'updatedBy', 'tags', 'tags.parent') |
78
|
|
|
->with([ |
79
|
|
|
'tags' => function (Relation $query) use ($sortOrder) { |
80
|
|
|
$query->orderBy('name', $sortOrder); |
81
|
|
|
}, |
82
|
|
|
]) |
83
|
|
|
->status($status) |
84
|
|
|
->assignedTo(array_get($filter, 'assignto')) |
85
|
|
|
->searchContent(array_get($filter, 'keyword')) |
86
|
|
|
->createdBy(array_get($filter, 'created_by')) |
87
|
|
|
->whereTags(array_get($filter, 'tag_status'), array_get($filter, 'tag_type')); |
88
|
|
|
|
89
|
|
|
// Sort |
90
|
|
View Code Duplication |
if ($sortBy === 'updated') { |
|
|
|
|
91
|
|
|
$this->sortByUpdated($query, $sortOrder); |
92
|
|
|
} elseif (($tagGroup = substr($sortBy, strlen('tag:'))) > 0) { |
93
|
|
|
return $this->sortByTag($query, $tagGroup, $sortOrder); |
|
|
|
|
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return $query->get(); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Fetch and filter issues in the project. |
101
|
|
|
* |
102
|
|
|
* @param int $status |
103
|
|
|
* @param array $filter |
104
|
|
|
* |
105
|
|
|
* @return \Illuminate\Database\Eloquent\Collection |
106
|
|
|
*/ |
107
|
|
|
public function getIssuesForLoggedUser($status = Project\Issue::STATUS_OPEN, array $filter = []) |
108
|
|
|
{ |
109
|
|
|
if ($this->model->isPrivateInternal() && $this->getLoggedUser()->isUser()) { |
110
|
|
|
$request['created_by'] = $this->getLoggedUser()->id; |
|
|
|
|
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return $this->getIssues($status, $filter); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Fetch issues assigned to a user. |
118
|
|
|
* |
119
|
|
|
* @param UserInterface $user |
120
|
|
|
* |
121
|
|
|
* @return \Illuminate\Database\Eloquent\Collection |
122
|
|
|
*/ |
123
|
|
|
public function getAssignedOrCreatedIssues(UserInterface $user) |
124
|
|
|
{ |
125
|
|
|
if ($user->isUser()) { |
126
|
|
|
return $this->getCreatedIssues($user); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
return $this->getAssignedIssues($user); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
public function getCreatedIssues(UserInterface $user) |
133
|
|
|
{ |
134
|
|
|
return $this->model->openIssues() |
135
|
|
|
->with('countComments', 'user', 'updatedBy') |
136
|
|
|
->createdBy($user) |
137
|
|
|
->orderBy('updated_at', 'DESC') |
138
|
|
|
->get(); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
public function getAssignedIssues(UserInterface $user) |
142
|
|
|
{ |
143
|
|
|
return $this->model->openIssues() |
144
|
|
|
->with('countComments', 'user', 'updatedBy') |
145
|
|
|
->assignedTo($user) |
146
|
|
|
->orderBy('updated_at', 'DESC') |
147
|
|
|
->get(); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
public function getRecentActivities(UserInterface $user = null, $limit = 10) |
151
|
|
|
{ |
152
|
|
|
$activities = $this->model->activities() |
153
|
|
|
->with('activity', 'issue', 'user', 'assignTo', 'comment', 'note') |
154
|
|
|
->orderBy('users_activity.created_at', 'DESC') |
155
|
|
|
->take($limit); |
156
|
|
|
|
157
|
|
|
// Internal project and logged user can see created only |
158
|
|
|
if ($this->model->isPrivateInternal() && $user instanceof UserInterface && $user->isUser()) { |
159
|
|
|
$activities->join('projects_issues', 'projects_issues.id', '=', 'item_id'); |
160
|
|
|
$activities->where('created_by', '=', $user->id); |
|
|
|
|
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
return $activities->get(); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Returns projects with issues details eager loaded. |
168
|
|
|
* |
169
|
|
|
* @return Relations\HasMany |
170
|
|
|
*/ |
171
|
|
|
public function getPublicProjectsWithRecentIssues() |
172
|
|
|
{ |
173
|
|
|
return $this->model |
174
|
|
|
->active() |
175
|
|
|
->public() |
176
|
|
|
->with('openIssuesWithUpdater', 'issues.user', 'issues.countComments') |
177
|
|
|
->orderBy('name') |
178
|
|
|
->get(); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Returns collection of tags for Kanban view. |
183
|
|
|
* |
184
|
|
|
* @param UserInterface $user |
185
|
|
|
* |
186
|
|
|
* @return mixed |
187
|
|
|
*/ |
188
|
|
|
public function getKanbanTagsForUser(UserInterface $user) |
189
|
|
|
{ |
190
|
|
|
return $this->model->kanbanTags()->accessibleToUser($user)->get(); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
public function getKanbanTags() |
194
|
|
|
{ |
195
|
|
|
return $this->model->kanbanTags()->get(); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Returns users assigned to the project that can fix issues (with edit permission). |
200
|
|
|
* |
201
|
|
|
* @return Relations\BelongsToMany |
202
|
|
|
*/ |
203
|
|
|
public function getUsersCanFixIssue() |
204
|
|
|
{ |
205
|
|
|
// $r = $this->users()->where('users.role_id', '>', 1)->where('users.deleted', '=', User::NOT_DELETED_USERS);//->get(); |
206
|
|
|
//return $r; |
207
|
|
|
return $this->model->users()->developerOrHigher()->notDeleted()->get(); |
208
|
|
|
//return $r2->get(); |
209
|
|
|
// dd($r->toSql(), $r2->toSql()); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
public function getUsers() |
213
|
|
|
{ |
214
|
|
|
return $this->model->users()->notDeleted()->get(); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* Sort by updated_at column. |
219
|
|
|
* |
220
|
|
|
* @param HasMany $query |
221
|
|
|
* @param string $order |
222
|
|
|
* |
223
|
|
|
* @return void |
224
|
|
|
*/ |
225
|
|
|
protected function sortByUpdated(HasMany $query, $order = 'asc') |
226
|
|
|
{ |
227
|
|
|
$query->orderBy('updated_at', $order); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* Sort by issues tag group |
232
|
|
|
* Note: this sort will return the collection. |
233
|
|
|
* |
234
|
|
|
* @param HasMany $query |
235
|
|
|
* @param string $tagGroup |
236
|
|
|
* @param string $order |
237
|
|
|
* |
238
|
|
|
* @return Eloquent\Collection |
239
|
|
|
*/ |
240
|
|
View Code Duplication |
protected function sortByTag(HasMany $query, $tagGroup, $order = 'asc') |
|
|
|
|
241
|
|
|
{ |
242
|
|
|
// If tag group is string prefixed with tag: |
243
|
|
|
if (!is_numeric($tagGroup)) { |
244
|
|
|
$tagGroup = substr($tagGroup, strlen('tag:')); |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
$results = $query->get() |
248
|
|
|
->sort(function (Project\Issue $issue1, Project\Issue $issue2) use ($tagGroup, $order) { |
249
|
|
|
$tag1 = $issue1->tags->where('parent.id', $tagGroup)->first(); |
250
|
|
|
$tag2 = $issue2->tags->where('parent.id', $tagGroup)->first(); |
251
|
|
|
$tag1 = $tag1 ? $tag1->name : ''; |
252
|
|
|
$tag2 = $tag2 ? $tag2->name : ''; |
253
|
|
|
|
254
|
|
|
if ($order === 'asc') { |
255
|
|
|
return strcmp($tag1, $tag2); |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
return strcmp($tag2, $tag1); |
259
|
|
|
}); |
260
|
|
|
|
261
|
|
|
return $results; |
262
|
|
|
} |
263
|
|
|
|
264
|
|
|
/** |
265
|
|
|
* Returns projects with open issue count. |
266
|
|
|
* |
267
|
|
|
* @param int $status |
268
|
|
|
* @param int $private |
269
|
|
|
* |
270
|
|
|
* @return Collection |
271
|
|
|
*/ |
272
|
|
View Code Duplication |
public function getProjectsWithOpenIssuesCount($status = Project::STATUS_OPEN, $private = Project::PRIVATE_YES) |
|
|
|
|
273
|
|
|
{ |
274
|
|
|
$query = $this->model->with('openIssuesCount')->status($status); |
275
|
|
|
|
276
|
|
|
if ($private !== Project::PRIVATE_ALL) { |
277
|
|
|
$query->where('private', '=', $private); |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
return $query->get(); |
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
/** |
284
|
|
|
* Return projects with count of open & closed issues. |
285
|
|
|
* |
286
|
|
|
* @param array $projectIds |
287
|
|
|
* |
288
|
|
|
* @return Collection |
289
|
|
|
*/ |
290
|
|
|
public function getProjectsWithCountIssues(array $projectIds) |
291
|
|
|
{ |
292
|
|
|
return $this->model |
293
|
|
|
->with('openIssuesCount', 'closedIssuesCount') |
294
|
|
|
->whereIn('id', $projectIds) |
295
|
|
|
->get(); |
296
|
|
|
} |
297
|
|
|
} |
298
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..