2018-02-23 19:04:05 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
include_once __DIR__.'/../../core.php';
|
|
|
|
|
|
|
|
switch (post('op')) {
|
|
|
|
case 'import':
|
2019-02-21 22:21:30 +01:00
|
|
|
$include_first_row = post('include_first_row');
|
|
|
|
$selected_fields = post('fields');
|
|
|
|
$page = post('page');
|
|
|
|
|
|
|
|
$limit = 500;
|
2018-02-23 19:04:05 +01:00
|
|
|
|
|
|
|
// Pulizia dei campi inutilizzati
|
|
|
|
foreach ($selected as $key => $value) {
|
|
|
|
if (!is_numeric($value)) {
|
|
|
|
unset($selected[$key]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$fields = Import::getFields($id_record);
|
|
|
|
|
2019-02-21 22:21:30 +01:00
|
|
|
$csv = Import::getCSV($id_record, $record['id']);
|
2018-03-15 17:39:19 +01:00
|
|
|
|
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
|
|
|
$csv = $csv->setOffset($offset)
|
2019-09-12 16:14:38 +02:00
|
|
|
->setLimit($limit);
|
2018-02-23 19:04:05 +01:00
|
|
|
|
2019-02-21 22:21:30 +01:00
|
|
|
// Chiavi per la lettura CSV
|
|
|
|
$keys = [];
|
|
|
|
foreach ($selected_fields as $id => $field_id) {
|
|
|
|
if (is_numeric($field_id)) {
|
|
|
|
$value = $fields[$field_id]['field'];
|
|
|
|
} else {
|
|
|
|
$value = -($id + 1);
|
|
|
|
}
|
2018-02-23 19:04:05 +01:00
|
|
|
|
2019-02-21 22:21:30 +01:00
|
|
|
$keys[] = $value;
|
|
|
|
}
|
2018-02-23 19:04:05 +01:00
|
|
|
|
2019-02-21 22:21:30 +01:00
|
|
|
// Query dei campi selezionati
|
|
|
|
$queries = [];
|
|
|
|
foreach ($fields as $key => $field) {
|
|
|
|
if (!empty($field['query'])) {
|
|
|
|
$queries[$field['field']] = $field['query'];
|
|
|
|
}
|
|
|
|
}
|
2018-02-23 19:04:05 +01:00
|
|
|
|
2019-02-21 22:21:30 +01:00
|
|
|
// Lettura dei record
|
|
|
|
$rows = $csv->fetchAssoc($keys, function ($row) use ($queries, $dbo) {
|
|
|
|
foreach ($row as $key => $value) {
|
|
|
|
if (is_int($key)) {
|
|
|
|
unset($row[$key]);
|
|
|
|
} elseif (isset($queries[$key])) {
|
|
|
|
$query = str_replace('|value|', prepare($value), $queries[$key]);
|
|
|
|
|
|
|
|
$value = $dbo->fetchOne($query)['result'];
|
|
|
|
|
|
|
|
$row[$key] = $value;
|
2018-02-23 19:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-21 22:21:30 +01:00
|
|
|
return $row;
|
|
|
|
});
|
|
|
|
|
|
|
|
// Gestione automatica dei valori convertiti
|
|
|
|
$rows = iterator_to_array($rows);
|
|
|
|
$data = Filter::parse($rows);
|
2018-02-23 19:04:05 +01:00
|
|
|
|
2018-03-15 17:39:19 +01:00
|
|
|
$primary_key = post('primary_key');
|
|
|
|
|
2018-02-23 19:04:05 +01:00
|
|
|
// Richiamo delle operazioni specifiche
|
|
|
|
include $imports[$id_record]['import'];
|
|
|
|
|
2019-02-21 22:21:30 +01:00
|
|
|
$count = count($rows);
|
|
|
|
$more = $count == $limit;
|
|
|
|
|
|
|
|
echo json_encode([
|
|
|
|
'more' => $more,
|
|
|
|
'count' => $count,
|
|
|
|
]);
|
2018-02-23 19:04:05 +01:00
|
|
|
|
|
|
|
break;
|
|
|
|
}
|