|
25 | 25 | use Models\View; |
26 | 26 |
|
27 | 27 | switch (filter('op')) { |
| 28 | + case 'export_module': |
| 29 | + // Esportazione del modulo in formato JSON |
| 30 | + $module = Module::find($id_record); |
| 31 | + |
| 32 | + if (!$module) { |
| 33 | + echo json_encode([ |
| 34 | + 'success' => false, |
| 35 | + 'message' => tr('Modulo non trovato'), |
| 36 | + ]); |
| 37 | + break; |
| 38 | + } |
| 39 | + |
| 40 | + // Recupera i dati del modulo |
| 41 | + $module_data = [ |
| 42 | + 'name' => $module->name, |
| 43 | + 'directory' => $module->directory, |
| 44 | + 'options' => $module->options, |
| 45 | + 'options2' => $module->options2, |
| 46 | + 'icon' => $module->icon, |
| 47 | + 'version' => $module->version, |
| 48 | + 'compatibility' => $module->compatibility, |
| 49 | + 'order' => $module->order, |
| 50 | + 'parent' => null, |
| 51 | + 'default' => $module->default, |
| 52 | + 'enabled' => $module->enabled, |
| 53 | + 'use_notes' => $module->use_notes, |
| 54 | + 'use_checklists' => $module->use_checklists, |
| 55 | + ]; |
| 56 | + |
| 57 | + // Se c'è un modulo parent, usa il nome come riferimento |
| 58 | + if ($module->parent) { |
| 59 | + $parent = Module::find($module->parent); |
| 60 | + if ($parent) { |
| 61 | + $module_data['parent_name'] = $parent->name; |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + // Recupera le traduzioni del modulo |
| 66 | + $module_langs = $dbo->fetchArray('SELECT `id_lang`, `title` FROM `zz_modules_lang` WHERE `id_record` = '.prepare($id_record)); |
| 67 | + $module_data['translations'] = []; |
| 68 | + foreach ($module_langs as $lang) { |
| 69 | + $module_data['translations'][$lang['id_lang']] = [ |
| 70 | + 'title' => $lang['title'], |
| 71 | + ]; |
| 72 | + } |
| 73 | + |
| 74 | + // Recupera le viste del modulo |
| 75 | + $views = View::where('id_module', $id_record)->get(); |
| 76 | + $module_data['views'] = []; |
| 77 | + |
| 78 | + foreach ($views as $view) { |
| 79 | + $view_data = [ |
| 80 | + 'name' => $view->name, |
| 81 | + 'query' => $view->query, |
| 82 | + 'order' => $view->order, |
| 83 | + 'search' => $view->search, |
| 84 | + 'slow' => $view->slow, |
| 85 | + 'format' => $view->format, |
| 86 | + 'html_format' => $view->html_format, |
| 87 | + 'search_inside' => $view->search_inside, |
| 88 | + 'order_by' => $view->order_by, |
| 89 | + 'visible' => $view->visible, |
| 90 | + 'summable' => $view->summable, |
| 91 | + 'avg' => $view->avg, |
| 92 | + ]; |
| 93 | + |
| 94 | + // Recupera le traduzioni della vista |
| 95 | + $view_langs = $dbo->fetchArray('SELECT `id_lang`, `title` FROM `zz_views_lang` WHERE `id_record` = '.prepare($view->id)); |
| 96 | + $view_data['translations'] = []; |
| 97 | + foreach ($view_langs as $lang) { |
| 98 | + $view_data['translations'][$lang['id_lang']] = [ |
| 99 | + 'title' => $lang['title'], |
| 100 | + ]; |
| 101 | + } |
| 102 | + |
| 103 | + // Recupera i gruppi associati alla vista |
| 104 | + $view_groups = $dbo->fetchArray('SELECT `id_gruppo` FROM `zz_group_view` WHERE `id_vista` = '.prepare($view->id)); |
| 105 | + $view_data['groups'] = []; |
| 106 | + foreach ($view_groups as $group) { |
| 107 | + $group_info = $dbo->fetchArray('SELECT `nome` FROM `zz_groups` WHERE `id` = '.prepare($group['id_gruppo'])); |
| 108 | + if (!empty($group_info)) { |
| 109 | + $view_data['groups'][] = $group_info[0]['nome']; |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + $module_data['views'][] = $view_data; |
| 114 | + } |
| 115 | + |
| 116 | + // Recupera i filtri del modulo |
| 117 | + $clauses = Clause::where('idmodule', $id_record)->get(); |
| 118 | + $module_data['clauses'] = []; |
| 119 | + |
| 120 | + foreach ($clauses as $clause) { |
| 121 | + $clause_data = [ |
| 122 | + 'name' => $clause->name, |
| 123 | + 'clause' => $clause->clause, |
| 124 | + 'position' => $clause->position, |
| 125 | + 'enabled' => $clause->enabled, |
| 126 | + 'default' => $clause->default, |
| 127 | + ]; |
| 128 | + |
| 129 | + // Recupera il gruppo associato al filtro |
| 130 | + $group_info = $dbo->fetchArray('SELECT `nome` FROM `zz_groups` WHERE `id` = '.prepare($clause->idgruppo)); |
| 131 | + if (!empty($group_info)) { |
| 132 | + $clause_data['group'] = $group_info[0]['nome']; |
| 133 | + } |
| 134 | + |
| 135 | + // Recupera le traduzioni del filtro |
| 136 | + $clause_langs = $dbo->fetchArray('SELECT `id_lang`, `title` FROM `zz_group_module_lang` WHERE `id_record` = '.prepare($clause->id)); |
| 137 | + $clause_data['translations'] = []; |
| 138 | + foreach ($clause_langs as $lang) { |
| 139 | + $clause_data['translations'][$lang['id_lang']] = [ |
| 140 | + 'title' => $lang['title'], |
| 141 | + ]; |
| 142 | + } |
| 143 | + |
| 144 | + $module_data['clauses'][] = $clause_data; |
| 145 | + } |
| 146 | + |
| 147 | + // Prepara il nome del file |
| 148 | + $filename = 'module_' . strtolower(str_replace(' ', '_', $module->name)) . '.json'; |
| 149 | + |
| 150 | + // Restituisci i dati in formato JSON |
| 151 | + echo json_encode([ |
| 152 | + 'success' => true, |
| 153 | + 'data' => $module_data, |
| 154 | + 'filename' => $filename, |
| 155 | + ]); |
| 156 | + break; |
| 157 | + |
| 158 | + case 'import_module': |
| 159 | + // Importazione del modulo da un file JSON |
| 160 | + $response = [ |
| 161 | + 'success' => false, |
| 162 | + 'message' => '', |
| 163 | + ]; |
| 164 | + |
| 165 | + // Verifica che sia stato caricato un file |
| 166 | + if (!isset($_FILES['file']) || $_FILES['file']['error'] !== UPLOAD_ERR_OK) { |
| 167 | + $response['message'] = tr('Errore durante il caricamento del file'); |
| 168 | + echo json_encode($response); |
| 169 | + break; |
| 170 | + } |
| 171 | + |
| 172 | + // Leggi il contenuto del file |
| 173 | + $file_content = file_get_contents($_FILES['file']['tmp_name']); |
| 174 | + $module_data = json_decode($file_content, true); |
| 175 | + |
| 176 | + // Verifica che il JSON sia valido |
| 177 | + if (json_last_error() !== JSON_ERROR_NONE) { |
| 178 | + $response['message'] = tr('Il file non contiene un JSON valido'); |
| 179 | + echo json_encode($response); |
| 180 | + break; |
| 181 | + } |
| 182 | + |
| 183 | + // Verifica che il JSON contenga i dati necessari |
| 184 | + if (!isset($module_data['name'])) { |
| 185 | + $response['message'] = tr('Il file non contiene i dati necessari per importare il modulo'); |
| 186 | + echo json_encode($response); |
| 187 | + break; |
| 188 | + } |
| 189 | + |
| 190 | + // Verifica se il modulo esiste già |
| 191 | + $existing_module = Module::where('name', $module_data['name'])->first(); |
| 192 | + |
| 193 | + // Inizia una transazione per garantire l'integrità dei dati |
| 194 | + $dbo->beginTransaction(); |
| 195 | + |
| 196 | + try { |
| 197 | + // Se il modulo esiste, aggiornalo, altrimenti creane uno nuovo |
| 198 | + if ($existing_module) { |
| 199 | + $module_id = $existing_module->id; |
| 200 | + |
| 201 | + // Aggiorna i dati del modulo |
| 202 | + $dbo->update('zz_modules', [ |
| 203 | + 'directory' => $module_data['directory'], |
| 204 | + 'options' => $module_data['options'], |
| 205 | + 'options2' => $module_data['options2'], |
| 206 | + 'icon' => $module_data['icon'], |
| 207 | + 'version' => $module_data['version'], |
| 208 | + 'compatibility' => $module_data['compatibility'], |
| 209 | + 'order' => $module_data['order'], |
| 210 | + 'default' => $module_data['default'], |
| 211 | + 'enabled' => $module_data['enabled'], |
| 212 | + 'use_notes' => $module_data['use_notes'], |
| 213 | + 'use_checklists' => $module_data['use_checklists'], |
| 214 | + ], ['id' => $module_id]); |
| 215 | + } else { |
| 216 | + // Crea un nuovo modulo |
| 217 | + $dbo->insert('zz_modules', [ |
| 218 | + 'name' => $module_data['name'], |
| 219 | + 'directory' => $module_data['directory'], |
| 220 | + 'options' => $module_data['options'], |
| 221 | + 'options2' => $module_data['options2'], |
| 222 | + 'icon' => $module_data['icon'], |
| 223 | + 'version' => $module_data['version'], |
| 224 | + 'compatibility' => $module_data['compatibility'], |
| 225 | + 'order' => $module_data['order'], |
| 226 | + 'default' => $module_data['default'], |
| 227 | + 'enabled' => $module_data['enabled'], |
| 228 | + 'use_notes' => $module_data['use_notes'], |
| 229 | + 'use_checklists' => $module_data['use_checklists'], |
| 230 | + ]); |
| 231 | + |
| 232 | + $module_id = $dbo->lastInsertedID(); |
| 233 | + } |
| 234 | + |
| 235 | + // Aggiorna il parent se specificato |
| 236 | + if (isset($module_data['parent_name'])) { |
| 237 | + $parent = Module::where('name', $module_data['parent_name'])->first(); |
| 238 | + if ($parent) { |
| 239 | + $dbo->update('zz_modules', ['parent' => $parent->id], ['id' => $module_id]); |
| 240 | + } |
| 241 | + } |
| 242 | + |
| 243 | + // Aggiorna le traduzioni del modulo |
| 244 | + if (isset($module_data['translations'])) { |
| 245 | + foreach ($module_data['translations'] as $id_lang => $translation) { |
| 246 | + // Verifica se la traduzione esiste già |
| 247 | + $existing_translation = $dbo->fetchArray('SELECT `id` FROM `zz_modules_lang` WHERE `id_record` = '.prepare($module_id).' AND `id_lang` = '.prepare($id_lang)); |
| 248 | + |
| 249 | + if (!empty($existing_translation)) { |
| 250 | + // Aggiorna la traduzione esistente |
| 251 | + $dbo->update('zz_modules_lang', [ |
| 252 | + 'title' => $translation['title'], |
| 253 | + ], ['id' => $existing_translation[0]['id']]); |
| 254 | + } else { |
| 255 | + // Crea una nuova traduzione |
| 256 | + $dbo->insert('zz_modules_lang', [ |
| 257 | + 'id_record' => $module_id, |
| 258 | + 'id_lang' => $id_lang, |
| 259 | + 'title' => $translation['title'], |
| 260 | + ]); |
| 261 | + } |
| 262 | + } |
| 263 | + } |
| 264 | + |
| 265 | + // Gestisci le viste |
| 266 | + if (isset($module_data['views'])) { |
| 267 | + // Elimina tutte le viste esistenti per il modulo |
| 268 | + $existing_views = $dbo->fetchArray('SELECT `id` FROM `zz_views` WHERE `id_module` = '.prepare($module_id)); |
| 269 | + |
| 270 | + // Elimina prima le associazioni con i gruppi |
| 271 | + foreach ($existing_views as $view) { |
| 272 | + $dbo->query('DELETE FROM `zz_group_view` WHERE `id_vista` = '.prepare($view['id'])); |
| 273 | + $dbo->query('DELETE FROM `zz_views_lang` WHERE `id_record` = '.prepare($view['id'])); |
| 274 | + } |
| 275 | + |
| 276 | + // Elimina tutte le viste |
| 277 | + $dbo->query('DELETE FROM `zz_views` WHERE `id_module` = '.prepare($module_id)); |
| 278 | + |
| 279 | + // Crea tutte le nuove viste dal file JSON |
| 280 | + foreach ($module_data['views'] as $index => $view_data) { |
| 281 | + $view_array = [ |
| 282 | + 'name' => $view_data['name'], |
| 283 | + 'query' => $view_data['query'], |
| 284 | + 'order' => $view_data['order'] ?? $index, // Usa l'indice come ordine se non specificato |
| 285 | + 'search' => $view_data['search'], |
| 286 | + 'slow' => $view_data['slow'], |
| 287 | + 'format' => $view_data['format'], |
| 288 | + 'html_format' => $view_data['html_format'], |
| 289 | + 'search_inside' => $view_data['search_inside'], |
| 290 | + 'order_by' => $view_data['order_by'], |
| 291 | + 'visible' => $view_data['visible'], |
| 292 | + 'summable' => $view_data['summable'], |
| 293 | + 'avg' => $view_data['avg'], |
| 294 | + 'id_module' => $module_id, |
| 295 | + ]; |
| 296 | + |
| 297 | + // Crea la nuova vista |
| 298 | + $dbo->insert('zz_views', $view_array); |
| 299 | + $view_id = $dbo->lastInsertedID(); |
| 300 | + |
| 301 | + // Crea le traduzioni della vista |
| 302 | + if (isset($view_data['translations'])) { |
| 303 | + foreach ($view_data['translations'] as $id_lang => $translation) { |
| 304 | + $dbo->insert('zz_views_lang', [ |
| 305 | + 'id_record' => $view_id, |
| 306 | + 'id_lang' => $id_lang, |
| 307 | + 'title' => $translation['title'], |
| 308 | + ]); |
| 309 | + } |
| 310 | + } |
| 311 | + |
| 312 | + // Gestisci i gruppi associati alla vista |
| 313 | + if (isset($view_data['groups'])) { |
| 314 | + foreach ($view_data['groups'] as $group_name) { |
| 315 | + $group = $dbo->fetchArray('SELECT `id` FROM `zz_groups` WHERE `nome` = '.prepare($group_name)); |
| 316 | + if (!empty($group)) { |
| 317 | + $dbo->insert('zz_group_view', [ |
| 318 | + 'id_vista' => $view_id, |
| 319 | + 'id_gruppo' => $group[0]['id'], |
| 320 | + ]); |
| 321 | + } |
| 322 | + } |
| 323 | + } |
| 324 | + } |
| 325 | + } |
| 326 | + |
| 327 | + // Gestisci i filtri |
| 328 | + if (isset($module_data['clauses'])) { |
| 329 | + // Elimina tutti i filtri esistenti per il modulo |
| 330 | + $existing_clauses = $dbo->fetchArray('SELECT `id` FROM `zz_group_module` WHERE `idmodule` = '.prepare($module_id)); |
| 331 | + |
| 332 | + // Elimina prima le traduzioni dei filtri |
| 333 | + foreach ($existing_clauses as $clause) { |
| 334 | + $dbo->query('DELETE FROM `zz_group_module_lang` WHERE `id_record` = '.prepare($clause['id'])); |
| 335 | + } |
| 336 | + |
| 337 | + // Elimina tutti i filtri |
| 338 | + $dbo->query('DELETE FROM `zz_group_module` WHERE `idmodule` = '.prepare($module_id)); |
| 339 | + |
| 340 | + // Crea tutti i nuovi filtri dal file JSON |
| 341 | + foreach ($module_data['clauses'] as $clause_data) { |
| 342 | + // Trova l'ID del gruppo |
| 343 | + $group_id = null; |
| 344 | + if (isset($clause_data['group'])) { |
| 345 | + $group = $dbo->fetchArray('SELECT `id` FROM `zz_groups` WHERE `nome` = '.prepare($clause_data['group'])); |
| 346 | + if (!empty($group)) { |
| 347 | + $group_id = $group[0]['id']; |
| 348 | + } |
| 349 | + } |
| 350 | + |
| 351 | + // Salta se non è stato trovato il gruppo |
| 352 | + if (!$group_id) { |
| 353 | + continue; |
| 354 | + } |
| 355 | + |
| 356 | + $clause_array = [ |
| 357 | + 'name' => $clause_data['name'], |
| 358 | + 'idgruppo' => $group_id, |
| 359 | + 'idmodule' => $module_id, |
| 360 | + 'clause' => $clause_data['clause'], |
| 361 | + 'position' => $clause_data['position'], |
| 362 | + 'enabled' => $clause_data['enabled'], |
| 363 | + 'default' => $clause_data['default'], |
| 364 | + ]; |
| 365 | + |
| 366 | + // Crea il nuovo filtro |
| 367 | + $dbo->insert('zz_group_module', $clause_array); |
| 368 | + $clause_id = $dbo->lastInsertedID(); |
| 369 | + |
| 370 | + // Crea le traduzioni del filtro |
| 371 | + if (isset($clause_data['translations'])) { |
| 372 | + foreach ($clause_data['translations'] as $id_lang => $translation) { |
| 373 | + $dbo->insert('zz_group_module_lang', [ |
| 374 | + 'id_record' => $clause_id, |
| 375 | + 'id_lang' => $id_lang, |
| 376 | + 'title' => $translation['title'], |
| 377 | + ]); |
| 378 | + } |
| 379 | + } |
| 380 | + } |
| 381 | + } |
| 382 | + |
| 383 | + // Commit della transazione |
| 384 | + $dbo->commitTransaction(); |
| 385 | + |
| 386 | + $response['success'] = true; |
| 387 | + $response['message'] = tr('Modulo importato con successo'); |
| 388 | + } catch (Exception $e) { |
| 389 | + // Rollback in caso di errore |
| 390 | + $dbo->rollbackTransaction(); |
| 391 | + |
| 392 | + $response['message'] = tr('Errore durante l\'importazione del modulo').': '.$e->getMessage(); |
| 393 | + } |
| 394 | + |
| 395 | + echo json_encode($response); |
| 396 | + break; |
| 397 | + |
28 | 398 | case 'update': |
29 | 399 | $options2 = htmlspecialchars_decode(post('options2'), ENT_QUOTES); |
30 | 400 |
|
|
0 commit comments