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
|
|
|
switch (filter('op')) {
|
|
|
|
case 'add':
|
2021-02-26 11:04:06 +01:00
|
|
|
$id_import = filter('id_import');
|
|
|
|
$import = Import::find($id_import);
|
|
|
|
|
|
|
|
$id_record = $import->id;
|
2020-08-06 18:51:56 +02:00
|
|
|
|
2021-03-08 11:20:04 +01:00
|
|
|
Uploads::upload($_FILES['file'], [
|
2021-02-26 11:04:06 +01:00
|
|
|
'id_module' => $import->getModule()->id,
|
2020-08-06 18:51:56 +02:00
|
|
|
'id_record' => $id_record,
|
|
|
|
]);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'example':
|
2021-02-26 11:04:06 +01:00
|
|
|
$id_import = filter('id_import');
|
|
|
|
|
|
|
|
$import = Import::find($id_import);
|
|
|
|
$import_manager = $import->class;
|
2020-08-06 18:51:56 +02:00
|
|
|
|
2021-02-26 11:04:06 +01:00
|
|
|
$modulo_collegato = $import->moduloCollegato;
|
|
|
|
if (!empty($import_manager)) {
|
2020-08-06 18:51:56 +02:00
|
|
|
// Generazione percorso
|
2021-02-26 11:04:06 +01:00
|
|
|
$file = $modulo_collegato->upload_directory.'/example-'.strtolower($modulo_collegato->title).'.csv';
|
2020-09-23 13:36:37 +02:00
|
|
|
$filepath = base_dir().'/'.$file;
|
2020-08-06 18:51:56 +02:00
|
|
|
|
|
|
|
// Generazione del file
|
2021-02-26 11:04:06 +01:00
|
|
|
$import_manager::createExample($filepath);
|
2020-08-06 18:51:56 +02:00
|
|
|
|
2021-10-23 16:37:21 +02:00
|
|
|
echo base_path().'/'.$file;
|
2020-08-06 18:51:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
2018-02-23 19:04:05 +01:00
|
|
|
case 'import':
|
2020-08-06 18:51:56 +02:00
|
|
|
// Individuazione del modulo
|
2021-02-26 11:04:06 +01:00
|
|
|
$import = Import::find($id_record);
|
|
|
|
$import_manager = $import->class;
|
2020-08-06 18:51:56 +02:00
|
|
|
|
|
|
|
// Dati indicati
|
2019-02-21 22:21:30 +01:00
|
|
|
$include_first_row = post('include_first_row');
|
2020-08-06 18:51:56 +02:00
|
|
|
$fields = (array) post('fields');
|
2019-02-21 22:21:30 +01:00
|
|
|
$page = post('page');
|
|
|
|
|
|
|
|
$limit = 500;
|
2018-02-23 19:04:05 +01:00
|
|
|
|
2020-08-06 18:51:56 +02:00
|
|
|
// Inizializzazione del lettore CSV
|
2021-02-26 11:04:06 +01:00
|
|
|
$csv = new $import_manager($record->filepath);
|
2020-08-06 18:51:56 +02:00
|
|
|
foreach ($fields as $key => $value) {
|
2021-02-26 15:47:20 +01:00
|
|
|
$csv->setColumnAssociation($key, $value - 1);
|
2018-02-23 19:04:05 +01:00
|
|
|
}
|
|
|
|
|
2020-08-06 18:51:56 +02:00
|
|
|
// Generazione offset sulla base della pagina
|
2019-02-21 22:21:30 +01:00
|
|
|
$offset = isset($page) ? $page * $limit : 0;
|
2018-09-04 10:03:02 +02:00
|
|
|
|
2019-02-21 22:21:30 +01:00
|
|
|
// Ignora la prima riga se composta da header
|
|
|
|
if ($offset == 0 && empty($include_first_row)) {
|
|
|
|
++$offset;
|
|
|
|
}
|
2018-02-23 19:04:05 +01:00
|
|
|
|
2019-02-21 22:21:30 +01:00
|
|
|
// Gestione automatica dei valori convertiti
|
2018-03-15 17:39:19 +01:00
|
|
|
$primary_key = post('primary_key');
|
2021-02-26 15:47:20 +01:00
|
|
|
$csv->setPrimaryKey($primary_key - 1);
|
2018-03-15 17:39:19 +01:00
|
|
|
|
2020-12-03 10:02:50 +01:00
|
|
|
// Operazioni di inizializzazione per l'importazione
|
2021-02-26 15:47:20 +01:00
|
|
|
if (!isset($page) || empty($page)) {
|
2020-12-03 10:02:50 +01:00
|
|
|
$csv->init();
|
|
|
|
}
|
|
|
|
|
2020-08-07 10:14:09 +02:00
|
|
|
$count = $csv->importRows($offset, $limit);
|
2019-02-21 22:21:30 +01:00
|
|
|
$more = $count == $limit;
|
|
|
|
|
2020-12-03 10:02:50 +01:00
|
|
|
// Operazioni di finalizzazione per l'importazione
|
|
|
|
if (!$more) {
|
|
|
|
$csv->complete();
|
|
|
|
}
|
|
|
|
|
2019-02-21 22:21:30 +01:00
|
|
|
echo json_encode([
|
|
|
|
'more' => $more,
|
|
|
|
'count' => $count,
|
|
|
|
]);
|
2018-02-23 19:04:05 +01:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|