File.php
2.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<?php
declare(strict_types=1);
namespace ACP\Migrate\Export\Response;
use AC\ListScreenCollection;
use ACP\Migrate\Export\Response;
use ACP\Migrate\MessageTrait;
use ACP\Search\SegmentCollection;
use ACP\Storage\EncoderFactory;
use ACP\Storage\Serializer\JsonSerializer;
final class File implements Response
{
use MessageTrait;
private $list_screens;
private $encoder_factory;
private $json_serializer;
public function __construct(
ListScreenCollection $list_screens,
SegmentCollection $segment_collection,
EncoderFactory $encoder_factory,
JsonSerializer $json_serializer
) {
$this->list_screens = $list_screens;
$this->encoder_factory = $encoder_factory;
$this->json_serializer = $json_serializer;
$this->segment_collection = $segment_collection;
}
/**
* @return void
*/
public function send()
{
if ( ! $this->list_screens->count()) {
$this->set_message(__('No screens selected for export.', 'codepress-admin-columns'));
return;
}
$segments = [];
foreach ($this->segment_collection as $segment) {
$segments[(string)$segment->get_list_screen_id()][] = $segment;
}
$output = [];
foreach ($this->list_screens as $list_screen) {
$encoder = $this->encoder_factory->create();
$encoder->set_list_screen($list_screen);
if (array_key_exists($list_screen->get_id()->get_id(), $segments)) {
$encoder->set_segments(
new SegmentCollection($segments[(string)$list_screen->get_id()])
);
}
$output[] = $encoder->encode();
}
$headers = [
'content-disposition' => 'attachment; filename="' . $this->get_file_name() . '"',
'content-type' => 'application/json',
];
foreach ($headers as $header => $value) {
header($header . ': ' . $value);
}
echo $this->json_serializer->serialize($output);
exit;
}
private function get_file_name()
{
return sprintf(
'%s-%s.%s',
'admin-columns-export',
date('Y-m-d-Hi'),
'json'
);
}
}