Skip to content

Commit 70c5e39

Browse files
committed
refactor: gestione immagini impianto
1 parent 700d29d commit 70c5e39

4 files changed

Lines changed: 84 additions & 38 deletions

File tree

modules/impianti/actions.php

Lines changed: 15 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
use Models\Module;
2424
use Modules\Checklists\Check;
25+
use Modules\Impianti\Impianto;
2526

2627
$op = post('op');
2728

@@ -62,20 +63,15 @@
6263
if (!empty($_FILES) && !empty($_FILES['immagine']['name'])) {
6364
$upload = Uploads::upload($_FILES['immagine'], [
6465
'name' => 'Immagine',
66+
'category' => 'Immagini',
6567
'id_module' => $id_module,
6668
'id_record' => $id_record,
69+
'key' => 'cover',
6770
], [
6871
'thumbnails' => true,
6972
]);
70-
$filename = $upload->filename;
71-
72-
if (!empty($filename)) {
73-
$dbo->update('my_impianti', [
74-
'immagine' => $filename,
75-
], [
76-
'id' => $id_record,
77-
]);
78-
} else {
73+
74+
if (empty($upload)) {
7975
flash()->warning(tr('Errore durante il caricamento del file in _DIR_!', [
8076
'_DIR_' => $upload_dir,
8177
]));
@@ -84,16 +80,10 @@
8480

8581
// Eliminazione file
8682
if (!empty(post('delete_immagine'))) {
87-
Uploads::delete($record['immagine'], [
88-
'id_module' => $id_module,
89-
'id_record' => $id_record,
90-
]);
91-
92-
$dbo->update('my_impianti', [
93-
'immagine' => null,
94-
], [
95-
'id' => $id_record,
96-
]);
83+
$impianto = Impianto::find($id_record);
84+
if (!empty($impianto->immagine_upload)) {
85+
$impianto->immagine_upload->delete();
86+
}
9787
}
9888
}
9989
break;
@@ -310,16 +300,11 @@
310300
}
311301

312302
// Operazioni aggiuntive per l'immagine
313-
if (filter('op') == 'rimuovi-allegato' && filter('filename') == $record['immagine']) {
314-
$dbo->update('my_impianti', [
315-
'immagine' => null,
316-
], [
317-
'id' => $id_record,
318-
]);
303+
if (filter('op') == 'rimuovi-allegato' && filter('nome_allegato') == 'Immagine') {
304+
$impianto = Impianto::find($id_record);
305+
if (!empty($impianto->immagine_upload)) {
306+
$impianto->immagine_upload->delete();
307+
}
319308
} elseif (filter('op') == 'aggiungi-allegato' && filter('nome_allegato') == 'Immagine') {
320-
$dbo->update('my_impianti', [
321-
'immagine' => $upload->filename,
322-
], [
323-
'id' => $id_record,
324-
]);
309+
// L'upload è già stato gestito dal FileManager con key='cover'
325310
}

modules/impianti/edit.php

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,13 @@
1919

2020
include_once __DIR__.'/../../core.php';
2121
use Models\Module;
22+
use Modules\Impianti\Impianto;
2223

2324
$id_modulo_categorie_impianti = Module::where('name', 'Categorie')->first()->id;
2425
$id_modulo_marca_impianti = Module::where('name', 'Marche')->first()->id;
2526

26-
$img = null;
27-
if (!empty($record['immagine'])) {
28-
$fileinfo = Uploads::fileInfo($record['immagine']);
29-
30-
$default_img = '/'.Uploads::getDirectory($id_module).'/'.$fileinfo['filename'].'_thumb600.'.$fileinfo['extension'];
31-
32-
$img = file_exists(base_dir().$default_img) ? base_path().$default_img : base_path().'/'.Uploads::getDirectory($id_module).'/'.$record['immagine'];
33-
}
27+
$impianto = Impianto::find($id_record);
28+
$img = !empty($impianto) ? $impianto->image : null;
3429

3530
?><form action="" method="post" id="edit-form" enctype="multipart/form-data">
3631
<input type="hidden" name="backto" value="record-edit">

modules/impianti/src/Impianto.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222

2323
use Common\SimpleModelTrait;
2424
use Illuminate\Database\Eloquent\Model;
25+
use Models\Module;
26+
use Models\Upload;
2527
use Modules\Anagrafiche\Anagrafica;
2628
use Modules\Articoli\Categoria;
2729

@@ -55,4 +57,32 @@ public function categoria()
5557
{
5658
return $this->belongsTo(Categoria::class, 'id_categoria');
5759
}
60+
61+
public function getImmagineUploadAttribute()
62+
{
63+
$module = Module::where('name', 'Impianti')->first();
64+
return $module->files($this->id, true)->where('key', 'cover')->first();
65+
}
66+
67+
public function getImageAttribute()
68+
{
69+
$module = Module::where('name', 'Impianti')->first();
70+
$upload = $module->files($this->id, true)
71+
->where('key', 'cover')
72+
->first();
73+
74+
if (empty($upload)) {
75+
return null;
76+
}
77+
78+
$fileinfo = \Uploads::fileInfo($upload->filename);
79+
80+
$directory = '/'.$module->upload_directory.'/';
81+
$image = $directory.$upload->filename;
82+
$image_thumbnail = $directory.$fileinfo['filename'].'_thumb600.'.$fileinfo['extension'];
83+
84+
$url = file_exists(base_dir().$image_thumbnail) ? base_path().$image_thumbnail : base_path().$image;
85+
86+
return $url;
87+
}
5888
}

update/2_9_6.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,39 @@
7676
$database->query('ALTER TABLE `in_interventi` DROP COLUMN `firma_nome`');
7777
}
7878

79+
// Migrazione immagini da my_impianti a zz_files
80+
$id_module = Module::where('name', 'Impianti')->first()->id;
81+
82+
if (!empty($id_module)) {
83+
$impianti = $database->fetchArray('SELECT `id`, `immagine` FROM `my_impianti` WHERE `immagine` IS NOT NULL');
84+
85+
foreach ($impianti as $impianto) {
86+
$file_exists = $database->selectOne('zz_files', ['id'], [
87+
'id_module' => $id_module,
88+
'id_record' => $impianto['id'],
89+
'filename' => $impianto['immagine'],
90+
]);
91+
92+
if (empty($file_exists)) {
93+
$database->insert('zz_files', [
94+
'id_module' => $id_module,
95+
'id_record' => $impianto['id'],
96+
'nome' => 'Immagine',
97+
'filename' => $impianto['immagine'],
98+
'original' => $impianto['immagine'],
99+
'key' => 'cover',
100+
]);
101+
} else {
102+
$database->update('zz_files', [
103+
'key' => 'cover',
104+
], [
105+
'id_module' => $id_module,
106+
'id_record' => $impianto['id'],
107+
'filename' => $impianto['immagine'],
108+
]);
109+
}
110+
}
111+
112+
$database->query('ALTER TABLE `my_impianti` DROP COLUMN `immagine`');
113+
}
114+

0 commit comments

Comments
 (0)