]> BookStack Code Mirror - bookstack/blobdiff - app/Exports/Controllers/ImportController.php
ZIP Imports: Added parent and permission check pre-import
[bookstack] / app / Exports / Controllers / ImportController.php
index 640b4c1089147bf1dac2d31d25de4acc6c67ca89..ec5ac80808bd84ffb2127010bee820740e70efec 100644 (file)
@@ -1,7 +1,10 @@
 <?php
 
+declare(strict_types=1);
+
 namespace BookStack\Exports\Controllers;
 
+use BookStack\Activity\ActivityType;
 use BookStack\Exceptions\ZipValidationException;
 use BookStack\Exports\ImportRepo;
 use BookStack\Http\Controller;
@@ -16,15 +19,25 @@ class ImportController extends Controller
         $this->middleware('can:content-import');
     }
 
-    public function start(Request $request)
+    /**
+     * Show the view to start a new import, and also list out the existing
+     * in progress imports that are visible to the user.
+     */
+    public function start()
     {
-        // TODO - Show existing imports for user (or for all users if admin-level user)
+        $imports = $this->imports->getVisibleImports();
+
+        $this->setPageTitle(trans('entities.import'));
 
         return view('exports.import', [
+            'imports' => $imports,
             'zipErrors' => session()->pull('validation_errors') ?? [],
         ]);
     }
 
+    /**
+     * Upload, validate and store an import file.
+     */
     public function upload(Request $request)
     {
         $this->validate($request, [
@@ -39,6 +52,57 @@ class ImportController extends Controller
             return redirect('/import');
         }
 
-        return redirect("imports/{$import->id}");
+        $this->logActivity(ActivityType::IMPORT_CREATE, $import);
+
+        return redirect($import->getUrl());
+    }
+
+    /**
+     * Show a pending import, with a form to allow progressing
+     * with the import process.
+     */
+    public function show(int $id)
+    {
+        $import = $this->imports->findVisible($id);
+
+        $this->setPageTitle(trans('entities.import_continue'));
+
+        return view('exports.import-show', [
+            'import' => $import,
+            'data' => $import->decodeMetadata(),
+        ]);
+    }
+
+    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') {
+            $data = $this->validate($request, [
+                '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
+    }
+
+    /**
+     * Delete an active pending import from the filesystem and database.
+     */
+    public function delete(int $id)
+    {
+        $import = $this->imports->findVisible($id);
+        $this->imports->deleteImport($import);
+
+        $this->logActivity(ActivityType::IMPORT_DELETE, $import);
+
+        return redirect('/import');
     }
 }