Skip to content

Commit 515304f

Browse files
committed
feat: Aggiunta possibilità di allegare immagini alle checklist
1 parent e98264f commit 515304f

11 files changed

Lines changed: 192 additions & 41 deletions

File tree

modules/checklists/ajax.php

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
} else {
3434
$record = Check::find($id);
3535
}
36-
3736
$record->delete();
3837

3938
break;
@@ -96,6 +95,37 @@
9695
$record = Check::find($id_record);
9796
}
9897

98+
// Upload file
99+
if (!empty($_FILES) && !empty($_FILES['immagine']['name'])) {
100+
$upload = Uploads::upload($_FILES['immagine'], [
101+
'name' => 'Immagine',
102+
'id_category' => null,
103+
'id_module' => $record->id_module,
104+
'id_record' => $record->id_record,
105+
], [
106+
'thumbnails' => true,
107+
]);
108+
$id_immagine = $upload->id;
109+
110+
if (!empty($id_immagine)) {
111+
$record->id_immagine = $id_immagine;
112+
} else {
113+
flash()->warning(tr("Errore durante il caricamento dell'immagine!"));
114+
}
115+
}
116+
117+
// Eliminazione file
118+
if (!empty(post('delete_immagine'))) {
119+
$file = Models\Upload::find($record->id_immagine);
120+
121+
Uploads::delete($file->filename, [
122+
'id_module' => $record->id_module,
123+
'id_record' => $record->id_record,
124+
]);
125+
126+
$record->id_immagine = null;
127+
}
128+
99129
$record->content = post('content');
100130
$record->is_titolo = post('is_titolo');
101131
$record->save();

modules/checklists/components/edit-check.php

Lines changed: 48 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
include_once __DIR__.'/../../../core.php';
2121
use Modules\Checklists\Check;
2222
use Modules\Checklists\ChecklistItem;
23+
use Models\Upload;
24+
use Models\Module;
2325

2426
$id_record = get('id_record');
2527
$main_check = get('main_check');
@@ -31,46 +33,64 @@
3133
}
3234

3335
?>
36+
<form action="" method="post" id="edit_check-form" enctype="multipart/form-data">
37+
<input type="hidden" name="backto" value="record-edit">
38+
<input type="hidden" name="op" value="edit_check">
39+
<input type="hidden" name="id_record" value="<?php echo $id_record; ?>">
40+
<input type="hidden" name="main_check" value="<?php echo $main_check; ?>">
3441

35-
<div class="row">
36-
<div class="col-md-12">
37-
<?php
38-
echo input([
39-
'type' => 'ckeditor',
40-
'label' => tr('Descrizione'),
41-
'name' => 'content_edit',
42-
'required' => 1,
43-
'value' => htmlentities((string) $record->content),
44-
]);
45-
?>
42+
<div class="row">
43+
<div class="col-md-12">
44+
<?php
45+
echo input([
46+
'type' => 'ckeditor',
47+
'label' => tr('Descrizione'),
48+
'name' => 'content',
49+
'required' => 1,
50+
'value' => htmlentities((string) $record->content),
51+
]);
52+
?>
53+
</div>
4654
</div>
47-
</div>
4855

49-
<div class="row">
50-
<div class="col-md-4">
51-
{[ "type": "checkbox", "label": "<?php echo tr('Utilizza come titolo'); ?>", "name": "is_titolo", "value": "<?php echo $record->is_titolo; ?>" ]}
56+
<div class="row">
57+
<div class="col-md-4">
58+
{[ "type": "checkbox", "label": "<?php echo tr('Utilizza come titolo'); ?>", "name": "is_titolo", "value": "<?php echo $record->is_titolo; ?>" ]}
59+
</div>
60+
<div class="col-md-4">
61+
{[ "type": "image", "label": "<?php echo tr('Immagine');?>", "name": "immagine", "class": "img-thumbnail", "value": "<?php echo $record->image;?>", "accept": "image/x-png,image/gif,image/jpeg" ]}
62+
</div>
5263
</div>
53-
54-
<div class="col-md-8 text-right">
55-
<br><br><button type="button" class="btn btn-success" id="save-btn"><i class='fa fa-check'></i> <?php echo tr('Salva'); ?></button>
64+
<div class="row">
65+
<div class="col-md-12 text-right">
66+
<button type="button" class="btn btn-success" id="save-btn"><i class='fa fa-check'></i> <?php echo tr('Salva'); ?></button>
67+
</div>
5668
</div>
57-
</div>
69+
</form>
5870

