Skip to content

Commit 11191d1

Browse files
committed
pass arguments to custom handler
1 parent 3e3b0f7 commit 11191d1

File tree

1 file changed

+19
-2
lines changed

1 file changed

+19
-2
lines changed

src/ExcelImportExport.php

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use PhpOffice\PhpSpreadsheet\Reader\IReader;
2222
use PhpOffice\PhpSpreadsheet\Writer\IWriter;
2323
use SilverStripe\Assets\FileNameFilter;
24+
use SilverStripe\Core\Injector\Injector;
2425

2526
/**
2627
* Support class for the module
@@ -240,20 +241,36 @@ public static function checkImportForm($controller)
240241
}
241242

242243
/**
243-
* @param string $handler
244+
* This can be used in your ModelAdmin class
245+
*
246+
* public function import(array $data, Form $form): HTTPResponse
247+
* {
248+
* if (!ExcelImportExport::checkImportForm($this)) {
249+
* throw new Exception("Invalid import form");
250+
* }
251+
* $handler = $data['ImportHandler'] ?? null;
252+
* if ($handler == "default") {
253+
* return parent::import($data, $form);
254+
* }
255+
* return ExcelImportExport::useCustomHandler($handler, $form, $this);
256+
* }
257+
*
258+
* @param class-string $handler
244259
* @param Form $form
245260
* @param Controller $controller
246261
* @return HTTPResponse
247262
*/
248263
public static function useCustomHandler($handler, Form $form, Controller $controller)
249264
{
265+
// Check if the class has a ::load method
250266
if (!$handler || !method_exists($handler, "load")) {
251267
$form->sessionMessage("Invalid handler: $handler", 'bad');
252268
return $controller->redirectBack();
253269
}
254270
$file = $_FILES['_CsvFile']['tmp_name'];
255271
$name = $_FILES['_CsvFile']['name'];
256-
$inst = new $handler();
272+
273+
$inst = new $handler($form, $controller);
257274

258275
if (!empty($_POST['OnlyUpdateRecords']) && method_exists($inst, 'setOnlyUpdate')) {
259276
$inst->setOnlyUpdate(true);

0 commit comments

Comments
 (0)