186 lines
6.7 KiB
PHP
186 lines
6.7 KiB
PHP
<?php
|
|
|
|
namespace Modules\Document\Http\Controllers;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Str;
|
|
use Modules\CCMS\Models\Country;
|
|
use Modules\Document\Models\Document;
|
|
use Modules\CCMS\Models\Service;
|
|
use Modules\CCMS\Models\Test;
|
|
use Modules\Document\Services\DocumentService;
|
|
use Yajra\DataTables\Facades\DataTables;
|
|
use Illuminate\Support\Facades\Validator;
|
|
use Illuminate\Support\Facades\File;
|
|
|
|
|
|
class DocumentController extends Controller
|
|
{
|
|
protected $documentService;
|
|
|
|
public function __construct(DocumentService $documentService)
|
|
{
|
|
$this->documentService = $documentService;
|
|
}
|
|
|
|
public function index()
|
|
{
|
|
$title = 'Upload Documents';
|
|
$countryOptions = Country::whereNull('parent_id')->pluck('title', 'id')->mapWithKeys(fn($title, $id) => ["Country:$id" => "Country - $title"]);
|
|
$serviceOptions = Service::whereNull('parent_id')->pluck('title', 'id')->mapWithKeys(fn($title, $id) => ["Service:$id" => "Service - $title"]);
|
|
$testOptions = Test::whereNull('parent_id')->pluck('title', 'id')->mapWithKeys(fn($title, $id) => ["Test:$id" => "Test - $title"]);
|
|
|
|
$modelOptions = $countryOptions->merge($serviceOptions)->merge($testOptions);
|
|
|
|
return view('document::document.index', compact('modelOptions', 'title'));
|
|
}
|
|
|
|
|
|
public function destroy($id)
|
|
{
|
|
try {
|
|
$document = Document::findOrFail($id);
|
|
if (File::exists('storage/' . $document->document_path)) {
|
|
File::delete('storage/' . $document->document_path);
|
|
}
|
|
|
|
$document->delete();
|
|
|
|
session()->flash('Document Deleted');
|
|
return response()->json(['status' => true, 'msg' => 'Document Deleted']);
|
|
} catch (\Throwable $th) {
|
|
session()->flash('error', $th->getMessage());
|
|
}
|
|
}
|
|
|
|
public function uploadDocument(Request $request)
|
|
{
|
|
$isNumeric = is_numeric($request->title);
|
|
|
|
$validator = Validator::make($request->all(), [
|
|
'title' => $isNumeric ? 'required|exists:required_documents,id' : 'required|string',
|
|
'model' => 'required',
|
|
'document' => 'required|array',
|
|
], [
|
|
'title.required' => 'Document title is required!',
|
|
'title.exists' => 'Document does not exists in record!',
|
|
'document' => 'Please upload document first!',
|
|
'model' => 'Please Select Category'
|
|
]);
|
|
|
|
if ($validator->fails()) {
|
|
return response()->json(['status' => 422, 'errors' => $validator->errors()], 422);
|
|
}
|
|
|
|
try {
|
|
|
|
$modelValues = explode(':', $request->model);
|
|
$modelClass = "Modules\\CCMS\\Models\\{$modelValues[0]}";
|
|
|
|
$model = $modelClass::findOrFail($modelValues[1]);
|
|
|
|
|
|
foreach ($request->input('document', []) as $file) {
|
|
$model->addToDocumentCollection(collectionName: 'uploads/document', file: $file, documentName: $request->title, referenceDocumentId: null);
|
|
}
|
|
|
|
return response()->json([
|
|
'status' => true,
|
|
'msg' => 'Document has been uploaded!',
|
|
'view' => view('document::document.partials.table', [
|
|
'model' => $model,
|
|
])->render(),
|
|
], 200);
|
|
} catch (\Throwable $th) {
|
|
return response()->json([
|
|
'status' => false,
|
|
'msg' => $th->getMessage(),
|
|
], 500);
|
|
}
|
|
}
|
|
|
|
public function store(Request $request)
|
|
{
|
|
$path = storage_path('tmp/uploads');
|
|
|
|
if (!file_exists($path)) {
|
|
mkdir($path, 0777, true);
|
|
}
|
|
|
|
$file = $request->file('file');
|
|
|
|
$name = uniqid() . '_' . trim($file->getClientOriginalName());
|
|
|
|
$file->move($path, $name);
|
|
|
|
return response()->json([
|
|
'name' => $name,
|
|
'original_name' => $file->getClientOriginalName(),
|
|
]);
|
|
}
|
|
|
|
public function getAllDocuments()
|
|
{
|
|
$model = Document::query()->latest();
|
|
return DataTables::eloquent($model)
|
|
->setRowClass('tableRow')
|
|
->addColumn('name', function (Document $document) {
|
|
$extension = $document->getExtension();
|
|
$assetUrl = $document->getUrl();
|
|
$html = $document->isImageFile()
|
|
? "<div class='flex-shrink-0'>
|
|
<div class='avatar-sm bg-light rounded p-1'>
|
|
<a href='{$assetUrl}' data-fancybox='gallery' data-caption='{$document->title}'>
|
|
<img src='{$assetUrl}' alt='' class='avatar-sm img-fluid d-block'>
|
|
</a>
|
|
</div>
|
|
</div>"
|
|
: "<div class='flex-shrink-0'>
|
|
<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>
|
|
</div>";
|
|
|
|
return "<div class='d-flex align-items-center'>
|
|
{$html} <div class='flex-grow-1 ms-3'>
|
|
<h6 class='fs-12 mb-0'>{$document->title}</h6>
|
|
</div>
|
|
</div>";
|
|
})
|
|
->addColumn('type', function (Document $document) {
|
|
return $document->getExtension();
|
|
})
|
|
->addColumn('size', function (Document $document) {
|
|
return $document->getSize();
|
|
})
|
|
->editColumn('created_at', function (Document $document) {
|
|
return getFormatted($document->created_at);
|
|
})
|
|
->addColumn('action', function (Document $document) {
|
|
return view('document::document.partials.action', ['document' => $document]);
|
|
})
|
|
->rawColumns(['action', 'name', 'size'])
|
|
->toJson();
|
|
}
|
|
|
|
public function reorder(Request $request)
|
|
{
|
|
Document::chunkById(100, function (Collection $documents) use ($request) {
|
|
foreach ($documents as $document) {
|
|
foreach ($request->order as $order) {
|
|
if ($order['id'] == $document->id) {
|
|
$document->update(['order' => $order['position']]);
|
|
}
|
|
}
|
|
}
|
|
});
|
|
|
|
return response(['status' => true, 'message' => 'Reordered successfully'], 200);
|
|
}
|
|
}
|