Skip to content

Commit 728f45f

Browse files
committed
refactor: ottimizzazione lettura file json moduli premium
1 parent fe36df2 commit 728f45f

5 files changed

Lines changed: 463 additions & 407 deletions

File tree

modules/aggiornamenti/database.php

Lines changed: 17 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -88,45 +88,6 @@ function settings_diff($expected, $current)
8888
break;
8989
}
9090

91-
// Carica il file modules.json per ottenere i nomi corretti dei moduli
92-
$modules_json_file = base_dir().'/modules.json';
93-
$modules_json_data = [];
94-
if (file_exists($modules_json_file)) {
95-
$modules_json_contents = file_get_contents($modules_json_file);
96-
$modules_json_data = json_decode($modules_json_contents, true);
97-
}
98-
99-
// Funzione per ottenere il nome del modulo dal file di riferimento appropriato
100-
if (!function_exists('getModuleNameFromReference')) {
101-
function getModuleNameFromReference($reference_file, $folder_name, $modules_json_data)
102-
{
103-
$module_name = $folder_name; // Default: usa il nome della cartella
104-
105-
// Verifica se esiste il file di riferimento
106-
if (file_exists($reference_file)) {
107-
$reference_contents = file_get_contents($reference_file);
108-
$reference_data = json_decode($reference_contents, true);
109-
110-
if (!empty($reference_data) && is_array($reference_data)) {
111-
foreach ($reference_data as $name => $module_info) {
112-
// Cerca una corrispondenza parziale o esatta
113-
if (stripos(strtolower((string) $folder_name), strtolower((string) $name)) !== false) {
114-
$module_name = $name;
115-
break;
116-
}
117-
// Seconda prova: cerca se il nome del modulo (senza spazi) è contenuto nel nome della cartella
118-
if (stripos(strtolower((string) $folder_name), strtolower(str_replace(' ', '', $name))) !== false) {
119-
$module_name = $name;
120-
break;
121-
}
122-
}
123-
}
124-
}
125-
126-
return $module_name;
127-
}
128-
}
129-
13091
// Traccia da quale modulo proviene ogni campo (per identificare i campi premium)
13192
$premium_fields = [];
13293

@@ -157,78 +118,10 @@ function getModuleNameFromReference($reference_file, $folder_name, $modules_json
157118
return;
158119
}
159120

160-
// Carica e accoda le definizioni del database dai file mysql.json presenti nelle sottocartelle di modules/
161-
$modules_dir = base_dir().'/modules/';
162-
$database_json_files = glob($modules_dir.'*/'.$file_to_check_database);
163-
164-
// Se non sono stati trovati file con il nome specifico per la versione del database, cerca anche mysql.json di default
165-
if (empty($database_json_files) && $file_to_check_database !== 'mysql.json') {
166-
$database_json_files = glob($modules_dir.'*/mysql.json');
167-
}
168-
169-
if (!empty($database_json_files)) {
170-
foreach ($database_json_files as $database_json_file) {
171-
$database_contents = file_get_contents($database_json_file);
172-
$database_data = json_decode($database_contents, true);
173-
174-
if (!empty($database_data) && is_array($database_data)) {
175-
// Estrai il nome della cartella dal percorso del file
176-
$path_parts = explode('/', $database_json_file);
177-
$folder_name = $path_parts[count($path_parts) - 2];
178-
179-
// Ottieni il nome del modulo dal file modules.json
180-
$module_name = getModuleNameFromReference($modules_json_file, $folder_name, $modules_json_data);
181-
182-
// Accoda le definizioni del modulo a quelle principali
183-
// Unisci i campi delle tabelle invece di sovrascrivere le tabelle intere
184-
foreach ($database_data as $table => $table_data) {
185-
if (!isset($data[$table])) {
186-
// Se la tabella non esiste, aggiungila
187-
$data[$table] = $table_data;
188-
} else {
189-
// Se la tabella esiste, unisci i campi
190-
foreach ($table_data as $field_name => $field_data) {
191-
if ($field_name === 'foreign_keys' && is_array($field_data)) {
192-
// Unisci le chiavi esterne
193-
if (!isset($data[$table]['foreign_keys'])) {
194-
$data[$table]['foreign_keys'] = [];
195-
}
196-
// Unisci le chiavi esterne senza sovrascrivere quelle esistenti
197-
foreach ($field_data as $fk_name => $fk_data) {
198-
if (!isset($data[$table]['foreign_keys'][$fk_name])) {
199-
$data[$table]['foreign_keys'][$fk_name] = $fk_data;
200-
}
201-
}
202-
} elseif (is_array($field_data)) {
203-
// Unisci i campi della tabella
204-
if (!isset($data[$table][$field_name])) {
205-
$data[$table][$field_name] = $field_data;
206-
} else {
207-
// Se il campo esiste, unisci i dati (array_merge ricorsivo)
208-
$data[$table][$field_name] = array_merge($data[$table][$field_name], $field_data);
209-
}
210-
} else {
211-
// Se non è un array, sovrascrivi
212-
$data[$table][$field_name] = $field_data;
213-
}
214-
}
215-
}
216-
}
217-
218-
// Traccia i campi provenienti da questo modulo premium
219-
foreach ($database_data as $table => $table_data) {
220-
if (is_array($table_data)) {
221-
foreach ($table_data as $field_name => $field_data) {
222-
if (!isset($premium_fields[$table])) {
223-
$premium_fields[$table] = [];
224-
}
225-
$premium_fields[$table][$field_name] = $module_name;
226-
}
227-
}
228-
}
229-
}
230-
}
231-
}
121+
// Carica e accoda le definizioni del database dai file JSON presenti nelle sottocartelle di moduli e plugin
122+
$database_reference_data = aggiornamentiMergeDatabaseReferenceData($data, $file_to_check_database);
123+
$data = $database_reference_data['data'];
124+
$premium_fields = $database_reference_data['premium_fields'];
232125

