2018-02-23 19:04:05 +01:00
|
|
|
<?php
|
2020-09-07 15:04:06 +02:00
|
|
|
/*
|
|
|
|
* OpenSTAManager: il software gestionale open source per l'assistenza tecnica e la fatturazione
|
2021-01-20 15:08:51 +01:00
|
|
|
* Copyright (C) DevCode s.r.l.
|
2020-09-07 15:04:06 +02:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2018-02-23 19:04:05 +01:00
|
|
|
|
2021-02-26 11:04:06 +01:00
|
|
|
use Modules\Importazione\Import;
|
|
|
|
|
2018-02-23 19:04:05 +01:00
|
|
|
include_once __DIR__.'/../../core.php';
|
|
|
|
|
2020-08-06 18:51:56 +02:00
|
|
|
// Gestione del redirect in caso di caricamento del file
|
|
|
|
if (filter('op')) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-02-23 19:04:05 +01:00
|
|
|
if (empty($id_record)) {
|
2020-09-23 13:36:37 +02:00
|
|
|
require base_dir().'/add.php';
|
2018-02-23 19:04:05 +01:00
|
|
|
} else {
|
2021-02-26 11:04:06 +01:00
|
|
|
$import = Import::find($id_record);
|
|
|
|
$import_selezionato = $import->class;
|
2018-02-23 19:04:05 +01:00
|
|
|
|
2020-08-06 18:51:56 +02:00
|
|
|
// Inizializzazione del lettore CSV
|
2024-03-19 13:23:08 +01:00
|
|
|
$csv = new $import_selezionato($record->local_filepath);
|
2020-08-06 18:51:56 +02:00
|
|
|
$fields = $csv->getAvailableFields();
|
2018-02-23 19:04:05 +01:00
|
|
|
|
2020-08-06 18:51:56 +02:00
|
|
|
// Generazione della base per i campi da selezionare
|
|
|
|
$campi_disponibili = [];
|
2018-02-23 19:04:05 +01:00
|
|
|
foreach ($fields as $key => $value) {
|
2020-08-06 18:51:56 +02:00
|
|
|
$campi_disponibili[] = [
|
2021-03-01 09:41:49 +01:00
|
|
|
'id' => $key + 1,
|
2018-02-23 19:04:05 +01:00
|
|
|
'text' => $value['label'],
|
|
|
|
];
|
2018-03-01 01:56:57 +01:00
|
|
|
|
2018-03-15 17:39:19 +01:00
|
|
|
if ($value['primary_key']) {
|
2021-03-01 09:41:49 +01:00
|
|
|
$primary_key = $key + 1;
|
2018-03-01 01:56:57 +01:00
|
|
|
}
|
2018-02-23 19:04:05 +01:00
|
|
|
}
|
|
|
|
|
2018-03-01 01:56:57 +01:00
|
|
|
echo '
|
|
|
|
<form action="" method="post" id="edit-form">
|
|
|
|
<input type="hidden" name="backto" value="record-list">
|
|
|
|
<input type="hidden" name="op" value="import">
|
|
|
|
|
|
|
|
<div class="row">
|
|
|
|
<div class="col-md-8">
|
2019-02-21 22:21:30 +01:00
|
|
|
{[ "type": "checkbox", "label": "'.tr('Importa prima riga').'", "name": "include_first_row", "extra":"", "value": "1" ]}
|
2018-03-01 01:56:57 +01:00
|
|
|
</div>
|
2021-02-26 15:47:20 +01:00
|
|
|
|
2018-03-01 01:56:57 +01:00
|
|
|
<div class="col-md-4">
|
2020-08-06 18:51:56 +02:00
|
|
|
{[ "type": "select", "label": "'.tr('Chiave primaria').'", "name": "primary_key", "values": '.json_encode($campi_disponibili).', "value": "'.$primary_key.'" ]}
|
2018-03-01 01:56:57 +01:00
|
|
|
</div>
|
|
|
|
</div>';
|
|
|
|
|
2020-08-06 18:51:56 +02:00
|
|
|
// Lettura delle prime righe disponibili
|
2020-08-07 10:14:09 +02:00
|
|
|
$righe = $csv->getRows(0, 10);
|
|
|
|
$prima_riga = $csv->getHeader();
|
2020-08-06 18:51:56 +02:00
|
|
|
$numero_colonne = count($prima_riga);
|
2018-11-22 15:34:44 +01:00
|
|
|
|
2020-08-06 18:51:56 +02:00
|
|
|
// Trasformazione dei nomi indicati per i campi in lowercase
|
|
|
|
$nomi_disponibili = [];
|
|
|
|
foreach ($fields as $key => $value) {
|
|
|
|
$nomi_disponibili[$key] = [];
|
|
|
|
$names = isset($value['names']) ? $value['names'] : [$value['label']];
|
|
|
|
foreach ($names as $name) {
|
2020-10-29 16:48:37 +01:00
|
|
|
$nomi_disponibili[$key][] = trim(string_lowercase($name));
|
2020-08-06 18:51:56 +02:00
|
|
|
}
|
|
|
|
}
|
2018-02-23 19:04:05 +01:00
|
|
|
|
|
|
|
echo '
|
|
|
|
<div class="row">';
|
|
|
|
|
2020-08-06 18:51:56 +02:00
|
|
|
for ($column = 0; $column < $numero_colonne; ++$column) {
|
2018-02-23 19:04:05 +01:00
|
|
|
echo '
|
|
|
|
<div class="col-sm-6 col-lg-4">
|
|
|
|
<div class="panel panel-primary">
|
|
|
|
<div class="panel-heading">
|
|
|
|
<h3 class="panel-title">'.tr('Colonna _NUM_', [
|
2024-03-22 15:52:24 +01:00
|
|
|
'_NUM_' => $column + 1,
|
|
|
|
]).'</h3>
|
2018-02-23 19:04:05 +01:00
|
|
|
</div>
|
|
|
|
|
|
|
|
<div class="panel-body">';
|
|
|
|
|
|
|
|
// Individuazione delle corrispondenze
|
2020-08-06 18:51:56 +02:00
|
|
|
$selezionato = null;
|
2018-02-23 19:04:05 +01:00
|
|
|
foreach ($fields as $key => $value) {
|
2020-08-06 18:51:56 +02:00
|
|
|
// Confronto per l'individuazione della relativa colonna
|
2020-10-29 16:48:37 +01:00
|
|
|
$nome = trim(string_lowercase($prima_riga[$column]));
|
2020-08-06 18:51:56 +02:00
|
|
|
if (in_array($nome, $nomi_disponibili[$key])) {
|
|
|
|
$escludi_prima_riga = 1;
|
2021-03-01 09:41:49 +01:00
|
|
|
$selezionato = $key + 1;
|
|
|
|
break;
|
2018-02-23 19:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
echo '
|
2020-08-06 18:51:56 +02:00
|
|
|
{[ "type": "select", "label": "'.tr('Campo').'", "name": "fields['.$column.']", "values": '.json_encode($campi_disponibili).', "value": "'.$selezionato.'" ]}
|
2018-02-23 19:04:05 +01:00
|
|
|
|
|
|
|
<table class="table table-striped">
|
|
|
|
<thead>
|
|
|
|
<tr>
|
|
|
|
<th>'.tr('#').'</th>
|
|
|
|
<th>'.tr('Valore').'</th>
|
|
|
|
</tr>
|
|
|
|
</thead>
|
|
|
|
<tbody>';
|
|
|
|
|
2020-08-06 18:51:56 +02:00
|
|
|
foreach ($righe as $key => $row) {
|
2018-02-23 19:04:05 +01:00
|
|
|
echo '
|
|
|
|
<tr>
|
|
|
|
<td>'.($key + 1).'</td>
|
|
|
|
<td>'.$row[$column].'</td>
|
|
|
|
</tr>';
|
|
|
|
}
|
|
|
|
|
|
|
|
echo '
|
|
|
|
|
|
|
|
</tbody>
|
|
|
|
</table>
|
|
|
|
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>';
|
|
|
|
}
|
|
|
|
|
|
|
|
echo '
|
|
|
|
</div>
|
|
|
|
</form>';
|
2018-03-15 17:39:19 +01:00
|
|
|
|
|
|
|
echo '
|
2018-02-28 21:40:16 +01:00
|
|
|
<script>
|
2020-08-06 18:51:56 +02:00
|
|
|
var count = 0;
|
2020-07-31 14:25:50 +02:00
|
|
|
$(document).ready(function() {';
|
2018-03-15 17:39:19 +01:00
|
|
|
|
2020-08-06 18:51:56 +02:00
|
|
|
if ($escludi_prima_riga) {
|
2018-03-15 17:39:19 +01:00
|
|
|
echo '
|
2019-02-21 22:21:30 +01:00
|
|
|
$("#include_first_row").prop("checked", false).trigger("change");';
|
2018-03-15 17:39:19 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
echo '
|
2021-03-15 11:10:46 +01:00
|
|
|
$("#save-buttons").find(".dropdown-menu, .dropdown-toggle").remove();
|
2020-07-31 14:25:50 +02:00
|
|
|
|
2021-03-15 11:10:46 +01:00
|
|
|
var save = $("#save");
|
|
|
|
save.html("<i class=\"fa fa-flag-checkered\"></i> '.tr('Avvia importazione').'");
|
|
|
|
|
|
|
|
save.unbind("click");
|
|
|
|
save.on("click", function() {
|
2020-08-06 18:51:56 +02:00
|
|
|
count = 0;
|
2019-02-21 22:21:30 +01:00
|
|
|
importPage(0);
|
|
|
|
});
|
2018-02-28 23:44:30 +01:00
|
|
|
});
|
2019-02-21 22:21:30 +01:00
|
|
|
|
2020-08-06 18:51:56 +02:00
|
|
|
function importPage(page) {
|
2021-02-26 15:47:20 +01:00
|
|
|
// Ricerca della chiave primaria tra i campi selezionati
|
|
|
|
let primary_key = input("primary_key").get();
|
|
|
|
if (primary_key) {
|
|
|
|
let primary_key_found = false;
|
|
|
|
$("[name^=fields]").each(function() {
|
|
|
|
primary_key_found = primary_key_found || (input(this).get() == primary_key);
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!primary_key_found) {
|
|
|
|
swal({
|
|
|
|
title: "'.tr('Chiave primaria selezionata non presente tra i campi').'",
|
|
|
|
type: "error",
|
|
|
|
});
|
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-21 22:21:30 +01:00
|
|
|
$("#main_loading").show();
|
|
|
|
|
2021-02-26 15:47:20 +01:00
|
|
|
let data = {
|
2019-02-21 22:21:30 +01:00
|
|
|
id_module: "'.$id_module.'",
|
|
|
|
id_plugin: "'.$id_plugin.'",
|
|
|
|
id_record: "'.$id_record.'",
|
|
|
|
page: page,
|
|
|
|
};
|
2020-07-31 14:25:50 +02:00
|
|
|
|
2019-02-21 22:21:30 +01:00
|
|
|
$("#edit-form").ajaxSubmit({
|
|
|
|
url: globals.rootdir + "/actions.php",
|
|
|
|
data: data,
|
|
|
|
type: "post",
|
|
|
|
success: function(data) {
|
|
|
|
data = JSON.parse(data);
|
2020-07-31 14:25:50 +02:00
|
|
|
|
2019-02-21 22:21:30 +01:00
|
|
|
count += data.count;
|
2020-07-31 14:25:50 +02:00
|
|
|
|
2019-02-21 22:21:30 +01:00
|
|
|
if(data.more) {
|
|
|
|
importPage(page + 1);
|
|
|
|
} else {
|
|
|
|
$("#main_loading").fadeOut();
|
|
|
|
|
|
|
|
swal({
|
|
|
|
title: "'.tr('Importazione completata: _COUNT_ righe processate', [
|
2024-03-22 15:52:24 +01:00
|
|
|
'_COUNT_' => '" + count + "',
|
|
|
|
]).'",
|
2019-02-21 22:21:30 +01:00
|
|
|
type: "success",
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
error: function(data) {
|
|
|
|
$("#main_loading").fadeOut();
|
|
|
|
|
|
|
|
alert("'.tr('Errore').': " + data);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
2018-03-15 17:39:19 +01:00
|
|
|
</script>';
|
2018-02-23 19:04:05 +01:00
|
|
|
}
|