mirror of
https://github.com/vitodeploy/vito.git
synced 2025-07-03 23:12:35 +00:00
2.x
This commit is contained in:
73
app/Web/Pages/Settings/Projects/Widgets/AddUser.php
Normal file
73
app/Web/Pages/Settings/Projects/Widgets/AddUser.php
Normal file
@ -0,0 +1,73 @@
|
||||
<?php
|
||||
|
||||
namespace App\Web\Pages\Settings\Projects\Widgets;
|
||||
|
||||
use App\Models\Project;
|
||||
use App\Models\User;
|
||||
use Filament\Forms\Components\Actions\Action;
|
||||
use Filament\Forms\Components\Section;
|
||||
use Filament\Forms\Components\Select;
|
||||
use Filament\Forms\Concerns\InteractsWithForms;
|
||||
use Filament\Forms\Contracts\HasForms;
|
||||
use Filament\Notifications\Notification;
|
||||
use Filament\Widgets\Widget;
|
||||
|
||||
class AddUser extends Widget implements HasForms
|
||||
{
|
||||
use InteractsWithForms;
|
||||
|
||||
protected static string $view = 'web.components.form';
|
||||
|
||||
public Project $project;
|
||||
|
||||
public ?int $user;
|
||||
|
||||
public function mount(Project $project): void
|
||||
{
|
||||
$this->project = $project;
|
||||
}
|
||||
|
||||
public function getFormSchema(): array
|
||||
{
|
||||
return [
|
||||
Section::make()
|
||||
->heading('Add User')
|
||||
->schema([
|
||||
Select::make('user')
|
||||
->name('user')
|
||||
->options(fn () => User::query()->pluck('name', 'id'))
|
||||
->searchable()
|
||||
->rules(\App\Actions\Projects\AddUser::rules($this->project)['user']),
|
||||
])
|
||||
->footerActions([
|
||||
Action::make('add')
|
||||
->label('Add')
|
||||
->action(fn () => $this->submit()),
|
||||
]),
|
||||
];
|
||||
}
|
||||
|
||||
public function submit(): void
|
||||
{
|
||||
$this->authorize('update', $this->project);
|
||||
|
||||
$this->validate();
|
||||
|
||||
app(\App\Actions\Projects\AddUser::class)
|
||||
->add($this->project, [
|
||||
'user' => $this->user,
|
||||
]);
|
||||
|
||||
Notification::make()
|
||||
->title('User added!')
|
||||
->success()
|
||||
->send();
|
||||
|
||||
$this->user = null;
|
||||
}
|
||||
|
||||
public function updated(): void
|
||||
{
|
||||
$this->dispatch('userAdded');
|
||||
}
|
||||
}
|
53
app/Web/Pages/Settings/Projects/Widgets/ProjectUsersList.php
Normal file
53
app/Web/Pages/Settings/Projects/Widgets/ProjectUsersList.php
Normal file
@ -0,0 +1,53 @@
|
||||
<?php
|
||||
|
||||
namespace App\Web\Pages\Settings\Projects\Widgets;
|
||||
|
||||
use App\Models\Project;
|
||||
use App\Models\User;
|
||||
use Filament\Tables;
|
||||
use Filament\Tables\Table;
|
||||
use Filament\Widgets\TableWidget as Widget;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
|
||||
class ProjectUsersList extends Widget
|
||||
{
|
||||
protected $listeners = ['userAdded' => '$refresh'];
|
||||
|
||||
public Project $project;
|
||||
|
||||
public function mount(Project $project): void
|
||||
{
|
||||
$this->project = $project;
|
||||
}
|
||||
|
||||
protected function getTableQuery(): Builder
|
||||
{
|
||||
return User::query()->whereHas('projects', function (Builder $query) {
|
||||
$query->where('project_id', $this->project->id);
|
||||
});
|
||||
}
|
||||
|
||||
protected function getTableColumns(): array
|
||||
{
|
||||
return [
|
||||
Tables\Columns\TextColumn::make('id')->width('20%'),
|
||||
Tables\Columns\TextColumn::make('name')->width('20%'),
|
||||
Tables\Columns\TextColumn::make('email')->width('20%'),
|
||||
];
|
||||
}
|
||||
|
||||
public function getTable(): Table
|
||||
{
|
||||
return $this->table->actions([
|
||||
Tables\Actions\DeleteAction::make()
|
||||
->label('Remove')
|
||||
->modalHeading('Remove user from project')
|
||||
->visible(function ($record) {
|
||||
return $this->authorize('update', $this->project)->allowed() && $record->id !== auth()->id();
|
||||
})
|
||||
->using(function ($record) {
|
||||
$this->project->users()->detach($record);
|
||||
}),
|
||||
])->paginated(false);
|
||||
}
|
||||
}
|
47
app/Web/Pages/Settings/Projects/Widgets/ProjectsList.php
Normal file
47
app/Web/Pages/Settings/Projects/Widgets/ProjectsList.php
Normal file
@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
namespace App\Web\Pages\Settings\Projects\Widgets;
|
||||
|
||||
use App\Models\Project;
|
||||
use App\Web\Pages\Settings\Projects\Settings;
|
||||
use Filament\Tables\Actions\Action;
|
||||
use Filament\Tables\Columns\TextColumn;
|
||||
use Filament\Tables\Table;
|
||||
use Filament\Widgets\TableWidget as Widget;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
|
||||
class ProjectsList extends Widget
|
||||
{
|
||||
protected function getTableQuery(): Builder
|
||||
{
|
||||
return Project::query();
|
||||
}
|
||||
|
||||
protected static ?string $heading = '';
|
||||
|
||||
protected function getTableColumns(): array
|
||||
{
|
||||
return [
|
||||
TextColumn::make('name')
|
||||
->searchable()
|
||||
->sortable(),
|
||||
TextColumn::make('created_at_by_timezone')
|
||||
->label('Created At')
|
||||
->searchable()
|
||||
->sortable(),
|
||||
];
|
||||
}
|
||||
|
||||
public function getTable(): Table
|
||||
{
|
||||
return $this->table
|
||||
->recordUrl(fn (Project $record) => Settings::getUrl(['project' => $record]))
|
||||
->actions([
|
||||
Action::make('settings')
|
||||
->label('Settings')
|
||||
->icon('heroicon-o-cog-6-tooth')
|
||||
->authorize(fn ($record) => auth()->user()->can('update', $record))
|
||||
->url(fn (Project $record) => Settings::getUrl(['project' => $record])),
|
||||
]);
|
||||
}
|
||||
}
|
61
app/Web/Pages/Settings/Projects/Widgets/SelectProject.php
Normal file
61
app/Web/Pages/Settings/Projects/Widgets/SelectProject.php
Normal file
@ -0,0 +1,61 @@
|
||||
<?php
|
||||
|
||||
namespace App\Web\Pages\Settings\Projects\Widgets;
|
||||
|
||||
use App\Models\Project;
|
||||
use Filament\Forms\Components\Select;
|
||||
use Filament\Forms\Concerns\InteractsWithForms;
|
||||
use Filament\Forms\Contracts\HasForms;
|
||||
use Filament\Widgets\Widget;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
|
||||
class SelectProject extends Widget implements HasForms
|
||||
{
|
||||
use InteractsWithForms;
|
||||
|
||||
protected static string $view = 'web.components.form';
|
||||
|
||||
public int|string|null $project;
|
||||
|
||||
protected function getFormSchema(): array
|
||||
{
|
||||
$options = Project::query()
|
||||
->where(function (Builder $query) {
|
||||
if (auth()->user()->isAdmin()) {
|
||||
return;
|
||||
}
|
||||
$query->where('user_id', auth()->id())
|
||||
->orWhereHas('users', fn ($query) => $query->where('user_id', auth()->id()));
|
||||
})
|
||||
->get()
|
||||
->mapWithKeys(fn ($project) => [$project->id => $project->name])
|
||||
->toArray();
|
||||
|
||||
return [
|
||||
Select::make('project')
|
||||
->name('project')
|
||||
->model($this->project)
|
||||
->label('Project')
|
||||
->searchable()
|
||||
->options($options)
|
||||
->searchPrompt('Select a project...')
|
||||
->extraAttributes(['class' => '-mx-2 pointer-choices'])
|
||||
->selectablePlaceholder(false)
|
||||
->live(),
|
||||
];
|
||||
}
|
||||
|
||||
public function updatedProject($value): void
|
||||
{
|
||||
$project = Project::query()->findOrFail($value);
|
||||
$this->authorize('view', $project);
|
||||
auth()->user()->update(['current_project_id' => $value]);
|
||||
|
||||
$this->redirect('/app');
|
||||
}
|
||||
|
||||
public function mount(): void
|
||||
{
|
||||
$this->project = auth()->user()->current_project_id;
|
||||
}
|
||||
}
|
66
app/Web/Pages/Settings/Projects/Widgets/UpdateProject.php
Normal file
66
app/Web/Pages/Settings/Projects/Widgets/UpdateProject.php
Normal file
@ -0,0 +1,66 @@
|
||||
<?php
|
||||
|
||||
namespace App\Web\Pages\Settings\Projects\Widgets;
|
||||
|
||||
use App\Models\Project;
|
||||
use Filament\Forms\Components\Actions\Action;
|
||||
use Filament\Forms\Components\Section;
|
||||
use Filament\Forms\Components\TextInput;
|
||||
use Filament\Forms\Concerns\InteractsWithForms;
|
||||
use Filament\Forms\Contracts\HasForms;
|
||||
use Filament\Notifications\Notification;
|
||||
use Filament\Widgets\Widget;
|
||||
|
||||
class UpdateProject extends Widget implements HasForms
|
||||
{
|
||||
use InteractsWithForms;
|
||||
|
||||
protected static string $view = 'web.components.form';
|
||||
|
||||
public Project $project;
|
||||
|
||||
public string $name;
|
||||
|
||||
public function mount(Project $project): void
|
||||
{
|
||||
$this->project = $project;
|
||||
$this->name = $project->name;
|
||||
}
|
||||
|
||||
public function getFormSchema(): array
|
||||
{
|
||||
return [
|
||||
Section::make()
|
||||
->heading('Project Information')
|
||||
->schema([
|
||||
TextInput::make('name')
|
||||
->name('name')
|
||||
->label('Name')
|
||||
->rules(\App\Actions\Projects\UpdateProject::rules($this->project)['name'])
|
||||
->placeholder('Enter the project name'),
|
||||
])
|
||||
->footerActions([
|
||||
Action::make('save')
|
||||
->label('Save')
|
||||
->action(fn () => $this->submit()),
|
||||
]),
|
||||
];
|
||||
}
|
||||
|
||||
public function submit(): void
|
||||
{
|
||||
$this->authorize('update', $this->project);
|
||||
|
||||
$this->validate();
|
||||
|
||||
app(\App\Actions\Projects\UpdateProject::class)
|
||||
->update($this->project, [
|
||||
'name' => $this->name,
|
||||
]);
|
||||
|
||||
Notification::make()
|
||||
->title('Project updated successfully!')
|
||||
->success()
|
||||
->send();
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user