migrate/app/Http/Controllers/CategoriesController.php

288 lines
9.4 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Services\MagentoCategoryMigrationService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
class CategoriesController extends Controller
{
protected $migrationService;
public function __construct(MagentoCategoryMigrationService $migrationService)
{
$this->migrationService = $migrationService;
}
/**
* Show the categories page
*/
public function index()
{
$m1Categories = $this->migrationService->getMagento1Categories();
$m2Categories = $this->migrationService->getMagento2Categories();
$m2CategoriesNotInM1 = $this->migrationService->getM2CategoriesNotInM1();
$m1CategoriesNotInM2 = $this->migrationService->getM1CategoriesNotInM2();
return view('categories.index', [
'm1CategoriesCount' => $m1Categories->count(),
'm2CategoriesCount' => $m2Categories->count(),
'm2CategoriesNotInM1' => $m2CategoriesNotInM1,
'm1CategoriesNotInM2' => $m1CategoriesNotInM2,
]);
}
/**
* 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, 'm1');
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 1 category tree with products
*/
public function getMagento1CategoryTreeWithProducts()
{
try {
$tree = $this->migrationService->getMagento1CategoryTreeWithProducts();
return response()->json([
'success' => true,
'tree' => $tree
]);
} catch (\Exception $e) {
Log::error('Error fetching M1 category tree with products: ' . $e->getMessage());
return response()->json([
'success' => false,
'message' => 'Failed to load category tree with products: ' . $e->getMessage(),
'tree' => []
], 500);
}
}
/**
* Get Magento 2 category tree
*/
public function getMagento2CategoryTree()
{
try {
$categories = $this->migrationService->getMagento2Categories();
$tree = $this->migrationService->buildCategoryTreeHierarchy($categories, 'm2');
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);
}
}
/**
* Get Magento 2 category tree with products
*/
public function getMagento2CategoryTreeWithProducts()
{
try {
$tree = $this->migrationService->getMagento2CategoryTreeWithProducts();
return response()->json([
'success' => true,
'tree' => $tree
]);
} catch (\Exception $e) {
Log::error('Error fetching M2 category tree with products: ' . $e->getMessage());
return response()->json([
'success' => false,
'message' => 'Failed to load category tree with products: ' . $e->getMessage(),
'tree' => []
], 500);
}
}
/**
* Get M2 categories not in M1 (for AJAX refresh)
*/
public function getM2CategoriesNotInM1()
{
try {
$categories = $this->migrationService->getM2CategoriesNotInM1();
return response()->json([
'success' => true,
'categories' => $categories->map(function($category) {
return [
'entity_id' => $category->entity_id,
'name' => $category->name ?? 'Unnamed Category',
'level' => $category->level ?? 'N/A',
'is_active' => $category->is_active ?? 0,
'root_category_name' => $category->root_category_name ?? 'N/A',
'root_category_id' => $category->root_category_id ?? null,
'path' => $category->path ?? 'N/A',
];
}),
'count' => $categories->count(),
]);
} catch (\Exception $e) {
Log::error('Error fetching M2 categories not in M1: ' . $e->getMessage());
return response()->json([
'success' => false,
'message' => 'Failed to fetch categories: ' . $e->getMessage(),
], 500);
}
}
/**
* Get M1 categories not in M2 (for AJAX refresh)
*/
public function getM1CategoriesNotInM2()
{
try {
$categories = $this->migrationService->getM1CategoriesNotInM2();
return response()->json([
'success' => true,
'categories' => $categories->map(function($category) {
return [
'entity_id' => $category->entity_id,
'name' => $category->name ?? 'Unnamed Category',
'level' => $category->level ?? 'N/A',
'is_active' => $category->is_active ?? 0,
'root_category_name' => $category->root_category_name ?? 'N/A',
'root_category_id' => $category->root_category_id ?? null,
'path' => $category->path ?? 'N/A',
];
}),
'count' => $categories->count(),
]);
} catch (\Exception $e) {
Log::error('Error fetching M1 categories not in M2: ' . $e->getMessage());
return response()->json([
'success' => false,
'message' => 'Failed to fetch categories: ' . $e->getMessage(),
], 500);
}
}
/**
* Delete a category
*/
public function deleteCategory(Request $request, $categoryId)
{
$request->validate([
'source' => 'nullable|in:m1,m2',
]);
try {
$source = $request->input('source', 'm2');
$result = $this->migrationService->deleteCategory($categoryId, $source);
return response()->json($result, $result['success'] ? 200 : 400);
} catch (\Exception $e) {
Log::error('Category deletion error: ' . $e->getMessage());
return response()->json([
'success' => false,
'message' => 'Deletion failed: ' . $e->getMessage(),
], 500);
}
}
/**
* Migrate a single category from Magento 1 to Magento 2
*/
public function migrateCategory(Request $request, $categoryId)
{
$request->validate([
'store_id' => 'nullable|integer',
]);
try {
$storeId = $request->input('store_id', 0);
$result = $this->migrationService->migrateSingleCategory($categoryId, $storeId);
return response()->json($result, $result['success'] ? 200 : 400);
} catch (\Exception $e) {
Log::error('Category migration error: ' . $e->getMessage());
return response()->json([
'success' => false,
'message' => 'Migration failed: ' . $e->getMessage(),
], 500);
}
}
/**
* Rename a Magento 2 category
*/
public function renameCategory(Request $request, $categoryId)
{
$request->validate([
'name' => 'required|string|max:255',
'store_id' => 'nullable|integer',
]);
try {
$newName = $request->input('name');
$storeId = $request->input('store_id', 0);
$result = $this->migrationService->renameCategory($categoryId, $newName, $storeId);
return response()->json($result, $result['success'] ? 200 : 400);
} catch (\Exception $e) {
Log::error('Category rename error: ' . $e->getMessage());
return response()->json([
'success' => false,
'message' => 'Rename failed: ' . $e->getMessage(),
], 500);
}
}
}