2018-02-23 19:04:05 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
include_once __DIR__.'/../../core.php';
|
|
|
|
|
2020-08-06 18:51:56 +02:00
|
|
|
switch (filter('op')) {
|
|
|
|
case 'add':
|
|
|
|
$modulo_selezionato = Modules::get(filter('module'));
|
|
|
|
$id_record = $modulo_selezionato->id;
|
|
|
|
|
|
|
|
$upload = Uploads::upload($_FILES['file'], [
|
|
|
|
'id_module' => $modulo_import->id,
|
|
|
|
'id_record' => $id_record,
|
|
|
|
]);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'example':
|
|
|
|
$module = filter('module');
|
|
|
|
$modulo_selezionato = Modules::get(filter('module'));
|
|
|
|
$import_selezionato = $moduli_disponibili[$module];
|
|
|
|
|
|
|
|
if (!empty($import_selezionato)) {
|
|
|
|
// Generazione percorso
|
|
|
|
$file = $modulo_selezionato->upload_directory.'/example-'.strtolower($modulo_selezionato->title).'.csv';
|
|
|
|
$filepath = DOCROOT.'/'.$file;
|
|
|
|
|
|
|
|
// Generazione del file
|
|
|
|
$import_selezionato::createExample($filepath);
|
|
|
|
|
|
|
|
echo ROOTDIR.'/'.$file;
|
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
2018-02-23 19:04:05 +01:00
|
|
|
case 'import':
|
2020-08-06 18:51:56 +02:00
|
|
|
// Individuazione del modulo
|
|
|
|
$modulo_selezionato = Modules::get($id_record);
|
|
|
|
$import_selezionato = $moduli_disponibili[$modulo_selezionato->name];
|
|
|
|
|
|
|
|
// 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
|
|
|
|
$csv = new $import_selezionato($record->filepath);
|
|
|
|
foreach ($fields as $key => $value) {
|
|
|
|
$csv->setColumnAssociation($key, $value);
|
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');
|
2020-08-06 18:51:56 +02:00
|
|
|
$csv->setPrimaryKey($primary_key);
|
2018-03-15 17:39:19 +01:00
|
|
|
|
2020-08-07 10:14:09 +02:00
|
|
|
$count = $csv->importRows($offset, $limit);
|
2019-02-21 22:21:30 +01:00
|
|
|
$more = $count == $limit;
|
|
|
|
|
|
|
|
echo json_encode([
|
|
|
|
'more' => $more,
|
|
|
|
'count' => $count,
|
|
|
|
]);
|
2018-02-23 19:04:05 +01:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|