[24a616f] | 1 | <?php
|
---|
| 2 |
|
---|
| 3 | namespace App\Http\Controllers\Dashboard;
|
---|
| 4 |
|
---|
| 5 | use App\Helpers\Alert;
|
---|
| 6 | use App\Http\Requests\Dashboard\DocumentRequest;
|
---|
| 7 | use App\Models\Department;
|
---|
| 8 | use App\Models\Document;
|
---|
| 9 | use App\Models\File;
|
---|
| 10 | use App\Services\UploadService;
|
---|
| 11 | use Illuminate\Http\Request;
|
---|
| 12 | use Illuminate\Support\Facades\Storage;
|
---|
| 13 | use App\Http\Controllers\Controller;
|
---|
| 14 |
|
---|
| 15 | class DocumentsController extends Controller
|
---|
| 16 | {
|
---|
[d795fa6] | 17 | public function index(Request $request)
|
---|
[24a616f] | 18 | {
|
---|
[d795fa6] | 19 | if($request->department) {
|
---|
| 20 | $documents = Document::with('department')->where('department', function ($query) {
|
---|
| 21 | $query->where('id', $request->department)->get();
|
---|
| 22 | });
|
---|
| 23 | }
|
---|
| 24 | else{
|
---|
[24a616f] | 25 | $documents = Document::all();
|
---|
[d795fa6] | 26 | }
|
---|
| 27 |
|
---|
| 28 | $departments = Department::all();
|
---|
[24a616f] | 29 |
|
---|
| 30 | return view("dashboard.documents.index")->with([
|
---|
| 31 | "documents" => $documents,
|
---|
| 32 | "currentUser" => auth()->user(),
|
---|
[d795fa6] | 33 | "departments" => $departments,
|
---|
[24a616f] | 34 | "docsCount" => Department::withCount('document')->get(),
|
---|
| 35 | 'totalDocs' => Document::all()->count()
|
---|
| 36 | ]);
|
---|
[d795fa6] | 37 |
|
---|
[24a616f] | 38 | }
|
---|
| 39 |
|
---|
| 40 | public function create()
|
---|
| 41 | {
|
---|
| 42 | return view("dashboard.documents.create")->with([
|
---|
| 43 | "departments" => Department::all()
|
---|
| 44 | ]);
|
---|
| 45 | }
|
---|
| 46 |
|
---|
| 47 | public function editShow($id)
|
---|
| 48 | {
|
---|
| 49 | // if (!auth()->user()->hasPermission("edit_all_documents")) {
|
---|
| 50 | // return redirect()->route("dashboard.documents.index");
|
---|
| 51 | // }
|
---|
| 52 |
|
---|
| 53 | return view("dashboard.documents.edit")->with([
|
---|
| 54 | "document" => Document::findOrFail($id),
|
---|
| 55 | "departments" => Department::all()
|
---|
| 56 | ]);
|
---|
| 57 | }
|
---|
| 58 |
|
---|
| 59 | public function edit(DocumentRequest $request, $id)
|
---|
| 60 | {
|
---|
| 61 | $document = Document::findOrFail($id);
|
---|
| 62 |
|
---|
| 63 | $department = Department::find($request->department);
|
---|
| 64 |
|
---|
| 65 | $document->department()->associate($department);
|
---|
| 66 |
|
---|
| 67 | $document->name = $request->name;
|
---|
| 68 | $document->arch_id = $request->arch_id;
|
---|
| 69 | $document->description = $request->description;
|
---|
[d795fa6] | 70 | $document->updated_at = $request->Carbon::now();;
|
---|
[24a616f] | 71 |
|
---|
| 72 | $document->save();
|
---|
| 73 |
|
---|
| 74 | Alert::flash("Document edited successfully");
|
---|
| 75 |
|
---|
| 76 | return redirect()->route("dashboard.documents.index");
|
---|
| 77 | }
|
---|
| 78 |
|
---|
| 79 | public function store(DocumentRequest $request, UploadService $uploadService)
|
---|
| 80 | {
|
---|
| 81 | $document = new Document();
|
---|
| 82 | $user = auth()->user();
|
---|
| 83 | $department = Department::find($request->department);
|
---|
| 84 |
|
---|
| 85 | $document->user()->associate($user);
|
---|
| 86 | $document->department()->associate($department);
|
---|
| 87 |
|
---|
| 88 | $document->arch_id = $request->arch_id;
|
---|
| 89 | $document->name = $request->name;
|
---|
| 90 | $document->description = $request->description;
|
---|
| 91 |
|
---|
| 92 | if(!Storage::disk('local')->has($document->department()->pluck('location')->join("") . '/' . $request->arch_id)){
|
---|
| 93 | Storage::disk('local')->makeDirectory($document->department()->pluck('location')->join("") . '/' . $request->arch_id);
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 | $documentFile = $uploadService->upload(File::class, [
|
---|
| 97 | "file_item" => $request->file_item,
|
---|
| 98 | ], "link", true);
|
---|
| 99 |
|
---|
| 100 | $document->save();
|
---|
| 101 |
|
---|
| 102 | foreach ($documentFile as $df) {
|
---|
| 103 | $file = File::find($df);
|
---|
| 104 | $file->document()->associate($document);
|
---|
| 105 | $file->save();
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 | Alert::flash("New document created successfully");
|
---|
| 109 |
|
---|
| 110 | return redirect()->route("dashboard.documents.index");
|
---|
| 111 | }
|
---|
| 112 |
|
---|
| 113 | }
|
---|