source: app/Http/Controllers/Dashboard/DocumentsController.php@ 24a616f

develop
Last change on this file since 24a616f was 24a616f, checked in by Berat Kjufliju <kufliju@…>, 3 years ago

added documents crud, added last_seen_to_user, edited views

  • Property mode set to 100644
File size: 3.8 KB
Line 
1<?php
2
3namespace App\Http\Controllers\Dashboard;
4
5use App\Helpers\Alert;
6use App\Http\Requests\Dashboard\DocumentRequest;
7use App\Models\Department;
8use App\Models\Document;
9use App\Models\File;
10use App\Models\Files;
11use App\Models\User;
12use App\Notifications\NewDocumentCreated;
13use App\Services\UploadService;
14use Illuminate\Http\Request;
15use Illuminate\Support\Facades\Storage;
16use Mews\Purifier\Facades\Purifier;
17use App\Http\Controllers\Controller;
18use Illuminate\Support\Facades\Notification;
19
20class 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}
Note: See TracBrowser for help on using the repository browser.