149 lines
4.3 KiB
PHP
149 lines
4.3 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use App\Services\MagentoCategoryMigrationService;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
class MagentoMigrationController extends Controller
|
|
{
|
|
protected $migrationService;
|
|
|
|
public function __construct(MagentoCategoryMigrationService $migrationService)
|
|
{
|
|
$this->migrationService = $migrationService;
|
|
}
|
|
|
|
/**
|
|
* Show the migration interface
|
|
*/
|
|
public function index()
|
|
{
|
|
$m1Stores = $this->migrationService->getMagento1Stores();
|
|
$m2Stores = $this->migrationService->getMagento2Stores();
|
|
$connectionTest = $this->migrationService->testConnections();
|
|
$m1Categories = $this->migrationService->getMagento1Categories();
|
|
|
|
return view('migration.index', [
|
|
'm1Stores' => $m1Stores,
|
|
'm2Stores' => $m2Stores,
|
|
'connectionTest' => $connectionTest,
|
|
'm1CategoriesCount' => $m1Categories->count(),
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Test database connections
|
|
*/
|
|
public function testConnections()
|
|
{
|
|
$results = $this->migrationService->testConnections();
|
|
|
|
return response()->json([
|
|
'success' => $results['magento1'] && $results['magento2'],
|
|
'results' => $results,
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Get Magento 1 categories preview
|
|
*/
|
|
public function getMagento1Categories()
|
|
{
|
|
$categories = $this->migrationService->getMagento1Categories();
|
|
|
|
return response()->json([
|
|
'success' => true,
|
|
'count' => $categories->count(),
|
|
'categories' => $categories->take(50)->map(function($cat) {
|
|
return [
|
|
'id' => $cat->entity_id,
|
|
'name' => $cat->name ?? 'N/A',
|
|
'level' => $cat->level,
|
|
'parent_id' => $cat->parent_id,
|
|
'is_active' => $cat->is_active ?? 0,
|
|
];
|
|
}),
|
|
]);
|
|
}
|
|
|
|
/**
|
|
* Get Magento 1 category tree
|
|
*/
|
|
public function getMagento1CategoryTree()
|
|
{
|
|
try {
|
|
$categories = $this->migrationService->getMagento1Categories();
|
|
$tree = $this->migrationService->buildCategoryTreeHierarchy($categories);
|
|
|
|
return response()->json([
|
|
'success' => true,
|
|
'tree' => $tree,
|
|
]);
|
|
} catch (\Exception $e) {
|
|
Log::error('Error fetching M1 category tree: ' . $e->getMessage());
|
|
return response()->json([
|
|
'success' => false,
|
|
'message' => 'Failed to fetch category tree: ' . $e->getMessage(),
|
|
], 500);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Get Magento 2 category tree
|
|
*/
|
|
public function getMagento2CategoryTree()
|
|
{
|
|
try {
|
|
$categories = $this->migrationService->getMagento2Categories();
|
|
$tree = $this->migrationService->buildCategoryTreeHierarchy($categories);
|
|
|
|
return response()->json([
|
|
'success' => true,
|
|
'tree' => $tree,
|
|
]);
|
|
} catch (\Exception $e) {
|
|
Log::error('Error fetching M2 category tree: ' . $e->getMessage());
|
|
return response()->json([
|
|
'success' => false,
|
|
'message' => 'Failed to fetch category tree: ' . $e->getMessage(),
|
|
], 500);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Execute the migration
|
|
*/
|
|
public function migrate(Request $request)
|
|
{
|
|
$request->validate([
|
|
'store_mapping' => 'required|array',
|
|
'store_mapping.*' => 'required|integer',
|
|
]);
|
|
|
|
try {
|
|
$storeMapping = $request->input('store_mapping');
|
|
|
|
// Convert array format: ['m1_store_id' => 'm2_store_id']
|
|
$mapping = [];
|
|
foreach ($storeMapping as $m1StoreId => $m2StoreId) {
|
|
$mapping[(int)$m1StoreId] = (int)$m2StoreId;
|
|
}
|
|
|
|
$result = $this->migrationService->migrateCategories($mapping);
|
|
|
|
return response()->json($result);
|
|
|
|
} catch (\Exception $e) {
|
|
Log::error('Migration error: ' . $e->getMessage());
|
|
|
|
return response()->json([
|
|
'success' => false,
|
|
'message' => 'Migration failed: ' . $e->getMessage(),
|
|
], 500);
|
|
}
|
|
}
|
|
}
|
|
|