1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace A17\Twill\Models; |
4
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Builder; |
6
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes; |
7
|
|
|
use A17\Twill\Models\Behaviors\HasPermissions; |
8
|
|
|
use A17\Twill\Models\Behaviors\IsTranslatable; |
9
|
|
|
use Illuminate\Database\Eloquent\Model as BaseModel; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Group model |
13
|
|
|
* |
14
|
|
|
* @property-read string $titleInBrowser Title |
15
|
|
|
* @property-read string $createdAt Date of creation |
16
|
|
|
* @property-read boolean $canEdit Check if the group is editable (ie. not the Everyone group) |
17
|
|
|
* @property-read boolean $canPublish Check if the group is publishable (ie. not the Everyone group) |
18
|
|
|
* @property-read string $usersCount Formatted number of users in this group (ie. '123 users') |
19
|
|
|
* @method static Builder published() Get published groups |
20
|
|
|
* @method static Builder draft() Get draft groups |
21
|
|
|
* @method static Builder onlyTrashed() Get trashed groups |
22
|
|
|
*/ |
23
|
|
|
class Group extends BaseModel |
24
|
|
|
{ |
25
|
|
|
use HasPermissions, SoftDeletes, IsTranslatable; |
|
|
|
|
26
|
|
|
|
27
|
|
|
public $timestamps = true; |
28
|
|
|
|
29
|
|
|
protected $fillable = [ |
30
|
|
|
'name', |
31
|
|
|
'description', |
32
|
|
|
'published', |
33
|
|
|
'subdomains_access', |
34
|
|
|
]; |
35
|
|
|
|
36
|
|
|
protected $dates = [ |
37
|
|
|
'deleted_at', |
38
|
|
|
]; |
39
|
|
|
|
40
|
|
|
public $checkboxes = ['published']; |
41
|
|
|
|
42
|
|
|
protected $casts = [ |
43
|
|
|
'subdomains_access' => 'array', |
44
|
|
|
]; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Return the Everyone group |
48
|
|
|
* |
49
|
|
|
* @return BaseModel |
50
|
|
|
*/ |
51
|
|
|
public static function getEveryoneGroup() |
52
|
|
|
{ |
53
|
|
|
return Group::where([['is_everyone_group', true], ['name', 'Everyone']])->firstOrFail(); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Return the group title |
58
|
|
|
* |
59
|
|
|
* @return string |
60
|
|
|
*/ |
61
|
|
|
public function getTitleInBrowserAttribute() |
62
|
|
|
{ |
63
|
|
|
return $this->name; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Scope published groups. |
68
|
|
|
* |
69
|
|
|
* @param Builder $query |
70
|
|
|
* @return Builder |
71
|
|
|
*/ |
72
|
|
|
public function scopePublished($query) |
73
|
|
|
{ |
74
|
|
|
return $query->wherePublished(true); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Scope unpublished (draft) groups. |
79
|
|
|
* |
80
|
|
|
* @param Builder $query |
81
|
|
|
* @return Builder |
82
|
|
|
*/ |
83
|
|
|
public function scopeDraft($query) |
84
|
|
|
{ |
85
|
|
|
return $query->wherePublished(false); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Scope trashed groups. |
90
|
|
|
* |
91
|
|
|
* @param Builder $query |
92
|
|
|
* @return Builder |
93
|
|
|
*/ |
94
|
|
|
public function scopeOnlyTrashed($query) |
95
|
|
|
{ |
96
|
|
|
return $query->whereNotNull('deleted_at'); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* User model relationship |
101
|
|
|
* |
102
|
|
|
* @return BelongsToMany|Collection|User[] |
|
|
|
|
103
|
|
|
*/ |
104
|
|
|
public function users() |
105
|
|
|
{ |
106
|
|
|
return $this->belongsToMany(twillModel('user'), 'group_twill_user', 'group_id', 'twill_user_id'); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Check if current group is the Everyone group |
111
|
|
|
* |
112
|
|
|
* @return boolean |
113
|
|
|
*/ |
114
|
|
|
public function isEveryoneGroup() |
115
|
|
|
{ |
116
|
|
|
return $this->id === $this->getEveryoneGroup()->id; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Return the formatted created date |
121
|
|
|
* |
122
|
|
|
* @return string |
123
|
|
|
*/ |
124
|
|
|
public function getCreatedAtAttribute($value) |
125
|
|
|
{ |
126
|
|
|
return \Carbon\Carbon::parse($value)->format('d M Y'); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Check if the group can be edited (not a system group, ie. Everyone) |
131
|
|
|
* |
132
|
|
|
* @return boolean |
133
|
|
|
*/ |
134
|
|
|
public function getCanEditAttribute() |
135
|
|
|
{ |
136
|
|
|
return !$this->isEveryoneGroup(); |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Check if the group can be published (not a system group, ie. Everyone) |
141
|
|
|
* |
142
|
|
|
* @return boolean |
143
|
|
|
*/ |
144
|
|
|
public function getCanPublishAttribute() |
145
|
|
|
{ |
146
|
|
|
return !$this->isEveryoneGroup(); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Return the formatted number of users in this group |
151
|
|
|
* |
152
|
|
|
* @return string |
153
|
|
|
*/ |
154
|
|
|
public function getUsersCountAttribute() |
155
|
|
|
{ |
156
|
|
|
return $this->users->count() . ' users'; |
|
|
|
|
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Return viewable items |
161
|
|
|
* |
162
|
|
|
* @return Collection |
163
|
|
|
*/ |
164
|
|
|
public function viewableItems() |
165
|
|
|
{ |
166
|
|
|
return Permission::where('name', 'view-item')->whereHas('groups', function ($query) { |
167
|
|
|
$query->where('id', $this->id); |
168
|
|
|
})->with('permissionable')->get()->pluck('permissionable'); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Return ids of permissionable items |
173
|
|
|
* |
174
|
|
|
* @return int[] |
175
|
|
|
*/ |
176
|
|
|
public function permissionableIds() |
177
|
|
|
{ |
178
|
|
|
return $this->permissions->pluck('id')->toArray(); |
|
|
|
|
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
/** |
182
|
|
|
* Return permissionable items |
183
|
|
|
* |
184
|
|
|
* @return Collection |
185
|
|
|
*/ |
186
|
|
|
public function permissionableItems() |
187
|
|
|
{ |
188
|
|
|
return Permission::whereHas('groups', function ($query) { |
189
|
|
|
$query->where('id', $this->id); |
190
|
|
|
})->with('permissionable')->get()->pluck('permissionable'); |
191
|
|
|
} |
192
|
|
|
} |
193
|
|
|
|