Files
jouwdriedee/app/Http/Controllers/Admin/MakerWorldController.php
T
2026-10-31 14:23:53 +01:00

261 lines
7.9 KiB
PHP

<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use App\Models\Category;
use App\Models\Product;
use App\Services\MakerWorldService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\View\View;
class MakerWorldController extends Controller
{
public function __construct(
public MakerWorldService $makerWorld
) {}
/**
* Display the MakerWorld importer dashboard.
*/
public function index(Request $request): View
{
$isAuthenticated = $this->makerWorld->isAuthenticated();
$userProfile = $this->makerWorld->getUserProfile();
$tab = $request->query('tab', $isAuthenticated ? 'favorites' : 'community');
$models = [];
if ($tab === 'favorites' && $isAuthenticated) {
$models = $this->makerWorld->getFavorites();
} elseif ($tab === 'published' && $isAuthenticated) {
$models = $this->makerWorld->getPublished();
} elseif ($tab === 'community') {
$models = $this->makerWorld->getCommunityShowcase();
}
$importedProducts = Product::with('category')
->whereNotNull('makerworld_id')
->latest()
->paginate(15);
$importedIds = Product::whereNotNull('makerworld_id')
->pluck('makerworld_id')
->toArray();
$categories = Category::orderBy('name')->get();
return view('admin.makerworld.index', [
'isAuthenticated' => $isAuthenticated,
'userProfile' => $userProfile,
'currentTab' => $tab,
'models' => $models,
'importedProducts' => $importedProducts,
'importedIds' => $importedIds,
'categories' => $categories,
]);
}
/**
* Authenticate with MakerWorld credentials.
*/
public function login(Request $request): RedirectResponse
{
$validated = $request->validate([
'account' => 'required|string',
'password' => 'required|string',
]);
$result = $this->makerWorld->loginWithCredentials(
$validated['account'],
$validated['password']
);
if ($result['status'] === 'success') {
return redirect()
->route('admin.makerworld.index', ['tab' => 'favorites'])
->with('success', $result['message']);
}
if ($result['status'] === '2fa_required') {
return redirect()
->route('admin.makerworld.index')
->with('warning', $result['message'])
->with('makerworld_2fa_account', $result['account']);
}
return redirect()
->route('admin.makerworld.index')
->with('error', $result['message']);
}
/**
* Submit email verification code for 2FA.
*/
public function verifyCode(Request $request): RedirectResponse
{
$validated = $request->validate([
'account' => 'required|string',
'code' => 'required|string|size:6',
]);
$result = $this->makerWorld->verifyLoginCode(
$validated['account'],
$validated['code']
);
if ($result['status'] === 'success') {
return redirect()
->route('admin.makerworld.index', ['tab' => 'favorites'])
->with('success', $result['message']);
}
return redirect()
->route('admin.makerworld.index')
->with('error', $result['message'])
->with('makerworld_2fa_account', $validated['account']);
}
/**
* Save MakerWorld session / bearer token directly.
*/
public function saveToken(Request $request): RedirectResponse
{
$validated = $request->validate([
'token' => 'required|string|min:10',
]);
$this->makerWorld->setAccessToken($validated['token']);
return redirect()
->route('admin.makerworld.index', ['tab' => 'favorites'])
->with('success', 'MakerWorld token succesvol opgeslagen en verbonden!');
}
/**
* Disconnect MakerWorld account.
*/
public function disconnect(): RedirectResponse
{
$this->makerWorld->logout();
return redirect()
->route('admin.makerworld.index', ['tab' => 'community'])
->with('success', 'MakerWorld account succesvol ontkoppeld.');
}
/**
* Ajax preview endpoint for single model by URL or ID.
*/
public function preview(Request $request): JsonResponse
{
$input = $request->query('query');
if (! $input) {
return response()->json(['error' => 'Geen model URL of ID opgegeven.'], 400);
}
$id = $this->makerWorld->parseModelId($input);
if (! $id) {
return response()->json(['error' => 'Ongeldige MakerWorld URL of model-ID.'], 400);
}
$details = $this->makerWorld->getDesignDetails($id);
if (! $details) {
return response()->json(['error' => "Model #{$id} kon niet worden opgehaald bij MakerWorld."], 404);
}
$formatted = $this->makerWorld->formatDesignForImport($details);
$alreadyImported = Product::where('makerworld_id', $id)->first();
return response()->json([
'model' => $formatted,
'raw' => $details,
'is_imported' => (bool) $alreadyImported,
'existing_product' => $alreadyImported,
]);
}
/**
* Import a single model into shop.
*/
public function import(Request $request): RedirectResponse|JsonResponse
{
$validated = $request->validate([
'makerworld_id' => 'required|integer',
'category_id' => 'nullable|exists:categories,id',
'custom_price' => 'nullable|numeric|min:0.5',
'custom_name' => 'nullable|string|max:255',
'is_active' => 'nullable|boolean',
]);
$details = $this->makerWorld->getDesignDetails($validated['makerworld_id']);
if (! $details) {
$msg = "Model #{$validated['makerworld_id']} kon niet worden opgehaald.";
if ($request->wantsJson()) {
return response()->json(['error' => $msg], 404);
}
return redirect()->back()->with('error', $msg);
}
$product = $this->makerWorld->importDesign(
$details,
$validated['category_id'] ?? null,
$validated['custom_price'] ?? null,
$validated['custom_name'] ?? null,
$request->boolean('is_active', true)
);
$msg = "Model \"{$product->name}\" is succesvol toegevoegd aan je webshop!";
if ($request->wantsJson()) {
return response()->json([
'success' => true,
'message' => $msg,
'product' => $product,
]);
}
return redirect()->back()->with('success', $msg);
}
/**
* Batch import multiple selected models.
*/
public function batchImport(Request $request): RedirectResponse
{
$validated = $request->validate([
'ids' => 'required|array',
'ids.*' => 'integer',
'category_id' => 'nullable|exists:categories,id',
'is_active' => 'nullable|boolean',
]);
$count = 0;
$isActive = $request->boolean('is_active', true);
$categoryId = $validated['category_id'] ?? null;
foreach ($validated['ids'] as $id) {
$details = $this->makerWorld->getDesignDetails($id);
if ($details) {
$this->makerWorld->importDesign(
$details,
$categoryId,
null,
null,
$isActive
);
$count++;
}
}
return redirect()->back()->with('success', "{$count} modellen succesvol geïmporteerd naar je webshop!");
}
}