|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace LeKoala\ExcelImportExport\Extensions; |
|
4
|
|
|
|
|
5
|
|
|
use SilverStripe\Forms\Form; |
|
6
|
|
|
use SilverStripe\Core\Extension; |
|
7
|
|
|
use SilverStripe\Security\Group; |
|
8
|
|
|
use SilverStripe\Security\Member; |
|
9
|
|
|
use SilverStripe\View\Requirements; |
|
10
|
|
|
use SilverStripe\Security\Permission; |
|
11
|
|
|
use SilverStripe\Forms\GridField\GridField; |
|
12
|
|
|
use SilverStripe\Core\Config\Config_ForClass; |
|
13
|
|
|
use LeKoala\ExcelImportExport\ExcelImportExport; |
|
14
|
|
|
use SilverStripe\Forms\GridField\GridFieldConfig; |
|
15
|
|
|
use LeKoala\ExcelImportExport\ExcelGroupImportForm; |
|
16
|
|
|
use LeKoala\ExcelImportExport\ExcelMemberImportForm; |
|
17
|
|
|
use SilverStripe\Forms\GridField\GridFieldExportButton; |
|
18
|
|
|
use SilverStripe\Forms\GridField\GridFieldImportButton; |
|
19
|
|
|
use LeKoala\ExcelImportExport\ExcelGridFieldExportButton; |
|
20
|
|
|
use LeKoala\ExcelImportExport\ExcelGridFieldImportButton; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Extends {@link SecurityAdmin}. to bind new forms and features |
|
24
|
|
|
* |
|
25
|
|
|
* @author Koala |
|
26
|
|
|
*/ |
|
27
|
|
|
class SecurityAdminExcelExtension extends Extension |
|
28
|
|
|
{ |
|
29
|
|
|
private static $allowed_actions = array( |
|
|
|
|
|
|
30
|
|
|
'excelmemberimport', |
|
31
|
|
|
'ExcelMemberImportForm', |
|
32
|
|
|
'excelgroupimport', |
|
33
|
|
|
'ExcelGroupImportForm', |
|
34
|
|
|
'downloadsample', |
|
35
|
|
|
); |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @param Form $form |
|
39
|
|
|
* @return GridField |
|
40
|
|
|
*/ |
|
41
|
|
|
protected function getMembersGridField($form) |
|
42
|
|
|
{ |
|
43
|
|
|
return $form->Fields()->dataFieldByName('Members'); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @param Form $form |
|
48
|
|
|
* @return GridField |
|
49
|
|
|
*/ |
|
50
|
|
|
protected function getGroupsGridField($form) |
|
51
|
|
|
{ |
|
52
|
|
|
return $form->Fields()->dataFieldByName('Groups'); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
public function updateEditForm(Form $form) |
|
56
|
|
|
{ |
|
57
|
|
|
/* @var $owner SecurityAdmin */ |
|
58
|
|
|
$owner = $this->owner; |
|
59
|
|
|
/* @var $config Config_ForClass */ |
|
60
|
|
|
$classConfig = $owner->config(); |
|
61
|
|
|
|
|
62
|
|
|
$members = $this->getMembersGridField($form); |
|
63
|
|
|
$membersConfig = $members->getConfig(); |
|
64
|
|
|
$groups = $this->getGroupsGridField($form); |
|
65
|
|
|
$groupsConfig = $groups->getConfig(); |
|
66
|
|
|
|
|
67
|
|
|
if ($classConfig->allow_import && Permission::check('ADMIN')) { |
|
68
|
|
|
$membersConfig->removeComponentsByType(GridFieldImportButton::class); |
|
69
|
|
|
$ExcelGridFieldImportButton = new ExcelGridFieldImportButton('buttons-before-left'); |
|
70
|
|
|
$ExcelGridFieldImportButton->setImportIframe($this->owner->Link('excelmemberimport')); |
|
71
|
|
|
$ExcelGridFieldImportButton->setModalTitle(_t('ExcelImportExport.IMPORTMEMBERSFROMFILE', 'Import members from a file')); |
|
72
|
|
|
$membersConfig->addComponent($ExcelGridFieldImportButton); |
|
73
|
|
|
|
|
74
|
|
|
$groupsConfig->removeComponentsByType(GridFieldImportButton::class); |
|
75
|
|
|
$ExcelGridFieldImportButton = new ExcelGridFieldImportButton('buttons-before-left'); |
|
76
|
|
|
$ExcelGridFieldImportButton->setImportIframe($this->owner->Link('excelgroupimport')); |
|
77
|
|
|
$ExcelGridFieldImportButton->setModalTitle(_t('ExcelImportExport.IMPORTGROUPSFROMFILE', 'Import groups from a file')); |
|
78
|
|
|
$groupsConfig->addComponent($ExcelGridFieldImportButton); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
// Export features |
|
82
|
|
|
$this->addExportButton($members, $classConfig); |
|
83
|
|
|
$this->addExportButton($groups, $classConfig); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
public function downloadsample() |
|
87
|
|
|
{ |
|
88
|
|
|
$class = $this->owner->getRequest()->param('ID'); |
|
89
|
|
|
switch ($class) { |
|
90
|
|
|
case 'Group': |
|
91
|
|
|
$class = Group::class; |
|
92
|
|
|
break; |
|
93
|
|
|
case 'Member': |
|
94
|
|
|
$class = Member::class; |
|
95
|
|
|
break; |
|
96
|
|
|
} |
|
97
|
|
|
ExcelImportExport::sampleFileForClass($class); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
protected function requireAdminAssets() |
|
101
|
|
|
{ |
|
102
|
|
|
Requirements::clear(); |
|
103
|
|
|
Requirements::javascript('silverstripe/admin: client/dist/js/vendor.js'); |
|
104
|
|
|
Requirements::javascript('silverstripe/admin: client/dist/js/MemberImportForm.js'); |
|
105
|
|
|
Requirements::css('silverstripe/admin: client/dist/styles/bundle.css'); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
protected function addExportButton( |
|
109
|
|
|
GridField $gridfield, |
|
110
|
|
|
Config_ForClass $classConfig |
|
111
|
|
|
) { |
|
112
|
|
|
if (!$gridfield) { |
|
|
|
|
|
|
113
|
|
|
return; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
$config = $gridfield->getConfig(); |
|
117
|
|
|
$class = $gridfield->getModelClass(); |
|
118
|
|
|
|
|
119
|
|
|
// Better export |
|
120
|
|
|
if ($classConfig->export_csv) { |
|
121
|
|
|
/* @var $export GridFieldExportButton */ |
|
122
|
|
|
$export = $this->getGridFieldExportButton($config); |
|
123
|
|
|
$export->setExportColumns(ExcelImportExport::exportFieldsForClass($class)); |
|
124
|
|
|
} else { |
|
125
|
|
|
$config->removeComponentsByType(GridFieldExportButton::class); |
|
126
|
|
|
} |
|
127
|
|
|
if ($classConfig->export_excel) { |
|
128
|
|
|
$ExcelGridFieldExportButton = new ExcelGridFieldExportButton('buttons-before-left'); |
|
129
|
|
|
$config->addComponent($ExcelGridFieldExportButton); |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* @param GridFieldConfig $config |
|
135
|
|
|
* @return GridFieldExportButton |
|
136
|
|
|
*/ |
|
137
|
|
|
protected function getGridFieldExportButton($config) |
|
138
|
|
|
{ |
|
139
|
|
|
return $config->getComponentByType(GridFieldExportButton::class); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* @see SecurityAdmin_MemberImportForm |
|
144
|
|
|
* |
|
145
|
|
|
* @return Form |
|
146
|
|
|
*/ |
|
147
|
|
|
public function ExcelMemberImportForm() |
|
148
|
|
|
{ |
|
149
|
|
|
if (!Permission::check('ADMIN')) { |
|
150
|
|
|
return false; |
|
|
|
|
|
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
$group = $this->owner->currentPage(); |
|
154
|
|
|
$form = new ExcelMemberImportForm( |
|
155
|
|
|
$this->owner, |
|
156
|
|
|
'ExcelMemberImportForm' |
|
157
|
|
|
); |
|
158
|
|
|
$form->setGroup($group); |
|
159
|
|
|
|
|
160
|
|
|
return $form; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
public function excelmemberimport() |
|
164
|
|
|
{ |
|
165
|
|
|
$this->requireAdminAssets(); |
|
166
|
|
|
|
|
167
|
|
|
return $this->owner->renderWith( |
|
168
|
|
|
'BlankPage', |
|
169
|
|
|
array('Form' => $this->ExcelMemberImportForm()->forTemplate(), 'Content' => ' ') |
|
170
|
|
|
); |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* @see SecurityAdmin_GroupImportForm |
|
175
|
|
|
* |
|
176
|
|
|
* @return Form |
|
177
|
|
|
*/ |
|
178
|
|
|
public function ExcelGroupImportForm() |
|
179
|
|
|
{ |
|
180
|
|
|
if (!Permission::check('ADMIN')) { |
|
181
|
|
|
return false; |
|
|
|
|
|
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
$form = new ExcelGroupImportForm( |
|
185
|
|
|
$this->owner, |
|
186
|
|
|
'ExcelGroupImportForm' |
|
187
|
|
|
); |
|
188
|
|
|
|
|
189
|
|
|
return $form; |
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
public function excelgroupimport() |
|
193
|
|
|
{ |
|
194
|
|
|
$this->requireAdminAssets(); |
|
195
|
|
|
|
|
196
|
|
|
return $this->owner->renderWith( |
|
197
|
|
|
'BlankPage', |
|
198
|
|
|
array('Form' => $this->ExcelGroupImportForm()->forTemplate(), 'Content' => ' ') |
|
199
|
|
|
); |
|
200
|
|
|
} |
|
201
|
|
|
} |
|
202
|
|
|
|