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; |
13
|
|
|
|
14
|
|
|
use Illuminate\Http\Request; |
15
|
|
|
use Illuminate\Foundation\Bus\DispatchesJobs; |
16
|
|
|
use Illuminate\Support\Facades\Redirect; |
17
|
|
|
use Illuminate\Support\Facades\Auth; |
18
|
|
|
use Illuminate\Support\Facades\View; |
19
|
|
|
use GrahamCampbell\Binput\Facades\Binput; |
20
|
|
|
use AltThree\Validator\ValidationException; |
21
|
|
|
use Gitamin\Commands\Project\AddProjectCommand; |
22
|
|
|
use Gitamin\Commands\Project\RemoveProjectCommand; |
23
|
|
|
use Gitamin\Commands\Project\UpdateProjectCommand; |
24
|
|
|
use Gitamin\Models\Project; |
25
|
|
|
use Gitamin\Models\Owner; |
26
|
|
|
use Gitamin\Models\Group; |
27
|
|
|
use Gitamin\Models\Tag; |
28
|
|
|
|
29
|
|
|
use Gitamin\Http\Controllers\Controller; |
30
|
|
|
|
31
|
|
|
class ProjectsController extends Controller |
32
|
|
|
{ |
33
|
|
|
use DispatchesJobs; |
34
|
|
|
/** |
35
|
|
|
* Display a listing of the resource. |
36
|
|
|
* |
37
|
|
|
* @return \Illuminate\Http\Response |
38
|
|
|
*/ |
39
|
|
|
public function indexAction() |
40
|
|
|
{ |
41
|
|
|
// |
42
|
|
|
echo "In projects controller"; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Show the form or adding a new resource. |
47
|
|
|
* |
48
|
|
|
* return \Illuminate\Http\Response |
49
|
|
|
*/ |
50
|
|
|
public function newAction() |
51
|
|
|
{ |
52
|
|
|
return View::make('projects.new') |
53
|
|
|
->withPageTitle(trans('dashboard.projects.new.title').' - '.trans('dashboard.dashboard')) |
54
|
|
|
->withGroupId('') |
55
|
|
|
->withGroups(Owner::Mine()->get()); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Show the form for creating a new resource. |
60
|
|
|
* |
61
|
|
|
* @return \Illuminate\Http\Response |
62
|
|
|
*/ |
63
|
|
View Code Duplication |
public function createAction() |
|
|
|
|
64
|
|
|
{ |
65
|
|
|
// |
66
|
|
|
$projectData = Binput::get('project'); |
67
|
|
|
$tags = array_pull($projectData, 'tags'); |
68
|
|
|
|
69
|
|
|
try { |
70
|
|
|
$projectData['creator_id'] = Auth::user()->id; |
71
|
|
|
$project = $this->dispatchFromArray(AddProjectCommand::class, $projectData); |
72
|
|
|
} catch (ValidationException $e) { |
73
|
|
|
return Redirect::route('projects.new') |
74
|
|
|
->withInput(Binput::all()) |
75
|
|
|
->withTitle(sprintf('%s %s', trans('dashboard.notifications.whoops'), trans('dashboard.projects.new.failure'))) |
76
|
|
|
->withErrors($e->getMessageBag()); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
// The project was added successfully, so now let's deal with the tags. |
80
|
|
|
$tags = preg_split('/ ?, ?/', $tags); |
81
|
|
|
|
82
|
|
|
// For every tag, do we need to create it? |
83
|
|
|
$projectTags = array_map(function ($taggable) use ($project) { |
84
|
|
|
return Tag::firstOrCreate(['name' => $taggable])->id; |
|
|
|
|
85
|
|
|
}, $tags); |
86
|
|
|
|
87
|
|
|
$project->tags()->sync($projectTags); |
88
|
|
|
|
89
|
|
|
return Redirect::route('dashboard.projects.index') |
90
|
|
|
->withSuccess(sprintf('%s %s', trans('dashboard.notifications.awesome'), trans('dashboard.projects.new.success'))); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Display the specified resource. |
95
|
|
|
* |
96
|
|
|
* @param string $owner |
97
|
|
|
* @param string $project_path |
98
|
|
|
* @return \Illuminate\Http\Response |
99
|
|
|
*/ |
100
|
|
|
public function showAction($owner, $project_path) |
101
|
|
|
{ |
102
|
|
|
$project = Project::leftJoin('owners', function($join) { |
103
|
|
|
$join->on('projects.owner_id', '=', 'owners.id'); |
104
|
|
|
})->where('projects.path', '=', $project_path)->where('owners.path', '=', $owner)->first(['projects.*']); |
105
|
|
|
|
106
|
|
|
return View::make('projects.show') |
107
|
|
|
->withPageTitle($project->name) |
108
|
|
|
->withActiveItem('project_show') |
109
|
|
|
->withProject($project) |
110
|
|
|
->withRepo('') |
111
|
|
|
->withRepository('') |
112
|
|
|
->withCurrentBranch('master') |
113
|
|
|
->withBranches([]) |
114
|
|
|
->withParentPath('') |
115
|
|
|
->withFiles([]); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Show the form for editing the specified resource. |
120
|
|
|
* |
121
|
|
|
* @param int $id |
|
|
|
|
122
|
|
|
* @return \Illuminate\Http\Response |
123
|
|
|
*/ |
124
|
|
|
public function editAction($namespace, $project_path) |
125
|
|
|
{ |
126
|
|
|
$project = Project::leftJoin('namespaces', function($join) { |
127
|
|
|
$join->on('projects.namespace_id', '=', 'namespaces.id'); |
128
|
|
|
})->where('projects.path', '=', $project_path)->where('namespaces.path', '=', $namespace)->first(['projects.*']); |
129
|
|
|
|
130
|
|
|
return View::make('projects.edit') |
131
|
|
|
->withPageTitle(trans('dashboard.projects.new.title').' - '.trans('dashboard.dashboard')) |
132
|
|
|
->withProject($project) |
133
|
|
|
->withGroupId('') |
134
|
|
|
->withGroups(Group::all()); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Update the specified resource in storage. |
139
|
|
|
* |
140
|
|
|
* @param \Illuminate\Http\Request $request |
|
|
|
|
141
|
|
|
* @param int $id |
|
|
|
|
142
|
|
|
* @return \Illuminate\Http\Response |
143
|
|
|
*/ |
144
|
|
|
public function updateAction($namespace, $project_path) |
|
|
|
|
145
|
|
|
{ |
146
|
|
|
$projectData = Binput::get('project'); |
147
|
|
|
$tags = array_pull($projectData, 'tags'); |
148
|
|
|
$project = Project::find($projectData['id']); |
149
|
|
|
$projectData['namespace_id'] = $project->namespace_id; |
150
|
|
|
|
151
|
|
|
try { |
152
|
|
|
$projectData['project'] = $project; |
153
|
|
|
$projectData['creator_id'] = Auth::user()->id; |
154
|
|
|
$project = $this->dispatchFromArray(UpdateProjectCommand::class, $projectData); |
155
|
|
|
} catch (ValidationException $e) { |
156
|
|
|
return Redirect::route('projects.project_edit', ['namespace' => $project->namespace, 'project' => $project->path]) |
157
|
|
|
->withInput(Binput::all()) |
158
|
|
|
->withTitle(sprintf('%s %s', trans('dashboard.notifications.whoops'), trans('dashboard.projects.edit.failure'))) |
159
|
|
|
->withErrors($e->getMessageBag()); |
160
|
|
|
} |
161
|
|
|
// The project was updated successfully, so now let's deal with the tags. |
162
|
|
|
$tags = preg_split('/ ?, ?/', $tags); |
|
|
|
|
163
|
|
|
|
164
|
|
|
return Redirect::route('projects.project_edit', ['namespace' => $project->namespace, 'project' => $project->path]) |
165
|
|
|
->withSuccess(sprintf('%s %s', trans('dashboard.notifications.awesome'), trans('dashboard.projects.edit.success'))); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Remove the specified resource from storage. |
170
|
|
|
* |
171
|
|
|
* @param int $id |
172
|
|
|
* @return \Illuminate\Http\Response |
173
|
|
|
*/ |
174
|
|
|
public function destroy($id) |
|
|
|
|
175
|
|
|
{ |
176
|
|
|
// |
177
|
|
|
} |
178
|
|
|
} |
179
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.