2020-08-06 18:51:56 +02:00
|
|
|
<?php
|
2020-09-07 15:04:06 +02:00
|
|
|
/*
|
|
|
|
* OpenSTAManager: il software gestionale open source per l'assistenza tecnica e la fatturazione
|
|
|
|
* Copyright (C) DevCode s.n.c.
|
|
|
|
*
|
|
|
|
* 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/>.
|
|
|
|
*/
|
2020-08-06 18:51:56 +02:00
|
|
|
|
2020-08-07 10:14:09 +02:00
|
|
|
namespace Importer;
|
2020-08-06 18:51:56 +02:00
|
|
|
|
|
|
|
use Filter;
|
|
|
|
use League\Csv\Reader;
|
|
|
|
|
2020-08-07 10:43:14 +02:00
|
|
|
/**
|
|
|
|
* Classe dedicata alla gestione dell'importazione da file CSV.
|
|
|
|
*
|
|
|
|
* @since 2.4.17
|
|
|
|
*/
|
2020-08-07 10:14:09 +02:00
|
|
|
abstract class CSVImporter implements ImporterInterface
|
2020-08-06 18:51:56 +02:00
|
|
|
{
|
|
|
|
protected $csv;
|
|
|
|
|
|
|
|
protected $column_associations;
|
|
|
|
protected $primary_key;
|
|
|
|
|
|
|
|
public function __construct($file)
|
|
|
|
{
|
|
|
|
// Impostazione automatica per i caratteri di fine riga
|
|
|
|
if (!ini_get('auto_detect_line_endings')) {
|
|
|
|
ini_set('auto_detect_line_endings', '1');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Gestione del file CSV
|
|
|
|
$this->csv = Reader::createFromPath($file, 'r');
|
|
|
|
$this->csv->setDelimiter(';');
|
|
|
|
|
|
|
|
$this->column_associations = [];
|
|
|
|
}
|
|
|
|
|
2020-12-03 10:02:50 +01:00
|
|
|
public function init()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
public function complete()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-08-06 18:51:56 +02:00
|
|
|
public function getColumnAssociations()
|
|
|
|
{
|
|
|
|
return $this->column_associations;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setColumnAssociation($column_key, $field_key)
|
|
|
|
{
|
|
|
|
$this->column_associations[$column_key] = $this->getAvailableFields()[$field_key]['field'];
|
|
|
|
}
|
|
|
|
|
|
|
|
abstract public function getAvailableFields();
|
|
|
|
|
2020-08-07 10:14:09 +02:00
|
|
|
public function getHeader()
|
|
|
|
{
|
|
|
|
$first_row = $this->getRows(0, 1);
|
|
|
|
|
|
|
|
return array_shift($first_row);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getRows($offset, $length)
|
2020-08-06 18:51:56 +02:00
|
|
|
{
|
|
|
|
$rows = [];
|
|
|
|
for ($i = 0; $i < $length; ++$i) {
|
|
|
|
// Lettura di una singola riga alla volta
|
|
|
|
$row = $this->csv->fetchOne($offset + $i);
|
|
|
|
if (empty($row)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Aggiunta all'insieme dei record
|
|
|
|
$rows[] = Filter::parse($row);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $rows;
|
|
|
|
}
|
|
|
|
|
2020-08-07 10:14:09 +02:00
|
|
|
public function importRows($offset, $length)
|
2020-08-06 18:51:56 +02:00
|
|
|
{
|
|
|
|
$associations = $this->getColumnAssociations();
|
|
|
|
|
2020-08-07 10:14:09 +02:00
|
|
|
$rows = $this->getRows($offset, $length);
|
2020-08-06 18:51:56 +02:00
|
|
|
foreach ($rows as $row) {
|
|
|
|
// Interpretazione della riga come record
|
|
|
|
$record = [];
|
|
|
|
foreach ($row as $key => $value) {
|
|
|
|
$field = isset($associations[$key]) ? $associations[$key] : null;
|
|
|
|
if (!empty($field)) {
|
|
|
|
$record[$field] = $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Importazione del record
|
|
|
|
$this->import($record);
|
|
|
|
}
|
|
|
|
|
|
|
|
return count($rows);
|
|
|
|
}
|
|
|
|
|
|
|
|
abstract public function import($record);
|
|
|
|
|
|
|
|
public function getPrimaryKey()
|
|
|
|
{
|
|
|
|
return $this->primary_key;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setPrimaryKey($field_key)
|
|
|
|
{
|
|
|
|
$this->primary_key = $this->getAvailableFields()[$field_key]['field'];
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function createExample($filepath)
|
|
|
|
{
|
|
|
|
$content = static::getExample();
|
|
|
|
|
|
|
|
$file = fopen($filepath, 'w');
|
|
|
|
fprintf($file, chr(0xEF).chr(0xBB).chr(0xBF));
|
|
|
|
|
|
|
|
foreach ($content as $row) {
|
|
|
|
fputcsv($file, $row, ';');
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose($file);
|
|
|
|
}
|
|
|
|
}
|