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