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

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

edited file upload, seeding and departments controller

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