1
|
|
|
<?php namespace Anomaly\Streams\Platform\Ui\Table\Component\Action\Handler; |
2
|
|
|
|
3
|
|
|
use Anomaly\Streams\Platform\Model\EloquentModel; |
4
|
|
|
use Anomaly\Streams\Platform\Ui\Table\Component\Action\ActionHandler; |
5
|
|
|
use Anomaly\Streams\Platform\Ui\Table\TableBuilder; |
6
|
|
|
use Illuminate\Contracts\Bus\SelfHandling; |
7
|
|
|
use Illuminate\Routing\ResponseFactory; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class ExportAll |
11
|
|
|
* |
12
|
|
|
* @link http://anomaly.is/streams-platform |
13
|
|
|
* @author AnomalyLabs, Inc. <[email protected]> |
14
|
|
|
* @author Ryan Thompson <[email protected]> |
15
|
|
|
* @package Anomaly\Streams\Platform\Ui\Table\Component\Action\Handler |
16
|
|
|
*/ |
17
|
|
|
class ExportAll extends ActionHandler implements SelfHandling |
18
|
|
|
{ |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* ExportAll the selected entries. |
22
|
|
|
* |
23
|
|
|
* @param TableBuilder $builder |
24
|
|
|
* @param ResponseFactory $response |
25
|
|
|
* @param array $selected |
26
|
|
|
*/ |
27
|
|
|
public function handle(TableBuilder $builder, ResponseFactory $response, array $selected) |
28
|
|
|
{ |
29
|
|
|
$model = $builder->getTableModel(); |
30
|
|
|
$stream = $builder->getTableStream(); |
31
|
|
|
|
32
|
|
|
$headers = [ |
33
|
|
|
'Content-Disposition' => 'attachment; filename=' . $stream->getSlug() . '.csv', |
34
|
|
|
'Cache-Control' => 'must-revalidate, post-check=0, pre-check=0', |
35
|
|
|
'Content-type' => 'text/csv', |
36
|
|
|
'Pragma' => 'public', |
37
|
|
|
'Expires' => '0' |
38
|
|
|
]; |
39
|
|
|
|
40
|
|
View Code Duplication |
$callback = function () use ($selected, $model) { |
|
|
|
|
41
|
|
|
|
42
|
|
|
$output = fopen('php://output', 'w'); |
43
|
|
|
|
44
|
|
|
/* @var EloquentModel $entry */ |
45
|
|
|
foreach ($model->all() as $k => $entry) { |
46
|
|
|
|
47
|
|
|
if ($k == 0) { |
48
|
|
|
fputcsv($output, array_keys($entry->toArray())); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
fputcsv($output, $entry->toArray()); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
fclose($output); |
55
|
|
|
}; |
56
|
|
|
|
57
|
|
|
$builder->setTableResponse($response->stream($callback, 200, $headers)); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.