sarga/packages/Webkul/Admin/src/Http/Controllers/ExportController.php

80 lines
1.8 KiB
PHP
Raw Normal View History

2018-12-04 04:23:52 +00:00
<?php
namespace Webkul\Admin\Http\Controllers;
use Webkul\Admin\Exports\DataGridExport;
use Excel;
class ExportController extends Controller
{
protected $exportableGrids = [
2020-03-04 06:32:53 +00:00
'OrderDataGrid',
'OrderInvoicesDataGrid',
'OrderShipmentsDataGrid',
'OrderRefundDataGrid',
'CustomerDataGrid',
'TaxRateDataGrid',
'ProductDataGrid',
'CMSPageDataGrid',
];
2018-12-04 04:23:52 +00:00
/**
* Create a new controller instance.
*
2020-03-05 13:37:08 +00:00
* @return void
2018-12-04 04:23:52 +00:00
*/
public function __construct()
{
$this->middleware('admin');
}
/**
* function for export datagrid
*
* @return \Illuminate\Http\Response
*/
public function export()
{
$criteria = request()->all();
2019-08-02 14:21:28 +00:00
2019-03-12 06:43:48 +00:00
$format = $criteria['format'];
$gridName = explode('\\', $criteria['gridName']);
2020-02-19 10:26:44 +00:00
$path = '\Webkul\Admin\DataGrids'.'\\'.last($gridName);
2019-03-12 06:43:48 +00:00
$proceed = false;
foreach ($this->exportableGrids as $exportableGrid) {
if (last($gridName) == $exportableGrid) {
2019-03-12 06:43:48 +00:00
$proceed = true;
}
}
if (! $proceed) {
2019-04-01 15:09:15 +00:00
return redirect()->back();
}
$gridInstance = new $path;
2020-02-19 10:26:44 +00:00
2019-04-01 15:09:15 +00:00
$records = $gridInstance->export();
2020-03-04 06:32:53 +00:00
if (! count($records)) {
2019-04-01 15:09:15 +00:00
session()->flash('warning', trans('admin::app.export.no-records'));
2019-01-16 11:13:36 +00:00
2019-04-01 15:09:15 +00:00
return redirect()->back();
}
2019-01-16 11:13:36 +00:00
2019-04-01 15:09:15 +00:00
if ($format == 'csv') {
return Excel::download(new DataGridExport($records), last($gridName).'.csv');
2019-03-12 06:43:48 +00:00
}
2019-04-02 18:20:44 +00:00
if ($format == 'xls') {
return Excel::download(new DataGridExport($records), last($gridName).'.xlsx');
}
2019-04-02 18:20:44 +00:00
session()->flash('warning', trans('admin::app.export.illegal-format'));
return redirect()->back();
2018-12-04 04:23:52 +00:00
}
}