feat: Enhance document upload functionality and improve resource template display

This commit is contained in:
2025-08-03 12:10:14 +05:45
parent a3b863970e
commit 5e4cb2767c
4 changed files with 148 additions and 100 deletions

View File

@@ -12,6 +12,7 @@ use Modules\CCMS\Models\Test;
use Modules\Document\Services\DocumentService; use Modules\Document\Services\DocumentService;
use Yajra\DataTables\Facades\DataTables; use Yajra\DataTables\Facades\DataTables;
use Illuminate\Support\Facades\Validator; use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\File;
class DocumentController extends Controller class DocumentController extends Controller
@@ -48,11 +49,9 @@ class DocumentController extends Controller
session()->flash('Document Deleted'); session()->flash('Document Deleted');
return response()->json(['status' => true, 'msg' => 'Document Deleted']); return response()->json(['status' => true, 'msg' => 'Document Deleted']);
} catch (\Throwable $th) { } catch (\Throwable $th) {
session()->flash('error', $th->getMessage()); session()->flash('error', $th->getMessage());
} }
} }
public function uploadDocument(Request $request) public function uploadDocument(Request $request)
@@ -89,9 +88,9 @@ class DocumentController extends Controller
return response()->json([ return response()->json([
'status' => true, 'status' => true,
'msg' => 'Document has been uploaded!', 'msg' => 'Document has been uploaded!',
// 'view' => view('student::student.section.document.table', [ 'view' => view('document::document.partials.table', [
// 'student' => $student, 'model' => $model,
// ])->render(), ])->render(),
], 200); ], 200);
} catch (\Throwable $th) { } catch (\Throwable $th) {
return response()->json([ return response()->json([
@@ -101,7 +100,7 @@ class DocumentController extends Controller
} }
} }
public function store(Request $request) public function store(Request $request)
{ {
$path = storage_path('tmp/uploads'); $path = storage_path('tmp/uploads');

View File

@@ -2,113 +2,110 @@
<div class="row"> <div class="row">
<div class="col-sm-12"> <div class="col-sm-12">
{{ html()->form('POST', route('documents.upload'))->class(['needs-validation'])->attributes(['novalidate', 'enctype' => 'multipart/form-data', 'id' => 'documentForm'])->open() }} {{ html()->form('POST', route('documents.upload'))->class(['needs-validation'])->attributes(['novalidate', 'enctype' => 'multipart/form-data', 'id' => 'documentForm'])->open() }}
<div class="mb-3"> <div class="mb-3">
{{ html()->label('Title')->for('title') }} {{ html()->label('Title')->for('title') }}
{{ html()->span('*')->class('text-danger') }} {{ html()->span('*')->class('text-danger') }}
{{ html()->text('title')->id('docTitle')->class('form-control')->placeholder('Enter Title')->required() }} {{ html()->text('title')->id('docTitle')->class('form-control')->placeholder('Enter Title')->required() }}
</div>
<div class="mb-3">
{{ html()->label('Select Model')->class('form-label')->for('model') }}
{{ html()->span('*')->class('text-danger') }}
{{ html()->select('model')->id('modelSelect')->class('form-select select2')->required()->options(['' => '-- Select --'] + $modelOptions->toArray()) }}
</div>
<div class="mb-3">
<x-document::file-upload :uploadUrl="route('documents.store')" dropzoneId="document-dropzone" formId="documentForm"
message="Upload Document (PDF or Images Only)" inputName="document" />
</div>
<div class="col-sm-12">
<div class="hstack justify-content-end gap-2">
<button type="submit" class="btn btn-primary upload-btn"
href="{{ route('documents.index') }}">Upload</button>
</div> </div>
</div>
<div class="mb-3"> {{ html()->form()->close() }}
{{ html()->label('Select Model')->class('form-label')->for('model') }}
{{ html()->span('*')->class('text-danger') }}
{{ html()->select('model')->id('modelSelect')->class('form-select select2')->required()->options(['' => '-- Select --'] + $modelOptions->toArray()) }}
</div>
<div class="mb-3">
<x-document::file-upload :uploadUrl="route('documents.store')" dropzoneId="document-dropzone"
formId="documentForm" message="Upload Document (PDF or Images Only)"
inputName="document" />
</div>
<div class="col-sm-12">
<div class="hstack justify-content-end gap-2">
<button type="submit" class="btn btn-primary upload-btn">Upload</button>
</div>
</div>
{{ html()->form()->close() }}
</div> </div>
</div> </div>
</div> </div>
@pushOnce('js')
<script>
function clearDropzone(dropzoneIds = [], formId = '', fileFieldNames = []) {
dropzoneIds.forEach(function(id) {
const dropzoneElement = Dropzone.forElement(`#${id}`);
if (dropzoneElement) {
dropzoneElement.removeAllFiles(true);
}
});
fileFieldNames.forEach(function(field) { @pushOnce('js')
$(`#${formId} input[name="${field}[]"]`).remove(); <script>
}); function clearDropzone(dropzoneIds = [], formId = '', fileFieldNames = []) {
} dropzoneIds.forEach(function(id) {
</script> const dropzoneElement = Dropzone.forElement(`#${id}`);
if (dropzoneElement) {
dropzoneElement.removeAllFiles(true);
}
});
<script> fileFieldNames.forEach(function(field) {
$(document).on('submit', '#documentForm', function(event) { $(`#${formId} input[name="${field}[]"]`).remove();
event.preventDefault(); });
}
</script>
const url = $(this).attr('action'); <script>
const formData = new FormData(this); $(document).on('submit', '#documentForm', function(event) {
const button = $(this).find('button[type="submit"]'); event.preventDefault();
$.ajax({ const url = $(this).attr('action');
type: "POST", const formData = new FormData(this);
url: url, const button = $(this).find('button[type="submit"]');
data: formData,
processData: false,
contentType: false,
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content'),
},
beforeSend: () => {
button.text('Uploading...').prop('disabled', true);
},
success: (response) => {
if (response.status == true) {
$('#documentForm')[0].reset();
// if (DataTable.isDataTable("#documents-table")) { $.ajax({
// $("#documents-table").DataTable().ajax.reload(); type: "POST",
// } else { url: url,
// target.html(response.view); data: formData,
// } processData: false,
contentType: false,
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content'),
},
beforeSend: () => {
button.text('Uploading...').prop('disabled', true);
},
success: (response) => {
if (response.status == true) {
location.reload();
// $('#documentForm')[0].reset();
clearDropzone(dropzoneIds = ["document-dropzone"], // $("#documents-table").DataTable().ajax.reload();
"documentForm", ["document"]);
button.text('Upload').prop('disabled', false); // clearDropzone(dropzoneIds = ["document-dropzone"],
toastr.success(response.msg); // "documentForm", ["document"]);
} // button.text('Upload').prop('disabled', false);
}, // toastr.success(response.msg);
error: (xhr) => {
if (xhr.status === 422) {
const errors = xhr.responseJSON.errors;
$('.error-message').remove();
for (const [key, value] of Object.entries(errors)) { }
const errorMessage = $( },
'<p class="error-message text-danger mt-2"></p>').text( error: (xhr) => {
value[0]); if (xhr.status === 422) {
$(`#${key}`).after(errorMessage); const errors = xhr.responseJSON.errors;
} $('.error-message').remove();
} else {
console.error(xhr); for (const [key, value] of Object.entries(errors)) {
} const errorMessage = $(
}, '<p class="error-message text-danger mt-2"></p>').text(
complete: () => { value[0]);
button.text('Upload'); $(`#${key}`).after(errorMessage);
button.prop('disabled', true); }
} } else {
}); console.error(xhr);
}); }
</script> },
@endPushOnce complete: () => {
button.text('Upload');
// button.prop('disabled', true);
}
});
});
</script>
@endPushOnce

