]> BookStack Code Mirror - bookstack/blobdiff - app/Exports/Controllers/ImportController.php
Merge pull request #5731 from BookStackApp/lexical_jul25
[bookstack] / app / Exports / Controllers / ImportController.php
index ec5ac80808bd84ffb2127010bee820740e70efec..b938dac8e29191b045eab9e8022afcf1018da678 100644 (file)
@@ -4,7 +4,7 @@ declare(strict_types=1);
 
 namespace BookStack\Exports\Controllers;
 
-use BookStack\Activity\ActivityType;
+use BookStack\Exceptions\ZipImportException;
 use BookStack\Exceptions\ZipValidationException;
 use BookStack\Exports\ImportRepo;
 use BookStack\Http\Controller;
@@ -48,12 +48,9 @@ class ImportController extends Controller
         try {
             $import = $this->imports->storeFromUpload($file);
         } catch (ZipValidationException $exception) {
-            session()->flash('validation_errors', $exception->errors);
-            return redirect('/import');
+            return redirect('/import')->with('validation_errors', $exception->errors);
         }
 
-        $this->logActivity(ActivityType::IMPORT_CREATE, $import);
-
         return redirect($import->getUrl());
     }
 
@@ -73,24 +70,31 @@ class ImportController extends Controller
         ]);
     }
 
+    /**
+     * Run the import process against an uploaded import ZIP.
+     */
     public function run(int $id, Request $request)
     {
-        // TODO - Test access/visibility
         $import = $this->imports->findVisible($id);
         $parent = null;
 
-        if ($import->getType() === 'page' || $import->getType() === 'chapter') {
+        if ($import->type === 'page' || $import->type === 'chapter') {
+            session()->setPreviousUrl($import->getUrl());
             $data = $this->validate($request, [
-                'parent' => ['required', 'string']
+                'parent' => ['required', 'string'],
             ]);
             $parent = $data['parent'];
         }
 
-        // TODO  - Run import
-           // TODO - Validate again before
-           // TODO - Check permissions before (create for main item, create for children, create for related items [image, attachments])
-        // TODO - Redirect to result
-        // TODO - Or redirect back with errors
+        try {
+            $entity = $this->imports->runImport($import, $parent);
+        } catch (ZipImportException $exception) {
+            session()->flush();
+            $this->showErrorNotification(trans('errors.import_zip_failed_notification'));
+            return redirect($import->getUrl())->with('import_errors', $exception->errors);
+        }
+
+        return redirect($entity->getUrl());
     }
 
     /**
@@ -101,8 +105,6 @@ class ImportController extends Controller
         $import = $this->imports->findVisible($id);
         $this->imports->deleteImport($import);
 
-        $this->logActivity(ActivityType::IMPORT_DELETE, $import);
-
         return redirect('/import');
     }
 }