5971
<script>
6072
init();
6173
$('#save-btn').click(function() {
6274
$('#save-btn').attr('disabled', true);
6375
$('#save-btn').html('<i class="fa fa-spinner fa-spin"></i> <?php echo tr('Salvataggio in corso...'); ?>');
6476

65-
$.post('<?php echo $rootdir; ?>/modules/checklists/ajax.php', {
66-
op: "edit_check",
67-
id_module: globals.id_module,
68-
id_record: "<?php echo $id_record; ?>",
69-
content: input('content_edit').get(),
70-
is_titolo: input('is_titolo').get(),
71-
main_check: "<?php echo $main_check; ?>",
72-
}, function(){
73-
location.reload();
77+
// Creare FormData per gestire i file
78+
var formData = new FormData(document.getElementById('edit_check-form'));
79+
80+
$.ajax({
81+
url: '<?php echo $rootdir; ?>/modules/checklists/ajax.php',
82+
type: 'POST',
83+
data: formData,
84+
processData: false,
85+
contentType: false,
86+
success: function(response) {
87+
location.reload();
88+
},
89+
error: function() {
90+
$('#save-btn').attr('disabled', false);
91+
$('#save-btn').html('<i class="fa fa-check"></i> <?php echo tr('Salva'); ?>');
92+
alert('<?php echo tr('Errore durante il salvataggio'); ?>');
93+
}
7494
});
7595
});
7696
</script>

modules/checklists/edit.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ function delete_check(id){
221221
}
222222
223223
function edit_check(id){
224-
launch_modal("Modifica checklist", "'.$checklist_module->fileurl('components/edit-check.php').'?id_record="+id+"&main_check=1", 1);
224+
launch_modal("Modifica checklist", "'.$checklist_module->fileurl('components/edit-check.php').'?id_module=" + globals.id_module + "&id_plugin=" + globals.id_plugin + "&id_record="+id+"&main_check=1", 1);
225225
}
226226
227227
</script>';

modules/checklists/modutil.php

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
*/
2020

2121
if (!function_exists('renderChecklist')) {
22-
function renderChecklist($check, $level = 1, $parent = 0)
22+
function renderChecklist($check, $level = 1, $parent = 0, $has_images = false)
2323
{
2424
global $structure;
2525

@@ -45,6 +45,20 @@ function renderChecklist($check, $level = 1, $parent = 0)
4545
<input type="checkbox" class="checkbox unblockable" data-id="'.$check->id.'" value="'.(!empty($check->checked_at) ? '1' : '0').'" '.(!empty($check->checked_at) ? 'checked' : '').' '.(!$enabled ? 'disabled' : '').'>
4646
</td>';
4747

48+
if( !empty($has_images) ){
49+
if( !empty($check->image) ){
50+
$result .= '
51+
<td class="text-center" style="width:100px;border-top:0px;vertical-align:middle;">
52+
<img src="'.$check->image.'" class="img-fluid" >
53+
</td>';
54+
}else{
55+
$result .= '
56+
<td class="text-center" style="width:100px;border-top:0px;vertical-align:middle;">
57+
<i class="fa fa-camera fa-2x text-muted"></i>
58+
</td>';
59+
}
60+
}
61+
4862
$result .= '
4963
<td style="border-top:0px;">
5064
<span class="text unblockable" style="'.(!empty($check->checked_at) ? 'text-decoration:line-through;' : '').'">'.$check->content.' </span>
@@ -189,7 +203,7 @@ function renderChecklistInserimento($check, $level = 1, $parent = 0)
189203
}
190204

191205
if (!function_exists('renderChecklistHtml')) {
192-
function renderChecklistHtml($check, $level = 0)
206+
function renderChecklistHtml($check, $level = 0, $has_images = false)
193207
{
194208
$user = auth()->getUser();
195209
$enabled = $check->assignedUsers ? $check->assignedUsers->pluck('id')->search($user->id) !== false : true;
@@ -201,8 +215,30 @@ function renderChecklistHtml($check, $level = 0)
201215
<td class="text-center" style="width:30px;">
202216
'.(!empty($check->checked_at) ? '<img src="'.ROOTDIR.'/templates/interventi/check.png" style="width:10px;">' : '').'
203217
</td>
204-
<td style="padding-left:'.$width.'px;">
205-
<span class="text"><b>'.$check->content.'</b>'.(!empty($check->value) ? ': '.$check->value : '').'</span>
218+
<td style="padding-left:'.$width.'px;">';
219+
220+
if( !empty($has_images) ){
221+
$result .= '
222+
<table class="table">
223+
<tr>
224+
<td width="10%" style="border:none;">';
225+
if( !empty($check->image) ){
226+
$result .= '
227+
<img src="'.$check->image.'" style="max-width: 10%;height: auto;vertical-align: middle;" >';
228+
}
229+
$result .= '
230+
</td>
231+
<td style="border:none;vertical-align:middle;">
232+
<span class="text"><b>'.$check->content.'</b>'.(!empty($check->value) ? ': '.$check->value : '').'</span>
233+
</td>
234+
</tr>
235+
</table>';
236+
}else{
237+
$result .= '
238+
<span class="text"><b>'.$check->content.'</b>'.(!empty($check->value) ? ': '.$check->value : '').'</span>';
239+
}
240+
241+
$result .= '
206242
</td>
207243
</tr>';
208244

modules/checklists/src/Check.php

Lines changed: 53 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
use Models\User;
2929
use Modules\Checklists\Traits\ChecklistTrait;
3030
use Traits\HierarchyTrait;
31+
use Models\Upload;
3132

3233
class Check extends Model
3334
{
@@ -138,7 +139,16 @@ public function delete()
138139
*/
139140
public static function deleteLinked($data)
140141
{
141-
database()->delete('zz_checks', $data);
142+
$record = Check::where('id_module', $data['id_module'])
143+
->where('id_module', $data['id_module'])
144+
->where('id_record', $data['id_record'])
145+
->where('id_module_from', $data['id_module_from'])
146+
->where('id_record_from', $data['id_record_from'])
147+
->get();
148+
149+
foreach($record as $r){
150+
$r->delete();
151+
};
142152
}
143153

144154
/* Relazioni Eloquent */
@@ -167,4 +177,46 @@ public function module()
167177
{
168178
return $this->belongsTo(Module::class, 'id_module');
169179
}
180+
181+
public function getImageAttribute()
182+
{
183+
if (empty($this->id_immagine)) {
184+
return null;
185+
}
186+
187+
$file = Upload::find($this->id_immagine);
188+
189+
$module = Module::where('id',$file->id_module)->first();
190+
$fileinfo = \Uploads::fileInfo($file->filename);
191+
192+
$directory = '/'.$module->upload_directory.'/';
193+
$image = $directory.$file->filename;
194+
$image_thumbnail = $directory.$fileinfo['filename'].'_thumb600.'.$fileinfo['extension'];
195+
196+
$url = file_exists(base_dir().$image_thumbnail) ? base_path().$image_thumbnail : base_path().$image;
197+
198+
return $url;
199+
}
200+
201+
public function delete()
202+
{
203+
if( !empty($this->id_immagine) ){
204+
//Se sono valorizzati id_module_from e id_record_from verifico l'id_immagine. Se non presente allora è stato inserito per la check e posso eliminare il file
205+
if( !empty($this->id_module_from) && !empty($this->id_record_from) ){
206+
$check = Check::where('id_module_from', $this->id_module_from)->where('id_record_from', $this->id_record_from)->where('id_immagine', $this->id_immagine)->where('id','!=', $this->id)->first();
207+
if( !empty($check) ){
208+
return parent::delete();
209+
}
210+
}
211+
212+
$file = Upload::find($this->id_immagine);
213+
214+
\Uploads::delete($file->filename, [
215+
'id_module' => $this->id_module,
216+
'id_record' => $this->id_record,
217+
]);
218+
}
219+
220+
return parent::delete();
221+
}
170222
}

modules/impianti/actions.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@
136136
$check = Check::build($user, $structure, $id_record, $check_categoria['content'], $id_parent_new, $check_categoria['is_titolo'], $check_categoria['order']);
137137
$check->id_plugin = null;
138138
$check->note = $check_categoria['note'];
139+
$check->id_immagine = $check_categoria['id_immagine'];
139140
$check->save();
140141

141142
// Riporto anche i permessi della check
@@ -216,6 +217,7 @@
216217
$check = Check::build($user, $structure, $id_record, $check_categoria['content'], $id_parent_new, $check_categoria['is_titolo'], $check_categoria['order']);
217218
$check->id_plugin = null;
218219
$check->note = $check_categoria['note'];
220+
$check->id_immagine = $check_categoria['id_immagine'];
219221
$check->save();
220222
}
221223
flash()->info(tr('Checklist importate correttamente!'));

plugins/checks.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,10 @@
6161

6262
echo " <table class='table table-sm'>
6363
<tbody class='sort' data-sonof='0'>";
64+
65+
$has_images = $checks->where('id_immagine','!=', null)->count();
6466
foreach ($checks as $check) {
65-
echo renderChecklist($check);
67+
echo renderChecklist($check, 1, 0, $has_images);
6668
}
6769
echo ' </tbody>
6870
</table>';
@@ -173,7 +175,7 @@ function delete_check(id){
173175
}
174176
175177
function edit_check(id){
176-
launch_modal("Modifica checklist", "'.$checklist_module->fileurl('components/edit-check.php').'?id_record="+id, 1);
178+
launch_modal("Modifica checklist", "'.$checklist_module->fileurl('components/edit-check.php').'?id_module='.$id_module.'&id_plugin='.$id_plugin.'&id_record="+id, 1);
177179
}
178180
179181
function reload(){

plugins/impianti_intervento/actions.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
$check->id_module = $id_module;
4343
$check->id_plugin = $id_plugin;
4444
$check->note = $check_impianto['note'];
45+
$check->id_immagine = $check_impianto['id_immagine'];
4546
$check->save();
4647

4748
// Riporto anche i permessi della check
@@ -106,8 +107,9 @@
106107
$checks = Check::where('id_module_from', $id_modulo_impianti)->where('id_record_from', post('id_impianto'))->where('id_module', $id_module)->where('id_record', $id_record)->where('id_parent', null)->get();
107108

108109
$response = '';
110+
$has_images = $checks->where('id_immagine','!=', null)->count();
109111
foreach ($checks as $check) {
110-
$response .= renderChecklist($check);
112+
$response .= renderChecklist($check, 1, 0, $has_images);
111113
}
112114

113115
/*echo json_encode([

plugins/impianti_intervento/row-impianti.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ function delete_check(id){
222222
}
223223
224224
function edit_check(id){
225-
launch_modal("Modifica checklist", "'.$checklist_module->fileurl('components/edit-check.php').'?id_record="+id, 1);
225+
launch_modal("Modifica checklist", "'.$checklist_module->fileurl('components/edit-check.php').'?id_module='.$id_module.'&id_plugin='.$id_plugin.'&id_record="+id, 1);
226226
}
227227
228228
function saveNota(id) {

templates/interventi/body.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -627,14 +627,16 @@
627627

628628
$structure = Module::where('name', 'Interventi')->first();
629629
$checks = $structure->mainChecks($id_record);
630+
$has_images = $checks->where('id_immagine','!=', null)->count();
630631

631632
foreach ($checks as $check) {
632-
echo renderChecklistHtml($check);
633+
echo renderChecklistHtml($check, 0, $has_images);
633634
}
634635

635636
$impianti_collegati = $dbo->fetchArray('SELECT * FROM my_impianti_interventi INNER JOIN my_impianti ON my_impianti_interventi.idimpianto = my_impianti.id WHERE idintervento = '.prepare($id_record));
636637
foreach ($impianti_collegati as $impianto) {
637638
$checks = Check::where('id_module_from', Module::where('name', 'Impianti')->first()->id)->where('id_record_from', $impianto['id'])->where('id_module', Module::where('name', 'Interventi')->first()->id)->where('id_record', $id_record)->where('id_parent', null)->get();
639+
$has_images = $checks->where('id_immagine','!=', null)->count();
638640

639641
if (sizeof($checks)) {
640642
echo '
@@ -644,7 +646,7 @@
644646
</th>
645647
</tr>';
646648
foreach ($checks as $check) {
647-
echo renderChecklistHtml($check);
649+
echo renderChecklistHtml($check, 0, $has_images);
648650
}
649651
}
650652
}

0 commit comments

Comments
 (0)