|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Sunnysideup\ExportAllFromModelAdmin\Api; |
|
4
|
|
|
|
|
5
|
|
|
use SilverStripe\Core\Config\Configurable; |
|
6
|
|
|
use SilverStripe\Core\Extensible; |
|
7
|
|
|
use SilverStripe\Core\Injector\Injectable; |
|
8
|
|
|
|
|
9
|
|
|
use SilverStripe\Assets\Image; |
|
10
|
|
|
use SilverStripe\Control\Director; |
|
11
|
|
|
use SilverStripe\Core\Config\Config; |
|
12
|
|
|
use SilverStripe\Core\Injector\Injector; |
|
13
|
|
|
use SilverStripe\Security\Member; |
|
14
|
|
|
use Sunnysideup\ExportAllFromModelAdmin\ExportAllFromModelAdminTraitSettings; |
|
15
|
|
|
|
|
16
|
|
|
class AllFields |
|
17
|
|
|
{ |
|
18
|
|
|
use Injectable; |
|
19
|
|
|
use Configurable; |
|
20
|
|
|
use Extensible; |
|
21
|
|
|
protected string $modelClass = ''; |
|
22
|
|
|
protected array $exportFields = []; |
|
23
|
|
|
|
|
24
|
|
|
protected array $exportFieldLabels = []; |
|
25
|
|
|
|
|
26
|
|
|
protected array $exportFieldLabelsExclude = []; |
|
27
|
|
|
|
|
28
|
|
|
public function __construct($modelClass, ?array $exportFieldLabelsExclude = []) |
|
29
|
|
|
{ |
|
30
|
|
|
$this->modelClass = $modelClass; |
|
31
|
|
|
|
|
32
|
|
|
$this->exportFieldLabelsExclude = array_merge( |
|
33
|
|
|
$this->exportFieldLabelsExclude, |
|
34
|
|
|
$exportFieldLabelsExclude |
|
35
|
|
|
); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
public function getExportFields(): array |
|
39
|
|
|
{ |
|
40
|
|
|
$this->exportFields = []; |
|
41
|
|
|
$exclude1 = Config::inst()->get($this->modelClass, 'fields_to_exclude_from_export') ?: []; |
|
42
|
|
|
$exclude2 = Config::inst()->get(ExportAllFromModelAdminTraitSettings::class, 'fields_to_exclude_from_export_always') ?: []; |
|
43
|
|
|
$this->exportFieldLabelsExclude = array_merge($exclude1, $exclude2); |
|
44
|
|
|
$this->generateExportFieldLabels(); |
|
45
|
|
|
$this->generateDbExportFields(); |
|
46
|
|
|
$this->generateCastingExportFields(); |
|
47
|
|
|
$this->generateHasOneExportFields(); |
|
48
|
|
|
$this->generateManyExportFields(); |
|
49
|
|
|
return $this->exportFields; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
protected function generateDbExportFields() |
|
53
|
|
|
{ |
|
54
|
|
|
$dbs = Config::inst()->get($this->modelClass, 'db'); |
|
55
|
|
|
foreach (array_keys($dbs) as $fieldName) { |
|
56
|
|
|
if (!in_array($fieldName, $this->exportFieldLabelsExclude, true)) { |
|
57
|
|
|
$this->exportFields[$fieldName] = $this->exportFieldLabels[$fieldName] ?? $fieldName; |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
protected function generateCastingExportFields() |
|
63
|
|
|
{ |
|
64
|
|
|
$casting = Config::inst()->get($this->modelClass, 'casting'); |
|
65
|
|
|
foreach (array_keys($casting) as $fieldName) { |
|
66
|
|
|
if (!in_array($fieldName, $this->exportFieldLabelsExclude, true)) { |
|
67
|
|
|
$this->exportFields[$fieldName] = $this->exportFieldLabels[$fieldName] ?? $fieldName; |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
protected function generateHasOneExportFields() |
|
73
|
|
|
{ |
|
74
|
|
|
$hasOne = |
|
75
|
|
|
(Config::inst()->get($this->modelClass, 'has_one') ?: []) + |
|
76
|
|
|
(Config::inst()->get($this->modelClass, 'belongs') ?: []); |
|
77
|
|
|
foreach ($hasOne as $fieldName => $type) { |
|
78
|
|
|
if (!in_array($fieldName, $this->exportFieldLabelsExclude, true)) { |
|
79
|
|
|
switch ($type) { |
|
80
|
|
|
case Image::class: |
|
81
|
|
|
$this->exportFields[$fieldName] = function ($rel) { |
|
82
|
|
|
return Director::absoluteURL((string)$rel->Link()); |
|
83
|
|
|
}; |
|
84
|
|
|
|
|
85
|
|
|
break; |
|
86
|
|
|
case Member::class: |
|
87
|
|
|
$this->exportFields[$fieldName] = function ($rel) { |
|
88
|
|
|
return $rel->Email; |
|
89
|
|
|
}; |
|
90
|
|
|
|
|
91
|
|
|
break; |
|
92
|
|
|
default: |
|
93
|
|
|
$this->exportFields[$fieldName] = function ($rel) { |
|
94
|
|
|
return $rel->getTitle(); |
|
95
|
|
|
}; |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
protected function generateManyExportFields() |
|
102
|
|
|
{ |
|
103
|
|
|
$rels = |
|
104
|
|
|
(Config::inst()->get($this->modelClass, 'has_many') ?: []) + |
|
105
|
|
|
(Config::inst()->get($this->modelClass, 'many_many') ?: []) + |
|
106
|
|
|
(Config::inst()->get($this->modelClass, 'belongs_many_many') ?: []); |
|
107
|
|
|
$sep = Config::inst()->get(ExportAllFromModelAdminTraitSettings::class, 'export_separator'); |
|
108
|
|
|
$sepReplacer = Config::inst()->get(ExportAllFromModelAdminTraitSettings::class, 'export_separator_replacer'); |
|
109
|
|
|
foreach (array_keys($rels) as $fieldName) { |
|
110
|
|
|
if (!in_array($fieldName, $this->exportFieldLabelsExclude, true)) { |
|
111
|
|
|
$this->exportFields[$fieldName] = function ($rels) use ($sep, $sepReplacer): string { |
|
112
|
|
|
$a = []; |
|
113
|
|
|
foreach ($rels as $rel) { |
|
114
|
|
|
$a[] = str_replace((string) $sep, (string) $sepReplacer, (string) $rel->getTitle()); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
return implode(' ' . $sep . ' ', $a); |
|
118
|
|
|
}; |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
protected function generateExportFieldLabels() |
|
124
|
|
|
{ |
|
125
|
|
|
$singleton = Injector::inst()->get($this->modelClass); |
|
126
|
|
|
$singleton->FieldLabels(); |
|
127
|
|
|
foreach ($this->exportFieldLabels as $key => $name) { |
|
128
|
|
|
$this->exportFieldLabels[$key] = str_replace([',', '.', "\t", ";"], '-', (string) $name); |
|
129
|
|
|
} |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
|