|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** Created by PhpStorm, User: jonphipps, Date: 2017-05-22, Time: 11:44 AM */ |
|
4
|
|
|
|
|
5
|
|
|
namespace App\Models\Traits; |
|
6
|
|
|
|
|
7
|
|
|
use App\Models\Access\User\User; |
|
8
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany; |
|
9
|
|
|
|
|
10
|
|
|
trait HasMembers |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @return BelongsToMany |
|
14
|
|
|
*/ |
|
15
|
|
|
public function members(): ?BelongsToMany |
|
16
|
|
|
{ |
|
17
|
|
|
//determine the parent class |
|
18
|
|
|
$class = static::class; |
|
19
|
|
|
$classUser = $class . 'User'; |
|
20
|
|
|
$className = $this->get_class_name($class); |
|
21
|
|
|
$foreignKey = ['Vocabulary'=> 'vocabulary_id', 'Elementset' => 'schema_id', 'Project' => 'agent_id']; |
|
22
|
|
|
|
|
23
|
|
|
return $this->belongsToMany(User::class, |
|
|
|
|
|
|
24
|
|
|
$classUser::TABLE, |
|
25
|
|
|
$foreignKey[$className], |
|
26
|
|
|
'user_id')->withTimestamps()->withPivot('is_maintainer_for', |
|
27
|
|
|
'is_registrar_for', |
|
28
|
|
|
'is_admin_for', |
|
29
|
|
|
'languages', |
|
30
|
|
|
'default_language', |
|
31
|
|
|
'current_language'); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @return mixed |
|
36
|
|
|
*/ |
|
37
|
|
|
public function registrar() |
|
38
|
|
|
{ |
|
39
|
|
|
return $this->members()->where('is_registrar_for', true); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @return mixed |
|
44
|
|
|
*/ |
|
45
|
|
|
public function administrators() |
|
46
|
|
|
{ |
|
47
|
|
|
return $this->members()->where('is_admin_for', true); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @return mixed |
|
52
|
|
|
*/ |
|
53
|
|
|
public function languageMaintainers() |
|
54
|
|
|
{ |
|
55
|
|
|
return $this->members()->where('is_maintainer_for', true)->whereNotNull('languages'); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @return mixed |
|
60
|
|
|
*/ |
|
61
|
|
|
public function maintainersForLanguage($language) |
|
62
|
|
|
{ |
|
63
|
|
|
return $this->members() |
|
64
|
|
|
->where('is_maintainer_for', true) |
|
65
|
|
|
->whereNotNull('languages') |
|
66
|
|
|
->whereIn('languages', $language); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @return mixed |
|
71
|
|
|
*/ |
|
72
|
|
|
public function maintainers() |
|
73
|
|
|
{ |
|
74
|
|
|
return $this->members()->where('is_maintainer_for', true)->whereNull('languages'); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @return mixed |
|
79
|
|
|
*/ |
|
80
|
|
|
public function viewers() |
|
81
|
|
|
{ |
|
82
|
|
|
return $this->members()->whereNull('is_admin_for', true)->whereNull('is_maintainer_for'); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
private function get_class_name($classname) |
|
86
|
|
|
{ |
|
87
|
|
|
if ($pos = strrpos($classname, '\\')) { |
|
88
|
|
|
return substr($classname, $pos + 1); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
return $pos; |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
|