-
Notifications
You must be signed in to change notification settings - Fork 554
/
Group.php
164 lines (141 loc) · 3.9 KB
/
Group.php
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
<?php
namespace A17\Twill\Models;
use A17\Twill\Models\Behaviors\HasPermissions;
use A17\Twill\Models\Behaviors\IsTranslatable;
use A17\Twill\Models\Contracts\TwillModelContract;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model as BaseModel;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Collection;
class Group extends BaseModel implements TwillModelContract
{
use HasPermissions;
use SoftDeletes;
use IsTranslatable;
public $timestamps = true;
protected $fillable = [
'name',
'description',
'published',
'subdomains_access',
];
public $checkboxes = ['published'];
protected $casts = [
'subdomains_access' => 'array',
'deleted_at' => 'datetime'
];
/**
* Return the Everyone group.
*/
public static function getEveryoneGroup(): Group
{
return self::where([['is_everyone_group', true], ['name', 'Everyone']])->firstOrFail();
}
/**
* Return the group title.
*/
public function getTitleInBrowserAttribute(): string
{
return $this->name;
}
/**
* Scope published groups.
*/
public function scopePublished(Builder $query): Builder
{
return $query->wherePublished(true);
}
/**
* Scope unpublished (draft) groups.
*/
public function scopeDraft(Builder $query): Builder
{
return $query->wherePublished(false);
}
/**
* Scope trashed groups.
*/
public function scopeOnlyTrashed(Builder $query): Builder
{
return $query->onlyTrashed();
}
public function users(): BelongsToMany
{
return $this->belongsToMany(twillModel('user'), 'group_twill_user', 'group_id', 'twill_user_id');
}
/**
* Check if current group is the Everyone group.
*/
public function isEveryoneGroup(): bool
{
return $this->id === self::getEveryoneGroup()->id;
}
/**
* Return the formatted created date.
*/
public function getCreatedAtAttribute($value): string
{
return \Carbon\Carbon::parse($value)->format('d M Y');
}
/**
* Check if the group can be edited (not a system group, ie. Everyone).
*/
public function getCanEditAttribute(): bool
{
return !$this->isEveryoneGroup();
}
/**
* Check if the group can be published (not a system group, ie. Everyone).
*/
public function getCanPublishAttribute(): bool
{
return !$this->isEveryoneGroup();
}
/**
* Return the formatted number of users in this group.
*/
public function getUsersCountAttribute(): string
{
return $this->users->count() . ' users';
}
/**
* Return viewable items.
*/
public function viewableItems(): Collection
{
return Permission::where('name', 'view-item')->whereHas('groups', function ($query) {
$query->where('id', $this->id);
})->with('permissionable')->get()->pluck('permissionable');
}
/**
* Return ids of permissionable items.
*
* @return int[]
*/
public function permissionableIds(): array
{
return $this->permissions->pluck('id')->toArray();
}
/**
* Return permissionable items.
*/
public function permissionableItems(): Collection
{
return Permission::whereHas('groups', function ($query) {
$query->where('id', $this->id);
})->with('permissionable')->get()->pluck('permissionable');
}
/**
* @todo: This originally was not implemented, so I assume we can just pass this on.
* Perhaps we can add a check here `can:edit-user-groups`?
*/
public function scopeAccessible(Builder $query): Builder
{
return $query;
}
public function getTranslatedAttributes(): array
{
return [];
}
}