Skip to content

Commit 8bec77a

Browse files
committed
fix: gestione file json moduli premium
1 parent 11e3ff8 commit 8bec77a

4 files changed

Lines changed: 398 additions & 77 deletions

File tree

modules/aggiornamenti/database.php

Lines changed: 230 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,62 @@ function settings_diff($expected, $current)
9191
break;
9292
}
9393

94+
// Carica il file modules.json per ottenere i nomi corretti dei moduli
95+
$modules_json_file = base_dir().'/modules.json';
96+
$modules_json_data = [];
97+
if (file_exists($modules_json_file)) {
98+
$modules_json_contents = file_get_contents($modules_json_file);
99+
$modules_json_data = json_decode($modules_json_contents, true);
100+
}
101+
102+
// Funzione per ottenere il nome del modulo dal file di riferimento appropriato
103+
function getModuleNameFromReference($reference_file, $folder_name, $modules_json_data) {
104+
$module_name = $folder_name; // Default: usa il nome della cartella
105+
106+
// Verifica se esiste il file di riferimento
107+
if (file_exists($reference_file)) {
108+
$reference_contents = file_get_contents($reference_file);
109+
$reference_data = json_decode($reference_contents, true);
110+
111+
if (!empty($reference_data) && is_array($reference_data)) {
112+
foreach ($reference_data as $name => $module_info) {
113+
// Cerca una corrispondenza parziale o esatta
114+
if (stripos(strtolower($folder_name), strtolower($name)) !== false) {
115+
$module_name = $name;
116+
break;
117+
}
118+
// Seconda prova: cerca se il nome del modulo (senza spazi) è contenuto nel nome della cartella
119+
if (stripos(strtolower($folder_name), strtolower(str_replace(' ', '', $name))) !== false) {
120+
$module_name = $name;
121+
break;
122+
}
123+
}
124+
}
125+
}
126+
127+
return $module_name;
128+
}
129+
130+
// Traccia da quale modulo proviene ogni campo (per identificare i campi premium)
131+
$premium_fields = [];
132+
94133
// Carica il file di riferimento principale per il database
95134
$data = [];
96135
if (file_exists(base_dir().'/'.$file_to_check_database)) {
97136
$contents = file_get_contents(base_dir().'/'.$file_to_check_database);
98-
$data = json_decode($contents, true);
137+
$root_data = json_decode($contents, true);
138+
139+
if (!empty($root_data) && is_array($root_data)) {
140+
$data = array_merge($root_data, $data);
141+
}
142+
} else {
143+
echo '
144+
<div class="alert alert-danger alert-database">
145+
<i class="fa fa-times"></i> '.tr('File di riferimento del database non trovato: _FILE_', [
146+
'_FILE_' => '<b>'.$file_to_check_database.'</b>',
147+
]).'
148+
</div>';
149+
return;
99150
}
100151

101152
// Carica e accoda le definizioni del database dai file mysql.json presenti nelle sottocartelle di modules/
@@ -108,8 +159,27 @@ function settings_diff($expected, $current)
108159
$database_data = json_decode($database_contents, true);
109160

110161
if (!empty($database_data) && is_array($database_data)) {
111-
// Accoda le definizioni del database del modulo a quelle principali
162+
// Estrai il nome della cartella dal percorso del file
163+
$path_parts = explode('/', $database_json_file);
164+
$folder_name = $path_parts[count($path_parts) - 2];
165+
166+
// Ottieni il nome del modulo dal file modules.json
167+
$module_name = getModuleNameFromReference($modules_json_file, $folder_name, $modules_json_data);
168+
169+
// Accoda le definizioni del modulo a quelle principali
112170
$data = array_merge($data, $database_data);
171+
172+
// Traccia i campi provenienti da questo modulo premium
173+
foreach ($database_data as $table => $table_data) {
174+
if (is_array($table_data)) {
175+
foreach ($table_data as $field_name => $field_data) {
176+
if (!isset($premium_fields[$table])) {
177+
$premium_fields[$table] = [];
178+
}
179+
$premium_fields[$table][$field_name] = $module_name;
180+
}
181+
}
182+
}
113183
}
114184
}
115185
}
@@ -125,9 +195,19 @@ function settings_diff($expected, $current)
125195
return;
126196
}
127197

128-
$info = Update::getDatabaseStructure();
129-
$results = integrity_diff($data, $info);
130-
$results_added = integrity_diff($info, $data);
198+
try {
199+
$info = Update::getDatabaseStructure();
200+
$results = integrity_diff($data, $info);
201+
$results_added = integrity_diff($info, $data);
202+
} catch (Exception $e) {
203+
echo '
204+
<div class="alert alert-danger alert-database">
205+
<i class="fa fa-times"></i> '.tr('Errore durante il recupero della struttura del database: _ERROR_', [
206+
'_ERROR_' => htmlspecialchars($e->getMessage()),
207+
]).'
208+
</div>';
209+
return;
210+
}
131211

