Aggiunta immagine in import CSV articoli

This commit is contained in:
Beppe 2021-10-15 19:06:26 +02:00
parent 9d5bc51326
commit 24fe32040e
2 changed files with 62 additions and 0 deletions

View File

@ -19,7 +19,10 @@
namespace Modules\Articoli\Import;
use Uploads;
use Modules;
use Carbon\Carbon;
use Models\Upload;
use Importer\CSVImporter;
use Modules\Anagrafiche\Anagrafica;
use Modules\Anagrafiche\Sede;
@ -43,6 +46,14 @@ class CSV extends CSVImporter
'label' => 'Codice',
'primary_key' => true,
],
[
'field' => 'immagine',
'label' => 'Immagine',
'names' => [
'Immagine',
'Foto',
],
],
[
'field' => 'descrizione',
'label' => 'Descrizione',
@ -210,6 +221,8 @@ class CSV extends CSVImporter
{
$database = database();
$primary_key = $this->getPrimaryKey();
$url = $record['immagine'];
unset($record['immagine']);
// Fix per campi con contenuti derivati da query implicite
if (!empty($record['id_fornitore'])) {
@ -316,6 +329,34 @@ class CSV extends CSVImporter
$articolo->movimenta($qta_movimento, tr('Movimento da importazione'), new Carbon(), false, [
'idsede' => $id_sede,
]);
//Gestione immagine
if ( !empty($url) ) {
$file_content = file_get_contents($url);
if( !empty($file_content) ){
$name = "immagine_".$articolo->id.'.'.Upload::getExtensionFromMimeType($file_content);
$upload = Uploads::upload($file_content, [
'name' => 'Immagine',
'category' => 'Immagini',
'original_name' => $name,
'id_module' => Modules::get('Articoli')['id'],
'id_record' => $articolo->id,
], [
'thumbnails' => true,
]);
$filename = $upload->filename;
if (!empty($filename)) {
$database->update('mg_articoli', [
'immagine' => $filename,
], [
'id' => $articolo->id,
]);
}
}
}
}
public static function getExample()

View File

@ -38,6 +38,15 @@ class Upload extends Model
'htm' => 'text/html',
];
/** @var array Elenco delle estensioni file per mime type */
protected static $extension_association = [
'image/gif' => 'gif',
'image/bmp' => 'bmp',
'image/jpg' => 'jpg',
'image/jpeg' => 'jpg',
'image/png' => 'png',
];
protected $table = 'zz_files';
protected $file_info;
@ -211,6 +220,18 @@ class Upload extends Model
return in_array($this->extension, $list);
}
/**
* @return string
*/
public static function getExtensionFromMimeType($file_content)
{
$finfo = finfo_open();
$mime_type = finfo_buffer($finfo, $file_content, FILEINFO_MIME_TYPE);
finfo_close($finfo);
return self::$extension_association[$mime_type];
}
/**
* @return bool
*/