|
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\actions; |
|
11
|
|
|
|
|
12
|
|
|
use dosamigos\exportable\contracts\DownloadServiceInterface; |
|
13
|
|
|
use dosamigos\exportable\helpers\MimeTypeHelper; |
|
14
|
|
|
use dosamigos\exportable\services\DownloadService; |
|
15
|
|
|
use Yii; |
|
16
|
|
|
use yii\base\Action; |
|
17
|
|
|
use yii\base\InvalidConfigException; |
|
18
|
|
|
use yii\web\BadRequestHttpException; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* ExportableAction provides the functionality to handle form requests for file download to the ExportableButton widget. |
|
22
|
|
|
*/ |
|
23
|
|
|
class ExportableAction extends Action |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* @var string is the filename of the file to be downloaded. Defaults to "exportable" |
|
27
|
|
|
*/ |
|
28
|
|
|
public $fileName = 'exportable'; |
|
29
|
|
|
/** |
|
30
|
|
|
* @var \Closure an anonymous function that is called once BEFORE forcing download. The return result of the |
|
31
|
|
|
* function will be rendered on the file. It should have the following signature: |
|
32
|
|
|
* |
|
33
|
|
|
* ```php |
|
34
|
|
|
* function ($type){} |
|
35
|
|
|
* ``` |
|
36
|
|
|
* |
|
37
|
|
|
* - `type`: Is the format type to download. The anonymous function should return the content on the selected |
|
38
|
|
|
* format. |
|
39
|
|
|
*/ |
|
40
|
|
|
public $contentValue; |
|
41
|
|
|
/** |
|
42
|
|
|
* @var DownloadServiceInterface |
|
43
|
|
|
*/ |
|
44
|
|
|
public $downloadService; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @inheritdoc |
|
48
|
|
|
*/ |
|
49
|
|
|
public function init() |
|
50
|
|
|
{ |
|
51
|
|
|
if (!is_callable($this->contentValue)) { |
|
52
|
|
|
throw new InvalidConfigException('"contentValue" must be a valid callable.'); |
|
53
|
|
|
} |
|
54
|
|
|
if (null === $this->downloadService) { |
|
55
|
|
|
$this->downloadService = new DownloadService(); |
|
56
|
|
|
} |
|
57
|
|
|
parent::init(); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* @throws BadRequestHttpException |
|
62
|
|
|
* @return array|mixed |
|
63
|
|
|
*/ |
|
64
|
|
|
public function run() |
|
65
|
|
|
{ |
|
66
|
|
|
if (Yii::$app->request->isPost && Yii::$app->request->post('type')) { |
|
67
|
|
|
$type = Yii::$app->request->post('type'); |
|
68
|
|
|
$contents = call_user_func($this->contentValue, $type); |
|
69
|
|
|
$mime = MimeTypeHelper::getMimeType($type); |
|
70
|
|
|
$filename = $this->fileName . '.' . $type; |
|
71
|
|
|
|
|
72
|
|
|
if ($mime !== false) { |
|
73
|
|
|
$this->downloadService->run($filename, $mime, $contents); |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
throw new BadRequestHttpException('Your request is invalid.'); |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|