View File

@@ -0,0 +1,52 @@
@foreach ($model->documents as $document)
@php
$extension = $document->getExtension();
$assetUrl = $document->getUrl();
$fileSize = $document->getSize();
@endphp
<tr>
<td>
<input type="checkbox" class="lead-select" value="{{ $document->id }}"
data-name="{{ $document->title }}">
</td>
<td>
<div class="d-flex align-items-center">
@if ($document->isImageFile())
<div class="avatar-sm bg-light rounded p-1">
<a href="{{ $assetUrl }}" data-fancybox="gallery"
data-caption="{{ $document->title }}">
<img src="{{ $assetUrl }}" alt="" class="img-fluid d-block">
</a>
</div>
@else
<div class="avatar-sm">
<a href="{{ $assetUrl }}" data-fancybox="gallery"
data-caption="{{ $document->title }}">
<div
class="avatar-title bg-{{ getFileIcon($extension)[1] }}-subtle text-{{ getFileIcon($extension)[1] }} fs-20 material-shadow rounded">
<i class="{{ getFileIcon($extension)[0] }}"></i>
</div>
</a>
</div>
@endif
<div class="flex-grow-1 ms-3">
<h6 class="fs-12 mb-0">{{ $document->title }}</h6>
</div>
</div>
</td>
<td>{{ $extension }}</td>
{{-- <td>{{ $fileSize }}</td> --}}
<td>{{ $document->created_at?->format('d M, y') }}</td>
<td>
<div class="hstack flex-wrap gap-3">
<a href="{{ $assetUrl }}" class="link-primary fs-15" download><i
class="ri-download-2-line"></i></a>
<a href="javascript:void(0);" data-link="{{ route('documents.destroy', $document->id) }}"
data-id="{{ $document->id }}" class="link-danger fs-15 remove-item-btn"><i
class="ri-delete-bin-line"></i></a>
</div>
</td>
</tr>
@endforeach

View File

@@ -266,7 +266,7 @@
@endphp @endphp
<tr> <tr>
<td class="pl-20 text-brand text-16">{{$docss->index + 1}}</td> <td class="pl-20 text-brand text-16">{{$loop->index + 1}}</td>
<td class="text-brand text-16">{{$docss->title}}</td> <td class="text-brand text-16">{{$docss->title}}</td>
<td class=" flex items-center gap-10 justify-end"> <td class=" flex items-center gap-10 justify-end">
<a href="{{ $full_pathss }}" class="link-primary fs-15" download> <a href="{{ $full_pathss }}" class="link-primary fs-15" download>