- Added DocumentController for handling document uploads and management. - Created Document model with necessary attributes and relationships. - Implemented DocumentService for business logic related to documents. - Set up routes for document management in both web and API contexts. - Developed views for document upload using Dropzone for file handling. - Included necessary assets and styles for the Document module. - Created migration for documents table with appropriate fields. - Added configuration and service provider for the Document module.
72 lines
2.3 KiB
PHP
72 lines
2.3 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\CCMS\Models\Service;
|
|
use Modules\CCMS\Models\Test;
|
|
use Modules\Document\Services\DocumentService;
|
|
use Yajra\DataTables\Facades\DataTables;
|
|
|
|
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 dropzoneUpload(Request $request)
|
|
{
|
|
$request->validate([
|
|
'model' => 'required|string',
|
|
'file' => 'required|array',
|
|
'file.*' => 'file|mimes:pdf,doc,docx,jpg,png|max:5120',
|
|
'title' => 'nullable|string',
|
|
]);
|
|
|
|
$parts = explode(':', $request->model);
|
|
|
|
if (count($parts) !== 2) {
|
|
return response()->json(['error' => 'Invalid model format.'], 422);
|
|
}
|
|
|
|
[$modelType, $modelId] = $parts;
|
|
$modelClass = "App\\Models\\$modelType";
|
|
|
|
if (!class_exists($modelClass)) {
|
|
return response()->json(['error' => 'Invalid model selected.'], 422);
|
|
}
|
|
|
|
$model = $modelClass::findOrFail($modelId);
|
|
|
|
foreach ($request->file('file') as $uploadedFile) {
|
|
$path = $uploadedFile->store('documents');
|
|
|
|
$model->documents()->create([
|
|
'title' => $request->title ?? 'Untitled',
|
|
'file_path' => $path,
|
|
]);
|
|
}
|
|
|
|
return response()->json(['success' => 'Files uploaded successfully']);
|
|
}
|
|
}
|