1
0
mirror of https://github.com/devcode-it/openstamanager.git synced 2024-12-20 04:06:34 +01:00
openstamanager/src/Import.php

151 lines
3.8 KiB
PHP
Raw Normal View History

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
$results[$module['id']] = array_merge($module, [
'import' => file_exists($custom_file) ? $custom_file : $original_file,
'files' => $files,
]);
}
}
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();
// 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 = [
$value['field'],
$value['label'],
];
2018-09-04 10:03:02 +02:00
} else {
$names = $value['names'];
}
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-02-23 19:04:05 +01:00
return $fields;
}
/**
* Restituisce i contenuti del file CSV indicato.
*
* @param string|int $module
* @param int $file_id
* @param array $options
*
* @return array
*/
public static function getFile($module, $file_id, $options = [])
{
$import = self::get($module);
$ids = array_column($import['files'], 'id');
$find = array_search($file_id, $ids);
if ($find == -1) {
return [];
}
$file = DOCROOT.'/files/'.Modules::get('Import')['directory'].'/'.$import['files'][$find]['filename'];
// 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(';');
// Ignora la prima riga
$offset = 0;
if (!empty($options['headers'])) {
++$offset;
}
$rows = $csv->setOffset($offset);
// Limite di righe
if (!empty($options['limit'])) {
$rows = $rows->setLimit($options['limit']);
}
// Lettura
$rows = $rows->fetchAll();
return $rows;
}
}