Skip to content

Commit

Permalink
Merge pull request #7 from Baspa/feature/custom-import-command
Browse files Browse the repository at this point in the history
[Feature] Custom import command
  • Loading branch information
3x1io authored May 23, 2024
2 parents cdc1488 + ee05173 commit d9c6f44
Show file tree
Hide file tree
Showing 7 changed files with 174 additions and 84 deletions.
67 changes: 41 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ this plugin is build in [spatie/laravel-translation-loader](https://github.com/s
composer require tomatophp/filament-translations
```

## Publish Resource
### Publish Resource

you can publish the resource to your project
You can publish the resource to your project using:

```bash
php artisan vendor:publish --tag="filament-translations-migrations"
```

if you need to publish config
If you need to publish config run:

```bash
php artisan vendor:publish --tag="filament-translations-config"
Expand All @@ -43,61 +43,76 @@ Run migration:
php artisan migrate
```

and now clear cache
and now clear cache running:

```bash
php artisan optimize:clear
```

finally reigster the plugin on `/app/Providers/Filament/AdminPanelProvider.php`
### Publish Assets

You can publish views file by use this command:

```bash
php artisan vendor:publish --tag="filament-translations-views"
```

You can publish languages file by use this command:

```bash
php artisan vendor:publish --tag="filament-translations-lang"
```

You can publish migrations file by use this command:

```bash
php artisan vendor:publish --tag="filament-translations-migrations"
```

Finally reigster the plugin on `/app/Providers/Filament/AdminPanelProvider.php`

```php
$panel->plugin(\TomatoPHP\FilamentTranslations\FilamentTranslationsPlugin::make())
```
## Usage

## Scan Using Command Line
### Scan Using Command Line

you can scan your project to get all the languages tags and save them to the database
You can scan your project to get all the languages tags and save them to the database


```bash
php artisan filament-translations:import
```

## Change Scan to work on Queue
### Change Scan to work on Queue

on your config file just change the `use_queue_on_scan` to `true`
In your config file just change the `use_queue_on_scan` to `true`

```php

'use_queue_on_scan' => true,

```

## Publish Assets

you can publish config file by use this command

```bash
php artisan vendor:publish --tag="filament-translations-config"
```
### Custom Import Command

you can publish views file by use this command
You can create your own command to import the translations, add your custom import class to the config file like this:

```bash
php artisan vendor:publish --tag="filament-translations-views"
```php
'path_to_custom_import_command' => ImportTranslations::class,
```

you can publish languages file by use this command
This command will automatically run when you click on the "Scan For New Languages" button in the UI.

```bash
php artisan vendor:publish --tag="filament-translations-lang"
```
### Show or hide buttons in the UI

you can publish migrations file by use this command
You can show or hide the buttons in the UI by changing the config file. By default, all buttons are shown.

```bash
php artisan vendor:publish --tag="filament-translations-migrations"
```php
'show_import_button' => true,
'show_export_button' => false,
'show_scan_button' => false ,
```

## Support
Expand Down
21 changes: 19 additions & 2 deletions config/filament-translations.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@
|
*/

"excludedPaths" => [
],
"excludedPaths" => [],


/*
Expand Down Expand Up @@ -120,4 +119,22 @@
|
*/
'use_queue_on_scan' => true,

/*
|--------------------------------------------------------------------------
|
| Custom import command.
|
*/
'path_to_custom_import_command' => null,

/*
|--------------------------------------------------------------------------
|
| Show buttons in Translation resource.
|
*/
'scan_enabled' => true,
'export_enabled' => true,
'import_enabled' => true,
];
14 changes: 14 additions & 0 deletions src/Console/ImportCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,20 @@ class ImportCommand extends Command
*/
public function handle()
{
if (config('filament-translations.path_to_custom_import_command')) {
$response = spin(
function (){
$command = config('filament-translations.path_to_custom_import_command');
$command = new $command();
$command->handle();
},
'Fetching keys...'
);

$this->info('Done importing');
return;
}

$response = spin(
function (){
$scan = new SaveScan();
Expand Down
7 changes: 7 additions & 0 deletions src/Jobs/ScanJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ class ScanJob implements ShouldQueue
*/
public function handle(): void
{
if (config('filament-translations.path_to_custom_import_command')) {
$command = config('filament-translations.path_to_custom_import_command');
$command = new $command();
$command->handle();
return;
}

$saveScan = new SaveScan();
$saveScan->save();
}
Expand Down
18 changes: 0 additions & 18 deletions src/Resources/TranslationResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,8 @@ public static function form(Form $form): Form
->required()
->disabled()
->maxLength(255),
Forms\Components\TextInput::make('namespace')
->label(trans('filament-translations::translation.namespace'))
->required()
->disabled()
->default('*')
->maxLength(255),
Forms\Components\TextInput::make('key')
->label(trans('filament-translations::translation.key'))
->columnSpan(2)
->disabled()
->required()
->maxLength(255)
Expand All @@ -87,24 +80,13 @@ public static function table(Table $table): Table
{
$table
->columns([
Tables\Columns\TextColumn::make('group')
->label(trans('filament-translations::translation.group'))
->sortable(),
Tables\Columns\TextColumn::make('key')
->label(trans('filament-translations::translation.key'))
->sortable()
->searchable(),
Tables\Columns\TextColumn::make('text')
->label(trans('filament-translations::translation.text'))
->searchable(),
Tables\Columns\TextColumn::make('created_at')
->label(trans('filament-translations::global.created_at'))
->dateTime('M j, Y')
->sortable(),
Tables\Columns\TextColumn::make('updated_at')
->label(trans('filament-translations::global.updated_at'))
->dateTime('M j, Y')
->sortable(),
])
->filters([
Tables\Filters\SelectFilter::make('group')
Expand Down
41 changes: 31 additions & 10 deletions src/Resources/TranslationResource/Pages/ListTranslations.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,16 @@

namespace TomatoPHP\FilamentTranslations\Resources\TranslationResource\Pages;

use App\Models\User;
use Filament\Forms\Components\Select;
use Filament\Notifications\Notification;
use Filament\Pages\Actions\Action;
use Filament\Pages\Actions\ButtonAction;
use Filament\Resources\Pages\ListRecords;
use Maatwebsite\Excel\Facades\Excel;
use TomatoPHP\FilamentTranslations\Exports\TranslationsExport;
use TomatoPHP\FilamentTranslations\Imports\TranslationsImport;
use TomatoPHP\FilamentTranslations\Jobs\ScanJob;
use TomatoPHP\FilamentTranslations\Services\SaveScan;
use TomatoPHP\FilamentTranslations\Resources\TranslationResource;
use function Laravel\Prompts\spin;

class ListTranslations extends ListRecords
{
Expand Down Expand Up @@ -61,14 +59,37 @@ public function import(array $data)
*/
public function scan(): void
{
if(config('filament-translations.use_queue_on_scan')){
dispatch(new ScanJob());
if (config('filament-translations.use_queue_on_scan')) {
$this->dispatchScanJob();
} elseif (config('filament-translations.path_to_custom_import_command')) {
$this->runCustomImportCommand();
} else {
$this->saveScan();
}
else {
$scan = new SaveScan();
$scan->save();
}


$this->notify('success', 'Translation Has Been Loaded');
}

protected function dispatchScanJob(): void
{
dispatch(new ScanJob());
}

protected function runCustomImportCommand(): void
{
spin(
function () {
$command = config('filament-translations.path_to_custom_import_command');
$command = new $command();
$command->handle();
},
'Fetching keys...'
);
}

protected function saveScan(): void
{
$scan = new SaveScan();
$scan->save();
}
}
Loading

0 comments on commit d9c6f44

Please sign in to comment.