1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace hipanel\actions; |
4
|
|
|
|
5
|
|
|
use Yii; |
6
|
|
|
use hiqdev\yii2\export\exporters\Type; |
7
|
|
|
use hiqdev\yii2\export\models\CsvSettings; |
8
|
|
|
use hiqdev\yii2\export\models\TsvSettings; |
9
|
|
|
use hiqdev\yii2\export\models\XlsxSettings; |
10
|
|
|
use hiqdev\yii2\export\exporters\ExporterFactoryInterface; |
11
|
|
|
|
12
|
|
|
class ExportAction extends IndexAction |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var ExporterFactoryInterface |
16
|
|
|
*/ |
17
|
|
|
private $exporterFactory; |
18
|
|
|
|
19
|
|
|
public function __construct($id, $controller, ExporterFactoryInterface $exporterFactory) |
20
|
|
|
{ |
21
|
|
|
parent::__construct($id, $controller); |
22
|
|
|
$this->exporterFactory = $exporterFactory; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
public function run() |
26
|
|
|
{ |
27
|
|
|
$type = $this->getType(); |
28
|
|
|
$exporter = $this->exporterFactory->build($type); |
29
|
|
|
$settings = $this->loadSettings($type); |
30
|
|
|
if ($settings !== null) { |
31
|
|
|
$settings->applyTo($exporter); |
32
|
|
|
} |
33
|
|
|
$representation = $this->ensureRepresentationCollection()->getByName($this->getUiModel()->representation); |
34
|
|
|
$gridClassName = $this->guessGridClassName(); |
35
|
|
|
$indexAction = null; |
36
|
|
|
if (isset($this->controller->actions()['index'])) { |
37
|
|
|
$indexActionConfig = $this->controller->actions()['index']; |
38
|
|
|
$indexAction = Yii::createObject($indexActionConfig, ['index', $this->controller]); |
39
|
|
|
$indexAction->beforePerform(); |
40
|
|
|
} |
41
|
|
|
$dataProvider = $indexAction ? $indexAction->getDataProvider() : $this->getDataProvider(); |
42
|
|
|
$grid = new $gridClassName(['dataProvider' => $dataProvider, 'columns' => $representation->getColumns()]); |
43
|
|
|
$grid->dataColumnClass = \hiqdev\higrid\DataColumn::class; |
44
|
|
|
$result = $exporter->export($grid); |
45
|
|
|
$filename = $exporter->filename . '.' . $type; |
46
|
|
|
|
47
|
|
|
return Yii::$app->response->sendContentAsFile($result, $filename); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function loadSettings($type) |
51
|
|
|
{ |
52
|
|
|
$map = [ |
53
|
|
|
Type::CSV => CsvSettings::class, |
54
|
|
|
Type::TSV => TsvSettings::class, |
55
|
|
|
Type::XLSX => XlsxSettings::class, |
56
|
|
|
]; |
57
|
|
|
|
58
|
|
|
$settings = Yii::createObject($map[$type]); |
59
|
|
|
if ($settings->load(Yii::$app->request->get(), '') && $settings->validate()) { |
60
|
|
|
return $settings; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
return null; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
protected function getType() |
67
|
|
|
{ |
68
|
|
|
return Yii::$app->request->get('format'); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @return string |
73
|
|
|
* @throws \Exception |
74
|
|
|
*/ |
75
|
|
|
protected function guessGridClassName() |
76
|
|
|
{ |
77
|
|
|
$moduleName = $this->controller->module->id; |
78
|
|
|
$controllerName = $this->controller->id; |
79
|
|
|
$girdClassName = sprintf('\hipanel\modules\%s\grid\%sGridView', $moduleName, ucfirst($controllerName)); |
80
|
|
|
if (class_exists($girdClassName)) { |
81
|
|
|
return $girdClassName; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
throw new \Exception("ExportAction cannot find a {$girdClassName}"); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|