132212
$contents = file_get_contents(base_dir().'/settings.json');
133213
$data_settings = json_decode($contents, true);
@@ -392,9 +472,25 @@ function settings_diff($expected, $current)
392472
}
393473

394474
$foreign_keys = $errors['foreign_keys'] ?: [];
395-
$error_count = ($has_keys ? count(array_filter($errors, fn ($e) => isset($e['key']))) : 0) + count($foreign_keys);
475+
476+
// Calcola il conteggio corretto includendo tutti i tipi di errori
477+
$keys_count = ($has_keys ? count(array_filter($errors, fn ($e) => isset($e['key']))) : 0);
478+
$foreign_keys_count = count($foreign_keys);
479+
480+
// Conta i campi non previsti (escludendo le chiavi, le chiavi esterne e i campi premium)
481+
$fields_count = 0;
482+
foreach ($errors as $name => $diff) {
483+
if ($name != 'foreign_keys' && !isset($diff['key']) && !isset($results[$table][$name])) {
484+
// Non contare i campi premium (verranno mostrati in una sezione separata)
485+
if (!isset($premium_fields[$table][$name])) {
486+
$fields_count++;
487+
}
488+
}
489+
}
490+
491+
$error_count = $keys_count + $foreign_keys_count + $fields_count;
396492

397-
if ($table_not_expected || $has_keys || !empty($foreign_keys)) {
493+
if ($table_not_expected || $has_keys || !empty($foreign_keys) || $fields_count > 0) {
398494
echo '
399495
<div class="mb-3">
400496
<div class="d-flex align-items-center justify-content-between p-2 module-aggiornamenti db-section-header-dynamic" onclick="$(this).next().slideToggle();">
@@ -451,9 +547,16 @@ function settings_diff($expected, $current)
451547
</tr>';
452548
} else {
453549
// Campi non previsti
454-
$badge_text = 'Campo non previsto';
455-
$badge_color = 'info';
456-
echo '
550+
// Verifica se il campo proviene da un modulo premium
551+
// Se proviene da un modulo premium, non mostrarlo qui (verrà mostrato nella sezione dedicata)
552+
if (isset($premium_fields[$table][$name])) {
553+
// Salta i campi premium - verranno mostrati nella sezione "Campi modulo premium"
554+
continue;
555+
} else {
556+
$badge_text = 'Campo non previsto';
557+
$badge_color = 'info';
558+
$query_text = 'Campo non previsto';
559+
echo '
457560
<tr>
458561
<td class="column-name">
459562
'.$name.'
@@ -462,9 +565,10 @@ function settings_diff($expected, $current)
462565
<span class="badge badge-'.$badge_color.'">'.$badge_text.'</span>
463566
</td>
464567
<td class="column-conflict">
465-
Campo non previsto
568+
'.$query_text.'
466569
</td>
467570
</tr>';
571+
}
468572
}
469573
}
470574
}
@@ -524,17 +628,61 @@ function settings_diff($expected, $current)
524628
}
525629

526630
$campi_non_previsti = [];
631+
$campi_modulo_premium = [];
527632

