Skip to content

Commit

Permalink
🎨 Make export overrideable
Browse files Browse the repository at this point in the history
  • Loading branch information
rxcod9 committed Mar 8, 2022
1 parent 9bdb6cc commit 866559c
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 11 deletions.
8 changes: 6 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,16 @@
"extra": {
"laravel": {
"providers": [
"Joy\\VoyagerExport\\VoyagerExportServiceProvider"
"Joy\\VoyagerExport\\VoyagerExportServiceProvider",
"Joy\\VoyagerExport\\ExportServiceProvider"
]
}
},
"config": {
"sort-packages": true
"sort-packages": true,
"allow-plugins": {
"henzeb/composer-link": true
}
},
"minimum-stability": "dev",
"prefer-stable": true
Expand Down
15 changes: 12 additions & 3 deletions src/Actions/ExportAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,14 @@ public function getIcon()

public function getPolicy()
{
return 'read';
return 'browse';
}

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

Expand Down Expand Up @@ -76,11 +77,19 @@ public function massAction($ids, $comingFrom)
$writerType = $this->writerType ?? config('joy-voyager-export.writerType', Excel::XLSX);
$fileName = $this->fileName ?? ($dataType->slug . '.' . Str::lower($writerType));

return (new DataTypeExport(
$exportClass = 'joy-voyager-export.export';

if (app()->bound("joy-voyager-export.$slug.export")) {
$exportClass = "joy-voyager-export.$slug.export";
}

$export = app($exportClass);

return $export->set(
$dataType,
array_filter($ids),
request()->all(),
))->download(
)->download(
$fileName,
$writerType
);
Expand Down
6 changes: 5 additions & 1 deletion src/Console/Commands/AllDataTypesExport.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,11 @@ protected function exportAllDataTypes(
$url
));

(new ExportsAllDataTypesExport())->withOutput($this->output)->store(
$exportClass = 'joy-voyager-export.export-all';

$export = app()->make($exportClass);

$export->withOutput($this->output)->store(
$path,
$disk,
$writerType
Expand Down
10 changes: 9 additions & 1 deletion src/Console/Commands/DataTypeExport.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,15 @@ protected function exportDataType(
$url
));

(new ExportsDataTypeExport($dataType))->withOutput($this->output)->store(
$exportClass = 'joy-voyager-export.export';

if (app()->bound("joy-voyager-export." . $dataType->slug . ".export")) {
$exportClass = "joy-voyager-export." . $dataType->slug . ".export";
}

$export = app()->make($exportClass);

$export->set($dataType)->withOutput($this->output)->store(
$path,
$disk,
$writerType
Expand Down
52 changes: 52 additions & 0 deletions src/ExportServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

declare(strict_types=1);

namespace Joy\VoyagerExport;

use Illuminate\Contracts\Support\DeferrableProvider;
use Illuminate\Support\ServiceProvider;
use Joy\VoyagerExport\Exports\AllDataTypesExport;
use Joy\VoyagerExport\Exports\DataTypeExport;

/**
* Class ExportServiceProvider
*
* @category Package
* @package JoyVoyagerExport
* @author Ramakant Gangwar <gangwar.ramakant@gmail.com>
* @copyright 2021 Copyright (c) Ramakant Gangwar (https://github.com/rxcod9)
* @license http://github.com/rxcod9/joy-voyager-export/blob/main/LICENSE New BSD License
* @link https://github.com/rxcod9/joy-voyager-export
*/
class ExportServiceProvider extends ServiceProvider implements DeferrableProvider
{
/**
* Register any application services.
*
* @return void
*/
public function register()
{
$this->app->bind('joy-voyager-export.export', function ($app) {
return (new DataTypeExport());
});

$this->app->bind('joy-voyager-export.export-all', function ($app) {
return (new AllDataTypesExport());
});
}

/**
* Get the services provided by the provider.
*
* @return array
*/
public function provides()
{
return [
'joy-voyager-export.export',
'joy-voyager-export.export-all',
];
}
}
10 changes: 9 additions & 1 deletion src/Exports/AllDataTypesExport.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,15 @@ public function sheets(): array
$dataTypes = Voyager::model('DataType')->get();

foreach ($dataTypes as $dataType) {
$sheets[] = new DataTypeExport($dataType);

$exportClass = 'joy-voyager-export.export';

if (app()->bound("joy-voyager-export." . $dataType->slug . ".export")) {
$exportClass = "joy-voyager-export." . $dataType->slug . ".export";
}

$export = app()->make($exportClass);
$sheets[$dataType->getTranslatedAttribute('display_name_plural')] = $export->set($dataType);
}

return $sheets;
Expand Down
9 changes: 6 additions & 3 deletions src/Exports/DataTypeExport.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class DataTypeExport implements
* @param array $ids
* @param array $input
*/
public function __construct(
public function set(
DataType $dataType,
$ids = [],
$input = []
Expand All @@ -73,6 +73,7 @@ public function __construct(
$this->ids = $ids;
$this->input = $input;
$this->dataTypeContent = app($this->dataType->model_name);
return $this;
}

public function query()
Expand Down Expand Up @@ -168,7 +169,9 @@ public function map($data): array
$columns[] = $data->id;
foreach ($this->dataType->browseRows as $row) {
$column = null;
if ($data->{$row->field . '_browse'}) {
if ($data->{$row->field . '_export'}) {
$data->{$row->field} = $data->{$row->field . '_export'};
} elseif ($data->{$row->field . '_browse'}) {
$data->{$row->field} = $data->{$row->field . '_browse'};
}

Expand Down Expand Up @@ -340,7 +343,7 @@ public function map($data): array
$column = implode(', ', $values);
} else {
// view('voyager::multilingual.input-hidden-bread-browse');
$values[] = $data->{$row->field};
$column = $data->{$row->field};
}
$columns[] = $column;
}
Expand Down

0 comments on commit 866559c

Please sign in to comment.