HasAllegato.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. namespace App\Http\Livewire\Traits;
  3. use App\Models\AllegatiGalleryType;
  4. use App\Models\ReportAllegatiGallery;
  5. use Livewire\WithFileUploads;
  6. trait HasAllegato
  7. {
  8. public $allegatoId = 0;
  9. public $allegatoType = 0;
  10. public $allegatoGallery = 0;
  11. public $allegatoName = '';
  12. public $allegatoVisible = false;
  13. public $allegatoFiles = '';
  14. public $allegatiFiles = [];
  15. public $allegatiImmagini;
  16. public $allegatiDocumenti;
  17. public $allegati = [];
  18. public function getAllegatoType($type)
  19. {
  20. if ($type > 0) {
  21. $ret = AllegatiGalleryType::findOrFail($type);
  22. return $ret->name;
  23. }
  24. return "";
  25. }
  26. public function addAllegato($type)
  27. {
  28. $this->allegatoId = 0;
  29. $this->allegatoType = $type;
  30. $this->allegatoGallery = 0;
  31. $this->allegatoName = '';
  32. $this->allegatoVisible = false;
  33. $this->allegatoFiles = '';
  34. $this->allegatiFiles = [];
  35. $this->emit('attachments', "");
  36. }
  37. public function saveAllegato()
  38. {
  39. $files = implode("|", $this->allegatiFiles);
  40. if ($this->allegatoId > 0) {
  41. ReportAllegatiGallery::where('id', $this->allegatoId)->update([
  42. 'file_type' => $this->allegatoType,
  43. 'gallery_type' => $this->allegatoGallery,
  44. 'name' => $this->allegatoName,
  45. 'is_visible' => $this->allegatoVisible,
  46. 'files' => $files,
  47. 'state' => 0
  48. ]);
  49. } else {
  50. ReportAllegatiGallery::create([
  51. 'report_id' => $this->dataId,
  52. 'file_type' => $this->allegatoType,
  53. 'gallery_type' => $this->allegatoGallery,
  54. 'name' => $this->allegatoName,
  55. 'is_visible' => $this->allegatoVisible,
  56. 'files' => $files,
  57. 'state' => 0,
  58. 'created' => date("Y-m-d H:i:s"),
  59. 'created_by' => 0
  60. ]);
  61. }
  62. $this->resetAllegatoFields();
  63. $this->refreshAllegatiCollections();
  64. $this->emit('close-modal');
  65. }
  66. private function resetAllegatoFields()
  67. {
  68. $this->allegatoId = 0;
  69. $this->allegatoGallery = 0;
  70. $this->allegatoName = '';
  71. $this->allegatoVisible = false;
  72. $this->allegatoFiles = '';
  73. $this->allegatiFiles = [];
  74. $this->allegatoType = 0;
  75. }
  76. private function refreshAllegatiCollections()
  77. {
  78. $this->allegatiImmagini = ReportAllegatiGallery::where('report_id', $this->dataId)
  79. ->where('file_type', 0)
  80. ->orderBy('name')
  81. ->get();
  82. $this->allegatiDocumenti = ReportAllegatiGallery::where('report_id', $this->dataId)
  83. ->where('file_type', 1)
  84. ->orderBy('name')
  85. ->get();
  86. }
  87. public function editAllegato($id)
  88. {
  89. $this->emit('attachments', "");
  90. $a = ReportAllegatiGallery::where('id', $id)->first();
  91. if ($a != null) {
  92. $this->allegatoId = $id;
  93. $this->allegatoType = $a->file_type;
  94. $this->allegatoGallery = $a->gallery_type;
  95. $this->allegatoName = $a->name;
  96. $this->allegatoFiles = $a->files;
  97. $this->allegatiFiles = explode("|", $this->allegatoFiles);
  98. $this->emit('attachments', $this->allegatoFiles);
  99. }
  100. }
  101. public function removeAllegato($id)
  102. {
  103. ReportAllegatiGallery::findOrFail($id)->delete();
  104. $this->refreshAllegatiCollections();
  105. }
  106. public function updatedAllegati()
  107. {
  108. foreach ($this->allegati as $allegato) {
  109. $name = $allegato->getClientOriginalName();
  110. $allegato->storeAs('public', $name);
  111. $this->allegatiFiles[] = $name;
  112. }
  113. $this->emit('attachments', implode("|", $this->allegatiFiles));
  114. $this->allegati = [];
  115. }
  116. }