Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
bobimicroweber committed Sep 13, 2024
1 parent 912b209 commit 830ee55
Show file tree
Hide file tree
Showing 6 changed files with 206 additions and 554 deletions.
198 changes: 198 additions & 0 deletions web/Modules/Customer/App/Filament/Pages/FileManager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
<?php

namespace Modules\Customer\App\Filament\Pages;

use App\Models\Domain;
use App\Models\FileItem;
use App\Models\HostingSubscription;
use Filament\Forms\Components\FileUpload;
use Filament\Forms\Components\Textarea;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Concerns\InteractsWithForms;
use Filament\Forms\Contracts\HasForms;
use Filament\Pages\Page;
use Filament\Tables\Actions\Action;
use Filament\Tables\Actions\ActionGroup;
use Filament\Tables\Actions\BulkAction;
use Filament\Tables\Actions\DeleteAction;
use Filament\Tables\Actions\HeaderActionsPosition;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Concerns\InteractsWithTable;
use Filament\Tables\Contracts\HasTable;
use Filament\Tables\Table;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Number;
use Livewire\Attributes\Url;
use Livewire\Component;

class FileManager extends Page implements HasForms, HasTable
{
use InteractsWithTable;
use InteractsWithForms;

protected static ?string $navigationIcon = 'heroicon-o-folder';

protected static string $view = 'customer::filament.pages.file-manager';

protected static ?string $navigationGroup = 'Hosting';

protected static ?int $navigationSort = 3;

public string $disk = 'local';

#[Url(except: '')]
public string $path = '';

protected $listeners = ['updatePath' => '$refresh'];


public function table(Table $table): Table
{
$findDomain = Domain::select(['home_root', 'hosting_subscription_id', 'is_main'])
// ->where('hosting_subscription_id', $this->record->id)
->where('is_main',1)
->first();

$this->disk = $findDomain->home_root;

$storage = Storage::build([
'driver' => 'local',
'throw' => false,
'root' => $this->disk,
]);

return $table
// ->deferLoading()
->heading($this->disk .'/'. $this->path ?: 'Root')
->query(
FileItem::queryForDiskAndPath($this->disk, $this->path)
)
->paginated(false)
->columns([
TextColumn::make('name')
->icon(fn ($record): string => match ($record->type) {
'Folder' => 'heroicon-o-folder',
default => 'heroicon-o-document'
})
->iconColor(fn ($record): string => match ($record->type) {
'Folder' => 'warning',
default => 'gray',
})
->action(function (FileItem $record) {
if ($record->isFolder()) {
$this->path = $record->path;
$this->dispatch('updatePath');
}
}),
TextColumn::make('dateModified')
->dateTime(),
TextColumn::make('size')
->formatStateUsing(fn ($state) => $state ? Number::fileSize($state) : '0.0KB'),
TextColumn::make('type'),
])
->actions([

ActionGroup::make([
// ViewAction::make('open')
// ->label('Open')
// ->hidden(fn (FileItem $record): bool => ! $record->canOpen())
// ->url(fn (FileItem $record): string => $storage->url($record->path))
// ->openUrlInNewTab(),
Action::make('download')
->label('Download')
->icon('heroicon-o-document-arrow-down')
->hidden(fn (FileItem $record): bool => $record->isFolder())
->action(fn (FileItem $record) => $storage->download($record->path)),
DeleteAction::make('delete')
->successNotificationTitle('File deleted')
->hidden(fn (FileItem $record): bool => $record->isPreviousPath())
->action(function (FileItem $record, Action $action) {
if ($record->delete()) {
$action->sendSuccessNotification();
}

}),
]),
])
->bulkActions([
BulkAction::make('delete')
->icon('heroicon-o-trash')
->color('danger')
->requiresConfirmation()
->successNotificationTitle('Files deleted')
->deselectRecordsAfterCompletion()
->action(function (Collection $records, BulkAction $action) {
$records->each(fn (FileItem $record) => $record->delete());
$action->sendSuccessNotification();
}),
])
->checkIfRecordIsSelectableUsing(fn (FileItem $record): bool => ! $record->isPreviousPath())
->headerActionsPosition(HeaderActionsPosition::Bottom)
->headerActions([

// Action::make('home')
// ->label('Home')
// ->action(fn () => $this->path = '')
// ->icon('heroicon-o-home'),
//
// Action::make('back')
// ->label('Back')
// ->action(fn () => $this->path = dirname($this->path))
// ->icon('heroicon-o-arrow-left'),

Action::make('create_folder')
->label('Create Folder')
->icon('heroicon-o-folder-plus')
->form([
TextInput::make('name')
->label('Folder name')
->placeholder('Folder name')
->required(),
])
->successNotificationTitle('Folder created')
->action(function (array $data, Component $livewire, Action $action) use($storage) : void {
$storage->makeDirectory($livewire->path.'/'.$data['name']);

$this->resetTable();
$action->sendSuccessNotification();
}),

Action::make('create_file')
->label('Create File')
->icon('heroicon-o-document-plus')
->form([
TextInput::make('file_name')
->label('File name')
->placeholder('File name')
->required(),
Textarea::make('file_content')
->label('Content')
->required(),
])
->successNotificationTitle('File created')
->action(function (array $data, Component $livewire, Action $action) use($storage) : void {

$storage->put($livewire->path.'/'.$data['file_name'], $data['file_content']);

$this->resetTable();
$action->sendSuccessNotification();
}),

Action::make('upload_file')
->label('Upload files')
->icon('heroicon-o-document-arrow-up')
->color('info')
->form([
FileUpload::make('files')
->required()
->multiple()
->previewable(false)
->preserveFilenames()
->disk($this->disk)
->directory($this->path),
]),
]);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<x-filament-panels::page>

<div>
{{ $this->table }}
</div>

</x-filament-panels::page>
12 changes: 1 addition & 11 deletions web/app/Filament/Resources/HostingSubscriptionResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -190,11 +190,7 @@ public static function getRecordSubNavigation(Page $page): array
{
return $page->generateNavigationItems([
// Pages\ViewHos::class,
Pages\EditHostingSubscription::class,
Pages\ManageHostingSubscriptionDatabases::class,
Pages\ManageHostingSubscriptionBackups::class,
// Pages\ManageHostingSubscriptionFtpAccounts::class,
Pages\ManageHostingSubscriptionFileManager::class
Pages\EditHostingSubscription::class
]);
}

Expand All @@ -208,15 +204,9 @@ public static function getRelations(): array
public static function getPages(): array
{
return [
// 'index' => Pages\ManageHostingSubscriptions::route('/'),
'index' => Pages\ListHostingSubscriptions::route('/'),
'create' => Pages\CreateHostingSubscription::route('/create'),
'edit' => Pages\EditHostingSubscription::route('/{record}/edit'),
// 'view' => Pages\ViewHostingSubscription::route('/{record}'),
'databases' => Pages\ManageHostingSubscriptionDatabases::route('/{record}/databases'),
'backups' => Pages\ManageHostingSubscriptionBackups::route('/{record}/backups'),
// 'ftp-accounts' => Pages\ManageHostingSubscriptionFtpAccounts::route('/{record}/ftp-accounts'),
'file-manager' => Pages\ManageHostingSubscriptionFileManager::route('/{record}/file-manager'),
];
}

Expand Down
Loading

0 comments on commit 830ee55

Please sign in to comment.