1010use Filament \Actions \Action ;
1111use Filament \Forms \Components \DateTimePicker ;
1212use Filament \Forms \Components \Grid ;
13- use Filament \Forms \Components \Hidden ;
1413use Filament \Forms \Components \Placeholder ;
1514use Filament \Forms \Components \Select ;
1615use Filament \Forms \Components \ToggleButtons ;
@@ -57,7 +56,8 @@ public static function getSchema(): array
5756 && \Str::contains ($ _SERVER ['HTTP_REFERER ' ], 'my-soldiers-constraint ' )
5857 )
5958 ->options (fn () => CommanderSoldier::getCommanderSoldier ())
60- ->afterStateUpdated (fn ($ state ) => session ()->put ('soldier_id ' , $ state ))
59+ ->afterStateUpdated (fn ($ state ) => session ()->put ('soldiers_ids ' , $ state ))
60+ ->multiple (fn ($ get ) => $ get ('id ' ) === null )
6161 ->required (),
6262 ToggleButtons::make ('constraint_type ' )
6363 ->required ()
@@ -75,12 +75,6 @@ public static function getSchema(): array
7575 ->options (fn (Constraint $ constraint ) => [
7676 $ constraint ->constraint_type ->getLabel (),
7777 ]),
78- // Hidden::make('start_date')
79- // ->label(__('Start date'))
80- // ->required(),
81- // Hidden::make('end_date')
82- // ->label(__('End date'))
83- // ->required(),
8478 Placeholder::make ('' )
8579 ->content (__ ('The constraint will only be approved after approval by the commander ' ))
8680 ->visible (fn () => auth ()->user ()->getRoleNames ()->count () === 1 )
@@ -302,23 +296,33 @@ public static function updateDates(callable $set, $state, Get $get)
302296 }
303297 }
304298
305- protected static function booted ( )
299+ public static function createConstraint ( $ data )
306300 {
307- static ::creating (function ($ constraint ) {
308- $ constraint ->soldier_id = $ constraint ->soldier_id ?: ($ constraint ->getCurrentUserSoldier () ?: null );
309- session ()->put ('soldier_id ' , null );
301+ $ soldiers = self ::getCurrentUserSoldier ();
302+ $ soldiers = gettype ($ soldiers ) == 'integer ' ? [$ soldiers ] : $ soldiers ;
303+ collect ($ soldiers )->each (function ($ soldierId ) use ($ data ) {
304+ $ constraint = new Constraint ;
305+ $ constraint ->soldier_id = $ soldierId ;
306+ $ constraint ->constraint_type = $ data ['constraint_type ' ];
307+ $ constraint ->start_date = $ data ['start_date ' ];
308+ $ constraint ->end_date = $ data ['end_date ' ];
309+ $ constraint ->save ();
310310 });
311+ session ()->put ('soldiers_ids ' , null );
312+ }
311313
314+ protected static function booted ()
315+ {
312316 static ::updating (function ($ constraint ) {
313317 $ constraint ->soldier_id = $ constraint ->soldier_id ?: ($ constraint ->getCurrentUserSoldier () ?: null );
314- session ()->put ('soldier_id ' , null );
318+ session ()->put ('soldiers_ids ' , null );
315319 });
316320 }
317321
318- private function getCurrentUserSoldier ()
322+ protected static function getCurrentUserSoldier ()
319323 {
320- if (session ()->get ('soldier_id ' )) {
321- return session ()->get ('soldier_id ' );
324+ if (session ()->get ('soldiers_ids ' )) {
325+ return session ()->get ('soldiers_ids ' );
322326 }
323327 $ user = auth ()->user ();
324328 if ($ user && $ user ->userable instanceof Soldier) {
0 commit comments