Skip to content

Commit

Permalink
[Feature] Create Sub Folder inside Folder #3
Browse files Browse the repository at this point in the history
  • Loading branch information
3x1io committed Jul 8, 2024
1 parent 9991918 commit 7ce245d
Show file tree
Hide file tree
Showing 9 changed files with 396 additions and 152 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,17 @@ public function boot() {

please note that the `name ` of the component will be the same name of the collection.

## Allow Sub Folders

you can allow create and manage subfolders on your media manager on `/app/Providers/Filament/AdminPanelProvider.php`

```php
->plugin(
\TomatoPHP\FilamentMediaManager\FilamentMediaManagerPlugin::make()
->allowSubFolders()
)
```

## Publish Assets

you can publish config file by use this command
Expand Down
6 changes: 6 additions & 0 deletions resources/lang/ar/messages.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
<?php

return [
'empty' => [
'title' => "لا يوجد وسائط أو مجلدات",
],
'folders' => [
'title' => 'مدير الوسائط',
'single' => 'مجلد',
Expand All @@ -25,6 +28,9 @@
'collection_name' => 'اسم المجموعة',
],
'actions' => [
'sub_folder'=> [
'label' => "إنشاء مجلد فرعي"
],
'create' => [
'label' => 'إضافة وسائط',
'form' => [
Expand Down
6 changes: 6 additions & 0 deletions resources/lang/en/messages.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
<?php

return [
'empty' => [
'title' => "No Media or Folders Found",
],
'folders' => [
'title' => 'Media Manager',
'single' => 'Folder',
Expand All @@ -25,6 +28,9 @@
'collection_name' => 'Collection Name',
],
'actions' => [
'sub_folder'=> [
'label' => "Create Sub Folder"
],
'create' => [
'label' => 'Add Media',
'form' => [
Expand Down
353 changes: 202 additions & 151 deletions resources/views/pages/media.blade.php

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions src/FilamentMediaManagerPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,19 @@

class FilamentMediaManagerPlugin implements Plugin
{
public ?bool $allowSubFolders = false;

public function getId(): string
{
return 'filament-media-manager';
}

public function allowSubFolders(bool $condation = true): static
{
$this->allowSubFolders = $condation;
return $this;
}

public function register(Panel $panel): void
{
$panel->resources([
Expand Down
2 changes: 1 addition & 1 deletion src/Models/Folder.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class Folder extends Model implements HasMedia
use InteractsWithMedia;

protected $fillable = [
'parent_id',
'model_type',
'model_id',
'name',
Expand All @@ -34,5 +35,4 @@ public function model()
{
return $this->morphTo();
}

}
1 change: 1 addition & 0 deletions src/Resources/FolderResource/Pages/ListFolders.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use Filament\Resources\Pages\ManageRecords;
use Illuminate\Validation\ValidationException;
use TomatoPHP\FilamentMediaManager\Models\Folder;
use TomatoPHP\FilamentMediaManager\Models\Media;
use TomatoPHP\FilamentMediaManager\Resources\FolderResource;
use Filament\Actions;
use Filament\Resources\Pages\ListRecords;
Expand Down
1 change: 1 addition & 0 deletions src/Resources/MediaResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public static function table(Table $table): Table
}
}
})
->emptyState(fn()=>view('filament-media-manager::pages.media'))
->content(function () {
return view('filament-media-manager::pages.media');
})
Expand Down
160 changes: 160 additions & 0 deletions src/Resources/MediaResource/Pages/ListMedia.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Illuminate\Support\Str;
use TomatoPHP\FilamentIcons\Components\IconPicker;
use TomatoPHP\FilamentMediaManager\Models\Folder;
use TomatoPHP\FilamentMediaManager\Models\Media;
use TomatoPHP\FilamentMediaManager\Resources\MediaResource;
use Filament\Actions;
use Filament\Resources\Pages\ListRecords;
Expand Down Expand Up @@ -61,6 +62,71 @@ protected function getHeaderActions(): array
$folder_id = $this->folder_id;
$form = config('filament-media-manager.model.folder')::find($folder_id)?->toArray();
return [
Actions\Action::make('create_sub_folder')
->hidden(fn()=> !filament('filament-media-manager')->allowSubFolders)
->mountUsing(function () use ($folder_id){
session()->put('folder_id', $folder_id);
})
->color('info')
->label(trans('filament-media-manager::messages.media.actions.sub_folder.label'))
->icon('heroicon-o-folder-minus')
->form([
Forms\Components\TextInput::make('name')
->label(trans('filament-media-manager::messages.folders.columns.name'))
->columnSpanFull()
->lazy()
->afterStateUpdated(function (Forms\Set $set, Forms\Get $get) {
$set('collection', Str::slug($get('name')));
})
->required()
->maxLength(255),
Forms\Components\TextInput::make('collection')
->label(trans('filament-media-manager::messages.folders.columns.collection'))
->columnSpanFull()
->unique(Folder::class)
->required()
->maxLength(255),
Forms\Components\Textarea::make('description')
->label(trans('filament-media-manager::messages.folders.columns.description'))
->columnSpanFull()
->maxLength(255),
IconPicker::make('icon')
->label(trans('filament-media-manager::messages.folders.columns.icon')),
Forms\Components\ColorPicker::make('color')
->label(trans('filament-media-manager::messages.folders.columns.color')),
Forms\Components\Toggle::make('is_protected')
->label(trans('filament-media-manager::messages.folders.columns.is_protected'))
->live()
->columnSpanFull(),
Forms\Components\TextInput::make('password')
->label(trans('filament-media-manager::messages.folders.columns.password'))
->hidden(fn(Forms\Get $get) => !$get('is_protected'))
->password()
->revealable()
->required()
->maxLength(255),
Forms\Components\TextInput::make('password_confirmation')
->label(trans('filament-media-manager::messages.folders.columns.password_confirmation'))
->hidden(fn(Forms\Get $get) => !$get('is_protected'))
->password()
->required()
->revealable()
->maxLength(255)
])
->action(function (array $data) use ($folder_id) {
$folder = Folder::find($folder_id);
if($folder){
$data['model_id'] = $folder_id;
$data['model_type'] = Folder::class;
Folder::query()->create($data);
}

Notification::make()
->title('Folder Created')
->body('Folder Created Successfully')
->success()
->send();
}),
Actions\Action::make('create_media')
->mountUsing(function () use ($folder_id){
session()->put('folder_id', $folder_id);
Expand Down Expand Up @@ -179,4 +245,98 @@ protected function getHeaderActions(): array
})
];
}

public function folderAction(?Folder $item=null){
return Actions\Action::make('folderAction')
->requiresConfirmation(function (array $arguments){
if($arguments['record']['is_protected']){
return true;
}
else {
return false;
}
})
->form(function (array $arguments){
if($arguments['record']['is_protected']){
return [
TextInput::make('password')
->password()
->revealable()
->required()
->maxLength(255),
];
}
else {
return null;
}
})
->action(function (array $arguments, array $data){
if($arguments['record']['is_protected']){
if($arguments['record']['password'] != $data['password']){
Notification::make()
->title('Password is incorrect')
->danger()
->send();

return ;
}
else {
session()->put('folder_password', $data['password']);
}
}
if(!$arguments['record']['model_type']){
if(filament()->getTenant()){
return redirect()->to(url(filament()->getCurrentPanel()->getId() .'/'. filament()->getTenant()->id . '/media?folder_id='.$arguments['record']['id']));
}
else {
return redirect()->route('filament.'.filament()->getCurrentPanel()->getId().'.resources.media.index', ['folder_id' => $arguments['record']['id']]);
}
}
if(!$arguments['record']['model_id'] && !$arguments['record']['collection']){
if(filament()->getTenant()){
return redirect()->to(url(filament()->getCurrentPanel()->getId() .'/'. filament()->getTenant()->id . '/folders?model_type='.$arguments['record']['model_type']));
}
else {
return redirect()->route('filament.'.filament()->getCurrentPanel()->getId().'.resources.folders.index', ['model_type' => $arguments['record']['model_type']]);
}
}
else if(!$arguments['record']['model_id']){
if(filament()->getTenant()){
return redirect()->to(url(filament()->getCurrentPanel()->getId() .'/'. filament()->getTenant()->id . '/folders?model_type='.$arguments['record']['model_type'].'&collection='.$arguments['record']['collection']));
}
else {
return redirect()->route('filament.'.filament()->getCurrentPanel()->getId().'.resources.folders.index', ['model_type' => $arguments['record']['model_type'], 'collection' => $arguments['record']['collection']]);
}
}
else {
if(filament()->getTenant()) {
return redirect()->to(url(filament()->getCurrentPanel()->getId() .'/'. filament()->getTenant()->id . '/media?folder_id='.$arguments['record']['id']));
}
else {
return redirect()->route('filament.'.filament()->getCurrentPanel()->getId().'.resources.media.index', ['folder_id' => $arguments['record']['id']]);
}
}
})
->view('filament-media-manager::pages.folder-action', ['item' => $item]);
}


public function deleteMedia()
{
return Actions\Action::make('deleteMedia')
->label('Delete Media')
->icon('heroicon-s-trash')
->color('danger')
->requiresConfirmation()
->action(function (array $arguments) {
$media = Media::find($arguments['record']['id']);
$media->delete();

Notification::make()
->title('Folder deleted successfully')
->success()
->send();
});

}
}

0 comments on commit 7ce245d

Please sign in to comment.