diff --git a/public/userarea/get_json_by_code_example.php b/public/userarea/get_json_by_code_example.php new file mode 100644 index 0000000..5ad55f1 --- /dev/null +++ b/public/userarea/get_json_by_code_example.php @@ -0,0 +1,63 @@ + 'Invalid request method']); + exit; + } + + $input = json_decode(file_get_contents('php://input'), true); + $template_id = isset($input['template_id']) ? (int)$input['template_id'] : 0; + $code = isset($input['code']) ? trim($input['code']) : ''; + + if ($template_id <= 0) { + http_response_code(400); + echo json_encode(['error' => 'Missing or invalid template_id']); + exit; + } + + if ($code === '') { + http_response_code(400); + echo json_encode(['error' => 'Missing code']); + exit; + } + + /* + * TODO: Replace this block with your real API call. + * Expected response for import_json.php: + * { + * "success": true, + * "reference": "CODE123", + * "json": { ... real JSON payload ... } + * } + */ + + $sampleJson = [ + 'data' => [[ + 'type' => 'trf_data_request', + 'id' => $code, + 'attributes' => [ + 'trf_type' => 'apparel', + 'service_required' => 'regular', + 'submitter_information' => [ + 'submitter_type' => 'supplier' + ] + ] + ]] + ]; + + echo json_encode([ + 'success' => true, + 'reference' => $code, + 'json' => $sampleJson + ]); +} catch (Throwable $e) { + http_response_code(500); + echo json_encode(['error' => $e->getMessage()]); +} diff --git a/public/userarea/import_dashboard.php b/public/userarea/import_dashboard.php index cae5980..3147504 100644 --- a/public/userarea/import_dashboard.php +++ b/public/userarea/import_dashboard.php @@ -237,7 +237,18 @@ const iconClass = getTemplateIcon(sourceType); const btn = document.createElement("a"); - btn.href = `import_xls2.php?id=${template.id}`; + + // Redirect based on template source type + if (sourceType === 'XLS') { + btn.href = `import_xls2.php?id=${template.id}`; + } else if (sourceType === 'API' || sourceType === 'JSON') { + btn.href = `import_json.php?id=${template.id}`; + } else if (sourceType === 'PDF') { + btn.href = `import_pdf.php?id=${template.id}`; + } else { + btn.href = `import_xls2.php?id=${template.id}`; + } + btn.className = `btn ${sizeClass}`; btn.style.backgroundColor = template.button_bg_color || '#0d6efd'; btn.style.color = template.button_text_color || '#ffffff'; @@ -245,9 +256,9 @@ btn.setAttribute("data-source-type", sourceType); btn.innerHTML = ` - - ${escapeHtml(template.button_label || 'Unnamed')} - `; + + ${escapeHtml(template.button_label || 'Unnamed')} + `; return btn; } diff --git a/public/userarea/import_insert.php b/public/userarea/import_insert.php index 268565a..29d1824 100644 --- a/public/userarea/import_insert.php +++ b/public/userarea/import_insert.php @@ -25,6 +25,7 @@ $rows = json_decode(urldecode($_POST['rows'] ?? '[]'), true); $excelrows = json_decode(urldecode($_POST['excelrows'] ?? '[]'), true); $newFilename = $_POST['filename']; +$source_type = strtolower(trim($_POST['source_type'] ?? 'xls')); $_SESSION['template_id'] = $template_id; $_SESSION['selected_rows'] = $selected_rows; @@ -37,6 +38,7 @@ error_log("Received Data - Template ID: $template_id, Selected Rows: " . json_en error_log("Columns: " . json_encode($columns)); error_log("Rows: " . json_encode($rows)); error_log("Excelrows: " . json_encode($excelrows)); +error_log("Source type: " . $source_type); $user_id = $iduserlogin ?? 1; @@ -47,7 +49,22 @@ $pdo = $db->getConnection(); $importReferenceCode = date('YmdHis') . '-' . uniqid(); // Recupera tutti i mapping dal template -$stmt = $pdo->prepare("SELECT id, excel_column, data_type, is_required, manual_default, is_manual, field_label, field_id, main_field, auto_value FROM template_mapping WHERE template_id = ?"); +$stmt = $pdo->prepare(" + SELECT + id, + excel_column, + json_node, + data_type, + is_required, + manual_default, + is_manual, + field_label, + field_id, + main_field, + auto_value + FROM template_mapping + WHERE template_id = ? +"); $stmt->execute([$template_id]); $allMappings = $stmt->fetchAll(PDO::FETCH_ASSOC); @@ -67,12 +84,20 @@ foreach ($allMappings as $mapping) { // Inserisci le righe selezionate in datadb $insertedIds = []; -foreach ($selected_rows as $rowIndex) { - $row = $rows[$rowIndex] ?? null; - $excelrow = $excelrows[$rowIndex] ?? null; +foreach ($selected_rows as $loopIndex => $rowIndex) { + + if ($source_type === 'json') { + // JSON import sends only selected rows in rows/excelrows + $row = $rows[$loopIndex] ?? null; + $excelrow = $excelrows[$loopIndex] ?? ('JSON-' . ($loopIndex + 1)); + } else { + // XLS import keeps original row indexes + $row = $rows[$rowIndex] ?? null; + $excelrow = $excelrows[$rowIndex] ?? null; + } if ($row === null || $excelrow === null) { - error_log("Errore: riga o excelrow mancante per rowIndex $rowIndex"); + error_log("Errore: riga o excelrow mancante. Source type: $source_type, loopIndex: $loopIndex, rowIndex: $rowIndex"); continue; } @@ -104,14 +129,72 @@ foreach ($selected_rows as $rowIndex) { foreach ($allMappings as $mapping) { $fieldValue = null; if (!$mapping['is_manual']) { - $excelColumn = trim($mapping['excel_column']); - $excelColumnIndex = array_search($excelColumn, array_map('trim', $columns)); - if ($excelColumnIndex !== false && isset($row[$excelColumnIndex]) && $row[$excelColumnIndex] !== '') { - $fieldValue = $row[$excelColumnIndex]; - error_log("Found Excel column '$excelColumn' at index $excelColumnIndex, value: " . var_export($fieldValue, true)); + $sourceColumn = ''; + + if ($source_type === 'json') { + $sourceColumn = trim($mapping['json_node'] ?? ''); + } else { + $sourceColumn = trim($mapping['excel_column'] ?? ''); + } + + // Fallback: if JSON node is empty, try excel_column + if ($sourceColumn === '') { + $sourceColumn = trim($mapping['excel_column'] ?? ''); + } + + $columnsTrimmed = array_map('trim', $columns); + + $candidateColumns = []; + + if ($sourceColumn !== '') { + $candidateColumns[] = $sourceColumn; + + if ($source_type === 'json') { + // Common JSON path variants + $candidateColumns[] = preg_replace('/^data\[\]\./', '', $sourceColumn); + $candidateColumns[] = preg_replace('/^data\.0\./', '', $sourceColumn); + $candidateColumns[] = str_replace('data[].', 'data.0.', $sourceColumn); + $candidateColumns[] = str_replace('data.0.', 'data[].', $sourceColumn); + } + } + + // Remove empty and duplicate candidates + $candidateColumns = array_values(array_unique(array_filter($candidateColumns, function ($value) { + return trim((string)$value) !== ''; + }))); + + $sourceColumnIndex = false; + $matchedColumn = ''; + + foreach ($candidateColumns as $candidateColumn) { + $candidateColumn = trim($candidateColumn); + $index = array_search($candidateColumn, $columnsTrimmed); + + if ($index !== false) { + $sourceColumnIndex = $index; + $matchedColumn = $candidateColumn; + break; + } + } + + if ($sourceColumnIndex !== false && isset($row[$sourceColumnIndex]) && $row[$sourceColumnIndex] !== '') { + $fieldValue = $row[$sourceColumnIndex]; + + error_log( + "Found source column. Original: '$sourceColumn', Matched: '$matchedColumn', Index: $sourceColumnIndex, Value: " . + var_export($fieldValue, true) + ); } else { $fieldValue = $mapping['manual_default'] ?? ''; - error_log("Excel column '$excelColumn' not found or empty, using default: " . var_export($fieldValue, true)); + + error_log( + "Source column not found or empty. Original: '$sourceColumn'. Candidates: " . + json_encode($candidateColumns) . + ". Available columns: " . + json_encode($columnsTrimmed) . + ". Using default: " . + var_export($fieldValue, true) + ); } switch ($mapping['data_type']) { case 'INT': @@ -147,7 +230,7 @@ foreach ($selected_rows as $rowIndex) { if ($mapping['is_required'] && (is_null($fieldValue) || $fieldValue === '')) { error_log("Required field missing for mapping ID: " . $mapping['id'] . ", field: " . $mapping['field_label']); } - error_log("Inserting into import_data_details - Mapping ID: " . $mapping['id'] . ", Field Value: " . var_export($fieldValue, true) . ", Is Manual: " . $mapping['is_manual'] . ", Excel Column: " . ($mapping['excel_column'] ?? 'N/A') . ", Manual Default: " . ($mapping['manual_default'] ?? 'N/A')); + error_log("Inserting into import_data_details - Mapping ID: " . $mapping['id'] . ", Field Value: " . var_export($fieldValue, true) . ", Is Manual: " . $mapping['is_manual'] . ", Source Column: " . ($sourceColumn ?? 'N/A') . ", Source Type: " . $source_type . ", Manual Default: " . ($mapping['manual_default'] ?? 'N/A')); $stmt = $pdo->prepare("INSERT INTO import_data_details (id, mapping_id, field_value) VALUES (?, ?, ?)"); $stmt->execute([$iddatadb, $mapping['id'], $fieldValue]); error_log("Inserted into import_data_details for ID $iddatadb, Mapping ID: " . $mapping['id'] . ", Field Value: " . var_export($fieldValue, true)); @@ -158,4 +241,3 @@ $_SESSION['inserted_ids'] = $insertedIds; header("Location: imported.php?id=" . urlencode($template_id) . "&importref=" . urlencode($importReferenceCode)); exit; -?> diff --git a/public/userarea/import_json.php b/public/userarea/import_json.php new file mode 100644 index 0000000..4dd51e8 --- /dev/null +++ b/public/userarea/import_json.php @@ -0,0 +1,819 @@ +getConnection(); +$stmt = $pdo->prepare("SELECT * FROM excel_templates WHERE id = ?"); +$stmt->execute([$id]); +$template = $stmt->fetch(PDO::FETCH_ASSOC); + +if (!$template) { + header("Location: template_dashboard.php?status=error&message=" . urlencode("Template not found")); + exit; +} + +// Check mappings +$stmt = $pdo->prepare("SELECT id FROM template_mapping WHERE template_id = ?"); +$stmt->execute([$id]); +$hasMappings = $stmt->fetch(PDO::FETCH_ASSOC); + +error_log("Loaded JSON import template: " . print_r($template, true)); +?> + + + + +
+ + + + + + +