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

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

added files crud in table

  • Property mode set to 100644
File size: 11.1 KB
Line 
1<?php
2
3namespace App\Http\Controllers\Dashboard;
4
5use App\Helpers\Alert;
6use App\Http\Requests\Dashboard\DocumentRequest;
7use App\Http\Requests\Dashboard\FileRequest;
8use App\Http\Requests\Dashboard\PasswordSettingsRequest;
9use App\Models\Department;
10use App\Models\Document;
11use App\Models\File;
12use App\Services\UploadService;
13use Carbon\Carbon;
14use Illuminate\Http\Request;
15use Illuminate\Http\Response;
16use Illuminate\Support\Facades\Storage;
17use App\Http\Controllers\Controller;
18
19class DocumentsController extends Controller
20{
21 public function index(Request $request)
22 {
23 $queries = explode(" ", $request->search);
24 $result = collect();
25
26 foreach ($queries as $query) {
27 $result->push(Document::where("arch_id", "like", "%{$query}%")->orWhere("name", "like", "%{$query}%")->get());
28 }
29
30 $result = $result->flatten();
31
32 $deptName = "";
33 $deptCode = "";
34
35 if ($request->query('id')) {
36 $deptName = Department::find($request->query('id'))->getOriginal('name');
37 $deptCode = Department::find($request->query('id'))->getOriginal('code');
38 $documentsInDeptSort = Document::with('department')->when($request->has('id'), function ($query) use ($request) {
39 $query->where('department_id', $request->query('id'));
40 });
41
42 if ($request->query('sort') == 'newest') {
43 $documents = $documentsInDeptSort->orderBy('created_at', 'desc')->paginate(16);
44 } else if ($request->query('sort') == 'name') {
45 $documents = $documentsInDeptSort->orderBy('name', 'asc')->paginate(16);
46 } else {
47 $documents = Document::where('department_id', $request->query('id'))->paginate(16);
48 }
49 } else {
50 if ($request->query('sort') == 'newest') {
51 $documents = Document::orderBy('created_at', 'desc')->paginate(16);
52 } else if ($request->query('sort') == 'name') {
53 $documents = Document::orderBy('name', 'asc')->paginate(16);
54 } else if ($request->query('sort') == 'important') {
55 $documents = Document::where('is_important', true)->paginate(16);
56 } else if ($request->query('sort') == 'recent') {
57 $documents = Document::orderBy('created_at', 'desc')->paginate(16);
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 } else {
67 $documents = Document::paginate(20);
68 }
69 }
70
71 $departments = Department::all();
72
73 $diskTotal = disk_total_space('/');
74 $diskTotalSize = $diskTotal / 1073741824;
75
76 $diskFree = disk_free_space('/');
77 $used = $diskTotal - $diskFree;
78
79 $diskUsedSize = $used / 1073741824;
80 $diskUse1 = round(100 - (($diskUsedSize / $diskTotalSize) * 100));
81 $diskUse = round(100 - ($diskUse1)) . '%';
82
83 return view("dashboard.documents.index")->with([
84 "documents" => $documents,
85 "currentUser" => auth()->user(),
86 "departments" => $departments,
87 "docsCount" => Department::withCount('document')->get(),
88 "totalDocs" => Document::all()->count(),
89 "countImportant" => Document::where('is_important', true)->get()->count(),
90 "diskTotal" => $diskTotal,
91 "diskTotalSize" => $diskTotalSize,
92 "diskUse" => $diskUse,
93 "diskUsedSize" => $diskUsedSize,
94 "deptName" => $deptName,
95 "deptCode" => $deptCode,
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)
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 $location = 'Departments' . DIRECTORY_SEPARATOR . $document->department->code . DIRECTORY_SEPARATOR . $request->name;
122
123 if (!Storage::disk('local')->has($location)) {
124 Storage::disk('local')->makeDirectory($location);
125 }
126
127 $document->save();
128
129 if ($request->has('file_item')) {
130 foreach ($request->file_item as $file) {
131 $file->storeAs($location . DIRECTORY_SEPARATOR, $file->getClientOriginalName());
132 $newFile = new File();
133 $newFile->name = $file->getClientOriginalName();
134 $newFile->location = $location . DIRECTORY_SEPARATOR . $file->getClientOriginalName();
135 $newFile->document()->associate($document);
136 $newFile->save();
137 }
138 }
139
140 Alert::flash("New document created successfully");
141
142 return redirect()->route("dashboard.documents.index");
143 }
144
145 public function editShow($id)
146 {
147 return view("dashboard.documents.edit")->with([
148 "document" => Document::findOrFail($id),
149 "departments" => Department::all(),
150 "files" => File::where('document_id', $id)->get()
151 ]);
152 }
153
154 public function edit(DocumentRequest $request, $id)
155 {
156 $document = Document::findOrFail($id);
157 $files = File::where('document_id', $id)->get();
158
159 $department = Department::find($request->department);
160
161 $document->department()->associate($department);
162
163 $oldLocation = 'Departments' . DIRECTORY_SEPARATOR . $document->department->code . DIRECTORY_SEPARATOR . $document->name;
164
165 $document->name = $request->name;
166 $document->arch_id = $request->arch_id;
167 $document->description = $request->description;
168
169 $location = 'Departments' . DIRECTORY_SEPARATOR . $document->department->code . DIRECTORY_SEPARATOR . $request->name;
170
171 if ($document->isDirty('name')) {
172 if (!Storage::disk('local')->has($location)) {
173 Storage::disk('local')->move($oldLocation, $location);
174 foreach($files as $file){
175 $file->location = "test";
176 }
177 }
178 }
179
180 if ($request->has('file_item')) {
181 foreach ($request->file_item as $file) {
182 $fileName = $file->getClientOriginalName();
183 if (Storage::disk('local')->has($location . DIRECTORY_SEPARATOR . $fileName)) {
184 // $hasFileError = true;
185 // break;
186 $NewFileName = time() . $fileName;
187 $file->storeAs($location . DIRECTORY_SEPARATOR, $NewFileName);
188 $newFile = new File();
189 $newFile->name = rand() . $fileName;
190 $newFile->location = $location . DIRECTORY_SEPARATOR . $NewFileName;
191 $newFile->document()->associate($document);
192 $newFile->save();
193 } else {
194 $file->storeAs($location . DIRECTORY_SEPARATOR, $fileName);
195 $newFile = new File();
196 $newFile->name = $fileName;
197 $newFile->location = $location . DIRECTORY_SEPARATOR . $fileName;
198 $newFile->document()->associate($document);
199 $newFile->save();
200 }
201 }
202 }
203
204 $document->save();
205
206// if($hasFileError) {
207// Alert::flash('Document with the same name exists', 'error');
208// return redirect()->route("dashboard.documents.edit", ['id' => $document->id]);
209// }
210
211
212
213 Alert::flash("Document edited successfully");
214
215 return redirect()->back();
216 }
217
218 public function toggleImportant($id)
219 {
220 $document = Document::find($id);
221 $document->is_important = !$document->is_important;
222 $document->save();
223
224 if ($document->is_important == true)
225 Alert::flash("Document marked as important successfully");
226 else
227 Alert::flash("Document marked as not important successfully");
228
229 return redirect()->back();
230 }
231
232 public function search(Request $request)
233 {
234 $queries = explode(" ", $request->q);
235 $result = collect();
236
237 foreach ($queries as $query) {
238 $result->push(Document::where("arch_id", "like", "%{$query}%")->orWhere("name", "like", "%{$query}%")->get());
239 }
240
241 $result = $result->flatten();
242
243 $departments = Department::all();
244
245 return view("dashboard.documents.search")
246 ->with("searchQuery", $request->q)
247 ->with("results", $result)
248 ->with("departments", $departments)
249 ->with("countImportant", Document::where('is_important', true)->get()->count());
250 }
251
252 public function destroy($id)
253 {
254 $document = Document::find($id);
255 $files = File::where('document_id', $id)->get();
256 if (auth()->user()->hasPermission("delete_all_documents")) {
257 // @dd($files);
258 foreach ($files as $file) {
259 $file->delete();
260 }
261 $document->delete();
262 $location = 'Departments' . DIRECTORY_SEPARATOR . $document->department->code . DIRECTORY_SEPARATOR . $document->name;
263 Storage::disk('local')->deleteDirectory($location);
264 Alert::flash($document->name . " deleted successfully");
265 }
266 return redirect()->route("dashboard.documents.index");
267 }
268
269 public function deleteFile($id)
270 {
271 $file = File::find($id);
272 $file->delete();
273 Storage::disk('local')->delete($file->location);
274
275 Alert::flash($file->name . " deleted successfully");
276
277 return redirect()->back();
278 }
279
280 public function downloadFile($id)
281 {
282 $file = File::find($id);
283 return Storage::download($file->location);
284 }
285
286 public function renameFile(FileRequest $request, $id)
287 {
288 $file = File::find($id);
289 $fileExtension = explode('.', $file->name)[1];
290
291 $file->name = $request->name . '.' . $fileExtension;
292 $newLocation = 'Departments' . DIRECTORY_SEPARATOR . explode(DIRECTORY_SEPARATOR, $file->location)[1] . DIRECTORY_SEPARATOR . explode(DIRECTORY_SEPARATOR, $file->location)[2] . DIRECTORY_SEPARATOR . $file->name;
293
294 if(Storage::disk('local')->has($newLocation)) {
295 Alert::flash("A file with the same name already exists", "error");
296 return redirect()->back();
297 }
298 else {
299 Storage::disk('local')->copy($file->location, $newLocation);
300 Storage::disk('local')->delete($file->location);
301
302 $file->location = $newLocation;
303 $file->save();
304
305 Alert::flash($file->name . " updated successfully");
306 return redirect()->back();
307 }
308 }
309}
Note: See TracBrowser for help on using the repository browser.