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
|
|
|
# == Schema Information |
|
|
|
|
13
|
|
|
# |
14
|
|
|
# Table name: projects |
15
|
|
|
# |
16
|
|
|
# id :integer not null, primary key |
17
|
|
|
# name :string(255) |
18
|
|
|
# path :string(255) |
19
|
|
|
# description :text |
20
|
|
|
# created_at :timestamp |
21
|
|
|
# updated_at :timestamp |
22
|
|
|
# creator_id :integer |
23
|
|
|
# issues_enabled :boolean default(TRUE), not null |
24
|
|
|
# wall_enabled :boolean default(TRUE), not null |
25
|
|
|
# pull_requests_enabled :boolean default(TRUE), not null |
26
|
|
|
# wiki_enabled :boolean default(TRUE), not null |
27
|
|
|
# owner_id :integer |
28
|
|
|
# issues_tracker :string(255) default("gitlab"), not null |
29
|
|
|
# issues_tracker_id :string(255) |
30
|
|
|
# snippets_enabled :boolean default(TRUE), not null |
31
|
|
|
# last_activity_at :timestamp |
32
|
|
|
# import_url :string(255) |
33
|
|
|
# visibility_level :integer default(0), not null |
34
|
|
|
# archived :boolean default(FALSE), not null |
35
|
|
|
# avatar :string(255) |
36
|
|
|
# import_status :string(255) |
37
|
|
|
# repository_size :float default(0.0) |
38
|
|
|
# star_count :integer default(0), not null |
39
|
|
|
# import_type :string(255) |
40
|
|
|
# import_source :string(255) |
41
|
|
|
# commit_count :integer default(0) |
42
|
|
|
# |
43
|
|
|
|
44
|
|
|
namespace Gitamin\Models; |
45
|
|
|
|
46
|
|
|
use AltThree\Validator\ValidatingTrait; |
47
|
|
|
use Gitamin\Presenters\ProjectPresenter; |
48
|
|
|
use Gitamin\Presenters\Traits\HasVisibilities; |
49
|
|
|
use Illuminate\Database\Eloquent\Builder; |
50
|
|
|
use Illuminate\Database\Eloquent\Model; |
51
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes; |
52
|
|
|
use McCool\LaravelAutoPresenter\HasPresenter; |
53
|
|
|
|
54
|
|
|
class Project extends Model implements HasPresenter |
55
|
|
|
{ |
56
|
|
|
use SoftDeletes, ValidatingTrait, HasVisibilities; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* List of attributes that have default values. |
60
|
|
|
* |
61
|
|
|
* @var mixed[] |
62
|
|
|
*/ |
63
|
|
|
protected $attributes = [ |
64
|
|
|
'owner_id' => 0, |
65
|
|
|
'description' => '', |
66
|
|
|
'path' => '', |
67
|
|
|
'creator_id' => 0, |
68
|
|
|
]; |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* The attributes that should be casted to native types. |
72
|
|
|
* |
73
|
|
|
* @var string[] |
74
|
|
|
*/ |
75
|
|
|
protected $casts = [ |
76
|
|
|
'id' => 'int', |
77
|
|
|
'owner_id' => 'int', |
78
|
|
|
'description' => 'string', |
79
|
|
|
'path' => 'string', |
80
|
|
|
'issues_enabled' => 'boolean', |
81
|
|
|
'creator_id' => 'int', |
82
|
|
|
'deleted_at' => 'date', |
83
|
|
|
]; |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* The fillable properties. |
87
|
|
|
* |
88
|
|
|
* @var string[] |
89
|
|
|
*/ |
90
|
|
|
protected $fillable = [ |
91
|
|
|
'name', |
92
|
|
|
'description', |
93
|
|
|
'visibility_level', |
94
|
|
|
'tags', |
95
|
|
|
'path', |
96
|
|
|
'issues_enabled', |
97
|
|
|
'creator_id', |
98
|
|
|
'owner_id', |
99
|
|
|
]; |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* The validation rules. |
103
|
|
|
* |
104
|
|
|
* @var string[] |
105
|
|
|
*/ |
106
|
|
|
public $rules = [ |
107
|
|
|
'name' => 'required|string', |
108
|
|
|
'visibility_level' => 'int|required', |
109
|
|
|
'path' => 'required|string|max:15', |
110
|
|
|
]; |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Projects can belong to a group. |
114
|
|
|
* |
115
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
116
|
|
|
*/ |
117
|
|
|
public function owner() |
118
|
|
|
{ |
119
|
|
|
return $this->belongsTo(Group::class, 'owner_id', 'id'); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Projects can belong to a creator. |
124
|
|
|
* |
125
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo |
126
|
|
|
*/ |
127
|
|
|
public function creator() |
128
|
|
|
{ |
129
|
|
|
return $this->belongsTo(User::class, 'creator_id', 'id'); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Lookup all of the issues reported on the project. |
134
|
|
|
* |
135
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\HasMany |
136
|
|
|
*/ |
137
|
|
|
public function issues() |
138
|
|
|
{ |
139
|
|
|
return $this->hasMany(Issue::class, 'project_id', 'id'); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Projects can have many tags. |
144
|
|
|
* |
145
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany |
146
|
|
|
*/ |
147
|
|
|
public function tags() |
148
|
|
|
{ |
149
|
|
|
return $this->belongsToMany(Tag::class); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Find by owner_path & project_path, or throw an exception. |
154
|
|
|
* |
155
|
|
|
* @param string $owner_path |
156
|
|
|
* @param string $project_path |
157
|
|
|
* @param string[] $columns |
158
|
|
|
* |
159
|
|
|
* @throws \Illuminate\Database\Eloquent\ModelNotFoundException |
160
|
|
|
* |
161
|
|
|
* @return \Gitamin\Models\User |
162
|
|
|
*/ |
163
|
|
|
public static function findByPath($owner_path, $project_path, $columns = ['*']) |
164
|
|
|
{ |
165
|
|
|
$project = Owner::findByPath($owner_path)->project($project_path, $columns); |
|
|
|
|
166
|
|
|
/* Another way |
|
|
|
|
167
|
|
|
$project = static::leftJoin('owners', function ($join) { |
168
|
|
|
$join->on('projects.owner_id', '=', 'owners.id'); |
169
|
|
|
})->where('projects.path', '=', $project_path)->where('owners.path', '=', $owner_path)->first($columns); |
170
|
|
|
*/ |
171
|
|
|
if (! $project) { |
172
|
|
|
throw new ModelNotFoundException(); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
return $project; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Finds all projects by visibility_level. |
180
|
|
|
* |
181
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
182
|
|
|
* @param int $visibility_level |
183
|
|
|
* |
184
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
185
|
|
|
*/ |
186
|
|
|
public function scopeVisibilityLevel(Builder $query, $visibility_level) |
187
|
|
|
{ |
188
|
|
|
return $query->where('visibility_level', $visibility_level); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Finds all projects which don't have the given visibility_level. |
193
|
|
|
* |
194
|
|
|
* @param \Illuminate\Database\Eloquent\Builder $query |
195
|
|
|
* @param int $visibility_level |
196
|
|
|
* |
197
|
|
|
* @return \Illuminate\Database\Eloquent\Builder |
198
|
|
|
*/ |
199
|
|
|
public function scopeNotVisibilityLevel(Builder $query, $visibility_level) |
200
|
|
|
{ |
201
|
|
|
return $query->where('visibility_level', '<>', $visibility_level); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
/** |
205
|
|
|
* Looks up the human readable version of the visibility_level. |
206
|
|
|
* |
207
|
|
|
* @return string |
208
|
|
|
*/ |
209
|
|
|
public function getHumanVisibilityLevelAttribute() |
210
|
|
|
{ |
211
|
|
|
return trans('gitamin.projects.status.'.$this->visibility_level); |
|
|
|
|
212
|
|
|
} |
213
|
|
|
|
214
|
|
|
/** |
215
|
|
|
* Returns project route. |
216
|
|
|
* |
217
|
|
|
* @return string |
218
|
|
|
*/ |
219
|
|
|
public function getUrlAttribute() |
220
|
|
|
{ |
221
|
|
|
return route('projects.project_show', ['owner' => $this->owner_path, 'project' => $this->path]); |
|
|
|
|
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* Returns project owner path. |
226
|
|
|
* |
227
|
|
|
* @return string |
228
|
|
|
*/ |
229
|
|
|
public function getOwnerPathAttribute() |
230
|
|
|
{ |
231
|
|
|
return $this->owner->path; |
|
|
|
|
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* Returns all of the tags on this project. |
236
|
|
|
* |
237
|
|
|
* @return string |
238
|
|
|
*/ |
239
|
|
|
public function getTagsListAttribute() |
240
|
|
|
{ |
241
|
|
|
$tags = $this->tags->map(function ($tag) { |
|
|
|
|
242
|
|
|
return $tag->name; |
243
|
|
|
}); |
244
|
|
|
|
245
|
|
|
return implode(', ', $tags->toArray()); |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* Get the presenter class. |
250
|
|
|
* |
251
|
|
|
* @return string |
252
|
|
|
*/ |
253
|
|
|
public function getPresenterClass() |
254
|
|
|
{ |
255
|
|
|
return ProjectPresenter::class; |
256
|
|
|
} |
257
|
|
|
} |
258
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.