2018-02-23 19:04:05 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Classe per la gestione delle utenze.
|
|
|
|
*
|
|
|
|
* @since 2.4
|
|
|
|
*/
|
|
|
|
class Import
|
|
|
|
{
|
|
|
|
/** @var int Identificativo del modulo corrente */
|
|
|
|
protected static $imports;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Restituisce tutte le informazioni di tutti i moduli installati.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public static function getImports()
|
|
|
|
{
|
|
|
|
if (empty(self::$imports)) {
|
|
|
|
$modules = Modules::getModules();
|
|
|
|
|
2018-09-20 12:05:22 +02:00
|
|
|
$database = database();
|
2018-02-23 19:04:05 +01:00
|
|
|
|
|
|
|
$results = [];
|
|
|
|
foreach ($modules as $module) {
|
|
|
|
$file = DOCROOT.'/modules/'.$module['directory'].'|custom|/import.php';
|
|
|
|
|
|
|
|
$original_file = str_replace('|custom|', '', $file);
|
|
|
|
$custom_file = str_replace('|custom|', '/custom', $file);
|
|
|
|
|
|
|
|
if (file_exists($custom_file) || file_exists($original_file)) {
|
2018-08-31 11:39:38 +02:00
|
|
|
$files = Uploads::get([
|
|
|
|
'id_module' => Modules::get('Import')['id'],
|
|
|
|
'id_record' => $module['id'],
|
|
|
|
]);
|
2018-02-23 19:04:05 +01:00
|
|
|
|
2018-10-29 16:52:11 +01:00
|
|
|
$results[$module['id']] = array_merge($module->toArray(), [
|
2018-02-23 19:04:05 +01:00
|
|
|
'import' => file_exists($custom_file) ? $custom_file : $original_file,
|
2018-11-22 15:34:44 +01:00
|
|
|
'files' => array_reverse($files),
|
2018-02-23 19:04:05 +01:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
self::$imports = $results;
|
|
|
|
}
|
|
|
|
|
|
|
|
return self::$imports;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Restituisce le informazioni relative a un singolo modulo specificato.
|
|
|
|
*
|
|
|
|
* @param string|int $module
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public static function get($module)
|
|
|
|
{
|
2018-06-23 15:41:32 +02:00
|
|
|
$module = Modules::get($module)['id'];
|
2018-02-23 19:04:05 +01:00
|
|
|
|
|
|
|
return self::getImports()[$module];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Restituisce l'elenco dei campi previsti dal modulo.
|
|
|
|
*
|
|
|
|
* @param string|int $module
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public static function getFields($module)
|
|
|
|
{
|
|
|
|
$import = self::get($module);
|
|
|
|
|
|
|
|
ob_start();
|
|
|
|
$fields = require $import['import'];
|
|
|
|
ob_end_clean();
|
|
|
|
|
2018-03-15 17:39:19 +01:00
|
|
|
// Impostazione automatica dei nomi "ufficiali" dei campi
|
|
|
|
foreach ($fields as $key => $value) {
|
|
|
|
if (!isset($value['names'])) {
|
2018-09-04 10:03:02 +02:00
|
|
|
$names = [
|
2018-03-15 17:39:19 +01:00
|
|
|
$value['field'],
|
|
|
|
$value['label'],
|
|
|
|
];
|
2018-09-04 10:03:02 +02:00
|
|
|
} else {
|
|
|
|
$names = $value['names'];
|
2018-03-15 17:39:19 +01:00
|
|
|
}
|
2018-09-04 10:03:02 +02:00
|
|
|
|
|
|
|
// Impostazione dei nomi in minuscolo
|
|
|
|
foreach ($names as $k => $v) {
|
|
|
|
$names[$k] = str_to_lower($v);
|
|
|
|
}
|
|
|
|
|
|
|
|
$fields[$key]['names'] = $names;
|
2018-03-15 17:39:19 +01:00
|
|
|
}
|
|
|
|
|
2018-02-23 19:04:05 +01:00
|
|
|
return $fields;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Restituisce i contenuti del file CSV indicato.
|
|
|
|
*
|
|
|
|
* @param string|int $module
|
|
|
|
* @param int $file_id
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2019-02-21 22:21:30 +01:00
|
|
|
public static function getCSV($module, $file_id)
|
2018-02-23 19:04:05 +01:00
|
|
|
{
|
|
|
|
$import = self::get($module);
|
|
|
|
|
|
|
|
$ids = array_column($import['files'], 'id');
|
|
|
|
$find = array_search($file_id, $ids);
|
|
|
|
|
|
|
|
if ($find == -1) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
|
2019-02-21 22:21:30 +01:00
|
|
|
$file = Modules::get('Import')->upload_directory.'/'.$import['files'][$find]['filename'];
|
2018-02-23 19:04:05 +01:00
|
|
|
|
2018-02-24 09:09:53 +01:00
|
|
|
// Impostazione automatica per i caratteri di fine riga
|
|
|
|
if (!ini_get('auto_detect_line_endings')) {
|
|
|
|
ini_set('auto_detect_line_endings', '1');
|
|
|
|
}
|
|
|
|
|
2018-02-23 19:04:05 +01:00
|
|
|
// Gestione del file CSV
|
|
|
|
$csv = League\Csv\Reader::createFromPath($file, 'r');
|
|
|
|
$csv->setDelimiter(';');
|
|
|
|
|
2019-02-21 22:21:30 +01:00
|
|
|
return $csv;
|
2018-02-23 19:04:05 +01:00
|
|
|
}
|
|
|
|
}
|