-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathProject.php
More file actions
387 lines (349 loc) · 9.88 KB
/
Project.php
File metadata and controls
387 lines (349 loc) · 9.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
<?php
namespace App\Models;
use App\Events\DraftProcessed;
use App\Events\ProjectArchival;
use App\Events\ProjectDeletion;
use App\Notifications\ProjectDeletionFailureNotification;
use App\Notifications\ProjectDeletionReminderNotification;
use App\Notifications\ProjectInactivityReminderNotification;
use App\Traits\CacheClear;
use Auth;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Support\Facades\Notification;
use Laravel\Scout\Searchable;
use Maize\Markable\Markable;
use Maize\Markable\Models\Bookmark;
use Maize\Markable\Models\Like;
use OwenIt\Auditing\Contracts\Auditable;
use Spatie\Tags\HasTags;
use Storage;
class Project extends Model implements Auditable
{
use CacheClear;
use HasDOI;
use HasFactory;
use HasTags;
use Markable;
use \OwenIt\Auditing\Auditable;
use Searchable;
protected $fillable = [
'name',
'slug',
'color',
'starred',
'location',
'is_public',
'active',
'obfuscationcode',
'description',
'type',
'uuid',
'access',
'access_type',
'team_id',
'owner_id',
'draft_id',
'fs_id',
'project_photo_path',
'license_id',
'release_date',
'deleted_on',
'species',
];
/**
* Casts for the model attributes.
*/
protected function casts(): array
{
return [
'active' => 'boolean',
];
}
protected static $marks = [
Like::class,
Bookmark::class,
];
/**
* The accessors to append to the model's array form.
*
* @var array
*/
protected $appends = ['public_url', 'private_url', 'project_photo_url', 'is_bookmarked', 'is_published'];
/**
* Get the URL to the project's profile photo.
*
* @return string
*/
public function getProjectPhotoUrlAttribute()
{
return $this->project_photo_path
? Storage::disk(env('FILESYSTEM_DRIVER_PUBLIC'))->url($this->project_photo_path)
: '';
}
public function getIsPublishedAttribute()
{
if ($this->is_public) {
return true;
} else {
if ($this->release_date && $this->doi) {
return Carbon::now()->startOfDay()->gte($this->release_date);
} else {
return false;
}
}
return false;
}
/**
* check if the project is bookmarked by current user
*
* @return string
*/
public function getIsBookmarkedAttribute()
{
$user = Auth::user();
if (! $user) {
return false;
} else {
return Bookmark::has($this, $user);
}
}
/**
* Get the project identifier
*/
protected function identifier(): Attribute
{
return Attribute::make(
get: fn ($value) => $value ? 'NMRXIV:P'.$value : null,
);
}
public function studies(): HasMany
{
return $this->hasMany(Study::class, 'project_id');
}
public function team(): BelongsTo
{
return $this->belongsTo(Team::class, 'team_id');
}
public function nonPersonalTeam()
{
return $this->team()->where('personal_team', false);
}
public function draft(): BelongsTo
{
return $this->belongsTo(Draft::class, 'draft_id');
}
public function likesCount()
{
return Like::count($this);
}
/**
* Get the owner of the project.
*/
public function owner(): BelongsTo
{
return $this->belongsTo(User::class, 'owner_id');
}
/**
* Get all of the project's users including its owner.
*
* @return \Illuminate\Support\Collection
*/
public function allUsers()
{
return $this->users;
}
/**
* Get all of the users that belong to the project.
*/
public function users(): BelongsToMany
{
return $this->belongsToMany(User::class)
->withPivot('role')
->withTimestamps()
->as('projectMembership');
}
/**
* Determine if the given user belongs to the project.
*
* @param \App\Models\User $user
* @return bool
*/
public function hasUser($user)
{
return $this->users->contains($user) || $user->ownsProject($this);
}
/**
* Determine if the given email address belongs to a user on the project.
*
* @return bool
*/
public function hasUserWithEmail(string $email)
{
return $this->allUsers()->contains(function ($user) use ($email) {
return $user->email === $email;
});
}
/**
* Get the user with the given email if belongs to the project
*
* @return bool
*/
public function userWithEmail(string $email)
{
return $this->allUsers()->first(function ($user) use ($email) {
return $user->email === $email;
});
}
/**
* Get the user project role
*
* @return bool
*/
public function userProjectRole(string $email)
{
$user = $this->userWithEmail($email);
if ($user) {
if ($user['projectMembership']) {
return $user['projectMembership']['role'];
} elseif ($this->owner_id == $user->id) {
return 'owner';
}
}
}
// /**
// * Determine if the given user has the given permission on the project.
// *
// * @param \App\Models\User $user
// * @param string $permission
// * @return bool
// */
// public function userHasPermission($user, $permission)
// {
// return $user->hasProjectPermission($this, $permission);
// }
/**
* Get all of the pending user invitations for the project.
*/
public function projectInvitations(): HasMany
{
return $this->hasMany(ProjectInvitation::class);
}
/**
* Remove the given user from the project.
*
* @param \App\Models\User $user
* @return void
*/
public function removeUser($user)
{
$this->users()->detach($user);
}
protected function getPublicUrlAttribute()
{
// return env('APP_URL', null).'/projects/'.$this->owner->username.'/'.urlencode($this->slug);
return env('APP_URL', null).'/project/P'.$this->getRawOriginal('identifier');
}
protected function getPrivateUrlAttribute()
{
return env('APP_URL', null).'/projects/'.urlencode($this->url);
}
/**
* Get the license of the project.
*/
public function license(): BelongsTo
{
return $this->belongsTo(License::class, 'license_id');
}
public function validation(): BelongsTo
{
return $this->belongsTo(Validation::class, 'validation_id');
}
/**
* Model boot method to ensure that any meaningful update re-activates the project.
*/
protected static function booted(): void
{
static::saving(function (Project $project): void {
// If something changes other than 'active' itself, mark as active again
if ($project->isDirty() && ! $project->isDirty('active')) {
$project->active = true;
}
});
}
/**
* Determine if the model should be searchable.
*
* @return bool
*/
public function shouldBeSearchable()
{
if ($this->is_public && ! $this->is_archived) {
return true;
}
}
/**
* Authors that belongs to project.
*/
public function authors(): BelongsToMany
{
return $this->belongsToMany(Author::class)
->withPivot('contributor_type', 'sort_order')->orderBy('sort_order', 'asc');
}
public function scopeFilter($query, array $filters)
{
$query->when($filters['search'] ?? null, function ($query, $search) {
$query->where(function ($query) use ($search) {
$query->where('name', 'ILIKE', '%'.$search.'%')
->orWhere('description', 'ILIKE', '%'.$search.'%');
});
})->when($filters['sort'] ?? 'creation', function ($query, $sort) {
if ($sort === 'newest') {
$query->orderByDesc('updated_at');
} elseif ($sort === 'rating') {
$query->orderByDesc('likes');
} elseif ($sort === 'creation') {
$query->orderByDesc('created_at');
}
});
}
public function citations(): BelongsToMany
{
return $this->belongsToMany(Citation::class);
}
/**
* Send Notification via email.
*
* @param string $notifyType (deletion / deletionReminder / archival / archivalAdmin)
* @param array sendTo
* @return void
*/
public function sendNotification($notifyType, $sendTo)
{
switch ($notifyType) {
case 'deletion':
event(new ProjectDeletion($this, $sendTo));
break;
case 'deletionReminder':
Notification::send($sendTo, new ProjectDeletionReminderNotification($this));
break;
case 'archival':
event(new ProjectArchival($this, $sendTo));
break;
case 'deletionFailure':
Notification::send($sendTo, new ProjectDeletionFailureNotification($this));
break;
case 'publish':
event(new DraftProcessed($this, $sendTo));
break;
case 'inactivityReminder':
Notification::send($sendTo, new ProjectInactivityReminderNotification($this));
break;
}
}
}