Skip to content

Commit 1a7d998

Browse files
committed
Fix exporting relationship fields
1 parent 282bf76 commit 1a7d998

File tree

3 files changed

+47
-29
lines changed

3 files changed

+47
-29
lines changed

src/Exporters/AbstractExporter.php

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,36 +4,31 @@
44

55
use JackSleight\StatamicMemberbox\Facades\Member;
66
use Statamic\Facades\User;
7-
use Statamic\Support\Arr;
87

98
abstract class AbstractExporter
109
{
11-
protected function getHeaders()
12-
{
13-
return User::blueprint()
14-
->fields()
15-
->except(['groups', 'roles', 'password'])
16-
->all()
17-
->keys()
18-
->merge(['id', 'last_login'])
19-
->all();
20-
}
10+
abstract public function contentType();
11+
12+
abstract public function export();
2113

2214
protected function getData()
2315
{
24-
$headers = $this->getHeaders();
25-
$default = array_combine($headers, array_fill(0, count($headers), null));
26-
27-
$users = Member::query()->get()->toArray();
28-
29-
return collect($users)
30-
->map(function ($user) use ($headers, $default) {
31-
return array_merge($default, Arr::only($user, $headers));
32-
})
16+
return Member::query()->get()
3317
->map(function ($user) {
34-
return collect($user)->map(function ($value) {
35-
return (is_array($value)) ? implode(', ', $value) : $value;
36-
})->all();
18+
$data = $user->data();
19+
20+
return User::blueprint()->fields()->all()->keys()->flip()
21+
->reject(function ($field, $key) {
22+
return in_array($key, ['id', 'groups', 'roles', 'password']);
23+
})
24+
->map(function ($field, $key) use ($data) {
25+
return $data[$key] ?? null;
26+
})
27+
->merge([
28+
'id' => $user->id(),
29+
'last_login' => $user->lastLogin(),
30+
])
31+
->all();
3732
})
3833
->all();
3934
}

src/Exporters/CsvExporter.php

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use League\Csv\Writer;
66
use SplTempFileObject;
7+
use Statamic\Facades\User;
78

89
class CsvExporter extends AbstractExporter
910
{
@@ -14,6 +15,11 @@ public function __construct()
1415
$this->writer = Writer::createFromFileObject(new SplTempFileObject);
1516
}
1617

18+
public function contentType()
19+
{
20+
return 'text/csv';
21+
}
22+
1723
public function export()
1824
{
1925
$this->writer->insertOne($this->getHeaders());
@@ -22,8 +28,25 @@ public function export()
2228
return (string) $this->writer;
2329
}
2430

25-
public function contentType()
31+
protected function getHeaders()
2632
{
27-
return 'text/csv';
33+
return User::blueprint()
34+
->fields()
35+
->except(['id', 'groups', 'roles', 'password'])
36+
->all()
37+
->keys()
38+
->merge(['id', 'last_login'])
39+
->all();
40+
}
41+
42+
protected function getData()
43+
{
44+
return collect(parent::getData())
45+
->map(function ($user) {
46+
return collect($user)->map(function ($value) {
47+
return (is_array($value)) ? implode(', ', $value) : $value;
48+
})->all();
49+
})
50+
->all();
2851
}
2952
}

src/Exporters/JsonExporter.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44

55
class JsonExporter extends AbstractExporter
66
{
7-
public function export()
7+
public function contentType()
88
{
9-
return json_encode($this->getData());
9+
return 'application/json';
1010
}
1111

12-
public function contentType()
12+
public function export()
1313
{
14-
return 'application/json';
14+
return json_encode($this->getData());
1515
}
1616
}

0 commit comments

Comments
 (0)