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
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 $deptName = "";
29 $deptCode = "";
30
31 if ($request->query('id')) {
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) {
35 $query->where('department_id', $request->query('id'));
36 });
37
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);
44 }
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')) {
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;
62 } else {
63 $documents = Document::paginate(20);
64 }
65 }
66
67 $departments = Department::all();
68
69 $diskTotal = disk_total_space('/');
70 $diskTotalSize = $diskTotal / 1073741824;
71
72 $diskFree = disk_free_space('/');
73 $used = $diskTotal - $diskFree;
74
75 $diskUsedSize = $used / 1073741824;
76 $diskUse1 = round(100 - (($diskUsedSize / $diskTotalSize) * 100));
77 $diskUse = round(100 - ($diskUse1)) . '%';
78
79 return view("dashboard.documents.index")->with([
80 "documents" => $documents,
81 "currentUser" => auth()->user(),
82 "departments" => $departments,
83 "docsCount" => Department::withCount('document')->get(),
84 "totalDocs" => Document::all()->count(),
85 "countImportant" => Document::where('is_important', true)->get()->count(),
86 "diskTotal" => $diskTotal,
87 "diskTotalSize" => $diskTotalSize,
88 "diskUse" => $diskUse,
89 "diskUsedSize" => $diskUsedSize,
90 "deptName" => $deptName,
91 "deptCode" => $deptCode,
92
93 ]);
94
95 }
96
97 public function create()
98 {
99 return view("dashboard.documents.create")->with([
100 "departments" => Department::all()
101 ]);
102 }
103
104 public function store(DocumentRequest $request)
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
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);
121 }
122
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 }
130
131 $document->save();
132
133 Alert::flash("New document created successfully");
134
135 return redirect()->route("dashboard.documents.index");
136 }
137
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
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
188 $document->save();
189
190 Alert::flash("Document edited successfully");
191
192 return redirect()->route("dashboard.documents.index");
193 }
194
195 public function toggleImportant($id)
196 {
197 $document = Document::find($id);
198 $document->is_important = !$document->is_important;
199 $document->save();
200
201 if ($document->is_important == true)
202 Alert::flash("Document marked as important successfully");
203 else
204 Alert::flash("Document marked as not important successfully");
205
206 return redirect()->back();
207 }
208
209 public function search(Request $request)
210 {
211 $queries = explode(" ", $request->q);
212 $result = collect();
213
214 foreach ($queries as $query) {
215 $result->push(Document::where("arch_id", "like", "%{$query}%")->orWhere("name", "like", "%{$query}%")->get());
216 }
217
218 $result = $result->flatten();
219
220 $departments = Department::all();
221
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 }
228
229 public function destroy($id)
230 {
231 $document = Document::find($id);
232 if (auth()->user()->hasPermission("delete_all_documents")) {
233 $document->delete();
234 Alert::flash($document->name . " deleted successfully");
235 }
236 return redirect()->route("dashboard.documents.index");
237 }
238}
Note: See TracBrowser for help on using the repository browser.