Skip to content

Commit 4aa4229

Browse files
committed
refactor: gestione allegati articoli
1 parent a8ae86e commit 4aa4229

16 files changed

Lines changed: 88 additions & 84 deletions

File tree

modules/anagrafiche/plugins/allegati.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@
121121
</tr>';
122122

123123
foreach ($documenti as $documento) {
124-
$allegati = $dbo->fetchArray('SELECT * FROM zz_files WHERE id_module='.prepare($documento['id_module']).' AND id_record='.prepare($documento['id_record']));
124+
$allegati = $dbo->fetchArray('SELECT * FROM zz_files WHERE id_module='.prepare($documento['id_module']).' AND id_record='.prepare($documento['id_record']).' AND `key` IS NULL');
125125

126126
foreach ($allegati as $allegato) {
127127
$file = Upload::find($allegato['id']);

modules/articoli/actions.php

Lines changed: 7 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -246,34 +246,22 @@
246246
'category' => 'Immagini',
247247
'id_module' => $id_module,
248248
'id_record' => $id_record,
249+
'key' => 'cover',
249250
], [
250251
'thumbnails' => true,
251252
]);
252-
$filename = $upload->filename;
253253

254-
if (!empty($filename)) {
255-
$dbo->update('mg_articoli', [
256-
'immagine' => $filename,
257-
], [
258-
'id' => $id_record,
259-
]);
260-
} else {
254+
if (empty($upload)) {
261255
flash()->warning(tr("Errore durante il caricamento dell'immagine!"));
262256
}
263257
}
264258

265-
// Eliminazione file
259+
// Eliminazione immagine
266260
if (!empty(post('delete_immagine'))) {
267-
Uploads::delete($record['immagine'], [
268-
'id_module' => $id_module,
269-
'id_record' => $id_record,
270-
]);
271-
272-
$dbo->update('mg_articoli', [
273-
'immagine' => null,
274-
], [
275-
'id' => $id_record,
276-
]);
261+
$immagine = $articolo->immagine_upload;
262+
if (!empty($immagine)) {
263+
$immagine->delete();
264+
}
277265
}
278266

279267
flash()->info(tr('Informazioni salvate correttamente!'));
@@ -293,8 +281,6 @@
293281

294282
$new->codice = $codice;
295283
$new->qta = 0;
296-
$nome_immagine = $articolo->immagine;
297-
$new->immagine = $nome_immagine;
298284
$new->save();
299285

300286
$new->setTranslation('title', $articolo->getTranslation('title'));
@@ -545,18 +531,3 @@
545531

546532
break;
547533
}
548-
549-
// Operazioni aggiuntive per l'immagine
550-
if (filter('op') == 'rimuovi-allegato' && filter('filename') == $record['immagine']) {
551-
$dbo->update('mg_articoli', [
552-
'immagine' => null,
553-
], [
554-
'id' => $id_record,
555-
]);
556-
} elseif (filter('op') == 'aggiungi-allegato' && filter('nome_allegato') == 'Immagine') {
557-
$dbo->update('mg_articoli', [
558-
'immagine' => $upload->filename,
559-
], [
560-
'id' => $id_record,
561-
]);
562-
}

modules/articoli/header.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
include_once __DIR__.'/../../core.php';
2222
use Modules\Articoli\Marca;
2323

24-
$immagine_articolo = $articolo->immagine ? base_path().'/files/articoli/'.$articolo->immagine : App::getPaths()['img'].'/logo_header.png';
24+
$immagine_articolo = $articolo->image ?: App::getPaths()['img'].'/logo_header.png';
2525

2626
echo '
2727
<hr>

modules/articoli/src/Articolo.php

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -257,24 +257,25 @@ public function isVariante()
257257

258258
public function getImmagineUploadAttribute()
259259
{
260-
if (empty($this->immagine)) {
261-
return null;
262-
}
263-
264-
return $this->uploads()->where('filename', $this->immagine)->first();
260+
$module = $this->getModule();
261+
return $module->files($this->id, true)->where('key', 'cover')->first();
265262
}
266263

