33namespace App \Http \Livewire ;
44
55use App \Core \CrudDialogHelper ;
6+ use App \Models \Company ;
67use App \Models \Project ;
78use App \Models \User ;
89use App \Notifications \ProjectCreatedNotification ;
10+ use Closure ;
911use Filament \Forms \Components \Grid ;
1012use Filament \Forms \Components \RichEditor ;
1113use Filament \Forms \Components \Select ;
1214use Filament \Forms \Components \TextInput ;
1315use Filament \Forms \Concerns \InteractsWithForms ;
1416use Filament \Forms \Contracts \HasForms ;
15- use Filament \Notifications \Actions \Action ;
1617use Filament \Notifications \Notification ;
1718use Livewire \Component ;
1819
@@ -32,6 +33,7 @@ public function mount(): void
3233 'ticket_prefix ' => $ this ->project ->ticket_prefix ,
3334 'description ' => $ this ->project ->description ,
3435 'owner_id ' => $ this ->project ->owner_id ?? auth ()->user ()->id ,
36+ 'company_id ' => $ this ->project ->company_id
3537 ]);
3638 }
3739
@@ -48,11 +50,40 @@ public function render()
4850 protected function getFormSchema (): array
4951 {
5052 return [
51- Select::make ('owner_id ' )
52- ->label (__ ('Owner ' ))
53- ->required ()
54- ->searchable ()
55- ->options (User::all ()->pluck ('name ' , 'id ' )),
53+ Grid::make ()
54+ ->schema ([
55+ Select::make ('owner_id ' )
56+ ->label (__ ('Owner ' ))
57+ ->required ()
58+ ->searchable ()
59+ ->reactive ()
60+ ->options (function () {
61+ $ query = User::query ();
62+ if (auth ()->user ()->can ('View company users ' ) && !auth ()->user ()->can ('View all users ' )) {
63+ $ query ->whereHas (
64+ 'companies ' ,
65+ fn ($ query ) => $ query ->whereIn (
66+ 'companies.id ' ,
67+ auth ()->user ()->ownCompanies ->pluck ('id ' )->toArray ()
68+ )
69+ )->orWhere ('id ' , auth ()->user ()->id );
70+ }
71+ return $ query ->get ()->pluck ('name ' , 'id ' )->toArray ();
72+ }),
73+
74+ Select::make ('company_id ' )
75+ ->label (__ ('Company ' ))
76+ ->searchable ()
77+ ->options (function (Closure $ get ) {
78+ $ query = Company::query ();
79+ if ($ get ('owner_id ' )) {
80+ $ query ->where ('responsible_id ' , $ get ('owner_id ' ));
81+ } elseif (auth ()->user ()->can ('View own companies ' )) {
82+ $ query ->where ('responsible_id ' , auth ()->user ()->id );
83+ }
84+ return $ query ->get ()->pluck ('name ' , 'id ' )->toArray ();
85+ }),
86+ ]),
5687
5788 Grid::make (3 )
5889 ->schema ([
@@ -92,7 +123,8 @@ public function save(): void
92123 'name ' => $ data ['name ' ],
93124 'description ' => $ data ['description ' ],
94125 'owner_id ' => $ data ['owner_id ' ],
95- 'ticket_prefix ' => $ data ['ticket_prefix ' ]
126+ 'ticket_prefix ' => $ data ['ticket_prefix ' ],
127+ 'company_id ' => $ data ['company_id ' ],
96128 ]);
97129 Notification::make ()
98130 ->success ()
@@ -103,6 +135,7 @@ public function save(): void
103135 $ this ->project ->name = $ data ['name ' ];
104136 $ this ->project ->description = $ data ['description ' ];
105137 $ this ->project ->owner_id = $ data ['owner_id ' ];
138+ $ this ->project ->company_id = $ data ['company_id ' ];
106139 $ this ->project ->ticket_prefix = $ data ['ticket_prefix ' ];
107140 $ this ->project ->save ();
108141 Notification::make ()
0 commit comments