1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of Gitamin. |
5
|
|
|
* |
6
|
|
|
* Copyright (C) 2015-2016 The Gitamin Team |
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 Gitamin\Http\Controllers\Dashboard; |
13
|
|
|
|
14
|
|
|
use AltThree\Validator\ValidationException; |
15
|
|
|
use Gitamin\Commands\Project\AddProjectCommand; |
16
|
|
|
use Gitamin\Commands\Project\RemoveProjectCommand; |
17
|
|
|
use Gitamin\Commands\Project\UpdateProjectCommand; |
18
|
|
|
use Gitamin\Models\Project; |
19
|
|
|
use Gitamin\Models\ProjectTeam; |
20
|
|
|
use Gitamin\Models\Tag; |
21
|
|
|
use GrahamCampbell\Binput\Facades\Binput; |
22
|
|
|
use Illuminate\Foundation\Bus\DispatchesJobs; |
23
|
|
|
use Illuminate\Routing\Controller; |
24
|
|
|
use Illuminate\Support\Facades\Redirect; |
25
|
|
|
use Illuminate\Support\Facades\View; |
26
|
|
|
|
27
|
|
|
class ProjectController extends Controller |
28
|
|
|
{ |
29
|
|
|
use DispatchesJobs; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Array of sub-menu items. |
33
|
|
|
* |
34
|
|
|
* @var array |
35
|
|
|
*/ |
36
|
|
|
protected $subMenu = []; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Creates a new project controller instance. |
40
|
|
|
* |
41
|
|
|
* @return void |
|
|
|
|
42
|
|
|
*/ |
43
|
|
|
public function __construct() |
44
|
|
|
{ |
45
|
|
|
$this->subMenu = [ |
46
|
|
|
'yours' => [ |
47
|
|
|
'title' => trans('dashboard.projects.yours'), |
48
|
|
|
'url' => route('dashboard.projects.index'), |
49
|
|
|
'icon' => 'fa fa-edit', |
50
|
|
|
'active' => false, |
51
|
|
|
], |
52
|
|
|
'starred' => [ |
53
|
|
|
'title' => trans('dashboard.projects.starred'), |
54
|
|
|
'url' => route('dashboard.projects.starred'), |
55
|
|
|
'icon' => 'fa fa-umbrella', |
56
|
|
|
'active' => false, |
57
|
|
|
], |
58
|
|
|
'explore' => [ |
59
|
|
|
'title' => trans('dashboard.projects.explore'), |
60
|
|
|
'url' => route('explore.index'), |
61
|
|
|
'icon' => 'fa fa-eye', |
62
|
|
|
'active' => false, |
63
|
|
|
], |
64
|
|
|
/* |
|
|
|
|
65
|
|
|
'<hr>' => [], |
66
|
|
|
'teams' => [ |
67
|
|
|
'title' => trans_choice('dashboard.teams.teams', 2), |
68
|
|
|
'url' => route('dashboard.teams.index'), |
69
|
|
|
'icon' => 'fa fa-folder', |
70
|
|
|
'active' => false, |
71
|
|
|
], |
72
|
|
|
'labels' => [ |
73
|
|
|
'title' => trans_choice('dashboard.projects.labels.labels', 2), |
74
|
|
|
'url' => route('dashboard.projects.index'), |
75
|
|
|
'icon' => 'fa fa-tags', |
76
|
|
|
'active' => false, |
77
|
|
|
],*/ |
78
|
|
|
]; |
79
|
|
|
|
80
|
|
|
View::share([ |
81
|
|
|
'sub_menu' => $this->subMenu, |
82
|
|
|
'sub_title' => trans_choice('dashboard.projects.projects', 2), |
83
|
|
|
]); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Shows the projects view. |
88
|
|
|
* |
89
|
|
|
* @return \Illuminate\View\View |
90
|
|
|
*/ |
91
|
|
View Code Duplication |
public function index() |
|
|
|
|
92
|
|
|
{ |
93
|
|
|
$projects = Project::orderBy('order')->orderBy('created_at')->get(); |
94
|
|
|
|
95
|
|
|
$this->subMenu['yours']['active'] = true; |
96
|
|
|
|
97
|
|
|
return View::make('dashboard.projects.index') |
98
|
|
|
->withPageTitle(trans_choice('dashboard.projects.projects', 2).' - '.trans('dashboard.dashboard')) |
99
|
|
|
->withProjects($projects) |
100
|
|
|
->withSubMenu($this->subMenu); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
View Code Duplication |
public function starred() |
|
|
|
|
104
|
|
|
{ |
105
|
|
|
|
106
|
|
|
$projects = Project::orderBy('order')->orderBy('created_at')->get(); |
107
|
|
|
|
108
|
|
|
$this->subMenu['starred']['active'] = true; |
109
|
|
|
|
110
|
|
|
return View::make('dashboard.projects.index') |
111
|
|
|
->withPageTitle(trans_choice('dashboard.projects.projects', 2).' - '.trans('dashboard.dashboard')) |
112
|
|
|
->withProjects($projects) |
113
|
|
|
->withSubMenu($this->subMenu); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* Shows the project view. |
118
|
|
|
* |
119
|
|
|
* @param \Gitamin\Models\Project $project |
120
|
|
|
* |
121
|
|
|
* @return \Illuminate\View\View |
122
|
|
|
*/ |
123
|
|
View Code Duplication |
public function showProject(Project $project) |
|
|
|
|
124
|
|
|
{ |
125
|
|
|
$teams = ProjectTeam::all(); |
126
|
|
|
|
127
|
|
|
$pageTitle = sprintf('"%s" - %s - %s', $project->name, trans('dashboard.projects.edit.title'), trans('dashboard.dashboard')); |
|
|
|
|
128
|
|
|
|
129
|
|
|
return View::make('dashboard.projects.show') |
130
|
|
|
->withPageTitle($pageTitle) |
131
|
|
|
->withProject($project) |
132
|
|
|
->withTeams($teams); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* Shows the edit project view. |
137
|
|
|
* |
138
|
|
|
* @param \Gitamin\Models\Project $project |
139
|
|
|
* |
140
|
|
|
* @return \Illuminate\View\View |
141
|
|
|
*/ |
142
|
|
View Code Duplication |
public function showEditProject(Project $project) |
|
|
|
|
143
|
|
|
{ |
144
|
|
|
$teams = ProjectTeam::all(); |
145
|
|
|
|
146
|
|
|
$pageTitle = sprintf('"%s" - %s - %s', $project->name, trans('dashboard.projects.edit.title'), trans('dashboard.dashboard')); |
|
|
|
|
147
|
|
|
|
148
|
|
|
return View::make('dashboard.projects.edit') |
149
|
|
|
->withPageTitle($pageTitle) |
150
|
|
|
->withProject($project) |
151
|
|
|
->withTeams($teams); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Updates a project. |
156
|
|
|
* |
157
|
|
|
* @param \Gitamin\Models\Project $projects |
|
|
|
|
158
|
|
|
* |
159
|
|
|
* @return \Illuminate\Http\RedirectResponse |
160
|
|
|
*/ |
161
|
|
|
public function updateProjectAction(Project $project) |
162
|
|
|
{ |
163
|
|
|
$projectData = Binput::get('project'); |
164
|
|
|
$tags = array_pull($projectData, 'tags'); |
165
|
|
|
|
166
|
|
|
try { |
167
|
|
|
$projectData['project'] = $project; |
168
|
|
|
$project = $this->dispatchFromArray(UpdateProjectCommand::class, $projectData); |
169
|
|
|
} catch (ValidationException $e) { |
170
|
|
|
return Redirect::route('dashboard.projects.edit', ['id' => $project->id]) |
171
|
|
|
->withInput(Binput::all()) |
172
|
|
|
->withTitle(sprintf('%s %s', trans('dashboard.notifications.whoops'), trans('dashboard.projects.edit.failure'))) |
173
|
|
|
->withErrors($e->getMessageBag()); |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
// The project was added successfully, so now let's deal with the tags. |
177
|
|
|
$tags = preg_split('/ ?, ?/', $tags); |
178
|
|
|
|
179
|
|
|
// For every tag, do we need to create it? |
180
|
|
|
$projectTags = array_map(function ($taggable) use ($project) { |
181
|
|
|
return Tag::firstOrCreate(['name' => $taggable])->id; |
|
|
|
|
182
|
|
|
}, $tags); |
183
|
|
|
|
184
|
|
|
$project->tags()->sync($projectTags); |
185
|
|
|
|
186
|
|
|
return Redirect::route('dashboard.projects.edit', ['id' => $project->id]) |
187
|
|
|
->withSuccess(sprintf('%s %s', trans('dashboard.notifications.awesome'), trans('dashboard.projects.edit.success'))); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Shows the add project view. |
192
|
|
|
* |
193
|
|
|
* @return \Illuminate\View\View |
194
|
|
|
*/ |
195
|
|
|
public function showAddProject() |
196
|
|
|
{ |
197
|
|
|
$teamId = (int) Binput::get('team_id'); |
198
|
|
|
|
199
|
|
|
return View::make('dashboard.projects.add') |
200
|
|
|
->withPageTitle(trans('dashboard.projects.add.title').' - '.trans('dashboard.dashboard')) |
201
|
|
|
->withTeamId($teamId) |
202
|
|
|
->withTeams(ProjectTeam::all()); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* Creates a new project. |
207
|
|
|
* |
208
|
|
|
* @return \Illuminate\Http\RedirectResponse |
209
|
|
|
*/ |
210
|
|
|
public function createProjectAction() |
211
|
|
|
{ |
212
|
|
|
$projectData = Binput::get('project'); |
213
|
|
|
$tags = array_pull($projectData, 'tags'); |
214
|
|
|
|
215
|
|
|
try { |
216
|
|
|
$project = $this->dispatchFromArray(AddProjectCommand::class, $projectData); |
217
|
|
|
} catch (ValidationException $e) { |
218
|
|
|
return Redirect::route('dashboard.projects.add') |
219
|
|
|
->withInput(Binput::all()) |
220
|
|
|
->withTitle(sprintf('%s %s', trans('dashboard.notifications.whoops'), trans('dashboard.projects.add.failure'))) |
221
|
|
|
->withErrors($e->getMessageBag()); |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
// The project was added successfully, so now let's deal with the tags. |
225
|
|
|
$tags = preg_split('/ ?, ?/', $tags); |
226
|
|
|
|
227
|
|
|
// For every tag, do we need to create it? |
228
|
|
|
$projectTags = array_map(function ($taggable) use ($project) { |
229
|
|
|
return Tag::firstOrCreate(['name' => $taggable])->id; |
|
|
|
|
230
|
|
|
}, $tags); |
231
|
|
|
|
232
|
|
|
$project->tags()->sync($projectTags); |
233
|
|
|
|
234
|
|
|
return Redirect::route('dashboard.projects.index') |
235
|
|
|
->withSuccess(sprintf('%s %s', trans('dashboard.notifications.awesome'), trans('dashboard.projects.add.success'))); |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
/** |
239
|
|
|
* Deletes a given project. |
240
|
|
|
* |
241
|
|
|
* @param \Gitamin\Models\Project $project |
242
|
|
|
* |
243
|
|
|
* @return \Illuminate\Http\RedirectResponse |
244
|
|
|
*/ |
245
|
|
|
public function deleteProjectAction(Project $project) |
246
|
|
|
{ |
247
|
|
|
$this->dispatch(new RemoveProjectCommand($project)); |
248
|
|
|
|
249
|
|
|
return Redirect::route('dashboard.projects.index') |
250
|
|
|
->withSuccess(sprintf('%s %s', trans('dashboard.notifications.awesome'), trans('dashboard.projects.delete.success'))); |
251
|
|
|
} |
252
|
|
|
} |
253
|
|
|
|
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.