267264
public function getImageAttribute()
268265
{
269-
if (empty($this->immagine)) {
266+
$module = $this->getModule();
267+
$upload = $module->files($this->id, true)
268+
->where('key', 'cover')
269+
->first();
270+
271+
if (empty($upload)) {
270272
return null;
271273
}
272274

273-
$module = Module::where('name', $this->module)->first();
274-
$fileinfo = \Uploads::fileInfo($this->immagine);
275+
$fileinfo = \Uploads::fileInfo($upload->filename);
275276

276277
$directory = '/'.$module->upload_directory.'/';
277-
$image = $directory.$this->immagine;
278+
$image = $directory.$upload->filename;
278279
$image_thumbnail = $directory.$fileinfo['filename'].'_thumb600.'.$fileinfo['extension'];
279280

280281
$url = file_exists(base_dir().$image_thumbnail) ? base_path().$image_thumbnail : base_path().$image;

modules/articoli/src/Import/CSV.php

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -722,38 +722,24 @@ protected function processaImmagine($articolo, $url, $record)
722722
return;
723723
}
724724

725-
$database = database();
726-
727725
if ($record['import_immagine'] == 2 || $record['import_immagine'] == 4) {
728726
\Uploads::deleteLinked([
729727
'id_module' => Module::where('name', 'Articoli')->first()->id,
730728
'id_record' => $articolo->id,
731729
]);
732-
733-
$database->update('mg_articoli', [
734-
'immagine' => '',
735-
], [
736-
'id' => $articolo->id,
737-
]);
738730
}
739731

740-
$name = 'immagine_'.$articolo->id.'.'.Upload::getExtensionFromMimeType($file_content);
741-
742-
$upload = \Uploads::upload($file_content, [
743-
'name' => 'Immagine',
744-
'category' => 'Immagini',
745-
'original_name' => $name,
746-
'id_module' => Module::where('name', 'Articoli')->first()->id,
747-
'id_record' => $articolo->id,
748-
], [
749-
'thumbnails' => true,
750-
]);
751-
752-
if ($upload && !empty($upload->filename) && ($record['import_immagine'] == 1 || $record['import_immagine'] == 2)) {
753-
$database->update('mg_articoli', [
754-
'immagine' => $upload->filename,
732+
if ($record['import_immagine'] == 1 || $record['import_immagine'] == 2) {
733+
$name = 'immagine_'.$articolo->id.'.'.Upload::getExtensionFromMimeType($file_content);
734+
735+
\Uploads::upload($file_content, [
736+
'name' => 'Immagine',
737+
'category' => 'Immagini',
738+
'original_name' => $name,
739+
'id_module' => Module::where('name', 'Articoli')->first()->id,
740+
'id_record' => $articolo->id,
755741
], [
756-
'id' => $articolo->id,
742+
'thumbnails' => true,
757743
]);
758744
}
759745
} catch (\Exception $e) {

modules/combinazioni_articoli/src/Combinazione.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -108,17 +108,13 @@ public function generaVariante($valori_attributi, $id_articolo = null, $nome_art
108108
$articolo_base = $articoli->first();
109109
$articolo = $articolo_base->replicate();
110110

111-
$nome_immagine = $articolo_base->immagine_upload->getTranslation('title');
112-
$allegato = $articolo_base->uploads()->where('name', $nome_immagine)->first();
111+
$immagine = $articolo_base->uploads()->where('name', 'Immagine')->first();
113112

114-
if (!empty($allegato)) {
115-
$allegato->copia([
113+
if (!empty($immagine)) {
114+
$immagine->copia([
116115
'id_module' => $articolo->getModule()->id,
117116
'id_record' => $articolo->id,
118117
]);
119-
120-
$articolo->immagine = $articolo->uploads()->where('name', $nome_immagine)->first()->filename;
121-
$articolo->save();
122118
}
123119
}
124120
$database->query("INSERT INTO `mg_articoli_lang` (`id_record`, `id_lang`, `title`) VALUES ('".$articolo->id."', ".\Models\Locale::getDefault()->id.", '".$nome_articolo."')");

modules/stampe/ajax/select.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
$where[] = '`zz_files`.`id_plugin` = '.prepare($id_plugin);
3737
}
3838
$where[] = '`zz_files`.`id_record` = '.prepare($id_record);
39+
$where[] = '`zz_files`.`key` IS NULL';
3940

4041
if ($is_fiscale != null) {
4142
$where[] = '`zz_segments`.`is_fiscale` = '.prepare($is_fiscale);

src/HTMLBuilder/Manager/FileManager.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ public function manage($options)
8787

8888
$count = 0;
8989

90-
$where = '`id_module` '.(!empty($options['id_module']) && empty($options['id_plugin']) ? '= '.prepare($options['id_module']) : 'IS NULL').' AND `id_plugin` '.(!empty($options['id_plugin']) ? '= '.prepare($options['id_plugin']) : 'IS NULL');
90+
$where = '`id_module` '.(!empty($options['id_module']) && empty($options['id_plugin']) ? '= '.prepare($options['id_module']) : 'IS NULL').' AND `id_plugin` '.(!empty($options['id_plugin']) ? '= '.prepare($options['id_plugin']) : 'IS NULL').' AND `key` IS NULL';
9191

9292
// Limitare alle categorie specificate
9393
if (!empty($id_categoria)) {

src/Models/Upload.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ public static function build($source = null, $data = null, $name = null, $catego
112112
$model->id_module = !empty($data['id_module']) && empty($data['id_plugin']) ? $data['id_module'] : null;
113113
$model->id_plugin = !empty($data['id_plugin']) ? $data['id_plugin'] : null;
114114
$model->id_record = !empty($data['id_record']) ? $data['id_record'] : null;
115+
$model->key = isset($data['key']) ? $data['key'] : null;
115116

116117
// Caricamento file con interfaccia di upload
117118

@@ -278,6 +279,7 @@ public function save(array $options = [], $skip_resize = false)
278279
public function copia($data)
279280
{
280281
$data['original_name'] = $this->original_name;
282+
$data['key'] = $this->key;
281283

282284
$adapter_config = FileAdapter::find($this->id_adapter);
283285
$class = $adapter_config->class;

src/Traits/Components/UploadTrait.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,14 @@ public function getUploadDirectoryAttribute()
4141
return $result;
4242
}
4343

44-
public function files($id_record)
44+
public function files($id_record, $include_images = false)
4545
{
46-
return $this->hasMany(Upload::class, $this->component_identifier)->where('id_record', $id_record)->get();
46+
$query = $this->hasMany(Upload::class, $this->component_identifier)->where('id_record', $id_record);
47+
48+
if (!$include_images) {
49+
$query->where('key', NULL);
50+
}
51+
52+
return $query->get();
4753
}
4854
}

0 commit comments

Comments
 (0)