Skip to content

Commit

Permalink
finish
Browse files Browse the repository at this point in the history
  • Loading branch information
tu6ge committed Feb 10, 2020
1 parent f1aa52c commit b72282f
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 91 deletions.
18 changes: 0 additions & 18 deletions resources/views/browse.blade.php

This file was deleted.

9 changes: 0 additions & 9 deletions routes/excel.php

This file was deleted.

57 changes: 57 additions & 0 deletions src/Actions/Export.php
@@ -0,0 +1,57 @@
<?php

namespace VoyagerExcel\Actions;

use TCG\Voyager\Actions\AbstractAction;
use Maatwebsite\Excel\Facades\Excel;
use VoyagerExcel\Exports\PostExport;
use Illuminate\Database\Eloquent\Model;

class Export extends AbstractAction
{
public function getTitle()
{
return __('voyager_excel::excel.export_excel');
}

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

public function shouldActionDisplayOnDataType()
{
if(empty($this->dataType->model_name)){
return false;
}
if(!class_exists($this->dataType->model_name)){
return false;
}
$model = new $this->dataType->model_name;
if(!($model instanceof Model)){
return false;
}
return true;
}

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

public function getDefaultRoute()
{
return null;
}


public function massAction($ids, $comingFrom)
{
if(empty(array_filter($ids))){
return redirect($comingFrom);
}
return Excel::download(new PostExport($this->dataType, $ids), 'demo-'.date('H:i:s').'.xls');
}
}
43 changes: 29 additions & 14 deletions src/Exports/PostExport.php
Expand Up @@ -7,21 +7,36 @@

class PostExport implements FromCollection
{
protected $dataType;
protected $model;
protected $ids;
public function __construct($dataType, $ids)
{
$this->dataType = $dataType;
$this->model = new $dataType->model_name;
$this->ids = $ids;
}
public function collection()
{
return collect([
[
'id',
'name',
],
[
1,
'wang',
],
[
2,
'honglei'
]
]);
$fields = $this->dataType->browseRows->map(function($res){
return $res['field'];
});

$table = $this->dataType->browseRows->map(function($res){
return $res['display_name'];
});

$rs = $this->model->whereIn('id', $this->ids)->get();
$rs = $rs->map(function($res)use($fields){
$arr = [];
foreach($fields as $val){
$arr[$val] = $res[$val];
}
return $arr;
});

$table = collect([$table->toArray()])->merge($rs);

return $table;
}
}
16 changes: 0 additions & 16 deletions src/Http/Constrollers/PostController.php

This file was deleted.

28 changes: 0 additions & 28 deletions src/Http/Middleware/VoyagerExcelMiddleware.php

This file was deleted.

9 changes: 3 additions & 6 deletions src/VoyagerExcelServiceProvider.php
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Support\ServiceProvider;
use Illuminate\Routing\Router;
use TCG\Voyager\Facades\Voyager;
use VoyagerExcel\Http\Middleware\VoyagerExcelMiddleware;

class VoyagerExcelServiceProvider extends ServiceProvider
Expand All @@ -16,12 +17,8 @@ public function register()

public function boot(Router $router, Dispatcher $event)
{
$this->loadRoutesFrom(__DIR__.'/../routes/excel.php');
$this->loadViewsFrom(__DIR__.'/../resources/views', 'voyager_excel');


$router->aliasMiddleware('voyager.excel', VoyagerExcelMiddleware::class);

$this->loadTranslationsFrom(realpath(__DIR__.'/../resources/lang'), 'voyager_excel');

Voyager::addAction(\VoyagerExcel\Actions\Export::class);
}
}

0 comments on commit b72282f

Please sign in to comment.