2018-12-04 04:23:52 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace Webkul\Admin\Http\Controllers;
|
|
|
|
|
|
|
|
|
|
use Excel;
|
2021-10-05 04:57:34 +00:00
|
|
|
use Webkul\Admin\Exports\DataGridExport;
|
2018-12-04 04:23:52 +00:00
|
|
|
|
|
|
|
|
class ExportController extends Controller
|
|
|
|
|
{
|
|
|
|
|
/**
|
|
|
|
|
* 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');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2021-10-05 04:57:34 +00:00
|
|
|
* Export datagrid.
|
2018-12-04 04:23:52 +00:00
|
|
|
*
|
|
|
|
|
* @return \Illuminate\Http\Response
|
2021-10-05 04:57:34 +00:00
|
|
|
*/
|
2018-12-04 04:23:52 +00:00
|
|
|
public function export()
|
|
|
|
|
{
|
2019-03-12 05:37:12 +00:00
|
|
|
$criteria = request()->all();
|
2019-08-02 14:21:28 +00:00
|
|
|
|
2019-03-12 06:43:48 +00:00
|
|
|
$format = $criteria['format'];
|
2019-03-12 05:37:12 +00:00
|
|
|
|
|
|
|
|
$gridName = explode('\\', $criteria['gridName']);
|
2020-02-19 10:26:44 +00:00
|
|
|
|
2021-10-05 04:57:34 +00:00
|
|
|
$path = '\Webkul\Admin\DataGrids' . '\\' . last($gridName);
|
2019-03-12 05:37:12 +00:00
|
|
|
|
2021-11-29 09:14:35 +00:00
|
|
|
$gridInstance = App::make($path);
|
2021-10-05 04:57:34 +00:00
|
|
|
|
2019-04-01 15:09:15 +00:00
|
|
|
$records = $gridInstance->export();
|
2019-03-12 05:37:12 +00:00
|
|
|
|
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') {
|
2021-10-05 04:57:34 +00:00
|
|
|
return Excel::download(new DataGridExport($records), last($gridName) . '.csv');
|
2019-03-12 06:43:48 +00:00
|
|
|
}
|
2019-04-08 11:18:43 +00:00
|
|
|
|
2019-04-02 18:20:44 +00:00
|
|
|
if ($format == 'xls') {
|
2021-10-05 04:57:34 +00:00
|
|
|
return Excel::download(new DataGridExport($records), last($gridName) . '.xlsx');
|
2019-04-02 18:20:44 +00:00
|
|
|
}
|
2019-04-08 11:18:43 +00:00
|
|
|
|
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
|
|
|
}
|
2021-10-05 04:57:34 +00:00
|
|
|
}
|