900 lines
30 KiB
PHP
900 lines
30 KiB
PHP
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>Magento Category Migration Tool</title>
|
|
<style>
|
|
* {
|
|
margin: 0;
|
|
padding: 0;
|
|
box-sizing: border-box;
|
|
}
|
|
|
|
body {
|
|
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
|
|
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
|
min-height: 100vh;
|
|
padding: 20px;
|
|
}
|
|
|
|
.container {
|
|
max-width: 1200px;
|
|
margin: 0 auto;
|
|
background: white;
|
|
border-radius: 12px;
|
|
box-shadow: 0 20px 60px rgba(0,0,0,0.3);
|
|
overflow: hidden;
|
|
}
|
|
|
|
.header {
|
|
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
|
color: white;
|
|
padding: 30px;
|
|
text-align: center;
|
|
}
|
|
|
|
.header h1 {
|
|
font-size: 2.5em;
|
|
margin-bottom: 10px;
|
|
}
|
|
|
|
.header p {
|
|
opacity: 0.9;
|
|
font-size: 1.1em;
|
|
}
|
|
|
|
.content {
|
|
padding: 30px;
|
|
}
|
|
|
|
.section {
|
|
margin-bottom: 30px;
|
|
padding: 20px;
|
|
background: #f8f9fa;
|
|
border-radius: 8px;
|
|
border-left: 4px solid #667eea;
|
|
}
|
|
|
|
.section h2 {
|
|
color: #333;
|
|
margin-bottom: 15px;
|
|
font-size: 1.5em;
|
|
}
|
|
|
|
.connection-status {
|
|
display: flex;
|
|
gap: 20px;
|
|
margin-top: 15px;
|
|
}
|
|
|
|
.status-badge {
|
|
padding: 10px 20px;
|
|
border-radius: 6px;
|
|
font-weight: 600;
|
|
display: inline-flex;
|
|
align-items: center;
|
|
gap: 8px;
|
|
}
|
|
|
|
.status-badge.success {
|
|
background: #d4edda;
|
|
color: #155724;
|
|
}
|
|
|
|
.status-badge.error {
|
|
background: #f8d7da;
|
|
color: #721c24;
|
|
}
|
|
|
|
.store-mapping {
|
|
display: grid;
|
|
grid-template-columns: 1fr auto 1fr;
|
|
gap: 15px;
|
|
align-items: center;
|
|
margin-top: 15px;
|
|
}
|
|
|
|
.store-select {
|
|
padding: 12px;
|
|
border: 2px solid #ddd;
|
|
border-radius: 6px;
|
|
font-size: 1em;
|
|
width: 100%;
|
|
background: white;
|
|
}
|
|
|
|
.store-select:focus {
|
|
outline: none;
|
|
border-color: #667eea;
|
|
}
|
|
|
|
.arrow {
|
|
font-size: 1.5em;
|
|
color: #667eea;
|
|
text-align: center;
|
|
}
|
|
|
|
.btn {
|
|
padding: 12px 30px;
|
|
border: none;
|
|
border-radius: 6px;
|
|
font-size: 1em;
|
|
font-weight: 600;
|
|
cursor: pointer;
|
|
transition: all 0.3s;
|
|
text-transform: uppercase;
|
|
letter-spacing: 0.5px;
|
|
}
|
|
|
|
.btn-primary {
|
|
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
|
|
color: white;
|
|
}
|
|
|
|
.btn-primary:hover {
|
|
transform: translateY(-2px);
|
|
box-shadow: 0 5px 15px rgba(102, 126, 234, 0.4);
|
|
}
|
|
|
|
.btn-primary:disabled {
|
|
opacity: 0.6;
|
|
cursor: not-allowed;
|
|
transform: none;
|
|
}
|
|
|
|
.btn-secondary {
|
|
background: #6c757d;
|
|
color: white;
|
|
}
|
|
|
|
.btn-secondary:hover {
|
|
background: #5a6268;
|
|
}
|
|
|
|
.actions {
|
|
display: flex;
|
|
gap: 15px;
|
|
margin-top: 20px;
|
|
}
|
|
|
|
.log-container {
|
|
background: #1e1e1e;
|
|
color: #d4d4d4;
|
|
padding: 20px;
|
|
border-radius: 6px;
|
|
max-height: 400px;
|
|
overflow-y: auto;
|
|
font-family: 'Courier New', monospace;
|
|
font-size: 0.9em;
|
|
margin-top: 15px;
|
|
display: none;
|
|
}
|
|
|
|
.log-container.active {
|
|
display: block;
|
|
}
|
|
|
|
.log-entry {
|
|
margin-bottom: 5px;
|
|
padding: 5px 0;
|
|
}
|
|
|
|
.log-entry.error {
|
|
color: #f48771;
|
|
}
|
|
|
|
.log-entry.success {
|
|
color: #4ec9b0;
|
|
}
|
|
|
|
.info-box {
|
|
background: #e7f3ff;
|
|
border-left: 4px solid #2196F3;
|
|
padding: 15px;
|
|
border-radius: 4px;
|
|
margin-top: 15px;
|
|
}
|
|
|
|
.info-box p {
|
|
margin: 5px 0;
|
|
color: #1976D2;
|
|
}
|
|
|
|
.loading {
|
|
display: none;
|
|
text-align: center;
|
|
padding: 20px;
|
|
}
|
|
|
|
.loading.active {
|
|
display: block;
|
|
}
|
|
|
|
.spinner {
|
|
border: 4px solid #f3f3f3;
|
|
border-top: 4px solid #667eea;
|
|
border-radius: 50%;
|
|
width: 40px;
|
|
height: 40px;
|
|
animation: spin 1s linear infinite;
|
|
margin: 0 auto;
|
|
}
|
|
|
|
@keyframes spin {
|
|
0% { transform: rotate(0deg); }
|
|
100% { transform: rotate(360deg); }
|
|
}
|
|
|
|
.stats {
|
|
display: grid;
|
|
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
|
|
gap: 15px;
|
|
margin-top: 15px;
|
|
}
|
|
|
|
.stat-card {
|
|
background: white;
|
|
padding: 20px;
|
|
border-radius: 8px;
|
|
text-align: center;
|
|
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
|
|
}
|
|
|
|
.stat-card .number {
|
|
font-size: 2em;
|
|
font-weight: bold;
|
|
color: #667eea;
|
|
}
|
|
|
|
.stat-card .label {
|
|
color: #666;
|
|
margin-top: 5px;
|
|
}
|
|
|
|
/* Tabs */
|
|
.tabs {
|
|
display: flex;
|
|
border-bottom: 2px solid #ddd;
|
|
margin-bottom: 20px;
|
|
background: #f8f9fa;
|
|
border-radius: 8px 8px 0 0;
|
|
}
|
|
|
|
.tab {
|
|
padding: 15px 30px;
|
|
cursor: pointer;
|
|
border: none;
|
|
background: transparent;
|
|
font-size: 1em;
|
|
font-weight: 600;
|
|
color: #666;
|
|
transition: all 0.3s;
|
|
border-bottom: 3px solid transparent;
|
|
margin-bottom: -2px;
|
|
}
|
|
|
|
.tab:hover {
|
|
background: #e9ecef;
|
|
color: #333;
|
|
}
|
|
|
|
.tab.active {
|
|
color: #667eea;
|
|
border-bottom-color: #667eea;
|
|
background: white;
|
|
}
|
|
|
|
.tab-content {
|
|
display: none;
|
|
}
|
|
|
|
.tab-content.active {
|
|
display: block;
|
|
}
|
|
|
|
/* Tree View */
|
|
.tree-container {
|
|
background: white;
|
|
border: 1px solid #ddd;
|
|
border-radius: 6px;
|
|
padding: 20px;
|
|
max-height: 600px;
|
|
overflow-y: auto;
|
|
}
|
|
|
|
.tree-node {
|
|
margin: 5px 0;
|
|
}
|
|
|
|
.tree-node-item {
|
|
display: flex;
|
|
align-items: center;
|
|
padding: 8px 12px;
|
|
border-radius: 4px;
|
|
cursor: pointer;
|
|
user-select: none;
|
|
transition: background 0.2s;
|
|
}
|
|
|
|
.tree-node-item:hover {
|
|
background: #f0f0f0;
|
|
}
|
|
|
|
.tree-toggle {
|
|
width: 20px;
|
|
height: 20px;
|
|
display: inline-flex;
|
|
align-items: center;
|
|
justify-content: center;
|
|
margin-right: 8px;
|
|
font-size: 12px;
|
|
color: #666;
|
|
cursor: pointer;
|
|
}
|
|
|
|
.tree-toggle.expanded::before {
|
|
content: '▼';
|
|
}
|
|
|
|
.tree-toggle.collapsed::before {
|
|
content: '▶';
|
|
}
|
|
|
|
.tree-toggle.leaf {
|
|
width: 20px;
|
|
margin-right: 8px;
|
|
}
|
|
|
|
.tree-label {
|
|
flex: 1;
|
|
display: flex;
|
|
align-items: center;
|
|
gap: 10px;
|
|
}
|
|
|
|
.tree-label-text {
|
|
font-weight: 500;
|
|
color: #333;
|
|
}
|
|
|
|
.tree-badge {
|
|
padding: 2px 8px;
|
|
border-radius: 12px;
|
|
font-size: 0.75em;
|
|
font-weight: 600;
|
|
}
|
|
|
|
.tree-badge.active {
|
|
background: #d4edda;
|
|
color: #155724;
|
|
}
|
|
|
|
.tree-badge.inactive {
|
|
background: #f8d7da;
|
|
color: #721c24;
|
|
}
|
|
|
|
.tree-children {
|
|
margin-left: 28px;
|
|
border-left: 2px solid #e0e0e0;
|
|
padding-left: 12px;
|
|
display: none;
|
|
}
|
|
|
|
.tree-children.expanded {
|
|
display: block;
|
|
}
|
|
|
|
.tree-loading {
|
|
text-align: center;
|
|
padding: 40px;
|
|
color: #666;
|
|
}
|
|
|
|
.tree-error {
|
|
background: #f8d7da;
|
|
color: #721c24;
|
|
padding: 15px;
|
|
border-radius: 6px;
|
|
margin: 10px 0;
|
|
}
|
|
|
|
.tree-delete-btn {
|
|
background: #dc3545;
|
|
color: white;
|
|
border: none;
|
|
border-radius: 4px;
|
|
padding: 4px 8px;
|
|
font-size: 0.75em;
|
|
cursor: pointer;
|
|
margin-left: 8px;
|
|
transition: background 0.2s;
|
|
}
|
|
|
|
.tree-delete-btn:hover {
|
|
background: #c82333;
|
|
}
|
|
|
|
.tree-delete-btn:disabled {
|
|
background: #ccc;
|
|
cursor: not-allowed;
|
|
}
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<div class="container">
|
|
<div class="header">
|
|
<h1>🛒 Magento Category Migration</h1>
|
|
<p>Migrate categories from Magento 1 to Magento 2 with multi-store support</p>
|
|
</div>
|
|
|
|
<div class="content">
|
|
<!-- Tabs -->
|
|
<div class="tabs">
|
|
<button class="tab active" onclick="switchTab('migration', this)">Migration</button>
|
|
<button class="tab" onclick="switchTab('categories', this)">Category Trees</button>
|
|
</div>
|
|
|
|
<!-- Migration Tab -->
|
|
<div id="tab-migration" class="tab-content active">
|
|
<!-- Connection Status -->
|
|
<div class="section">
|
|
<h2>📡 Database Connections</h2>
|
|
<div class="connection-status">
|
|
<div>
|
|
<strong>Magento 1:</strong>
|
|
<span class="status-badge {{ $connectionTest['magento1'] ? 'success' : 'error' }}">
|
|
{{ $connectionTest['magento1'] ? '✓ Connected' : '✗ Failed' }}
|
|
</span>
|
|
@if(!$connectionTest['magento1'] && isset($connectionTest['magento1_error']))
|
|
<div style="color: #721c24; margin-top: 5px; font-size: 0.9em;">
|
|
{{ $connectionTest['magento1_error'] }}
|
|
</div>
|
|
@endif
|
|
</div>
|
|
<div>
|
|
<strong>Magento 2:</strong>
|
|
<span class="status-badge {{ $connectionTest['magento2'] ? 'success' : 'error' }}">
|
|
{{ $connectionTest['magento2'] ? '✓ Connected' : '✗ Failed' }}
|
|
</span>
|
|
@if(!$connectionTest['magento2'] && isset($connectionTest['magento2_error']))
|
|
<div style="color: #721c24; margin-top: 5px; font-size: 0.9em;">
|
|
{{ $connectionTest['magento2_error'] }}
|
|
</div>
|
|
@endif
|
|
</div>
|
|
</div>
|
|
<button class="btn btn-secondary" onclick="testConnections()">Test Connections</button>
|
|
</div>
|
|
|
|
<!-- Statistics -->
|
|
<div class="section">
|
|
<h2>📊 Statistics</h2>
|
|
<div class="stats">
|
|
<div class="stat-card">
|
|
<div class="number">{{ $m1Stores->count() }}</div>
|
|
<div class="label">Magento 1 Stores</div>
|
|
</div>
|
|
<div class="stat-card">
|
|
<div class="number">{{ $m2Stores->count() }}</div>
|
|
<div class="label">Magento 2 Stores</div>
|
|
</div>
|
|
<div class="stat-card">
|
|
<div class="number">{{ $m1CategoriesCount }}</div>
|
|
<div class="label">Magento 1 Categories</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Store Mapping -->
|
|
<div class="section">
|
|
<h2>🔄 Store Mapping</h2>
|
|
<p>Map each Magento 1 store to its corresponding Magento 2 store:</p>
|
|
|
|
<div id="store-mappings">
|
|
@foreach($m1Stores as $m1Store)
|
|
<div class="store-mapping" style="margin-top: 15px;">
|
|
<div>
|
|
<strong>M1 Store:</strong> {{ $m1Store->name }} ({{ $m1Store->code }})
|
|
</div>
|
|
<div class="arrow">→</div>
|
|
<div>
|
|
<select class="store-select" name="store_mapping[{{ $m1Store->store_id }}]" id="store_{{ $m1Store->store_id }}">
|
|
<option value="">Select Magento 2 Store</option>
|
|
@foreach($m2Stores as $m2Store)
|
|
<option value="{{ $m2Store->store_id }}">{{ $m2Store->name }} ({{ $m2Store->code }})</option>
|
|
@endforeach
|
|
</select>
|
|
</div>
|
|
</div>
|
|
@endforeach
|
|
</div>
|
|
|
|
@if($m1Stores->isEmpty())
|
|
<div class="info-box">
|
|
<p>No Magento 1 stores found. Please check your database connection.</p>
|
|
</div>
|
|
@endif
|
|
</div>
|
|
|
|
<!-- Actions -->
|
|
<div class="section">
|
|
<h2>⚡ Actions</h2>
|
|
<div class="actions">
|
|
<button class="btn btn-primary" onclick="startMigration()" id="migrateBtn" {{ !$connectionTest['magento1'] || !$connectionTest['magento2'] ? 'disabled' : '' }}>
|
|
Start Migration
|
|
</button>
|
|
<button class="btn btn-secondary" onclick="previewCategories()">
|
|
Preview Categories
|
|
</button>
|
|
</div>
|
|
|
|
<div class="loading" id="loading">
|
|
<div class="spinner"></div>
|
|
<p style="margin-top: 15px;">Migration in progress...</p>
|
|
</div>
|
|
|
|
<div class="log-container" id="logContainer">
|
|
<div id="logContent"></div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Category Trees Tab -->
|
|
<div id="tab-categories" class="tab-content">
|
|
<div class="section">
|
|
<h2>🌳 Category Trees</h2>
|
|
<p>View category hierarchies from Magento 1 and Magento 2</p>
|
|
|
|
<div style="display: grid; grid-template-columns: 1fr 1fr; gap: 20px; margin-top: 20px;">
|
|
<!-- Magento 1 Tree -->
|
|
<div>
|
|
<h3 style="margin-bottom: 15px; color: #667eea;">Magento 1 Categories</h3>
|
|
<div class="tree-container" id="m1-tree-container">
|
|
<div class="tree-loading">Loading categories...</div>
|
|
</div>
|
|
</div>
|
|
|
|
<!-- Magento 2 Tree -->
|
|
<div>
|
|
<h3 style="margin-bottom: 15px; color: #764ba2;">Magento 2 Categories</h3>
|
|
<div class="tree-container" id="m2-tree-container">
|
|
<div class="tree-loading">Loading categories...</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<script>
|
|
function testConnections() {
|
|
fetch('{{ route("migration.test-connections") }}')
|
|
.then(response => response.json())
|
|
.then(data => {
|
|
if (data.success) {
|
|
alert('✓ Both database connections are working!');
|
|
location.reload();
|
|
} else {
|
|
alert('✗ Connection test failed. Check the error messages above.');
|
|
}
|
|
})
|
|
.catch(error => {
|
|
alert('Error testing connections: ' + error.message);
|
|
});
|
|
}
|
|
|
|
function previewCategories() {
|
|
fetch('{{ route("migration.magento1-categories") }}')
|
|
.then(response => response.json())
|
|
.then(data => {
|
|
if (data.success) {
|
|
let message = `Found ${data.count} categories in Magento 1.\n\nFirst 10 categories:\n\n`;
|
|
data.categories.forEach(cat => {
|
|
message += `ID: ${cat.id} - ${cat.name} (Level: ${cat.level})\n`;
|
|
});
|
|
alert(message);
|
|
} else {
|
|
alert('Failed to fetch categories');
|
|
}
|
|
})
|
|
.catch(error => {
|
|
alert('Error: ' + error.message);
|
|
});
|
|
}
|
|
|
|
function startMigration() {
|
|
const storeMapping = {};
|
|
const selects = document.querySelectorAll('select[name^="store_mapping"]');
|
|
|
|
let hasMapping = false;
|
|
selects.forEach(select => {
|
|
if (select.value) {
|
|
storeMapping[select.name.match(/\[(\d+)\]/)[1]] = select.value;
|
|
hasMapping = true;
|
|
}
|
|
});
|
|
|
|
if (!hasMapping) {
|
|
alert('Please map at least one store before starting migration.');
|
|
return;
|
|
}
|
|
|
|
if (!confirm('Are you sure you want to start the migration? This will modify your Magento 2 database.')) {
|
|
return;
|
|
}
|
|
|
|
document.getElementById('migrateBtn').disabled = true;
|
|
document.getElementById('loading').classList.add('active');
|
|
document.getElementById('logContainer').classList.add('active');
|
|
document.getElementById('logContent').innerHTML = '<div class="log-entry">Starting migration...</div>';
|
|
|
|
fetch('{{ route("migration.migrate") }}', {
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'X-CSRF-TOKEN': '{{ csrf_token() }}'
|
|
},
|
|
body: JSON.stringify({ store_mapping: storeMapping })
|
|
})
|
|
.then(response => response.json())
|
|
.then(data => {
|
|
document.getElementById('loading').classList.remove('active');
|
|
document.getElementById('migrateBtn').disabled = false;
|
|
|
|
if (data.success) {
|
|
addLogEntry(`✓ Migration completed successfully!`, 'success');
|
|
addLogEntry(`Total categories processed: ${data.migrated_count}`, 'success');
|
|
if (data.added_count !== undefined) {
|
|
addLogEntry(` - Added new categories: ${data.added_count}`, 'success');
|
|
}
|
|
if (data.existing_count !== undefined) {
|
|
addLogEntry(` - Found existing categories: ${data.existing_count}`, 'success');
|
|
}
|
|
|
|
if (data.log && data.log.length > 0) {
|
|
data.log.forEach(log => {
|
|
const type = log.includes('ERROR') ? 'error' : 'success';
|
|
addLogEntry(log, type);
|
|
});
|
|
}
|
|
} else {
|
|
addLogEntry(`✗ Migration failed: ${data.message}`, 'error');
|
|
if (data.log && data.log.length > 0) {
|
|
data.log.forEach(log => {
|
|
addLogEntry(log, log.includes('ERROR') ? 'error' : 'success');
|
|
});
|
|
}
|
|
}
|
|
})
|
|
.catch(error => {
|
|
document.getElementById('loading').classList.remove('active');
|
|
document.getElementById('migrateBtn').disabled = false;
|
|
addLogEntry(`✗ Error: ${error.message}`, 'error');
|
|
});
|
|
}
|
|
|
|
function addLogEntry(message, type = '') {
|
|
const logContent = document.getElementById('logContent');
|
|
const entry = document.createElement('div');
|
|
entry.className = `log-entry ${type}`;
|
|
entry.textContent = message;
|
|
logContent.appendChild(entry);
|
|
logContent.scrollTop = logContent.scrollHeight;
|
|
}
|
|
|
|
// Tab switching
|
|
function switchTab(tabName, tabElement) {
|
|
// Hide all tabs
|
|
document.querySelectorAll('.tab-content').forEach(content => {
|
|
content.classList.remove('active');
|
|
});
|
|
document.querySelectorAll('.tab').forEach(tab => {
|
|
tab.classList.remove('active');
|
|
});
|
|
|
|
// Show selected tab
|
|
document.getElementById('tab-' + tabName).classList.add('active');
|
|
tabElement.classList.add('active');
|
|
|
|
// Load category trees if switching to categories tab
|
|
if (tabName === 'categories') {
|
|
loadCategoryTrees();
|
|
}
|
|
}
|
|
|
|
// Load category trees
|
|
function loadCategoryTrees() {
|
|
loadM1Tree();
|
|
loadM2Tree();
|
|
}
|
|
|
|
// Load Magento 1 tree
|
|
function loadM1Tree() {
|
|
const container = document.getElementById('m1-tree-container');
|
|
container.innerHTML = '<div class="tree-loading">Loading categories...</div>';
|
|
|
|
fetch('{{ route("migration.magento1-category-tree") }}')
|
|
.then(response => response.json())
|
|
.then(data => {
|
|
if (data.success) {
|
|
container.innerHTML = '';
|
|
if (data.tree && data.tree.length > 0) {
|
|
renderTree(container, data.tree, 'm1');
|
|
} else {
|
|
container.innerHTML = '<div class="tree-loading">No categories found</div>';
|
|
}
|
|
} else {
|
|
container.innerHTML = `<div class="tree-error">Error: ${data.message || 'Failed to load categories'}</div>`;
|
|
}
|
|
})
|
|
.catch(error => {
|
|
container.innerHTML = `<div class="tree-error">Error: ${error.message}</div>`;
|
|
});
|
|
}
|
|
|
|
// Load Magento 2 tree
|
|
function loadM2Tree() {
|
|
const container = document.getElementById('m2-tree-container');
|
|
container.innerHTML = '<div class="tree-loading">Loading categories...</div>';
|
|
|
|
fetch('{{ route("migration.magento2-category-tree") }}')
|
|
.then(response => response.json())
|
|
.then(data => {
|
|
if (data.success) {
|
|
container.innerHTML = '';
|
|
if (data.tree && data.tree.length > 0) {
|
|
renderTree(container, data.tree, 'm2');
|
|
} else {
|
|
container.innerHTML = '<div class="tree-loading">No categories found</div>';
|
|
}
|
|
} else {
|
|
container.innerHTML = `<div class="tree-error">Error: ${data.message || 'Failed to load categories'}</div>`;
|
|
}
|
|
})
|
|
.catch(error => {
|
|
container.innerHTML = `<div class="tree-error">Error: ${error.message}</div>`;
|
|
});
|
|
}
|
|
|
|
// Delete category
|
|
function deleteCategory(categoryId, categoryName, source) {
|
|
if (!confirm(`Are you sure you want to delete the category "${categoryName}"?\n\nThis action cannot be undone.`)) {
|
|
return;
|
|
}
|
|
|
|
const container = source === 'm1' ? document.getElementById('m1-tree-container') : document.getElementById('m2-tree-container');
|
|
const originalContent = container.innerHTML;
|
|
container.innerHTML = '<div class="tree-loading">Deleting category...</div>';
|
|
|
|
const url = `{{ route("migration.delete-category", ["categoryId" => ":id"]) }}`.replace(':id', categoryId);
|
|
|
|
fetch(url, {
|
|
method: 'DELETE',
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'X-CSRF-TOKEN': '{{ csrf_token() }}'
|
|
},
|
|
body: JSON.stringify({ source: source })
|
|
})
|
|
.then(response => response.json())
|
|
.then(data => {
|
|
if (data.success) {
|
|
// Reload the tree
|
|
if (source === 'm1') {
|
|
loadM1Tree();
|
|
} else {
|
|
loadM2Tree();
|
|
}
|
|
alert('Category deleted successfully!');
|
|
} else {
|
|
container.innerHTML = originalContent;
|
|
alert('Error: ' + (data.message || 'Failed to delete category'));
|
|
}
|
|
})
|
|
.catch(error => {
|
|
container.innerHTML = originalContent;
|
|
alert('Error: ' + error.message);
|
|
});
|
|
}
|
|
|
|
// Render tree structure
|
|
function renderTree(container, nodes, source = 'm2') {
|
|
nodes.forEach(node => {
|
|
const nodeElement = createTreeNode(node, source);
|
|
container.appendChild(nodeElement);
|
|
});
|
|
}
|
|
|
|
// Create a tree node element
|
|
function createTreeNode(node, source = 'm2') {
|
|
const nodeDiv = document.createElement('div');
|
|
nodeDiv.className = 'tree-node';
|
|
nodeDiv.setAttribute('data-category-id', node.id);
|
|
|
|
const hasChildren = node.children && node.children.length > 0;
|
|
const isRootLevel = node.parent_id == 0 || node.parent_id == 1;
|
|
const canDelete = isRootLevel && !hasChildren;
|
|
|
|
const itemDiv = document.createElement('div');
|
|
itemDiv.className = 'tree-node-item';
|
|
|
|
const toggle = document.createElement('span');
|
|
toggle.className = hasChildren ? 'tree-toggle collapsed' : 'tree-toggle leaf';
|
|
if (hasChildren) {
|
|
toggle.onclick = function(e) {
|
|
e.stopPropagation();
|
|
toggleNode(this);
|
|
};
|
|
}
|
|
|
|
const label = document.createElement('div');
|
|
label.className = 'tree-label';
|
|
|
|
const labelText = document.createElement('span');
|
|
labelText.className = 'tree-label-text';
|
|
labelText.textContent = node.name || 'Unnamed Category';
|
|
|
|
const badge = document.createElement('span');
|
|
badge.className = `tree-badge ${node.is_active ? 'active' : 'inactive'}`;
|
|
badge.textContent = node.is_active ? 'Active' : 'Inactive';
|
|
|
|
label.appendChild(labelText);
|
|
label.appendChild(badge);
|
|
|
|
// Add delete button if category is eligible for deletion
|
|
if (canDelete) {
|
|
const deleteBtn = document.createElement('button');
|
|
deleteBtn.className = 'tree-delete-btn';
|
|
deleteBtn.textContent = 'Delete';
|
|
deleteBtn.title = 'Delete this category';
|
|
deleteBtn.onclick = function(e) {
|
|
e.stopPropagation();
|
|
deleteCategory(node.id, node.name, source);
|
|
};
|
|
label.appendChild(deleteBtn);
|
|
}
|
|
|
|
itemDiv.appendChild(toggle);
|
|
itemDiv.appendChild(label);
|
|
|
|
nodeDiv.appendChild(itemDiv);
|
|
|
|
if (hasChildren) {
|
|
const childrenDiv = document.createElement('div');
|
|
childrenDiv.className = 'tree-children';
|
|
node.children.forEach(child => {
|
|
childrenDiv.appendChild(createTreeNode(child, source));
|
|
});
|
|
nodeDiv.appendChild(childrenDiv);
|
|
}
|
|
|
|
return nodeDiv;
|
|
}
|
|
|
|
// Toggle node expansion
|
|
function toggleNode(toggleElement) {
|
|
const nodeItem = toggleElement.parentElement;
|
|
const nodeDiv = nodeItem.parentElement;
|
|
const childrenDiv = nodeDiv.querySelector('.tree-children');
|
|
|
|
if (childrenDiv) {
|
|
const isExpanded = childrenDiv.classList.contains('expanded');
|
|
if (isExpanded) {
|
|
childrenDiv.classList.remove('expanded');
|
|
toggleElement.classList.remove('expanded');
|
|
toggleElement.classList.add('collapsed');
|
|
} else {
|
|
childrenDiv.classList.add('expanded');
|
|
toggleElement.classList.remove('collapsed');
|
|
toggleElement.classList.add('expanded');
|
|
}
|
|
}
|
|
}
|
|
</script>
|
|
</body>
|
|
</html>
|
|
|