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\Http\Controllers; |
13
|
|
|
|
14
|
|
|
use Illuminate\Database\Eloquent\Relations\Relation; |
15
|
|
|
use Illuminate\Http\Request; |
16
|
|
|
use Tinyissue\Form\FilterIssue as FilterForm; |
17
|
|
|
use Tinyissue\Form\Note as NoteForm; |
18
|
|
|
use Tinyissue\Form\Project as Form; |
19
|
|
|
use Tinyissue\Http\Requests\FormRequest; |
20
|
|
|
use Tinyissue\Model\Project; |
21
|
|
|
use Tinyissue\Model\Project\Issue; |
22
|
|
|
use Tinyissue\Model\Project\Note; |
23
|
|
|
use Tinyissue\Services\Exporter; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* ProjectController is the controller class for managing request related to a project. |
27
|
|
|
* |
28
|
|
|
* @author Mohamed Alsharaf <[email protected]> |
29
|
|
|
*/ |
30
|
|
|
class ProjectController extends Controller |
31
|
|
|
{ |
32
|
|
|
/** |
33
|
|
|
* Display activity for a project. |
34
|
|
|
* |
35
|
|
|
* @param Project $project |
36
|
|
|
* |
37
|
|
|
* @return \Illuminate\View\View |
38
|
|
|
*/ |
39
|
12 |
|
public function getIndex(Project $project) |
40
|
|
|
{ |
41
|
12 |
|
$activities = $project->activities() |
42
|
12 |
|
->with('activity', 'issue', 'user', 'assignTo', 'comment', 'note') |
43
|
12 |
|
->orderBy('users_activity.created_at', 'DESC') |
44
|
12 |
|
->take(10); |
45
|
|
|
|
46
|
|
|
// Internal project and logged user can see created only |
47
|
12 |
|
if ($project->isPrivateInternal() && $this->auth->user()->isUser()) { |
|
|
|
|
48
|
|
|
$activities->join('projects_issues', 'projects_issues.id', '=', 'item_id'); |
49
|
|
|
$activities->where('created_by', '=', $this->auth->user()->id); |
|
|
|
|
50
|
|
|
} |
51
|
|
|
|
52
|
12 |
|
return view('project.index', [ |
|
|
|
|
53
|
12 |
|
'tabs' => $this->projectMainViewTabs($project, 'index'), |
54
|
12 |
|
'project' => $project, |
55
|
12 |
|
'active' => 'activity', |
56
|
12 |
|
'activities' => $activities->get(), |
57
|
12 |
|
'sidebar' => 'project', |
58
|
|
|
]); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Display issues for a project. |
63
|
|
|
* |
64
|
|
|
* @param FilterForm $filterForm |
65
|
|
|
* @param Request $request |
66
|
|
|
* @param Project $project |
67
|
|
|
* @param int $status |
68
|
|
|
* |
69
|
|
|
* @return \Illuminate\View\View |
70
|
|
|
*/ |
71
|
2 |
|
public function getIssues(FilterForm $filterForm, Request $request, Project $project, $status = Issue::STATUS_OPEN) |
72
|
|
|
{ |
73
|
2 |
|
if ($project->isPrivateInternal() && $this->auth->user()->isUser()) { |
|
|
|
|
74
|
|
|
$request['created_by'] = $this->auth->user()->id; |
|
|
|
|
75
|
|
|
} |
76
|
2 |
|
$active = $status == Issue::STATUS_OPEN ? 'open_issue' : 'closed_issue'; |
77
|
2 |
|
$issues = $project->listIssues($status, $request->all()); |
78
|
|
|
|
79
|
2 |
|
return view('project.index', [ |
|
|
|
|
80
|
2 |
|
'tabs' => $this->projectMainViewTabs($project, 'issues', $issues, $status), |
|
|
|
|
81
|
2 |
|
'project' => $project, |
82
|
2 |
|
'active' => $active, |
83
|
2 |
|
'issues' => $issues, |
84
|
2 |
|
'sidebar' => 'project', |
85
|
2 |
|
'filterForm' => $filterForm, |
86
|
|
|
]); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Display issues assigned to current user for a project. |
91
|
|
|
* |
92
|
|
|
* @param Project $project |
93
|
|
|
* |
94
|
|
|
* @return \Illuminate\View\View |
95
|
|
|
*/ |
96
|
1 |
View Code Duplication |
public function getAssigned(Project $project) |
|
|
|
|
97
|
|
|
{ |
98
|
1 |
|
$issues = $project->listAssignedOrCreatedIssues($this->auth->user()); |
|
|
|
|
99
|
|
|
|
100
|
1 |
|
return view('project.index', [ |
|
|
|
|
101
|
1 |
|
'tabs' => $this->projectMainViewTabs($project, 'assigned', $issues), |
102
|
1 |
|
'project' => $project, |
103
|
1 |
|
'active' => 'issue_assigned_to_you', |
104
|
1 |
|
'issues' => $issues, |
105
|
1 |
|
'sidebar' => 'project', |
106
|
|
|
]); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Display issues created to current user for a project. |
111
|
|
|
* |
112
|
|
|
* @param Project $project |
113
|
|
|
* |
114
|
|
|
* @return \Illuminate\View\View |
115
|
|
|
*/ |
116
|
|
View Code Duplication |
public function getCreated(Project $project) |
|
|
|
|
117
|
|
|
{ |
118
|
|
|
$issues = $project->listAssignedOrCreatedIssues($this->auth->user()); |
|
|
|
|
119
|
|
|
|
120
|
|
|
return view('project.index', [ |
|
|
|
|
121
|
|
|
'tabs' => $this->projectMainViewTabs($project, 'created', $issues), |
122
|
|
|
'project' => $project, |
123
|
|
|
'active' => 'issue_created_by_you', |
124
|
|
|
'issues' => $issues, |
125
|
|
|
'sidebar' => 'project', |
126
|
|
|
]); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Display notes for a project. |
131
|
|
|
* |
132
|
|
|
* @param Project $project |
133
|
|
|
* @param NoteForm $form |
134
|
|
|
* |
135
|
|
|
* @return \Illuminate\View\View |
136
|
|
|
*/ |
137
|
7 |
|
public function getNotes(Project $project, NoteForm $form) |
138
|
|
|
{ |
139
|
7 |
|
$notes = $project->notes()->with('createdBy')->get(); |
140
|
|
|
|
141
|
7 |
|
return view('project.index', [ |
|
|
|
|
142
|
7 |
|
'tabs' => $this->projectMainViewTabs($project, 'notes', $notes), |
143
|
7 |
|
'project' => $project, |
144
|
7 |
|
'active' => 'notes', |
145
|
7 |
|
'notes' => $notes, |
146
|
7 |
|
'sidebar' => 'project', |
147
|
7 |
|
'noteForm' => $form, |
148
|
|
|
]); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @param Project $project |
153
|
|
|
* @param string $view |
154
|
|
|
* @param null $data |
155
|
|
|
* @param bool $status |
156
|
|
|
* |
157
|
|
|
* @return array |
158
|
|
|
*/ |
159
|
22 |
|
protected function projectMainViewTabs(Project $project, $view, $data = null, $status = false) |
160
|
|
|
{ |
161
|
22 |
|
$notesCount = $view === 'note' ? $data->count() : $project->notes()->count(); |
|
|
|
|
162
|
22 |
|
$user = $this->auth->user(); |
163
|
22 |
|
$isLoggedIn = !$this->auth->guest(); |
164
|
22 |
|
$isUser = $isLoggedIn && $user->isUser(); |
165
|
22 |
|
$isInternalProject = $project->isPrivateInternal(); |
166
|
|
|
|
167
|
22 |
|
if ($view === 'issues') { |
168
|
2 |
|
if ($status == Issue::STATUS_OPEN) { |
169
|
2 |
|
$closedIssuesCount = $project->closedIssuesCount($user)->count(); |
|
|
|
|
170
|
2 |
|
$openIssuesCount = $data->count(); |
|
|
|
|
171
|
|
|
} else { |
172
|
1 |
|
$closedIssuesCount = $data->count(); |
|
|
|
|
173
|
2 |
|
$openIssuesCount = $project->openIssuesCount($user)->count(); |
|
|
|
|
174
|
|
|
} |
175
|
|
|
} else { |
176
|
20 |
|
$openIssuesCount = $project->openIssuesCount($user)->count(); |
|
|
|
|
177
|
20 |
|
$closedIssuesCount = $project->closedIssuesCount($user)->count(); |
|
|
|
|
178
|
|
|
} |
179
|
|
|
|
180
|
22 |
|
$tabs = []; |
181
|
22 |
|
$tabs[] = [ |
182
|
22 |
|
'url' => $project->to(), |
183
|
22 |
|
'page' => 'activity', |
184
|
|
|
]; |
185
|
22 |
|
$tabs[] = [ |
186
|
22 |
|
'url' => $project->to('issues'), |
187
|
22 |
|
'page' => 'open_issue', |
188
|
22 |
|
'prefix' => $openIssuesCount, |
189
|
|
|
]; |
190
|
22 |
|
$tabs[] = [ |
191
|
22 |
|
'url' => $project->to('issues') . '/0', |
192
|
22 |
|
'page' => 'closed_issue', |
193
|
22 |
|
'prefix' => $closedIssuesCount, |
194
|
|
|
]; |
195
|
22 |
|
if ($isLoggedIn && (!$isInternalProject || (!$isUser && $isInternalProject))) { |
196
|
21 |
|
if ($view !== 'assigned') { |
197
|
20 |
|
$method = $isUser ? 'createdIssuesCount' : 'assignedIssuesCount'; |
198
|
20 |
|
$assignedIssuesCount = $this->auth->user()->$method($project->id); |
199
|
|
|
} else { |
200
|
1 |
|
$assignedIssuesCount = $data->count(); |
|
|
|
|
201
|
|
|
} |
202
|
|
|
|
203
|
21 |
|
$tabs[] = [ |
204
|
21 |
|
'url' => $project->to($isUser ? 'created' : 'assigned'), |
205
|
21 |
|
'page' => ($isUser ? 'issue_created_by_you' : 'issue_assigned_to_you'), |
206
|
21 |
|
'prefix' => $assignedIssuesCount, |
207
|
|
|
]; |
208
|
|
|
} |
209
|
22 |
|
$tabs[] = [ |
210
|
22 |
|
'url' => $project->to('notes'), |
211
|
22 |
|
'page' => 'notes', |
212
|
22 |
|
'prefix' => $notesCount, |
213
|
|
|
]; |
214
|
|
|
|
215
|
22 |
|
return $tabs; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* Edit the project. |
220
|
|
|
* |
221
|
|
|
* @param Project $project |
222
|
|
|
* @param Form $form |
223
|
|
|
* |
224
|
|
|
* @return \Illuminate\View\View |
225
|
|
|
*/ |
226
|
3 |
|
public function getEdit(Project $project, Form $form) |
227
|
|
|
{ |
228
|
3 |
|
return view('project.edit', [ |
|
|
|
|
229
|
3 |
|
'form' => $form, |
230
|
3 |
|
'project' => $project, |
231
|
3 |
|
'sidebar' => 'project', |
232
|
|
|
]); |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* To update project details. |
237
|
|
|
* |
238
|
|
|
* @param Project $project |
239
|
|
|
* @param FormRequest\Project $request |
240
|
|
|
* |
241
|
|
|
* @return \Illuminate\Http\RedirectResponse |
242
|
|
|
*/ |
243
|
3 |
|
public function postEdit(Project $project, FormRequest\Project $request) |
244
|
|
|
{ |
245
|
|
|
// Delete the project |
246
|
3 |
|
if ($request->has('delete-project')) { |
247
|
1 |
|
$project->delete(); |
248
|
|
|
|
249
|
1 |
|
return redirect('projects') |
250
|
1 |
|
->with('notice', trans('tinyissue.project_has_been_deleted')); |
251
|
|
|
} |
252
|
|
|
|
253
|
2 |
|
$project->update($request->all()); |
254
|
|
|
|
255
|
2 |
|
return redirect($project->to()) |
256
|
2 |
|
->with('notice', trans('tinyissue.project_has_been_updated')); |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* Ajax: returns list of users that are not in the project. |
261
|
|
|
* |
262
|
|
|
* @param Project $project |
263
|
|
|
* |
264
|
|
|
* @return \Symfony\Component\HttpFoundation\Response |
265
|
|
|
*/ |
266
|
1 |
|
public function getInactiveUsers(Project $project = null) |
267
|
|
|
{ |
268
|
1 |
|
$users = $project->usersNotIn(); |
|
|
|
|
269
|
|
|
|
270
|
1 |
|
return response()->json($users); |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
/** |
274
|
|
|
* Ajax: add user to the project. |
275
|
|
|
* |
276
|
|
|
* @param Project $project |
277
|
|
|
* @param Request $request |
278
|
|
|
* |
279
|
|
|
* @return \Symfony\Component\HttpFoundation\Response |
280
|
|
|
*/ |
281
|
1 |
View Code Duplication |
public function postAssign(Project $project, Request $request) |
|
|
|
|
282
|
|
|
{ |
283
|
1 |
|
$status = false; |
284
|
1 |
|
if ($request->has('user_id')) { |
285
|
1 |
|
$project->assignUser((int) $request->input('user_id')); |
286
|
1 |
|
$status = true; |
287
|
|
|
} |
288
|
|
|
|
289
|
1 |
|
return response()->json(['status' => $status]); |
290
|
|
|
} |
291
|
|
|
|
292
|
|
|
/** |
293
|
|
|
* Ajax: remove user from the project. |
294
|
|
|
* |
295
|
|
|
* @param Project $project |
296
|
|
|
* @param Request $request |
297
|
|
|
* |
298
|
|
|
* @return \Symfony\Component\HttpFoundation\Response |
299
|
|
|
*/ |
300
|
1 |
View Code Duplication |
public function postUnassign(Project $project, Request $request) |
|
|
|
|
301
|
|
|
{ |
302
|
1 |
|
$status = false; |
303
|
1 |
|
if ($request->has('user_id')) { |
304
|
1 |
|
$project->unassignUser((int) $request->input('user_id')); |
305
|
1 |
|
$status = true; |
306
|
|
|
} |
307
|
|
|
|
308
|
1 |
|
return response()->json(['status' => $status]); |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
/** |
312
|
|
|
* To add a new note to the project. |
313
|
|
|
* |
314
|
|
|
* @param Project $project |
315
|
|
|
* @param Note $note |
316
|
|
|
* @param FormRequest\Note $request |
317
|
|
|
* |
318
|
|
|
* @return \Illuminate\Http\RedirectResponse |
319
|
|
|
*/ |
320
|
2 |
|
public function postAddNote(Project $project, Note $note, FormRequest\Note $request) |
321
|
|
|
{ |
322
|
2 |
|
$note->setRelation('project', $project); |
323
|
2 |
|
$note->setRelation('createdBy', $this->auth->user()); |
324
|
2 |
|
$note->createNote($request->all()); |
325
|
|
|
|
326
|
2 |
|
return redirect($note->to())->with('notice', trans('tinyissue.your_note_added')); |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
/** |
330
|
|
|
* Ajax: To update project note. |
331
|
|
|
* |
332
|
|
|
* @param Project $project |
333
|
|
|
* @param Note $note |
334
|
|
|
* @param Request $request |
335
|
|
|
* |
336
|
|
|
* @return \Symfony\Component\HttpFoundation\Response |
337
|
|
|
*/ |
338
|
1 |
|
public function postEditNote(Project $project, Project\Note $note, Request $request) |
339
|
|
|
{ |
340
|
1 |
|
$body = ''; |
341
|
1 |
|
if ($request->has('body')) { |
342
|
1 |
|
$note->setRelation('project', $project); |
343
|
1 |
|
$note->updateBody($request->input('body'), $this->auth->user()); |
|
|
|
|
344
|
|
|
|
345
|
1 |
|
$body = \Html::format($note->body); |
346
|
|
|
} |
347
|
|
|
|
348
|
1 |
|
return response()->json(['status' => true, 'text' => $body]); |
349
|
|
|
} |
350
|
|
|
|
351
|
|
|
/** |
352
|
|
|
* Ajax: to delete a project note. |
353
|
|
|
* |
354
|
|
|
* @param Project $project |
355
|
|
|
* @param Note $note |
356
|
|
|
* |
357
|
|
|
* @return \Symfony\Component\HttpFoundation\Response |
358
|
|
|
*/ |
359
|
1 |
|
public function getDeleteNote(Project $project, Project\Note $note) |
|
|
|
|
360
|
|
|
{ |
361
|
1 |
|
$note->deleteNote($this->auth->user()); |
|
|
|
|
362
|
|
|
|
363
|
1 |
|
return response()->json(['status' => true]); |
364
|
|
|
} |
365
|
|
|
|
366
|
|
|
/** |
367
|
|
|
* Ajax: generate the issues export file. |
368
|
|
|
* |
369
|
|
|
* @param Project $project |
370
|
|
|
* @param Exporter $exporter |
371
|
|
|
* @param Request $request |
372
|
|
|
* |
373
|
|
|
* @return \Symfony\Component\HttpFoundation\Response |
374
|
|
|
*/ |
375
|
4 |
|
public function postExportIssues(Project $project, Exporter $exporter, Request $request) |
376
|
|
|
{ |
377
|
|
|
// Generate export file |
378
|
4 |
|
$info = $exporter->exportFile( |
379
|
4 |
|
'Project\Issue', |
380
|
4 |
|
$request->input('format', Exporter::TYPE_CSV), |
|
|
|
|
381
|
4 |
|
$request->all() |
382
|
|
|
); |
383
|
|
|
|
384
|
|
|
// Download link |
385
|
4 |
|
$link = link_to( |
386
|
4 |
|
$project->to('download_export/' . $info['file']), |
387
|
4 |
|
trans('tinyissue.download_export'), |
388
|
4 |
|
['class' => 'btn btn-link'] |
389
|
|
|
); |
390
|
|
|
|
391
|
4 |
|
return response()->json([ |
392
|
4 |
|
'link' => $link, |
393
|
4 |
|
'title' => $info['title'], |
394
|
4 |
|
'file' => $info['file'], |
395
|
4 |
|
'ext' => $info['ext'], |
396
|
|
|
]); |
397
|
|
|
} |
398
|
|
|
|
399
|
|
|
/** |
400
|
|
|
* Download and then delete an export file. |
401
|
|
|
* |
402
|
|
|
* @param Project $project |
403
|
|
|
* @param string $file |
404
|
|
|
* |
405
|
|
|
* @return \Symfony\Component\HttpFoundation\BinaryFileResponse |
406
|
|
|
*/ |
407
|
4 |
|
public function getDownloadExport(Project $project, $file) |
|
|
|
|
408
|
|
|
{ |
409
|
|
|
// Filter out any characters that are not in pattern |
410
|
4 |
|
$file = preg_replace('/[^a-z0-9\_\.]/mi', '', $file); |
411
|
|
|
|
412
|
|
|
// Download export |
413
|
4 |
|
return response()->download(storage_path('exports/' . $file), $file)->deleteFileAfterSend(true); |
414
|
|
|
} |
415
|
|
|
} |
416
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: