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