|
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; |
|
11
|
|
|
|
|
12
|
|
|
use dosamigos\exportable\bundles\ExportableAsset; |
|
13
|
|
|
use dosamigos\exportable\helpers\TypeHelper; |
|
14
|
|
|
use Yii; |
|
15
|
|
|
use yii\bootstrap\ButtonDropdown; |
|
16
|
|
|
|
|
17
|
|
|
class ExportableButton extends ButtonDropdown |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @var string the action to submit to download exported data |
|
21
|
|
|
*/ |
|
22
|
|
|
public $url; |
|
23
|
|
|
/** |
|
24
|
|
|
* @var string the button label |
|
25
|
|
|
*/ |
|
26
|
|
|
public $label = 'Export'; |
|
27
|
|
|
/** |
|
28
|
|
|
* @var array the export types available. You can modify the list to display only certain types. For example, |
|
29
|
|
|
* |
|
30
|
|
|
* ``` |
|
31
|
|
|
* 'types' => [ 'xml' => 'As XML'] |
|
32
|
|
|
* ``` |
|
33
|
|
|
* |
|
34
|
|
|
* Will display only XML as the unique type of exportation. Defaults to all possible options available. |
|
35
|
|
|
* |
|
36
|
|
|
* The items are a key => label structure. The types (keys) will be used as a post value that will define the type |
|
37
|
|
|
* that needs to be exported whether by the ExportableAction or the ExportableBehavior when used with our |
|
38
|
|
|
* GridView Library. |
|
39
|
|
|
* |
|
40
|
|
|
* @see https://github.com/2amigos/yii2-grid-view-library |
|
41
|
|
|
*/ |
|
42
|
|
|
public $types = [ |
|
43
|
|
|
TypeHelper::CSV => 'CSV <span class="label label-default">.csv</span>', |
|
44
|
|
|
TypeHelper::XLSX => 'Excel 2007+ <span class="label label-default">.xlsx</span>', |
|
45
|
|
|
TypeHelper::ODS => 'Open Document Spreadsheet <span class="label label-default">.ods</span>', |
|
46
|
|
|
TypeHelper::JSON => 'JSON <span class="label label-default">.json</span>', |
|
47
|
|
|
TypeHelper::XML => 'XML <span class="label label-default">.xml</span>', |
|
48
|
|
|
TypeHelper::TXT => 'Text <span class="label label-default">.txt</span>', |
|
49
|
|
|
TypeHelper::HTML => 'HTML <span class="label label-default">.html</span>' |
|
50
|
|
|
]; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @inheritdoc |
|
54
|
|
|
*/ |
|
55
|
|
|
public function init() |
|
56
|
|
|
{ |
|
57
|
|
|
parent::init(); |
|
58
|
|
|
$this->encodeLabel = false; |
|
59
|
|
|
$this->initDropdownItems(); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* @inheritdoc |
|
64
|
|
|
*/ |
|
65
|
|
|
public function run() |
|
66
|
|
|
{ |
|
67
|
|
|
$this->registerClientScript(); |
|
68
|
|
|
|
|
69
|
|
|
if (empty($this->url)) { |
|
70
|
|
|
$this->url = Yii::$app->request->getUrl(); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
return parent::run(); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Initializes the dropdown items |
|
78
|
|
|
*/ |
|
79
|
|
|
public function initDropdownItems() |
|
80
|
|
|
{ |
|
81
|
|
|
$id = $this->options['id']; |
|
82
|
|
|
|
|
83
|
|
|
foreach ($this->types as $type => $label) { |
|
84
|
|
|
$this->dropdown['items'][] = [ |
|
85
|
|
|
'label' => $label, |
|
86
|
|
|
'url' => '#', |
|
87
|
|
|
'linkOptions' => [ |
|
88
|
|
|
'id' => $id . $type . 'exportable', |
|
89
|
|
|
'data-type' => $type, |
|
90
|
|
|
'class' => 'btn-da-exportable' |
|
91
|
|
|
] |
|
92
|
|
|
]; |
|
93
|
|
|
} |
|
94
|
|
|
$this->dropdown['encodeLabels'] = false; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Registers client script for the plugin |
|
99
|
|
|
*/ |
|
100
|
|
|
protected function registerClientScript() |
|
101
|
|
|
{ |
|
102
|
|
|
$id = $this->options['id']; |
|
103
|
|
|
$hash = hash('crc32', $id); |
|
104
|
|
|
$url = $this->url; |
|
105
|
|
|
$view = $this->getView(); |
|
106
|
|
|
|
|
107
|
|
|
ExportableAsset::register($view); |
|
108
|
|
|
|
|
109
|
|
|
$js = "dosamigos.exportable.registerHandler('.btn-da-exportable', '{$url}', '{$hash}');"; |
|
110
|
|
|
|
|
111
|
|
|
$view->registerJs($js); |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
|