Skip to content

Commit

Permalink
🚧 Add import Voyager Action
Browse files Browse the repository at this point in the history
+Console Command
+Export DataTypeTemplate
+Export AllDataTypesTemplate
  • Loading branch information
rxcod9 committed Mar 7, 2022
1 parent e1e6dbe commit 452a7d4
Show file tree
Hide file tree
Showing 18 changed files with 1,516 additions and 7 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/php-cs-fixer.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:

steps:
- name: Checkout code
uses: actions/checkout@v2
uses: actions/checkout@v3

- name: Fix style
uses: docker://oskarstark/php-cs-fixer-ga
Expand All @@ -21,7 +21,7 @@ jobs:
id: extract_branch

- name: Commit changes
uses: stefanzweifel/git-auto-commit-action@v2.3.0
uses: stefanzweifel/git-auto-commit-action@v4.13.1
with:
commit_message: Fix styling
branch: ${{ steps.extract_branch.outputs.branch }}
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jobs:
run: sudo apt-get update --fix-missing

- name: Checkout code
uses: actions/checkout@v2
uses: actions/checkout@v3

- name: Setup PHP
uses: shivammathur/setup-php@v2
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
vendor/*
.php-cs-fixer.cache
composer.lock
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ This [Laravel](https://laravel.com/)/[Voyager](https://voyager.devdojo.com/) mod

By 🐼 [Ramakant Gangwar](https://github.com/rxcod9).

[![Screenshot](https://raw.githubusercontent.com/rxcod9/joy-voyager-import/main/cover.jpg)](https://joy-voyager-import.herokuapp.com)
[![Screenshot](https://raw.githubusercontent.com/rxcod9/joy-voyager-import/main/cover.jpg)](https://joy-voyager.herokuapp.com/)

[![Latest Version](https://img.shields.io/github/v/release/rxcod9/joy-voyager-import?style=flat-square)](https://github.com/rxcod9/joy-voyager-import/releases)
![GitHub Workflow Status](https://img.shields.io/github/workflow/status/rxcod9/joy-voyager-import/run-tests?label=tests)
Expand Down Expand Up @@ -44,7 +44,7 @@ In order to override views delivered by Voyager DataTable, copy contents from ``

## Working Example

You can try laravel demo here [https://joy-voyager-import.herokuapp.com/api/documentation](https://joy-voyager-import.herokuapp.com/api/documentation).
You can try laravel demo here [https://joy-voyager.herokuapp.com/admin/users](https://joy-voyager.herokuapp.com/admin/users).

## Documentation

Expand Down
8 changes: 6 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@
"require": {
"php": "^7.3|^8.0",
"illuminate/support": "^7|^8",
"joy/voyager-core": "dev-main",
"joy/voyager-core": "^1.0",
"larapack/hooks": "^1.0.5",
"maatwebsite/excel": "^3.1",
"tcg/voyager": "^1.4"
},
"require-dev": {
Expand Down Expand Up @@ -60,7 +61,10 @@
}
},
"config": {
"sort-packages": true
"sort-packages": true,
"allow-plugins": {
"henzeb/composer-link": true
}
},
"minimum-stability": "dev",
"prefer-stable": true
Expand Down
44 changes: 44 additions & 0 deletions config/voyager-import.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,29 @@

return [

/*
* If enabled for voyager-import package.
*/
'enabled' => env('VOYAGER_IMPORT_ENABLED', true),

/*
| Here you can specify for which data type slugs import is enabled
|
| Supported: "*", or data type slugs "users", "roles"
|
*/

'allowed_slugs' => array_filter(explode(',', env('VOYAGER_IMPORT_ALLOWED_SLUGS', '*'))),

/*
| Here you can specify for which data type slugs import is not allowed
|
| Supported: "*", or data type slugs "users", "roles"
|
*/

'not_allowed_slugs' => array_filter(explode(',', env('VOYAGER_IMPORT_NOT_ALLOWED_SLUGS', ''))),

/*
* The config_key for voyager-import package.
*/
Expand All @@ -24,4 +47,25 @@
'controllers' => [
'namespace' => 'Joy\\VoyagerImport\\Http\\Controllers',
],

/*
| The default import disk.
*/
'disk' => env('VOYAGER_IMPORT_DISK', null),

/*
| The default import readerType.
|
| Supported: "Xlsx", "Csv", "Csv", "Ods", "Xls",
| "Slk", "Xml", "Gnumeric", "Html", "Mpdf", "Dompdf", "Tcpdf"
*/
'readerType' => env('VOYAGER_IMPORT_READER_TYPE', 'Xlsx'),