528633
if ($results_added) {
529634
foreach ($results_added as $table => $errors) {
530-
if (!empty($errors) && (($results[$table] && array_keys($results[$table]) != array_keys($errors)) || (empty($results[$table]) && !empty($errors)))) {
531-
foreach ($errors as $name => $diff) {
532-
if (!isset($results[$table][$name]) && !isset($diff['key']) && $name != 'foreign_keys') {
533-
$campi_non_previsti[] = [
534-
'tabella' => $table,
535-
'campo' => $name,
536-
'valore' => $diff['expected'] ?? '',
537-
];
635+
// Verifica che ci siano errori per questa tabella
636+
if (!empty($errors)) {
637+
// Se la tabella non è prevista nel file mysql.json principale, salta
638+
if (array_key_exists('current', $errors) && $errors['current'] == null) {
639+
continue;
640+
}
641+
642+
// Verifica che ci siano campi non previsti (non presenti in results)
643+
// Se la tabella non è in results, significa che tutti i campi sono in più
644+
if (!isset($results[$table])) {
645+
foreach ($errors as $name => $diff) {
646+
if (!isset($diff['key']) && $name != 'foreign_keys') {
647+
// Verifica se il campo proviene da un modulo premium
648+
if (isset($premium_fields[$table][$name])) {
649+
$module_name = $premium_fields[$table][$name];
650+
$campi_modulo_premium[] = [
651+
'tabella' => $table,
652+
'campo' => $name,
653+
'modulo' => $module_name,
654+
'valore' => $diff['expected'] ?? '',
655+
];
656+
} else {
657+
$campi_non_previsti[] = [
658+
'tabella' => $table,
659+
'campo' => $name,
660+
'valore' => $diff['expected'] ?? '',
661+
];
662+
}
663+
}
664+
}
665+
} else {
666+
// Se la tabella è in results, verifica solo i campi non presenti in results
667+
foreach ($errors as $name => $diff) {
668+
if (!isset($results[$table][$name]) && !isset($diff['key']) && $name != 'foreign_keys') {
669+
// Verifica se il campo proviene da un modulo premium
670+
if (isset($premium_fields[$table][$name])) {
671+
$module_name = $premium_fields[$table][$name];
672+
$campi_modulo_premium[] = [
673+
'tabella' => $table,
674+
'campo' => $name,
675+
'modulo' => $module_name,
676+
'valore' => $diff['expected'] ?? '',
677+
];
678+
} else {
679+
$campi_non_previsti[] = [
680+
'tabella' => $table,
681+
'campo' => $name,
682+
'valore' => $diff['expected'] ?? '',
683+
];
684+
}
685+
}
538686
}
539687
}
540688
}
@@ -639,12 +787,59 @@ function settings_diff($expected, $current)
639787
</div>';
640788
}
641789

790+
// Visualizza i campi dei moduli premium raggruppati per tabella
791+
if (!empty($campi_modulo_premium)) {
792+
// Raggruppa per tabella
793+
$campi_premium_per_tabella = [];
794+
foreach ($campi_modulo_premium as $campo) {
795+
if (!isset($campi_premium_per_tabella[$campo['tabella']])) {
796+
$campi_premium_per_tabella[$campo['tabella']] = [];
797+
}
798+
$campi_premium_per_tabella[$campo['tabella']][] = $campo;
799+
}
800+
801+
foreach ($campi_premium_per_tabella as $tabella => $campi) {
802+
echo '
803+
<div class="mb-3">
804+
<div class="d-flex align-items-center justify-content-between p-2 module-aggiornamenti db-section-header-dynamic" style="border-left-color: #007bff;" onclick="$(this).next().slideToggle();">
805+
<div>
806+
<strong>'.$tabella.' ('.tr('Campi modulo premium').')</strong>
807+
<span class="badge badge-primary ml-2">'.count($campi).'</span>
808+
</div>
809+
<i class="fa fa-chevron-down"></i>
810+
</div>
811+
<div class="module-aggiornamenti db-section-content">
812+
<div class="table-responsive">
813+
<table class="table table-hover table-striped table-sm mb-2">
814+
<thead class="thead-light">
815+
<tr>
816+
<th>'.tr('Campo').'</th>
817+
<th>'.tr('Modulo').'</th>
818+
</tr>
819+
</thead>
820+
<tbody>';
821+
foreach ($campi as $campo) {
822+
echo '
823+
<tr>
824+
<td class="column-name">'.$campo['campo'].'</td>
825+
<td><span class="badge badge-primary">Campo modulo '.$campo['modulo'].'</span></td>
826+
</tr>';
827+
}
828+
echo '
829+
</tbody>
830+
</table>
831+
</div>
832+
</div>
833+
</div>';
834+
}
835+
}
836+
642837
// Visualizza i campi non previsti raggruppati per tabella
643838
if (!empty($campi_non_previsti)) {
644839
// Raggruppa per tabella
645840
$campi_per_tabella = [];
646841
foreach ($campi_non_previsti as $campo) {
647-
$campi_per_tabella[$campo['tabella']][] = $campo['campo'];
842+
$campi_per_tabella[$campo['tabella']][] = $campo;
648843
}
649844

650845
foreach ($campi_per_tabella as $tabella => $campi) {
@@ -663,13 +858,26 @@ function settings_diff($expected, $current)
663858
<thead class="thead-light">
664859
<tr>
665860
<th>'.tr('Campo').'</th>
861+
<th>'.tr('Tipo').'</th>
666862
</tr>
667863
</thead>
668864
<tbody>';
669865
foreach ($campi as $campo) {
866+
// Verifica se il campo proviene da un modulo premium
867+
if (isset($premium_fields[$tabella][$campo['campo']])) {
868+
$module_name = $premium_fields[$tabella][$campo['campo']];
869+
$badge_text = 'Campo modulo '.$module_name;
870+
$badge_color = 'primary';
871+
} else {
872+
$badge_text = 'Campo non previsto';
873+
$badge_color = 'info';
874+
}
670875
echo '
671876
<tr>
672-
<td class="column-name">'.$campo.'</td>
877+
<td class="column-name">'.$campo['campo'].'</td>
878+
<td class="text-center">
879+
<span class="badge badge-'.$badge_color.'">'.$badge_text.'</span>
880+
</td>
673881
</tr>';
674882
}
675883
echo '
@@ -683,7 +891,7 @@ function settings_diff($expected, $current)
683891
} else {
684892
echo '
685893
<div class="alert alert-info alert-database">
686-
<i class="fa fa-info-circle"></i> '.tr('Il database non presenta problemi di integrità').'.
894+
<i class="fa fa-info-circle"></i> '.tr('Il database non presenta problemi di integrità').'
687895
</div>';
688896
}
689897

0 commit comments

Comments
 (0)