Skip to content

Instantly share code, notes, and snippets.

@bezhanSalleh
Last active July 31, 2022 23:26
Show Gist options
  • Save bezhanSalleh/bda7d8db237a0c45549b63dafaa387c1 to your computer and use it in GitHub Desktop.
Save bezhanSalleh/bda7d8db237a0c45549b63dafaa387c1 to your computer and use it in GitHub Desktop.
Filament Dynamic UI for Role and Permissions | Toggel + Checkboxes
public static function form(Form $form): Form
{
return $form
->schema([
Forms\Components\Grid::make()
->schema([
Forms\Components\Card::make()
->schema([
Forms\Components\TextInput::make('name')
->required()
->maxLength(255),
Forms\Components\Toggle::make('select_all')
->onIcon('heroicon-s-shield-check')
->offIcon('heroicon-s-shield-exclamation')
->label('Select All')
->helperText('Enable all Permissions for this role.')
->reactive()
->afterStateUpdated(function (Closure $set, $state) {
foreach (static::getEntities() as $entity) {
$set($entity, $state);
foreach(static::getPermissions() as $perm)
{
$set($entity.'_'.$perm, $state);
}
}
})
]),
]),
Forms\Components\Grid::make([
'sm' => 2,
'lg' => 3,
])
->schema(static::getEntitySchema())
->columns([
'sm' => 2,
'lg' => 3
])
]);
}
protected static function getEntities(): ?array
{
return collect(Filament::getResources())
->reduce(function ($options, $resource) {
$option = Str::before(Str::afterLast($resource,'\\'),'Resource');
$options[$option] = $option;
return $options;
}, []);
}
protected static function getPermissions(): array
{
return ['view','viewAny','create','delete','deleteAny','update'];
}
public static function getEntitySchema()
{
return collect(static::getEntities())->reduce(function($entities,$entity) {
$entities[] = Forms\Components\Card::make()
->schema([
Forms\Components\Toggle::make($entity)
->onIcon('heroicon-s-lock-open')
->offIcon('heroicon-s-lock-closed')
->reactive()
->afterStateUpdated(function (Closure $set,Closure $get, $state) use($entity) {
collect(static::getPermissions())->each(function ($permission) use($set, $entity, $state) {
$set($entity.'_'.$permission, $state);
});
if (! $state) {
$set('select_all',false);
}
$entityStates = [];
foreach(static::getEntities() as $ent) {
$entityStates [] = $get($ent);
}
if (in_array(false,$entityStates, true) === false) {
$set('select_all', true); // if all toggles on => turn select_all on
}
if (in_array(true,$entityStates, true) === false) {
$set('select_all', false); // if even one toggle off => turn select_all off
}
}),
Forms\Components\Fieldset::make('Permissions')
->extraAttributes(['class' => 'text-primary-600','style' => 'border-color:var(--primary)'])
->columns([
'default' => 2,
'xl' => 3
])
->schema(static::getPermissionsSchema($entity))
])
->columns(2)
->columnSpan(1);
return $entities;
},[]);
}
public static function getPermissionsSchema($entity)
{
return collect(static::getPermissions())->reduce(function ($permissions, $permission) use ($entity) {
$permissions[] = Forms\Components\Checkbox::make($entity.'_'.$permission)
->label($permission)
->extraAttributes(['class' => 'text-primary-600'])
->reactive()
->afterStateUpdated(function (Closure $set, Closure $get, $state) use($entity){
$permissionStates = [];
foreach(static::getPermissions() as $perm) {
$permissionStates [] = $get($entity.'_'.$perm);
}
if (in_array(false,$permissionStates, true) === false) {
$set($entity, true); // if all permissions true => turn toggle on
}
if (in_array(true,$permissionStates, true) === false) {
$set($entity, false); // if even one false => turn toggle off
}
if(!$state) {
$set($entity,false);
$set('select_all',false);
}
$entityStates = [];
foreach(static::getEntities() as $ent) {
$entityStates [] = $get($ent);
}
if (in_array(false,$entityStates, true) === false) {
$set('select_all', true); // if all toggles on => turn select_all on
}
if (in_array(true,$entityStates, true) === false) {
$set('select_all', false); // if even one toggle off => turn select_all off
}
});
return $permissions;
},[]);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment