source: app/Http/Controllers/Dashboard/DocumentsController.php@ e6c1f87

develop
Last change on this file since e6c1f87 was e6c1f87, checked in by beratkjufliju <kufliju@…>, 3 years ago

added pagination, is_important

  • Property mode set to 100644
File size: 7.0 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\Services\UploadService;
11use Illuminate\Http\Request;
12use Illuminate\Support\Facades\Storage;
13use App\Http\Controllers\Controller;
14
15class DocumentsController extends Controller
16{
17 public function index(Request $request)
18 {
19 $queries = explode(" ", $request->search);
20 $result = collect();
21
22 foreach ($queries as $query) {
23 $result->push(Document::where("arch_id", "like", "%{$query}%")->orWhere("name", "like", "%{$query}%")->get());
24 }
25
26 $result = $result->flatten();
27
28
29 if ($request->query('id')) {
30
31 $documentsInDeptSort = Document::with('department')->when($request->has('id'), function($query) use ($request) {
32 $query->where('department_id', $request->query('id'));
33 });
34
35 if($request->query('sort') == 'newest') {
36 $documents = $documentsInDeptSort->orderBy('created_at', 'desc')->paginate(20);
37 }
38 else if($request->query('sort') == 'name') {
39 $documents = $documentsInDeptSort->orderBy('name', 'asc')->paginate(20);
40 }
41 else{
42 $documents = Document::where('department_id', $request->query('id'))->paginate(20);
43 }
44 }
45 else {
46 if($request->query('sort') == 'newest') {
47 $documents = Document::orderBy('created_at', 'desc')->paginate(20);
48 }
49 else if($request->query('sort') == 'name') {
50 $documents = Document::orderBy('name', 'asc')->paginate(20);
51 }
52 else if($request->query('sort') == 'important'){
53 $documents = Document::where('is_important', true)->paginate(20);
54 }
55 else if($request->query('sort') == 'recent') {
56 $documents = Document::orderBy('created_at', 'desc')->paginate(20);
57 }
58 else if($request->query('search')){
59 $result = collect();
60
61 foreach ($queries as $query) {
62 $result->push(Document::where("arch_id", "like", "%{$query}%")->orWhere("name", "like", "%{$query}%")->get());
63 }
64 $result = $result->flatten();
65 $documents = $result;
66 }
67 else
68 {
69 $documents = Document::paginate(20);
70 }
71 }
72
73 $departments = Department::all();
74
75 $diskTotal = disk_total_space('/');
76 $diskTotalSize = $diskTotal / 1073741824;
77
78 $diskFree = disk_free_space('/');
79 $used = $diskTotal - $diskFree;
80
81 $diskUsedSize = $used / 1073741824;
82 $diskUse1 = round(100 - (($diskUsedSize / $diskTotalSize) * 100));
83 $diskUse = round(100 - ($diskUse1)) . '%';
84
85 return view("dashboard.documents.index")->with([
86 "documents" => $documents,
87 "currentUser" => auth()->user(),
88 "departments" => $departments,
89 "docsCount" => Department::withCount('document')->get(),
90 "totalDocs" => Document::all()->count(),
91 "countImportant" => Document::where('is_important', true)->get()->count(),
92 "diskTotal" => $diskTotal,
93 "diskTotalSize" => $diskTotalSize,
94 "diskUse" => $diskUse,
95 "diskUsedSize" => $diskUsedSize
96
97 ]);
98
99 }
100
101 public function create()
102 {
103 return view("dashboard.documents.create")->with([
104 "departments" => Department::all()
105 ]);
106 }
107
108 public function store(DocumentRequest $request, UploadService $uploadService)
109 {
110 $document = new Document();
111 $user = auth()->user();
112 $department = Department::find($request->department);
113
114 $document->user()->associate($user);
115 $document->department()->associate($department);
116
117 $document->arch_id = $request->arch_id;
118 $document->name = $request->name;
119 $document->description = $request->description;
120
121 if (!Storage::disk('local')->has($document->department()->pluck('location')->join("") . '/' . $request->arch_id)) {
122 Storage::disk('local')->makeDirectory($document->department()->pluck('location')->join("") . '/' . $request->arch_id);
123 }
124
125 $documentFile = $uploadService->upload(File::class, [
126 "file_item" => $request->file_item,
127 ], "link", true);
128
129 $document->save();
130
131 foreach ($documentFile as $df) {
132 $file = File::find($df);
133 $file->document()->associate($document);
134 $file->save();
135 }
136
137 Alert::flash("New document created successfully");
138
139 return redirect()->route("dashboard.documents.index");
140 }
141
142 public function editShow($id)
143 {
144// if (!auth()->user()->hasPermission("edit_all_documents")) {
145// return redirect()->route("dashboard.documents.index");
146// }
147
148 return view("dashboard.documents.edit")->with([
149 "document" => Document::findOrFail($id),
150 "departments" => Department::all()
151 ]);
152 }
153
154 public function edit(DocumentRequest $request, $id)
155 {
156 $document = Document::findOrFail($id);
157
158 $department = Department::find($request->department);
159
160 $document->department()->associate($department);
161
162 $document->name = $request->name;
163 $document->arch_id = $request->arch_id;
164 $document->description = $request->description;
165
166 $document->save();
167
168 Alert::flash("Document edited successfully");
169
170 return redirect()->route("dashboard.documents.index");
171 }
172
173 public function toggleImportant($id)
174 {
175 $document = Document::find($id);
176 $document->is_important = !$document->is_important;
177 $document->save();
178
179 if($document->is_important==true)
180 Alert::flash("Document marked as important successfully");
181 else
182 Alert::flash("Document marked as not important successfully");
183
184 return redirect()->back();
185 }
186
187 public function search(Request $request)
188 {
189 $queries = explode(" ", $request->q);
190 $result = collect();
191
192 foreach ($queries as $query) {
193 $result->push(Document::where("arch_id", "like", "%{$query}%")->orWhere("name", "like", "%{$query}%")->get());
194 }
195
196 $result = $result->flatten();
197
198 $departments = Department::all();
199
200 return view("dashboard.documents.search")
201 ->with("searchQuery", $request->q)
202 ->with("results", $result)
203 ->with("departments", $departments)
204 ->with("countImportant", Document::where('is_important', true)->get()->count());
205 }
206
207 public function destroy($id)
208 {
209 $document = Document::find($id);
210 if (auth()->user()->hasPermission("delete_all_posts")) {
211 $document->delete();
212 Alert::flash($document->name . " deleted successfully");
213 }
214 return redirect()->route("dashboard.documents.index");
215 }
216}
Note: See TracBrowser for help on using the repository browser.