openstamanager/modules/import/actions.php

114 lines
3.3 KiB
PHP
Raw Normal View History

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
2024-03-05 16:01:45 +01:00
use Models\Module;
use Modules\FileAdapters\FileAdapter;
2024-03-22 15:52:24 +01:00
use Modules\Importazione\Import;
2018-02-23 19:04:05 +01:00
include_once __DIR__.'/../../core.php';
2024-03-05 16:01:45 +01:00
$modulo_import = Module::find($id_module);
2023-05-30 16:48:38 +02:00
switch (filter('op')) {
case 'add':
$id_import = filter('id_import');
$import = Import::find($id_import);
$id_adapter = FileAdapter::getLocalConnector()->id;
$id_record = $import->id;
Uploads::upload($_FILES['file'], [
2023-05-30 16:48:38 +02:00
'id_module' => $id_module,
'id_record' => $id_record,
'id_adapter' => $id_adapter,
]);
break;
case 'example':
$id_import = filter('id_import');
$import = Import::find($id_import);
$import_manager = $import->class;
if (!empty($import_manager)) {
// Generazione percorso
2024-05-23 11:36:25 +02:00
$file = $modulo_import->upload_directory.'/example-'.strtolower((string) $import->getTranslation('title')).'.csv';
$filepath = base_dir().'/'.$file;
// Generazione del file
$import_manager::createExample($filepath);
echo base_path().'/'.$file;
}
break;
2018-02-23 19:04:05 +01:00
case 'import':
// Individuazione del modulo
$import = Import::find($id_record);
$import_manager = $import->class;
// Dati indicati
2019-02-21 22:21:30 +01:00
$include_first_row = post('include_first_row');
$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
// Inizializzazione del lettore CSV
2024-03-27 10:17:27 +01:00
$filepath = base_dir().'/files/'.$record->directory.'/'.$record->filename;
$csv = new $import_manager($filepath);
foreach ($fields as $key => $value) {
2023-08-04 14:54:28 +02:00
$csv->setColumnAssociation($key, (int) $value - 1);
2018-02-23 19:04:05 +01: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
$primary_key = post('primary_key');
$csv->setPrimaryKey($primary_key - 1);
// Operazioni di inizializzazione per l'importazione
if (!isset($page) || empty($page)) {
$csv->init();
}
$count = $csv->importRows($offset, $limit);
2019-02-21 22:21:30 +01:00
$more = $count == $limit;
// 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;
}