233126
if (empty($data)) {
234127
echo '
@@ -431,7 +324,10 @@ function generateDropForeignKeyQuery($table, $name)
431324
function renderTableRow($name, $badge_text, $badge_color, $query, $is_premium = false, $module_name = '')
432325
{
433326
if ($is_premium) {
434-
$badge_html = '<span class="badge badge-primary">Campo modulo '.$module_name.'</span>';
327+
$premium_type = is_array($module_name) ? ($module_name['type'] ?? 'module') : 'module';
328+
$premium_name = is_array($module_name) ? ($module_name['name'] ?? '') : $module_name;
329+
$premium_label = ($premium_type === 'plugin') ? 'Campo plugin ' : 'Campo modulo ';
330+
$badge_html = '<span class="badge badge-primary">'.$premium_label.$premium_name.'</span>';
435331
} else {
436332
$badge_html = '<span class="badge badge-'.$badge_color.'">'.$badge_text.'</span>';
437333
}
@@ -708,11 +604,11 @@ function renderUnifiedTable($errors, $table, $data, &$query_conflitti)
708604
foreach ($errors as $name => $diff) {
709605
if (!isset($diff['key']) && $name != 'foreign_keys') {
710606
if (isset($premium_fields[$table][$name])) {
711-
$module_name = $premium_fields[$table][$name];
607+
$premium_info = $premium_fields[$table][$name];
712608
$campi_modulo_premium[] = [
713609
'tabella' => $table,
714610
'campo' => $name,
715-
'modulo' => $module_name,
611+
'origine' => $premium_info,
716612
'valore' => $diff['expected'] ?? '',
717613
];
718614
}
@@ -737,7 +633,7 @@ function renderUnifiedTable($errors, $table, $data, &$query_conflitti)
737633
<div class="mb-3">
738634
<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();">
739635
<div>
740-
<strong>'.$tabella.' ('.tr('Campi modulo premium').')</strong>
636+
<strong>'.$tabella.' ('.tr('Campi premium').')</strong>
741637
<span class="badge badge-primary ml-2">'.count($campi).'</span>
742638
</div>
743639
<i class="fa fa-chevron-down"></i>
@@ -748,15 +644,19 @@ function renderUnifiedTable($errors, $table, $data, &$query_conflitti)
748644
<thead class="thead-light">
749645
<tr>
750646
<th>'.tr('Campo').'</th>
751-
<th>'.tr('Modulo').'</th>
647+
<th>'.tr('Componente').'</th>
752648
</tr>
753649
</thead>
754650
<tbody>';
755651
foreach ($campi as $campo) {
652+
$origine = $campo['origine'];
653+
$origine_type = is_array($origine) ? ($origine['type'] ?? 'module') : 'module';
654+
$origine_name = is_array($origine) ? ($origine['name'] ?? '') : $origine;
655+
$origine_label = ($origine_type === 'plugin') ? 'Campo plugin ' : 'Campo modulo ';
756656
echo '
757657
<tr>
758658
<td class="column-name">'.$campo['campo'].'</td>
759-
<td><span class="badge badge-primary">Campo modulo '.$campo['modulo'].'</span></td>
659+
<td><span class="badge badge-primary">'.$origine_label.$origine_name.'</span></td>
760660
</tr>';
761661
}
762662
echo '

0 commit comments

Comments
 (0)