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