|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the 2amigos/yii2-exportable-widget project. |
|
5
|
|
|
* (c) 2amigOS! <http://2amigos.us/> |
|
6
|
|
|
* For the full copyright and license information, please view |
|
7
|
|
|
* the LICENSE file that was distributed with this source code. |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace dosamigos\exportable\behaviors; |
|
11
|
|
|
|
|
12
|
|
|
use dosamigos\exportable\contracts\ExportableServiceInterface; |
|
13
|
|
|
use dosamigos\exportable\exceptions\UnknownExportTypeException; |
|
14
|
|
|
use dosamigos\exportable\helpers\TypeHelper; |
|
15
|
|
|
use dosamigos\exportable\services\ExportableService; |
|
16
|
|
|
use dosamigos\grid\contracts\RunnableBehaviorInterface; |
|
17
|
|
|
use Yii; |
|
18
|
|
|
use yii\base\Behavior; |
|
19
|
|
|
use yii\base\InvalidConfigException; |
|
20
|
|
|
use yii\grid\GridView; |
|
21
|
|
|
|
|
22
|
|
|
class ExportableBehavior extends Behavior implements RunnableBehaviorInterface |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* @var string the filename that will be used for the download. It must be without file extension! |
|
26
|
|
|
*/ |
|
27
|
|
|
public $filename = 'exportable'; |
|
28
|
|
|
/** |
|
29
|
|
|
* @var ExportableServiceInterface |
|
30
|
|
|
*/ |
|
31
|
|
|
public $exportableService; |
|
32
|
|
|
/** |
|
33
|
|
|
* @var array the columns to export |
|
34
|
|
|
*/ |
|
35
|
|
|
public $columns = []; |
|
36
|
|
|
/** |
|
37
|
|
|
* @var array of configurable writers by type. The type is the key. |
|
38
|
|
|
*/ |
|
39
|
|
|
public $writers = [ |
|
40
|
|
|
TypeHelper::CSV => 'Box\Spout\Writer\CSV\Writer', |
|
41
|
|
|
TypeHelper::XLSX => 'Box\Spout\Writer\XLSX\Writer', |
|
42
|
|
|
TypeHelper::ODS => 'Box\Spout\Writer\ODS\Writer', |
|
43
|
|
|
TypeHelper::XML => 'dosamigos\exportable\XmlWriter', |
|
44
|
|
|
TypeHelper::JSON => 'dosamigos\exportable\JsonWriter', |
|
45
|
|
|
TypeHelper::TXT => 'dosamigos\exportable\TextWriter', |
|
46
|
|
|
TypeHelper::HTML => 'dosamigos\exportable\HtmlWriter' |
|
47
|
|
|
]; |
|
48
|
|
|
/** |
|
49
|
|
|
* @var string |
|
50
|
|
|
*/ |
|
51
|
|
|
protected $type; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @inheritdoc |
|
55
|
|
|
*/ |
|
56
|
|
|
public function init() |
|
57
|
|
|
{ |
|
58
|
|
|
if ((int)Yii::$app->request->post('export') === 1) { |
|
59
|
|
|
$this->type = Yii::$app->request->post('type', TypeHelper::XLSX); |
|
60
|
|
|
if (!array_key_exists($this->type, $this->writers)) { |
|
61
|
|
|
throw new UnknownExportTypeException( |
|
62
|
|
|
sprintf( |
|
63
|
|
|
'Unknown type "%s". Make sure writers are properly configured.', |
|
64
|
|
|
$this->type |
|
65
|
|
|
) |
|
66
|
|
|
); |
|
67
|
|
|
} |
|
68
|
|
|
if (null === $this->exportableService) { |
|
69
|
|
|
$this->exportableService = new ExportableService(); |
|
70
|
|
|
} |
|
71
|
|
|
if (!$this->exportableService instanceof ExportableServiceInterface) { |
|
72
|
|
|
throw new InvalidConfigException( |
|
73
|
|
|
sprintf( |
|
74
|
|
|
'The "exportableService" class must implement %s', |
|
75
|
|
|
ExportableServiceInterface::class |
|
76
|
|
|
) |
|
77
|
|
|
); |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @inheritdoc |
|
84
|
|
|
*/ |
|
85
|
|
|
public function run() |
|
86
|
|
|
{ |
|
87
|
|
|
if ($this->type) { |
|
88
|
|
|
/** @var GridView $owner */ |
|
89
|
|
|
$owner = $this->owner; |
|
90
|
|
|
$filename = $this->filename . '.' . $this->type; |
|
91
|
|
|
$this->exportableService->run($owner, $this->type, $this->columns, $filename); |
|
92
|
|
|
} |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|