/*
| The default import writerType.
|
| Supported: "Xlsx", "Csv", "Csv", "Ods", "Xls",
| "Slk", "Xml", "Gnumeric", "Html", "Mpdf", "Dompdf", "Tcpdf"
*/
'writerType' => env('VOYAGER_IMPORT_WRITER_TYPE', 'Xlsx'),
];
8 changes: 8 additions & 0 deletions renovate.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
{
"packageRules": [
{
"matchPackagePatterns": [
"*"
],
"rangeStrategy": "replace"
}
],
"extends": [
"config:base"
]
Expand Down
99 changes: 99 additions & 0 deletions src/Actions/ImportAction.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?php

namespace Joy\VoyagerImport\Actions;

use Joy\VoyagerImport\Imports\DataTypeImport;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\Str;
use TCG\Voyager\Actions\AbstractAction;
use TCG\Voyager\Facades\Voyager;
use Maatwebsite\Excel\Excel;

class ImportAction extends AbstractAction
{
/**
* Optional File Name
*/
protected $fileName;

/**
* Optional Reader Type
*/
protected $readerType;

public function getTitle()
{
return 'Import';
}

public function getIcon()
{
return 'voyager-upload';
}

public function getPolicy()
{
return 'write';
}

public function getAttributes()
{
return [
'class' => 'btn btn-sm btn-primary',
];
}

public function getDefaultRoute()
{
// return route('my.route');
}

public function shouldActionDisplayOnDataType()
{
return config('joy-voyager-import.enabled', true) !== false
&& isInPatterns(
$this->dataType->slug,
config('joy-voyager-import.allowed_slugs', ['*'])
)
&& !isInPatterns(
$this->dataType->slug,
config('joy-voyager-import.not_allowed_slugs', [])
);
}

public function massAction($ids, $comingFrom)
{
// GET THE SLUG, ex. 'posts', 'pages', etc.
$slug = $this->getSlug(request());

// GET THE DataType based on the slug
$dataType = Voyager::model('DataType')->where('slug', '=', $slug)->first();

// Check permission
Gate::authorize('browse', app($dataType->model_name));

$readerType = $this->readerType ?? config('joy-voyager-import.readerType', Excel::XLSX);
$fileName = $this->fileName ?? ($dataType->slug . '.' . Str::lower($readerType));

return (new DataTypeImport(
$dataType,
array_filter($ids),
request()->all(),
))->import(
$fileName,
$readerType
);
}

protected function getSlug(Request $request)
{
if (isset($this->slug)) {
$slug = $this->slug;
} else {
$slug = explode('.', $request->route()->getName())[1];
}

return $slug;
}
}
73 changes: 73 additions & 0 deletions src/Console/Commands/AllDataTypesImport.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

namespace Joy\VoyagerImport\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use Joy\VoyagerImport\Imports\AllDataTypesImport as ExportsAllDataTypesExport;
use Maatwebsite\Excel\Excel;
use Symfony\Component\Console\Input\InputOption;

class AllDataTypesImport extends Command
{
protected $name = 'joy-import:all';

protected $description = 'Joy Voyager all DataTypes importer';

public function handle()
{
$this->output->title('Starting import');
$this->importAllDataTypes(
$this->option('disk'),
$this->option('readerType')
);
$this->output->success('Import successful');
}

protected function importAllDataTypes(
string $disk = null,
string $readerType = Excel::XLSX
) {
$path = 'public/imports/' . (($filePath ?? 'import-all') . '-' . date('YmdHis') . '.' . Str::lower($readerType));

$url = config('app.url') . Storage::disk($disk)->url($path);

$this->output->info(sprintf(
'Importing to >>' . PHP_EOL . 'path : %s' . PHP_EOL . 'url : %s',
storage_path($path),
$url
));

(new ImportsAllDataTypesImport())->withOutput($this->output)->import(
$path,
$disk,
$readerType
);
}

/**
* Get the console command options.
*
* @return array
*/
protected function getOptions()
{
return [
[
'disk',
'd',
InputOption::VALUE_OPTIONAL,
'The disk to where you want to import',
config('joy-voyager-import.disk')
],
[
'readerType',
'w',
InputOption::VALUE_OPTIONAL,
'The readerType in which format you want to import',
config('joy-voyager-import.readerType', 'Xlsx')
],
];
}
}
73 changes: 73 additions & 0 deletions src/Console/Commands/AllDataTypesTemplateExport.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?php

namespace Joy\VoyagerImport\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use Joy\VoyagerImport\Exports\AllDataTypesTemplateExport as ExportsAllDataTypesTemplateExport;
use Maatwebsite\Excel\Excel;
use Symfony\Component\Console\Input\InputOption;

class AllDataTypesTemplateExport extends Command
{
protected $name = 'joy-import:all-export-templates';

protected $description = 'Joy Voyager all DataTypes templates exporter';

public function handle()
{
$this->output->title('Starting export');
$this->exportAllDataTypes(
$this->option('disk'),
$this->option('writerType')
);
$this->output->success('Export successful');
}

protected function exportAllDataTypes(
string $disk = null,
string $writerType = Excel::XLSX
) {
$path = 'public/imports/' . (($filePath ?? 'import-all') . '-' . date('YmdHis') . '.' . Str::lower($writerType));

$url = config('app.url') . Storage::disk($disk)->url($path);

$this->output->info(sprintf(
'Exporting to >>' . PHP_EOL . 'path : %s' . PHP_EOL . 'url : %s',
storage_path($path),
$url
));

(new ExportsAllDataTypesTemplateExport())->withOutput($this->output)->store(
$path,
$disk,
$writerType
);
}

/**
* Get the console command options.
*
* @return array
*/
protected function getOptions()
{
return [
[
'disk',
'd',
InputOption::VALUE_OPTIONAL,
'The disk to where you want to export',
config('joy-voyager-export.disk')
],
[
'writerType',
'w',
InputOption::VALUE_OPTIONAL,
'The writerType in which format you want to export',
config('joy-voyager-export.writerType', 'Xlsx')
],
];
}
}
Loading

0 comments on commit 452a7d4

Please sign in to comment.