86 lines
3.7 KiB
PHP
86 lines
3.7 KiB
PHP
<?php
|
|
ob_start();
|
|
session_start();
|
|
require_once '../../vendor/autoload.php';
|
|
|
|
Dotenv\Dotenv::createImmutable(dirname(__DIR__, 2))->safeLoad();
|
|
date_default_timezone_set($_ENV['APP_TIMEZONE'] ?? 'Europe/Rome');
|
|
|
|
$response = ['error' => '', 'rows' => [], 'columns' => [], 'template_id' => 0, 'filename' => '', 'excel_data' => []];
|
|
|
|
try {
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
$input = json_decode(file_get_contents('php://input'), true);
|
|
$template_id = isset($input['template_id']) ? intval($input['template_id']) : 0;
|
|
$filename = $input['routine_data']['filename'] ?? '';
|
|
$headerrow = $input['routine_data']['headerrow'] ?? 1;
|
|
$excelData = $input['excel_data'] ?? [];
|
|
$routineData = $input['routine_data'] ?? [];
|
|
|
|
if (!$filename || empty($excelData)) {
|
|
throw new Exception("Dati della routine mancanti.");
|
|
}
|
|
|
|
// --- NUOVO: risolvi la routine dal DB per decidere legacy vs motore ---
|
|
// Riusa la connessione PDO del progetto.
|
|
require_once __DIR__ . '/class/db-functions.php';
|
|
$pdo = DBHandlerSelect::getInstance()->getConnection();
|
|
|
|
// Identifica la routine: preferisci idroutine se passato, altrimenti risali dal filename.
|
|
$idroutine = isset($input['routine_data']['idroutine'])
|
|
? (int)$input['routine_data']['idroutine']
|
|
: 0;
|
|
|
|
if ($idroutine > 0) {
|
|
$stmt = $pdo->prepare("SELECT idroutine, name, filename, engine FROM routine WHERE idroutine = :id LIMIT 1");
|
|
$stmt->execute([':id' => $idroutine]);
|
|
} else {
|
|
$stmt = $pdo->prepare("SELECT idroutine, name, filename, engine FROM routine WHERE filename = :fn LIMIT 1");
|
|
$stmt->execute([':fn' => $filename]);
|
|
}
|
|
$routineRow = $stmt->fetch();
|
|
|
|
// engine = 1 -> motore generico (_engine.php); 0 o record assente -> legacy come oggi.
|
|
$useEngine = $routineRow && (int)$routineRow['engine'] === 1;
|
|
|
|
if ($useEngine) {
|
|
// Il motore ha bisogno di sapere quale routine caricare dagli step.
|
|
$routineData['idroutine'] = (int)$routineRow['idroutine'];
|
|
$routineData['name'] = $routineData['name'] ?? ($routineRow['name'] ?? '');
|
|
$routineFile = __DIR__ . '/routines/_engine.php';
|
|
} else {
|
|
// Percorso legacy: il vecchio file della routine, identico a prima.
|
|
$legacyFilename = $routineRow['filename'] ?? $filename;
|
|
$routineFile = __DIR__ . '/routines/' . $legacyFilename;
|
|
}
|
|
|
|
if (file_exists($routineFile)) {
|
|
include_once $routineFile;
|
|
$routineData['xls_headers'] = $_SESSION['headers'] ?? [];
|
|
applyRoutine($excelData, $routineData); // Modifica $excelData in place
|
|
error_log("Routine " . ($routineData['name'] ?? '') . " applicata (" . ($useEngine ? "motore" : "file: {$filename}") . ") per template {$template_id}, header row: {$headerrow}");
|
|
} else {
|
|
throw new Exception("File della routine non trovato: $routineFile");
|
|
}
|
|
|
|
// Aggiorna la sessione con i dati modificati
|
|
$_SESSION['excel_data'] = $excelData;
|
|
|
|
$response['excel_data'] = $excelData;
|
|
$response['rows'] = array_column($excelData, 'data');
|
|
$response['columns'] = $_SESSION['headers'];
|
|
$response['template_id'] = $template_id;
|
|
$response['filename'] = $input['filename'] ?? '';
|
|
} else {
|
|
$response['error'] = "Richiesta non valida.";
|
|
}
|
|
} catch (Exception $e) {
|
|
$response['error'] = "Errore durante l'applicazione della routine: " . $e->getMessage();
|
|
error_log("Exception in apply_routine.php: " . $e->getMessage());
|
|
}
|
|
|
|
ob_end_clean();
|
|
header('Content-Type: application/json');
|
|
echo json_encode($response);
|
|
exit;
|