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 | {
|
---|
17 | public function index(Request $request)
|
---|
18 | {
|
---|
19 | if ($request->query('id')) {
|
---|
20 | $documents = Document::where('department_id', $request->query('id'))->get();
|
---|
21 | } else {
|
---|
22 | $documents = Document::all();
|
---|
23 | }
|
---|
24 |
|
---|
25 | $departments = Department::all();
|
---|
26 |
|
---|
27 | return view("dashboard.documents.index")->with([
|
---|
28 | "documents" => $documents,
|
---|
29 | "currentUser" => auth()->user(),
|
---|
30 | "departments" => $departments,
|
---|
31 | "docsCount" => Department::withCount('document')->get(),
|
---|
32 | 'totalDocs' => Document::all()->count()
|
---|
33 | ]);
|
---|
34 |
|
---|
35 | }
|
---|
36 |
|
---|
37 | public function create()
|
---|
38 | {
|
---|
39 | return view("dashboard.documents.create")->with([
|
---|
40 | "departments" => Department::all()
|
---|
41 | ]);
|
---|
42 | }
|
---|
43 |
|
---|
44 | public function editShow($id)
|
---|
45 | {
|
---|
46 | // if (!auth()->user()->hasPermission("edit_all_documents")) {
|
---|
47 | // return redirect()->route("dashboard.documents.index");
|
---|
48 | // }
|
---|
49 |
|
---|
50 | return view("dashboard.documents.edit")->with([
|
---|
51 | "document" => Document::findOrFail($id),
|
---|
52 | "departments" => Department::all()
|
---|
53 | ]);
|
---|
54 | }
|
---|
55 |
|
---|
56 | public function edit(DocumentRequest $request, $id)
|
---|
57 | {
|
---|
58 | $document = Document::findOrFail($id);
|
---|
59 |
|
---|
60 | $department = Department::find($request->department);
|
---|
61 |
|
---|
62 | $document->department()->associate($department);
|
---|
63 |
|
---|
64 | $document->name = $request->name;
|
---|
65 | $document->arch_id = $request->arch_id;
|
---|
66 | $document->description = $request->description;
|
---|
67 |
|
---|
68 | $document->save();
|
---|
69 |
|
---|
70 | Alert::flash("Document edited successfully");
|
---|
71 |
|
---|
72 | return redirect()->route("dashboard.documents.index");
|
---|
73 | }
|
---|
74 |
|
---|
75 | public function store(DocumentRequest $request, UploadService $uploadService)
|
---|
76 | {
|
---|
77 | $document = new Document();
|
---|
78 | $user = auth()->user();
|
---|
79 | $department = Department::find($request->department);
|
---|
80 |
|
---|
81 | $document->user()->associate($user);
|
---|
82 | $document->department()->associate($department);
|
---|
83 |
|
---|
84 | $document->arch_id = $request->arch_id;
|
---|
85 | $document->name = $request->name;
|
---|
86 | $document->description = $request->description;
|
---|
87 |
|
---|
88 | if (!Storage::disk('local')->has($document->department()->pluck('location')->join("") . '/' . $request->arch_id)) {
|
---|
89 | Storage::disk('local')->makeDirectory($document->department()->pluck('location')->join("") . '/' . $request->arch_id);
|
---|
90 | }
|
---|
91 |
|
---|
92 | $documentFile = $uploadService->upload(File::class, [
|
---|
93 | "file_item" => $request->file_item,
|
---|
94 | ], "link", true);
|
---|
95 |
|
---|
96 | $document->save();
|
---|
97 |
|
---|
98 | foreach ($documentFile as $df) {
|
---|
99 | $file = File::find($df);
|
---|
100 | $file->document()->associate($document);
|
---|
101 | $file->save();
|
---|
102 | }
|
---|
103 |
|
---|
104 | Alert::flash("New document created successfully");
|
---|
105 |
|
---|
106 | return redirect()->route("dashboard.documents.index");
|
---|
107 | }
|
---|
108 |
|
---|
109 | }
|
---|