|
21 | 21 | use PhpOffice\PhpSpreadsheet\Reader\IReader; |
22 | 22 | use PhpOffice\PhpSpreadsheet\Writer\IWriter; |
23 | 23 | use SilverStripe\Assets\FileNameFilter; |
| 24 | +use SilverStripe\Core\Injector\Injector; |
24 | 25 |
|
25 | 26 | /** |
26 | 27 | * Support class for the module |
@@ -240,20 +241,36 @@ public static function checkImportForm($controller) |
240 | 241 | } |
241 | 242 |
|
242 | 243 | /** |
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 |
244 | 259 | * @param Form $form |
245 | 260 | * @param Controller $controller |
246 | 261 | * @return HTTPResponse |
247 | 262 | */ |
248 | 263 | public static function useCustomHandler($handler, Form $form, Controller $controller) |
249 | 264 | { |
| 265 | + // Check if the class has a ::load method |
250 | 266 | if (!$handler || !method_exists($handler, "load")) { |
251 | 267 | $form->sessionMessage("Invalid handler: $handler", 'bad'); |
252 | 268 | return $controller->redirectBack(); |
253 | 269 | } |
254 | 270 | $file = $_FILES['_CsvFile']['tmp_name']; |
255 | 271 | $name = $_FILES['_CsvFile']['name']; |
256 | | - $inst = new $handler(); |
| 272 | + |
| 273 | + $inst = new $handler($form, $controller); |
257 | 274 |
|
258 | 275 | if (!empty($_POST['OnlyUpdateRecords']) && method_exists($inst, 'setOnlyUpdate')) { |
259 | 276 | $inst->setOnlyUpdate(true); |
|
0 commit comments